|
| 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 | +} |
0 commit comments