Skip to content

Commit 2cebce1

Browse files
committed
test + cassettes + linter
1 parent 5e882b3 commit 2cebce1

File tree

5 files changed

+820
-489
lines changed

5 files changed

+820
-489
lines changed

internal/services/ipam/helpers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ func GetResourcePrivateIPs(ctx context.Context, m interface{}, region scw.Region
8787
if opts.PrivateNetworkID != nil {
8888
req.PrivateNetworkID = opts.PrivateNetworkID
8989
}
90+
9091
if opts.ResourceID != nil {
9192
req.ResourceID = opts.ResourceID
9293
}
94+
9395
if opts.ResourceName != nil {
9496
req.ResourceName = opts.ResourceName
9597
}
98+
9699
if opts.ResourceType != nil {
97100
req.ResourceType = *opts.ResourceType
98101
}
@@ -108,11 +111,13 @@ func GetResourcePrivateIPs(ctx context.Context, m interface{}, region scw.Region
108111
}
109112

110113
ipList := make([]map[string]interface{}, 0, len(resp.IPs))
114+
111115
for _, ip := range resp.IPs {
112116
ipNet := ip.Address
113117
if ipNet.IP == nil {
114118
continue
115119
}
120+
116121
ipMap := map[string]interface{}{
117122
"id": regional.NewIDString(region, ip.ID),
118123
"address": ipNet.IP.String(),

internal/services/vpcgw/helpers.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
910
"github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1"
1011
v2 "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v2"
1112
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -14,6 +15,7 @@ import (
1415
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1516
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1617
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
18+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1719
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1820
)
1921

@@ -301,6 +303,56 @@ func readVPCGWNetworkResourceDataV2(d *schema.ResourceData, gatewayNetwork *v2.G
301303
return nil
302304
}
303305

306+
func getPrivateIPsV1(ctx context.Context, gn *vpcgw.GatewayNetwork, m interface{}) interface{} {
307+
var privateIPs []map[string]interface{}
308+
309+
resourceID := gn.ID
310+
311+
region, err := gn.Zone.Region()
312+
if err != nil {
313+
return diag.FromErr(err)
314+
}
315+
316+
resourceType := ipamAPI.ResourceTypeVpcGatewayNetwork
317+
opts := &ipam.GetResourcePrivateIPsOptions{
318+
ResourceID: &resourceID,
319+
ResourceType: &resourceType,
320+
PrivateNetworkID: &gn.PrivateNetworkID,
321+
}
322+
323+
privateIPs, err = ipam.GetResourcePrivateIPs(ctx, m, region, opts)
324+
if err != nil {
325+
return diag.FromErr(err)
326+
}
327+
328+
return privateIPs
329+
}
330+
331+
func getPrivateIPsV2(ctx context.Context, gn *v2.GatewayNetwork, m interface{}) interface{} {
332+
var privateIPs []map[string]interface{}
333+
334+
resourceID := gn.ID
335+
336+
region, err := gn.Zone.Region()
337+
if err != nil {
338+
return diag.FromErr(err)
339+
}
340+
341+
resourceType := ipamAPI.ResourceTypeVpcGatewayNetwork
342+
opts := &ipam.GetResourcePrivateIPsOptions{
343+
ResourceID: &resourceID,
344+
ResourceType: &resourceType,
345+
PrivateNetworkID: &gn.PrivateNetworkID,
346+
}
347+
348+
privateIPs, err = ipam.GetResourcePrivateIPs(ctx, m, region, opts)
349+
if err != nil {
350+
return diag.FromErr(err)
351+
}
352+
353+
return privateIPs
354+
}
355+
304356
// updateGatewayV1 performs the update of the public gateway using the v1 API
305357
func updateGatewayV1(ctx context.Context, d *schema.ResourceData, apiV1 *vpcgw.API, zone scw.Zone, id string) error {
306358
v1UpdateRequest := &vpcgw.UpdateGatewayRequest{

internal/services/vpcgw/network.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ func ResourceNetwork() *schema.Resource {
125125
Computed: true,
126126
Description: "The mac address on this network",
127127
},
128+
"private_ips": {
129+
Type: schema.TypeList,
130+
Computed: true,
131+
Description: "List of private IPv4 addresses associated with the resource.",
132+
Elem: &schema.Resource{
133+
Schema: map[string]*schema.Schema{
134+
"id": {
135+
Type: schema.TypeString,
136+
Computed: true,
137+
Description: "The ID of the IPv4 address resource.",
138+
},
139+
"address": {
140+
Type: schema.TypeString,
141+
Computed: true,
142+
Description: "The private IPv4 address.",
143+
},
144+
},
145+
},
146+
},
128147
"created_at": {
129148
Type: schema.TypeString,
130149
Computed: true,
@@ -218,6 +237,10 @@ func ResourceVPCGatewayNetworkRead(ctx context.Context, d *schema.ResourceData,
218237
return diag.FromErr(err)
219238
}
220239

240+
if gatewayNetwork.PrivateNetworkID != "" {
241+
_ = d.Set("private_ips", getPrivateIPsV1(ctx, gatewayV1, m))
242+
}
243+
221244
return readVPCGWNetworkResourceDataV1(d, gatewayV1)
222245
} else if httperrors.Is404(err) {
223246
d.SetId("")
@@ -228,6 +251,10 @@ func ResourceVPCGatewayNetworkRead(ctx context.Context, d *schema.ResourceData,
228251
return diag.FromErr(err)
229252
}
230253

254+
if gatewayNetwork.PrivateNetworkID != "" {
255+
_ = d.Set("private_ips", getPrivateIPsV2(ctx, gatewayNetwork, m))
256+
}
257+
231258
return readVPCGWNetworkResourceDataV2(d, gatewayNetwork)
232259
}
233260

internal/services/vpcgw/network_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func TestAccVPCGatewayNetwork_WithIPAMConfig(t *testing.T) {
6464
resource.TestCheckResourceAttr("scaleway_vpc_gateway_network.main", "ipam_config.0.push_default_route", "true"),
6565
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "ipam_config.0.ipam_ip_id"),
6666
resource.TestCheckResourceAttr("scaleway_vpc_gateway_network.main", "enable_masquerade", "true"),
67+
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "private_ips.0.id"),
68+
resource.TestCheckResourceAttrSet("scaleway_vpc_gateway_network.main", "private_ips.0.address"),
6769
),
6870
},
6971
{

0 commit comments

Comments
 (0)