Skip to content

Commit 913b7dc

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

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/resources/redis_cluster.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ the form `{zone}/{id}`, e.g. `fr-par-1/11111111-1111-1111-1111-111111111111`
195195
- `endpoint_id` - The ID of the endpoint.
196196
- `zone` - The zone of the Private Network.
197197

198+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
199+
- `id` - The ID of the IPv4 address resource.
200+
- `address` - The private IPv4 address.
201+
198202
- `created_at` - The date and time of creation of the Redis™ cluster.
199203
- `updated_at` - The date and time of the last update of the Redis™ cluster.
200204
- `certificate` - The PEM of the certificate used by redis, only when `tls_enabled` is true

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+
}

0 commit comments

Comments
 (0)