Skip to content

Commit e0b97c3

Browse files
authored
feat(instance): support ipam ips on pnic creation (#2660)
* feat(instance): support ipam ips on pnic creation * update cassette * update cassette
1 parent 5506f5f commit e0b97c3

File tree

4 files changed

+3039
-8
lines changed

4 files changed

+3039
-8
lines changed

docs/resources/instance_private_nic.md

+43-8
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,73 @@ Creates and manages Scaleway Instance Private NICs. For more information, see
1414

1515
```terraform
1616
resource "scaleway_instance_private_nic" "pnic01" {
17-
server_id = "fr-par-1/11111111-1111-1111-1111-111111111111"
18-
private_network_id = "fr-par-1/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
17+
server_id = "fr-par-1/11111111-1111-1111-1111-111111111111"
18+
private_network_id = "fr-par-1/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
1919
}
2020
```
2121

2222
### With zone
2323

2424
```terraform
25-
resource scaleway_vpc_private_network pn01 {
26-
name = "private_network_instance"
27-
zone = "fr-par-2"
25+
resource "scaleway_vpc_private_network" "pn01" {
26+
name = "private_network_instance"
27+
region = "fr-par"
2828
}
2929
3030
resource "scaleway_instance_server" "base" {
3131
image = "ubuntu_jammy"
3232
type = "DEV1-S"
33-
zone = scaleway_vpc_private_network.pn01.zone
33+
zone = scaleway_vpc_private_network.pn01.zone
3434
}
3535
3636
resource "scaleway_instance_private_nic" "pnic01" {
37-
server_id = scaleway_instance_server.base.id
37+
server_id = scaleway_instance_server.base.id
3838
private_network_id = scaleway_vpc_private_network.pn01.id
39-
zone = scaleway_vpc_private_network.pn01.zone
39+
zone = scaleway_vpc_private_network.pn01.zone
4040
}
4141
```
4242

43+
### With IPAM IP IDs
44+
45+
```terraform
46+
resource "scaleway_vpc" "vpc01" {
47+
name = "vpc_instance"
48+
}
49+
50+
resource "scaleway_vpc_private_network" "pn01" {
51+
name = "private_network_instance"
52+
ipv4_subnet {
53+
subnet = "172.16.64.0/22"
54+
}
55+
vpc_id = scaleway_vpc.vpc01.id
56+
}
57+
58+
resource "scaleway_ipam_ip" "ip01" {
59+
address = "172.16.64.7"
60+
source {
61+
private_network_id = scaleway_vpc_private_network.pn01.id
62+
}
63+
}
64+
65+
resource "scaleway_instance_server" "server01" {
66+
image = "ubuntu_focal"
67+
type = "PLAY2-MICRO"
68+
}
69+
70+
resource "scaleway_instance_private_nic" "pnic01" {
71+
private_network_id = scaleway_vpc_private_network.pn01.id
72+
server_id = scaleway_instance_server.server01.id
73+
ipam_ip_ids = [scaleway_ipam_ip.ip01.id]
74+
}
75+
```
76+
4377
## Argument Reference
4478

4579
The following arguments are required:
4680

4781
- `server_id` - (Required) The ID of the server associated with.
4882
- `private_network_id` - (Required) The ID of the private network attached to.
83+
- `ipam_ip_ids` - (Optional) IPAM IDs of a pre-reserved IP addresses to assign to the Instance in the requested private network.
4984
- `tags` - (Optional) The tags associated with the private NIC.
5085
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the server must be created.
5186

internal/services/instance/private_nic.go

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func ResourcePrivateNIC() *schema.Resource {
6868
Description: "IPAM ip list, should be for internal use only",
6969
ForceNew: true,
7070
},
71+
"ipam_ip_ids": {
72+
Type: schema.TypeList,
73+
Elem: &schema.Schema{
74+
Type: schema.TypeString,
75+
},
76+
Optional: true,
77+
ForceNew: true,
78+
Description: "IPAM IDs of a pre-reserved IP addresses to assign to the Instance in the requested private network",
79+
},
7180
"zone": zonal.Schema(),
7281
},
7382
CustomizeDiff: cdf.LocalityCheck("server_id", "private_network_id"),
@@ -91,6 +100,7 @@ func ResourceInstancePrivateNICCreate(ctx context.Context, d *schema.ResourceDat
91100
PrivateNetworkID: regional.ExpandID(d.Get("private_network_id").(string)).ID,
92101
Tags: types.ExpandStrings(d.Get("tags")),
93102
IPIDs: types.ExpandStringsPtr(d.Get("ip_ids")),
103+
IpamIPIDs: locality.ExpandIDs(d.Get("ipam_ip_ids")),
94104
}
95105

96106
privateNIC, err := instanceAPI.CreatePrivateNIC(

internal/services/instance/private_nic_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,72 @@ func TestAccPrivateNIC_Tags(t *testing.T) {
135135
})
136136
}
137137

138+
func TestAccPrivateNIC_WithIPAM(t *testing.T) {
139+
tt := acctest.NewTestTools(t)
140+
defer tt.Cleanup()
141+
resource.ParallelTest(t, resource.TestCase{
142+
PreCheck: func() { acctest.PreCheck(t) },
143+
ProviderFactories: tt.ProviderFactories,
144+
CheckDestroy: isPrivateNICDestroyed(tt),
145+
Steps: []resource.TestStep{
146+
{
147+
Config: `
148+
resource "scaleway_vpc" "vpc01" {
149+
name = "TestAccScalewayInstancePrivateNIC_IPAM"
150+
}
151+
152+
resource "scaleway_vpc_private_network" "pn01" {
153+
name = "TestAccScalewayInstancePrivateNIC_IPAM"
154+
ipv4_subnet {
155+
subnet = "172.16.64.0/22"
156+
}
157+
vpc_id = scaleway_vpc.vpc01.id
158+
}
159+
160+
resource "scaleway_ipam_ip" "ip01" {
161+
address = "172.16.64.7"
162+
source {
163+
private_network_id = scaleway_vpc_private_network.pn01.id
164+
}
165+
}
166+
167+
resource "scaleway_instance_server" "server01" {
168+
name = "TestAccScalewayInstancePrivateNIC_IPAM"
169+
image = "ubuntu_focal"
170+
type = "PLAY2-MICRO"
171+
}
172+
173+
resource "scaleway_instance_private_nic" "pnic01" {
174+
private_network_id = scaleway_vpc_private_network.pn01.id
175+
server_id = scaleway_instance_server.server01.id
176+
ipam_ip_ids = [scaleway_ipam_ip.ip01.id]
177+
}
178+
179+
data "scaleway_ipam_ip" "by_id" {
180+
resource {
181+
id = scaleway_instance_private_nic.pnic01.id
182+
type = "instance_private_nic"
183+
}
184+
type = "ipv4"
185+
}
186+
`,
187+
Check: resource.ComposeTestCheckFunc(
188+
isPrivateNICPresent(tt, "scaleway_instance_private_nic.pnic01"),
189+
resource.TestCheckResourceAttrPair(
190+
"scaleway_instance_private_nic.pnic01", "private_network_id",
191+
"scaleway_vpc_private_network.pn01", "id"),
192+
resource.TestCheckResourceAttrPair(
193+
"scaleway_instance_private_nic.pnic01", "ipam_ip_ids.0",
194+
"scaleway_ipam_ip.ip01", "id"),
195+
resource.TestCheckResourceAttrPair(
196+
"scaleway_ipam_ip.ip01", "address",
197+
"data.scaleway_ipam_ip.by_id", "address_cidr"),
198+
),
199+
},
200+
},
201+
})
202+
}
203+
138204
func isPrivateNICPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
139205
return func(s *terraform.State) error {
140206
rs, ok := s.RootModule().Resources[n]

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

+2,920
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)