|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/api/lb/v1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceScalewayLbIPs() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: dataSourceScalewayLbIPsRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "ip_cidr_range": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Optional: true, |
| 20 | + ValidateFunc: validation.IsCIDR, |
| 21 | + Description: "IPs within a CIDR block like it are listed.", |
| 22 | + }, |
| 23 | + "ips": { |
| 24 | + Type: schema.TypeList, |
| 25 | + Computed: true, |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "id": { |
| 29 | + Computed: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + }, |
| 32 | + "ip_address": { |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + "lb_id": { |
| 37 | + Computed: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + }, |
| 40 | + "reverse": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + }, |
| 44 | + "zone": zoneSchema(), |
| 45 | + "organization_id": organizationIDSchema(), |
| 46 | + "project_id": projectIDSchema(), |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + "zone": zoneSchema(), |
| 51 | + "organization_id": organizationIDSchema(), |
| 52 | + "project_id": projectIDSchema(), |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func dataSourceScalewayLbIPsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 58 | + lbAPI, zone, err := lbAPIWithZone(d, meta) |
| 59 | + if err != nil { |
| 60 | + return diag.FromErr(err) |
| 61 | + } |
| 62 | + res, err := lbAPI.ListIPs(&lb.ZonedAPIListIPsRequest{ |
| 63 | + Zone: zone, |
| 64 | + ProjectID: expandStringPtr(d.Get("project_id")), |
| 65 | + }, scw.WithContext(ctx)) |
| 66 | + if err != nil { |
| 67 | + return diag.FromErr(err) |
| 68 | + } |
| 69 | + |
| 70 | + var filteredList []*lb.IP |
| 71 | + for i := range res.IPs { |
| 72 | + if ipv4Match(d.Get("ip_cidr_range").(string), res.IPs[i].IPAddress) { |
| 73 | + filteredList = append(filteredList, res.IPs[i]) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + ips := []interface{}(nil) |
| 78 | + for _, ip := range filteredList { |
| 79 | + rawIP := make(map[string]interface{}) |
| 80 | + rawIP["id"] = newZonedID(ip.Zone, ip.ID).String() |
| 81 | + rawIP["ip_address"] = ip.IPAddress |
| 82 | + rawIP["lb_id"] = flattenStringPtr(ip.LBID) |
| 83 | + rawIP["reverse"] = ip.Reverse |
| 84 | + rawIP["zone"] = string(zone) |
| 85 | + rawIP["organization_id"] = ip.OrganizationID |
| 86 | + rawIP["project_id"] = ip.ProjectID |
| 87 | + |
| 88 | + ips = append(ips, rawIP) |
| 89 | + } |
| 90 | + |
| 91 | + d.SetId(zone.String()) |
| 92 | + _ = d.Set("ips", ips) |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
0 commit comments