Skip to content

Commit 8321b1c

Browse files
author
yann degat
committed
new datasource: ovh_dedicated_server_boots
1 parent 0efde59 commit 8321b1c

6 files changed

+278
-122
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10+
)
11+
12+
func dataSourceDedicatedServerBoots() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceDedicatedServerBootsRead,
15+
Schema: map[string]*schema.Schema{
16+
"service_name": {
17+
Type: schema.TypeString,
18+
Description: "The internal name of your dedicated server.",
19+
Required: true,
20+
},
21+
22+
"boot_type": {
23+
Type: schema.TypeString,
24+
Optional: true,
25+
Description: "Filter the value of bootType property",
26+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
27+
err := validateBootType(v.(string))
28+
if err != nil {
29+
errors = append(errors, err)
30+
}
31+
return
32+
},
33+
},
34+
35+
// Computed
36+
"result": {
37+
Type: schema.TypeList,
38+
Computed: true,
39+
Description: "Server compatibles netboots ids",
40+
Elem: &schema.Schema{
41+
Type: schema.TypeInt,
42+
},
43+
},
44+
},
45+
}
46+
}
47+
48+
func dataSourceDedicatedServerBootsRead(d *schema.ResourceData, meta interface{}) error {
49+
config := meta.(*Config)
50+
serviceName := d.Get("service_name")
51+
52+
ids := []int{}
53+
54+
endpoint := fmt.Sprintf("/dedicated/server/%s/boot", serviceName)
55+
56+
if bootType, ok := d.GetOk("boot_type"); ok {
57+
endpoint = fmt.Sprintf("%s?bootType=%s", endpoint, bootType)
58+
}
59+
60+
if err := config.OVHClient.Get(endpoint, &ids); err != nil {
61+
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
62+
}
63+
64+
// setting id by computing a hashcode of all the ids
65+
idsStr := make([]string, len(ids))
66+
for i, id := range ids {
67+
idsStr[i] = strconv.Itoa(id)
68+
}
69+
70+
// sort.Strings sorts in place, returns nothing
71+
sort.Strings(idsStr)
72+
73+
d.SetId(hashcode.Strings(idsStr))
74+
d.Set("result", ids)
75+
return nil
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccDedicatedServerBootsDataSource_basic(t *testing.T) {
12+
dedicated_server := os.Getenv("OVH_DEDICATED_SERVER_SERVICE_NAME")
13+
config := fmt.Sprintf(testAccDedicatedServerBootsDatasourceConfig_Basic, dedicated_server)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCredentials(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: config,
21+
Check: resource.TestCheckResourceAttrSet(
22+
"data.ovh_dedicated_server_boots.boots",
23+
"result.#",
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccDedicatedServerBootsDatasourceConfig_Basic = `
31+
data "ovh_dedicated_server_boots" "boots" {
32+
service_name = "%s"
33+
}
34+
`

ovh/helpers.go

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func validateStringEnum(value string, enum []string) error {
5050
return nil
5151
}
5252

53+
func validateBootType(value string) error {
54+
return validateStringEnum(value, []string{
55+
"harddisk",
56+
"internal",
57+
"ipxeCustomerScript",
58+
"network",
59+
"rescue",
60+
})
61+
}
62+
5363
func getNilBoolPointer(val interface{}) *bool {
5464
if val == nil {
5565
return nil

ovh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
DataSourcesMap: map[string]*schema.Resource{
4444
"ovh_cloud_region": dataSourcePublicCloudRegion(),
4545
"ovh_cloud_regions": dataSourcePublicCloudRegions(),
46+
"ovh_dedicated_server_boots": dataSourceDedicatedServerBoots(),
4647
"ovh_domain_zone": dataSourceDomainZone(),
4748
"ovh_iploadbalancing": dataSourceIpLoadbalancing(),
4849
"ovh_me_paymentmean_bankaccount": dataSourceMePaymentmeanBankaccount(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: "ovh"
3+
page_title: "OVH: dedicated_server_boots"
4+
sidebar_current: "docs-ovh-datasource-dedicated-server-boots"
5+
description: |-
6+
Get the list of compatible netboots for a dedicated server associated with your OVH Account.
7+
---
8+
9+
# ovh_me_dedicated_server_boots
10+
11+
Use this data source to get the list of compatible netboots for a dedicated server associated with your OVH Account.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "ovh_dedicated_server_boots" "netboots" {
17+
service_name = "myserver"
18+
boot_type = "harddisk"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
* `service_name` - (Required) The internal name of your dedicated server.
25+
26+
* `boot_type` - (Optional) Filter the value of bootType property (harddisk, rescue, ipxeCustomerScript, internal, network)
27+
28+
## Attributes Reference
29+
30+
The following attributes are exported:
31+
32+
* `result` - The list of dedicated server netboots.

0 commit comments

Comments
 (0)