Skip to content

Commit bfcb46c

Browse files
committed
handle detach and move + test
1 parent f91eb44 commit bfcb46c

File tree

3 files changed

+2096
-8
lines changed

3 files changed

+2096
-8
lines changed

internal/services/ipam/ip.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func ResourceIP() *schema.Resource {
7171
"custom_resource": {
7272
Type: schema.TypeList,
7373
Optional: true,
74-
ForceNew: true,
7574
Description: "The custom resource in which to book the IP",
7675
Elem: &schema.Resource{
7776
Schema: map[string]*schema.Schema{
@@ -292,13 +291,32 @@ func ResourceIPAMIPUpdate(ctx context.Context, d *schema.ResourceData, m interfa
292291
return diag.FromErr(err)
293292
}
294293

295-
_, err = ipamAPI.UpdateIP(&ipam.UpdateIPRequest{
296-
IPID: ID,
297-
Region: region,
298-
Tags: types.ExpandUpdatedStringsPtr(d.Get("tags")),
299-
}, scw.WithContext(ctx))
300-
if err != nil {
301-
return diag.FromErr(err)
294+
if d.HasChange("custom_resource") {
295+
oldCustomResourceRaw, newCustomResourceRaw := d.GetChange("custom_resource")
296+
oldCustomResource := expandCustomResource(oldCustomResourceRaw)
297+
newCustomResource := expandCustomResource(newCustomResourceRaw)
298+
299+
_, err = ipamAPI.MoveIP(&ipam.MoveIPRequest{
300+
Region: region,
301+
IPID: ID,
302+
FromResource: oldCustomResource,
303+
ToResource: newCustomResource,
304+
}, scw.WithContext(ctx))
305+
if err != nil {
306+
return diag.FromErr(err)
307+
308+
}
309+
}
310+
311+
if d.HasChange("tags") {
312+
_, err = ipamAPI.UpdateIP(&ipam.UpdateIPRequest{
313+
IPID: ID,
314+
Region: region,
315+
Tags: types.ExpandUpdatedStringsPtr(d.Get("tags")),
316+
}, scw.WithContext(ctx))
317+
if err != nil {
318+
return diag.FromErr(err)
319+
}
302320
}
303321

304322
return ResourceIPAMIPRead(ctx, d, m)
@@ -310,6 +328,17 @@ func ResourceIPAMIPDelete(ctx context.Context, d *schema.ResourceData, m interfa
310328
return diag.FromErr(err)
311329
}
312330

331+
if customResource, ok := d.GetOk("custom_resource"); ok {
332+
_, err = ipamAPI.DetachIP(&ipam.DetachIPRequest{
333+
Region: region,
334+
IPID: ID,
335+
Resource: expandCustomResource(customResource),
336+
}, scw.WithContext(ctx))
337+
if err != nil && !httperrors.Is404(err) {
338+
return diag.FromErr(err)
339+
}
340+
}
341+
313342
err = ipamAPI.ReleaseIP(&ipam.ReleaseIPRequest{
314343
Region: region,
315344
IPID: ID,

internal/services/ipam/ip_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,96 @@ func TestAccIPAMIP_WithTags(t *testing.T) {
157157
})
158158
}
159159

160+
func TestAccIPAMIP_WithCustomResource(t *testing.T) {
161+
tt := acctest.NewTestTools(t)
162+
defer tt.Cleanup()
163+
resource.ParallelTest(t, resource.TestCase{
164+
PreCheck: func() { acctest.PreCheck(t) },
165+
ProviderFactories: tt.ProviderFactories,
166+
CheckDestroy: ipamchecks.CheckIPDestroy(tt),
167+
Steps: []resource.TestStep{
168+
{
169+
Config: `
170+
resource scaleway_vpc vpc01 {
171+
name = "my vpc"
172+
}
173+
174+
resource scaleway_vpc_private_network pn01 {
175+
vpc_id = scaleway_vpc.vpc01.id
176+
ipv4_subnet {
177+
subnet = "172.16.32.0/22"
178+
}
179+
}
180+
181+
resource scaleway_ipam_ip ip01 {
182+
source {
183+
private_network_id = scaleway_vpc_private_network.pn01.id
184+
}
185+
custom_resource {
186+
mac_address = "bc:24:11:74:d0:5a"
187+
}
188+
}
189+
`,
190+
Check: resource.ComposeTestCheckFunc(
191+
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
192+
resource.TestCheckResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address", "bc:24:11:74:d0:5a"),
193+
),
194+
},
195+
{
196+
Config: `
197+
resource scaleway_vpc vpc01 {
198+
name = "my vpc"
199+
}
200+
201+
resource scaleway_vpc_private_network pn01 {
202+
vpc_id = scaleway_vpc.vpc01.id
203+
ipv4_subnet {
204+
subnet = "172.16.32.0/22"
205+
}
206+
}
207+
208+
resource scaleway_ipam_ip ip01 {
209+
source {
210+
private_network_id = scaleway_vpc_private_network.pn01.id
211+
}
212+
custom_resource {
213+
mac_address = "bc:24:11:74:d0:5b"
214+
}
215+
}
216+
`,
217+
Check: resource.ComposeTestCheckFunc(
218+
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
219+
resource.TestCheckResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address", "bc:24:11:74:d0:5b"),
220+
),
221+
},
222+
{
223+
Config: `
224+
resource scaleway_vpc vpc01 {
225+
name = "my vpc"
226+
}
227+
228+
resource scaleway_vpc_private_network pn01 {
229+
vpc_id = scaleway_vpc.vpc01.id
230+
ipv4_subnet {
231+
subnet = "172.16.32.0/22"
232+
}
233+
}
234+
235+
resource scaleway_ipam_ip ip01 {
236+
source {
237+
private_network_id = scaleway_vpc_private_network.pn01.id
238+
}
239+
}
240+
`,
241+
Check: resource.ComposeTestCheckFunc(
242+
testAccCheckIPAMIPExists(tt, "scaleway_ipam_ip.ip01"),
243+
resource.TestCheckNoResourceAttr("scaleway_ipam_ip.ip01", "custom_resource.0.mac_address"),
244+
),
245+
},
246+
},
247+
})
248+
}
249+
160250
func testAccCheckIPAMIPExists(tt *acctest.TestTools, n string) resource.TestCheckFunc {
161251
return func(s *terraform.State) error {
162252
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)