Skip to content

Commit b589390

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

18 files changed

+15530
-10168
lines changed

docs/resources/instance_private_nic.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ In addition to all arguments above, the following attributes are exported:
9393
~> **Important:** Instance private NICs' IDs are [zoned](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{zone}/{id}`, e.g. `fr-par-1/11111111-1111-1111-1111-111111111111`
9494

9595
- `mac_address` - The MAC address of the private NIC.
96+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
97+
- `id` - The ID of the IPv4 address resource.
98+
- `address` - The private IPv4 address.
9699

97100
## Import
98101

docs/resources/instance_server.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ attached to the server. Updates to this field will trigger a stop/start of the s
250250
- `private_network` - (Optional) The private network associated with the server.
251251
Use the `pn_id` key to attach a [private_network](https://www.scaleway.com/en/developers/api/instance/#path-private-nics-list-all-private-nics) on your instance.
252252

253+
- `private_ip` - The list of private IPv4 addresses associated with the resource.
254+
- `id` - The ID of the IPv4 address resource.
255+
- `address` - The private IPv4 address.
256+
253257
- `boot_type` - The boot Type of the server. Possible values are: `local`, `bootscript` or `rescue`.
254258

255259
- `replace_on_type_change` - (Defaults to false) If true, the server will be replaced if `type` is changed. Otherwise, the server will migrate.

internal/services/instance/private_nic.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
9+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
910
"github.com/scaleway/scaleway-sdk-go/scw"
1011
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1314
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1415
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1516
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
17+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1618
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1719
)
1820

@@ -68,6 +70,25 @@ func ResourcePrivateNIC() *schema.Resource {
6870
Description: "IPAM ip list, should be for internal use only",
6971
ForceNew: true,
7072
},
73+
"private_ip": {
74+
Type: schema.TypeList,
75+
Computed: true,
76+
Description: "List of private IPv4 addresses associated with the resource",
77+
Elem: &schema.Resource{
78+
Schema: map[string]*schema.Schema{
79+
"id": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "The ID of the IPv4 address resource",
83+
},
84+
"address": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
Description: "The private IPv4 address",
88+
},
89+
},
90+
},
91+
},
7192
"ipam_ip_ids": {
7293
Type: schema.TypeList,
7394
Elem: &schema.Schema{
@@ -163,6 +184,25 @@ func ResourceInstancePrivateNICRead(ctx context.Context, d *schema.ResourceData,
163184
_ = d.Set("tags", privateNIC.Tags)
164185
}
165186

187+
region, err := zone.Region()
188+
if err != nil {
189+
return diag.FromErr(err)
190+
}
191+
192+
resourceType := ipamAPI.ResourceTypeInstancePrivateNic
193+
opts := &ipam.GetResourcePrivateIPsOptions{
194+
ResourceID: &privateNIC.ID,
195+
ResourceType: &resourceType,
196+
PrivateNetworkID: &privateNIC.PrivateNetworkID,
197+
}
198+
199+
privateIP, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
200+
if err != nil {
201+
return diag.FromErr(err)
202+
}
203+
204+
_ = d.Set("private_ip", privateIP)
205+
166206
return nil
167207
}
168208

internal/services/instance/private_nic_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func TestAccPrivateNIC_Basic(t *testing.T) {
4141
resource.TestCheckResourceAttrSet("scaleway_instance_private_nic.nic01", "mac_address"),
4242
resource.TestCheckResourceAttrSet("scaleway_instance_private_nic.nic01", "private_network_id"),
4343
resource.TestCheckResourceAttrSet("scaleway_instance_private_nic.nic01", "server_id"),
44+
resource.TestCheckResourceAttrSet("scaleway_instance_private_nic.nic01", "private_ip.0.id"),
45+
resource.TestCheckResourceAttrSet("scaleway_instance_private_nic.nic01", "private_ip.0.address"),
4446
),
4547
},
4648
},

internal/services/instance/server.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2020
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
2121
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
22+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
2223
"github.com/scaleway/scaleway-sdk-go/api/marketplace/v2"
2324
"github.com/scaleway/scaleway-sdk-go/scw"
2425
scwvalidation "github.com/scaleway/scaleway-sdk-go/validation"
@@ -31,6 +32,7 @@ import (
3132
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
3233
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
3334
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/instancehelpers"
35+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
3436
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc"
3537
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
3638
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
@@ -350,6 +352,25 @@ func ResourceServer() *schema.Resource {
350352
},
351353
},
352354
},
355+
"private_ips": {
356+
Type: schema.TypeList,
357+
Computed: true,
358+
Description: "List of private IPv4 addresses associated with the resource",
359+
Elem: &schema.Resource{
360+
Schema: map[string]*schema.Schema{
361+
"id": {
362+
Type: schema.TypeString,
363+
Computed: true,
364+
Description: "The ID of the IPv4 address resource",
365+
},
366+
"address": {
367+
Type: schema.TypeString,
368+
Computed: true,
369+
Description: "The private IPv4 address",
370+
},
371+
},
372+
},
373+
},
353374
"zone": zonal.Schema(),
354375
"organization_id": account.OrganizationIDSchema(),
355376
"project_id": account.ProjectIDSchema(),
@@ -773,6 +794,37 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m i
773794
return diag.FromErr(err)
774795
}
775796

797+
privateNICIDs := []string(nil)
798+
for _, nic := range ph.privateNICsMap {
799+
privateNICIDs = append(privateNICIDs, nic.ID)
800+
}
801+
802+
allPrivateIPs := []map[string]interface{}(nil)
803+
resourceType := ipamAPI.ResourceTypeInstancePrivateNic
804+
805+
region, err := zone.Region()
806+
if err != nil {
807+
return diag.FromErr(err)
808+
}
809+
810+
for _, nicID := range privateNICIDs {
811+
opts := &ipam.GetResourcePrivateIPsOptions{
812+
ResourceType: &resourceType,
813+
ResourceID: &nicID,
814+
}
815+
816+
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
817+
if err != nil {
818+
return diag.FromErr(err)
819+
}
820+
821+
if privateIPs != nil {
822+
allPrivateIPs = append(allPrivateIPs, privateIPs...)
823+
}
824+
}
825+
826+
_ = d.Set("private_ips", allPrivateIPs)
827+
776828
////
777829
// Display warning if server will soon reach End of Service
778830
////

internal/services/instance/server_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,8 @@ func TestAccServer_PrivateNetwork(t *testing.T) {
11081108
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_network.0.zone"),
11091109
resource.TestCheckResourceAttrPair("scaleway_instance_server.base", "private_network.0.pn_id",
11101110
"scaleway_vpc_private_network.internal", "id"),
1111+
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_ips.0.id"),
1112+
resource.TestCheckResourceAttrSet("scaleway_instance_server.base", "private_ips.0.address"),
11111113
),
11121114
},
11131115
{

internal/services/instance/testdata/data-source-private-nic-basic.cassette.yaml

Lines changed: 1969 additions & 940 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/private-nic-basic.cassette.yaml

Lines changed: 590 additions & 443 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/private-nic-tags.cassette.yaml

Lines changed: 1244 additions & 607 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/private-nic-with-ipam.cassette.yaml

Lines changed: 657 additions & 559 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/server-private-network-missing-pnic.cassette.yaml

Lines changed: 1503 additions & 964 deletions
Large diffs are not rendered by default.

internal/services/instance/testdata/server-private-network.cassette.yaml

Lines changed: 2822 additions & 1989 deletions
Large diffs are not rendered by default.

internal/services/ipam/helpers.go

Lines changed: 63 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,64 @@ 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+
91+
if opts.ResourceID != nil {
92+
req.ResourceID = opts.ResourceID
93+
}
94+
95+
if opts.ResourceName != nil {
96+
req.ResourceName = opts.ResourceName
97+
}
98+
99+
if opts.ResourceType != nil {
100+
req.ResourceType = *opts.ResourceType
101+
}
102+
}
103+
104+
resp, err := ipamAPI.ListIPs(req, scw.WithContext(ctx))
105+
if err != nil {
106+
return nil, fmt.Errorf("error fetching IPs from IPAM: %w", err)
107+
}
108+
109+
if len(resp.IPs) == 0 {
110+
return nil, nil
111+
}
112+
113+
ipList := make([]map[string]interface{}, 0, len(resp.IPs))
114+
115+
for _, ip := range resp.IPs {
116+
ipNet := ip.Address
117+
if ipNet.IP == nil {
118+
continue
119+
}
120+
121+
ipMap := map[string]interface{}{
122+
"id": regional.NewIDString(region, ip.ID),
123+
"address": ipNet.IP.String(),
124+
}
125+
126+
ipList = append(ipList, ipMap)
127+
}
128+
129+
return ipList, nil
130+
}

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

Lines changed: 1040 additions & 844 deletions
Large diffs are not rendered by default.

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

Lines changed: 613 additions & 564 deletions
Large diffs are not rendered by default.

internal/services/vpc/testdata/vpc-route-basic.cassette.yaml

Lines changed: 2927 additions & 1457 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)