Skip to content

Commit 5e882b3

Browse files
Mia-Crossyfodil
andcommitted
feat(vpcgw): read gateway network's private IPs
Co-authored-by: Yacine FODIL <[email protected]>
1 parent 8801985 commit 5e882b3

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/resources/vpc_gateway_network.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ In addition to all arguments above, the following attributes are exported:
113113
- `created_at` - The date and time of the creation of the GatewayNetwork.
114114
- `updated_at` - The date and time of the last update of the GatewayNetwork.
115115
- `status` - The status of the Public Gateway's connection to the Private Network.
116+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
117+
- `id` - The ID of the IPv4 address resource.
118+
- `address` - The private IPv4 address.
116119

117120
## Import
118121

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)