Skip to content

Commit f5e6234

Browse files
Mia-Crossyfodil
andcommitted
feat(rdb): read instance's private IPs
Co-authored-by: Yacine FODIL <[email protected]>
1 parent 8801985 commit f5e6234

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

docs/resources/rdb_instance.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-1111111111
238238
- `port` - Port in the Private Network.
239239
- `name` - Name of the endpoint.
240240
- `hostname` - Hostname of the endpoint.
241+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
242+
- `id` - The ID of the IPv4 address resource.
243+
- `address` - The private IPv4 address.
241244
- `certificate` - Certificate of the Database Instance.
242245
- `organization_id` - The organization ID the Database Instance is associated with.
243246

internal/services/ipam/helpers.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ipam
22

33
import (
4+
"context"
5+
"fmt"
46
"net"
57
"time"
68

@@ -65,3 +67,58 @@ func diffSuppressFuncStandaloneIPandCIDR(_, oldValue, newValue string, _ *schema
6567

6668
return false
6769
}
70+
71+
type GetResourcePrivateIPsOptions struct {
72+
ResourceType *ipam.ResourceType
73+
ResourceID *string
74+
ResourceName *string
75+
PrivateNetworkID *string
76+
}
77+
78+
// GetResourcePrivateIPs fetches the private IP addresses of a resource in a private network.
79+
func GetResourcePrivateIPs(ctx context.Context, m interface{}, region scw.Region, opts *GetResourcePrivateIPsOptions) ([]map[string]interface{}, error) {
80+
ipamAPI := ipam.NewAPI(meta.ExtractScwClient(m))
81+
82+
req := &ipam.ListIPsRequest{
83+
Region: region,
84+
}
85+
86+
if opts != nil {
87+
if opts.PrivateNetworkID != nil {
88+
req.PrivateNetworkID = opts.PrivateNetworkID
89+
}
90+
if opts.ResourceID != nil {
91+
req.ResourceID = opts.ResourceID
92+
}
93+
if opts.ResourceName != nil {
94+
req.ResourceName = opts.ResourceName
95+
}
96+
if opts.ResourceType != nil {
97+
req.ResourceType = *opts.ResourceType
98+
}
99+
}
100+
101+
resp, err := ipamAPI.ListIPs(req, scw.WithContext(ctx))
102+
if err != nil {
103+
return nil, fmt.Errorf("error fetching IPs from IPAM: %w", err)
104+
}
105+
106+
if len(resp.IPs) == 0 {
107+
return nil, nil
108+
}
109+
110+
ipList := make([]map[string]interface{}, 0, len(resp.IPs))
111+
for _, ip := range resp.IPs {
112+
ipNet := ip.Address
113+
if ipNet.IP == nil {
114+
continue
115+
}
116+
ipMap := map[string]interface{}{
117+
"id": regional.NewIDString(region, ip.ID),
118+
"address": ipNet.IP.String(),
119+
}
120+
ipList = append(ipList, ipMap)
121+
}
122+
123+
return ipList, nil
124+
}

internal/services/rdb/instance.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
79
"io"
810

911
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -322,6 +324,25 @@ func ResourceInstance() *schema.Resource {
322324
Optional: true,
323325
Description: "Enable or disable encryption at rest for the database instance",
324326
},
327+
"private_ips": {
328+
Type: schema.TypeList,
329+
Computed: true,
330+
Description: "List of private IPv4 addresses associated with the resource",
331+
Elem: &schema.Resource{
332+
Schema: map[string]*schema.Schema{
333+
"id": {
334+
Type: schema.TypeString,
335+
Computed: true,
336+
Description: "The ID of the IPv4 address resource",
337+
},
338+
"address": {
339+
Type: schema.TypeString,
340+
Computed: true,
341+
Description: "The private IPv4 address",
342+
},
343+
},
344+
},
345+
},
325346
// Common
326347
"region": regional.Schema(),
327348
"organization_id": account.OrganizationIDSchema(),
@@ -640,9 +661,29 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
640661
_ = d.Set("logs_policy", flattenInstanceLogsPolicy(res.LogsPolicy))
641662

642663
// set endpoints
664+
var privateIPs []map[string]interface{}
643665
if pnI, pnExist := flattenPrivateNetwork(res.Endpoints); pnExist {
644666
_ = d.Set("private_network", pnI)
667+
668+
for _, endpoint := range res.Endpoints {
669+
if endpoint.PrivateNetwork.ProvisioningMode == rdb.EndpointPrivateNetworkDetailsProvisioningModeIpam {
670+
671+
resourceType := ipamAPI.ResourceTypeRdbInstance
672+
opts := &ipam.GetResourcePrivateIPsOptions{
673+
ResourceID: &res.ID,
674+
ResourceType: &resourceType,
675+
PrivateNetworkID: &res.Endpoints[0].PrivateNetwork.PrivateNetworkID,
676+
}
677+
privateIPs, err = ipam.GetResourcePrivateIPs(ctx, m, region, opts)
678+
if err != nil {
679+
return diag.FromErr(err)
680+
}
681+
}
682+
683+
}
684+
645685
}
686+
_ = d.Set("private_ips", privateIPs)
646687

647688
if lbI, lbExists := flattenLoadBalancer(res.Endpoints); lbExists {
648689
_ = d.Set("load_balancer", lbI)

0 commit comments

Comments
 (0)