Skip to content

Commit 4df915d

Browse files
committed
test(ipam): harden destroy checks with RetryContext
1 parent 9a3c1be commit 4df915d

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package ipamtestfuncs
22

33
import (
4+
"context"
45
"fmt"
6+
"time"
57

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
69
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
710
"github.com/hashicorp/terraform-plugin-testing/terraform"
811
ipam2 "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
@@ -13,30 +16,33 @@ import (
1316

1417
func CheckIPDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
1518
return func(state *terraform.State) error {
16-
for _, rs := range state.RootModule().Resources {
17-
if rs.Type != "scaleway_ipam_ip" {
18-
continue
19+
ctx := context.Background()
20+
21+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
22+
for _, rs := range state.RootModule().Resources {
23+
if rs.Type != "scaleway_ipam_ip" {
24+
continue
25+
}
26+
27+
ipamAPI, region, ID, err := ipam.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
28+
if err != nil {
29+
return retry.NonRetryableError(err)
30+
}
31+
32+
_, err = ipamAPI.GetIP(&ipam2.GetIPRequest{
33+
IPID: ID,
34+
Region: region,
35+
})
36+
switch {
37+
case err == nil:
38+
return retry.RetryableError(fmt.Errorf("IP (%s) still exists", rs.Primary.ID))
39+
case httperrors.Is404(err):
40+
continue
41+
default:
42+
return retry.NonRetryableError(err)
43+
}
1944
}
20-
21-
ipamAPI, region, ID, err := ipam.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
22-
if err != nil {
23-
return err
24-
}
25-
26-
_, err = ipamAPI.GetIP(&ipam2.GetIPRequest{
27-
IPID: ID,
28-
Region: region,
29-
})
30-
31-
if err == nil {
32-
return fmt.Errorf("IP (%s) still exists", rs.Primary.ID)
33-
}
34-
35-
if !httperrors.Is404(err) {
36-
return err
37-
}
38-
}
39-
40-
return nil
45+
return nil
46+
})
4147
}
4248
}

0 commit comments

Comments
 (0)