Skip to content

Commit bd155b9

Browse files
Mia-Crossyfodilremyleone
authored
feat(lb): read private ips (#3073)
* feat(lb): read private IPs Co-authored-by: Yacine FODIL <[email protected]> * fix wording in desc + doc --------- Co-authored-by: Yacine FODIL <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent dbcbc4f commit bd155b9

File tree

6 files changed

+1755
-1458
lines changed

6 files changed

+1755
-1458
lines changed

docs/resources/lb.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ In addition to all arguments above, the following attributes are exported:
129129
- `private_network` - List of private networks connected to your load balancer.
130130
- `status` - The status of the private network connection.
131131
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the private network was created.
132+
- `private_ips` - The list of private IPv4 and IPv6 addresses associated with the resource.
133+
- `id` - The ID of the IP address resource.
134+
- `address` - The private IP address.
132135
- `organization_id` - The ID of the Organization ID the Load Balancer is associated with.
133136

134137
~> **Important:** `release_ip` will not be supported. This prevents the destruction of the IP from releasing a Load Balancer.

internal/services/ipam/testdata/data-source-ipamip-instance-lb.cassette.yaml

Lines changed: 894 additions & 845 deletions
Large diffs are not rendered by default.

internal/services/lb/lb.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1415
lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1"
1516
"github.com/scaleway/scaleway-sdk-go/scw"
1617
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -20,6 +21,7 @@ import (
2021
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
2122
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
2223
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
24+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
2325
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2426
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2527
)
@@ -199,6 +201,25 @@ func ResourceLb() *schema.Resource {
199201
DiffSuppressFunc: dsf.OrderDiff,
200202
ConflictsWith: []string{"assign_flexible_ip", "assign_flexible_ipv6"},
201203
},
204+
"private_ips": {
205+
Type: schema.TypeList,
206+
Computed: true,
207+
Description: "List of private IPv4 and IPv6 addresses associated with the resource",
208+
Elem: &schema.Resource{
209+
Schema: map[string]*schema.Schema{
210+
"id": {
211+
Type: schema.TypeString,
212+
Computed: true,
213+
Description: "The ID of the IP address resource",
214+
},
215+
"address": {
216+
Type: schema.TypeString,
217+
Computed: true,
218+
Description: "The private IP address",
219+
},
220+
},
221+
},
222+
},
202223
"region": regional.ComputedSchema(),
203224
"zone": zonal.Schema(),
204225
"organization_id": account.OrganizationIDSchema(),
@@ -334,6 +355,32 @@ func resourceLbRead(ctx context.Context, d *schema.ResourceData, m interface{})
334355

335356
_ = d.Set("private_network", flattenPrivateNetworkConfigs(privateNetworks))
336357

358+
privateNetworkIDs := make([]string, 0, len(privateNetworks))
359+
for _, pn := range privateNetworks {
360+
privateNetworkIDs = append(privateNetworkIDs, pn.PrivateNetworkID)
361+
}
362+
363+
allPrivateIPs := []map[string]interface{}(nil)
364+
resourceType := ipamAPI.ResourceTypeLBServer
365+
366+
for _, privateNetworkID := range privateNetworkIDs {
367+
opts := &ipam.GetResourcePrivateIPsOptions{
368+
ResourceType: &resourceType,
369+
PrivateNetworkID: &privateNetworkID,
370+
}
371+
372+
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
373+
if err != nil {
374+
return diag.FromErr(err)
375+
}
376+
377+
if privateIPs != nil {
378+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
379+
}
380+
}
381+
382+
_ = d.Set("private_ips", allPrivateIPs)
383+
337384
return nil
338385
}
339386

internal/services/lb/lb_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ func TestAccLB_WithPrivateNetworksIPAMIDs(t *testing.T) {
418418
resource.TestCheckResourceAttrPair(
419419
"scaleway_ipam_ip.ip01", "address",
420420
"data.scaleway_ipam_ip.by_name", "address_cidr"),
421+
resource.TestCheckResourceAttrSet("scaleway_lb.lb01", "private_ips.0.id"),
422+
resource.TestCheckResourceAttrSet("scaleway_lb.lb01", "private_ips.0.address"),
421423
),
422424
},
423425
},

0 commit comments

Comments
 (0)