Skip to content

Commit 6cf799f

Browse files
authored
feat(lb): add ips datasource (#1791)
* feat(lb): add ips datasource * fix * fix * add support for returning multiple ips sharing the same prexif * typo * fix ci * fix ci * update cassette * rename to ipv4Match * fix * match with cidr
1 parent e0f6739 commit 6cf799f

File tree

6 files changed

+930
-0
lines changed

6 files changed

+930
-0
lines changed

docs/data-sources/lb_ips.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
page_title: "Scaleway: scaleway_lb_ips"
3+
description: |-
4+
Gets information about multiple Load Balancer IPs.
5+
---
6+
7+
# scaleway_lb_ips
8+
9+
Gets information about multiple Load Balancer IPs.
10+
11+
## Example Usage
12+
13+
```hcl
14+
# Find multiple IPs that share the same CIDR block
15+
data "scaleway_lb_ips" "my_key" {
16+
ip_cidr_range = "0.0.0.0/0"
17+
}
18+
# Find IPs by CIDR block and zone
19+
data "scaleway_lb_ips" "my_key" {
20+
ip_cidr_range = "0.0.0.0/0"
21+
zone = "fr-par-2"
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
- `ip_cidr_range` - (Optional) The IP CIDR range used as a filter. IPs within a CIDR block like it are listed.
28+
29+
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which IPs exist.
30+
31+
## Attributes Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
- `ips` - List of found IPs
36+
- `id` - The associated IP ID.
37+
- `lb_id` - The associated load-balancer ID if any
38+
- `ip_address` - The IP Address
39+
- `zone` - The [zone](../guides/regions_and_zones.md#zones) in which the load-balancer is.
40+
- `reverse` - The reverse domain associated with this IP.
41+
- `organization_id` - The organization ID the load-balancer is associated with.
42+
- `project_id` - The ID of the project the load-balancer is associated with.

scaleway/data_source_lb_ips.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package scaleway
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccScalewayDataSourceLbIPs_Basic(t *testing.T) {
10+
tt := NewTestTools(t)
11+
defer tt.Cleanup()
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: tt.ProviderFactories,
15+
CheckDestroy: testAccCheckScalewayLbIPDestroy(tt),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `
19+
resource scaleway_lb_ip ip1 {
20+
}
21+
`,
22+
},
23+
{
24+
Config: `
25+
resource scaleway_lb_ip ip1 {
26+
}
27+
resource scaleway_lb_ip ip2 {
28+
}
29+
30+
data "scaleway_lb_ips" "lbs_by_cidr_range" {
31+
ip_cidr_range = "0.0.0.0/0"
32+
depends_on = [scaleway_lb_ip.ip1, scaleway_lb_ip.ip2]
33+
}
34+
data "scaleway_lb_ips" "lbs_by_cidr_range_other_zone" {
35+
ip_cidr_range = "0.0.0.0/0"
36+
zone = "fr-par-2"
37+
depends_on = [scaleway_lb_ip.ip1, scaleway_lb_ip.ip2]
38+
}
39+
`,
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttrSet("data.scaleway_lb_ips.lbs_by_cidr_range", "ips.0.id"),
42+
resource.TestCheckResourceAttrSet("data.scaleway_lb_ips.lbs_by_cidr_range", "ips.0.ip_address"),
43+
resource.TestCheckResourceAttrSet("data.scaleway_lb_ips.lbs_by_cidr_range", "ips.1.id"),
44+
resource.TestCheckResourceAttrSet("data.scaleway_lb_ips.lbs_by_cidr_range", "ips.1.ip_address"),
45+
46+
resource.TestCheckNoResourceAttr("data.scaleway_lb_ips.lbs_by_cidr_range_other_zone", "ips.0.id"),
47+
),
48+
},
49+
},
50+
})
51+
}

scaleway/helpers_lb.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scaleway
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"reflect"
78
"strings"
89
"time"
@@ -577,3 +578,14 @@ func flattenLbIPs(ips []*lbSDK.IP) interface{} {
577578
}
578579
return flattenedIPs
579580
}
581+
582+
func ipv4Match(cidr, ipStr string) bool {
583+
_, cidrNet, err := net.ParseCIDR(cidr)
584+
if err != nil {
585+
return false
586+
}
587+
588+
ip := net.ParseIP(ipStr)
589+
590+
return cidrNet.Contains(ip)
591+
}

scaleway/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
202202
"scaleway_lb_frontend": dataSourceScalewayLbFrontend(),
203203
"scaleway_lb_frontends": dataSourceScalewayLbFrontends(),
204204
"scaleway_lb_ip": dataSourceScalewayLbIP(),
205+
"scaleway_lb_ips": dataSourceScalewayLbIPs(),
205206
"scaleway_lb_route": dataSourceScalewayLbRoute(),
206207
"scaleway_lb_routes": dataSourceScalewayLbRoutes(),
207208
"scaleway_marketplace_image": dataSourceScalewayMarketplaceImage(),

0 commit comments

Comments
 (0)