-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_dedicated_server_boots.go
112 lines (96 loc) · 2.56 KB
/
data_dedicated_server_boots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ovh
import (
"fmt"
"net/url"
"sort"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func dataSourceDedicatedServerBoots() *schema.Resource {
return &schema.Resource{
Read: dataSourceDedicatedServerBootsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your dedicated server.",
Required: true,
},
"boot_type": {
Type: schema.TypeString,
Optional: true,
Description: "Filter the value of bootType property",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateBootType(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"kernel": {
Type: schema.TypeString,
Optional: true,
Description: "Filter the value of kernel property",
},
// Computed
"result": {
Type: schema.TypeList,
Computed: true,
Description: "Server compatibles netboots ids",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
}
}
func dataSourceDedicatedServerBootsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
ids := []int64{}
endpoint := fmt.Sprintf(
"/dedicated/server/%s/boot",
url.PathEscape(serviceName),
)
if bootType, ok := d.GetOk("boot_type"); ok {
endpoint = fmt.Sprintf(
"%s?bootType=%s",
endpoint,
url.PathEscape(bootType.(string)),
)
}
if err := config.OVHClient.Get(endpoint, &ids); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
if kernel, ok := d.GetOk("kernel"); ok {
// refine boots on kernel name
new_ids := []int64{}
for _, id := range ids {
boot := &DedicatedServerBoot{}
endpoint := fmt.Sprintf(
"/dedicated/server/%s/boot/%d",
url.PathEscape(serviceName),
id,
)
if err := config.OVHClient.Get(endpoint, boot); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
if boot.Kernel == kernel {
new_ids = append(new_ids, id)
}
}
ids = new_ids
}
// setting id by computing a hashcode of all the ids
idsStr := make([]string, len(ids))
for i, id := range ids {
idsStr[i] = strconv.FormatInt(id, 10)
}
// sort.Strings sorts in place, returns nothing
sort.Strings(idsStr)
d.SetId(hashcode.Strings(idsStr))
d.Set("result", ids)
return nil
}