|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceIpLoadbalancingVrackNetworks() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceIpLoadbalancingVrackNetworksRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "service_name": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Description: "The internal name of your iploadbalancer.", |
| 18 | + Required: true, |
| 19 | + }, |
| 20 | + |
| 21 | + "subnet": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Description: "Filters on subnet", |
| 24 | + Optional: true, |
| 25 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 26 | + err := validateIpBlock(v.(string)) |
| 27 | + if err != nil { |
| 28 | + errors = append(errors, err) |
| 29 | + } |
| 30 | + return |
| 31 | + }, |
| 32 | + }, |
| 33 | + "vlan_id": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Description: "Filters on vlan id", |
| 36 | + Optional: true, |
| 37 | + }, |
| 38 | + |
| 39 | + "result": { |
| 40 | + Type: schema.TypeList, |
| 41 | + Computed: true, |
| 42 | + Elem: &schema.Schema{Type: schema.TypeInt}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func dataSourceIpLoadbalancingVrackNetworksRead(d *schema.ResourceData, meta interface{}) error { |
| 49 | + config := meta.(*Config) |
| 50 | + |
| 51 | + result := make([]int64, 0) |
| 52 | + |
| 53 | + serviceName := d.Get("service_name").(string) |
| 54 | + vlanId := "" |
| 55 | + subnet := "" |
| 56 | + filters := "" |
| 57 | + |
| 58 | + endpoint := fmt.Sprintf( |
| 59 | + "/ipLoadbalancing/%s/vrack/network", |
| 60 | + url.PathEscape(serviceName), |
| 61 | + ) |
| 62 | + |
| 63 | + if val, ok := d.GetOkExists("vlan_id"); ok { |
| 64 | + vlanId = strconv.Itoa(val.(int)) |
| 65 | + filters = fmt.Sprintf("%s&vlan=%s", filters, url.PathEscape(vlanId)) |
| 66 | + } |
| 67 | + |
| 68 | + if val, ok := d.GetOkExists("subnet"); ok { |
| 69 | + subnet = val.(string) |
| 70 | + filters = fmt.Sprintf("%s&subnet=%s", filters, url.PathEscape(subnet)) |
| 71 | + } |
| 72 | + |
| 73 | + // OVH IPLB API doens't parse the query string according to |
| 74 | + // the RFC and throws a 400 error with empty query string |
| 75 | + if filters != "" { |
| 76 | + endpoint = fmt.Sprintf("%s?%s", endpoint, filters) |
| 77 | + } |
| 78 | + |
| 79 | + if err := config.OVHClient.Get(endpoint, &result); err != nil { |
| 80 | + return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err) |
| 81 | + } |
| 82 | + |
| 83 | + d.SetId(fmt.Sprintf("%s_%s_%s", serviceName, subnet, vlanId)) |
| 84 | + d.Set("result", result) |
| 85 | + return nil |
| 86 | +} |
0 commit comments