Skip to content

Commit 9a3c1be

Browse files
committed
test(vpc): harden destroy checks with RetryContext
1 parent 627e4ff commit 9a3c1be

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

internal/services/vpc/testfuncs/checks.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package vpctestfuncs
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
vpc2 "github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
@@ -13,34 +16,35 @@ import (
1316

1417
func CheckPrivateNetworkDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
1518
return func(state *terraform.State) error {
16-
for _, rs := range state.RootModule().Resources {
17-
if rs.Type != "scaleway_vpc_private_network" {
18-
continue
19-
}
19+
ctx := context.Background()
2020

21-
vpcAPI, region, ID, err := vpc.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
22-
if err != nil {
23-
return err
24-
}
21+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
22+
for _, rs := range state.RootModule().Resources {
23+
if rs.Type != "scaleway_vpc_private_network" {
24+
continue
25+
}
2526

26-
_, err = vpcAPI.GetPrivateNetwork(&vpc2.GetPrivateNetworkRequest{
27-
PrivateNetworkID: ID,
28-
Region: region,
29-
})
27+
api, region, id, err := vpc.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
28+
if err != nil {
29+
return retry.NonRetryableError(err)
30+
}
3031

31-
if err == nil {
32-
return fmt.Errorf(
33-
"VPC private network %s still exists",
34-
rs.Primary.ID,
35-
)
36-
}
37-
// Unexpected api error we return it
38-
if !httperrors.Is404(err) {
39-
return err
32+
_, err = api.GetPrivateNetwork(&vpc2.GetPrivateNetworkRequest{
33+
Region: region,
34+
PrivateNetworkID: id,
35+
})
36+
switch {
37+
case err == nil:
38+
return retry.RetryableError(fmt.Errorf("VPC private network (%s) still exists", rs.Primary.ID))
39+
case httperrors.Is404(err):
40+
continue
41+
default:
42+
return retry.NonRetryableError(err)
43+
}
4044
}
41-
}
4245

43-
return nil
46+
return nil
47+
})
4448
}
4549
}
4650

internal/services/vpc/vpc_test.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package vpc_test
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
67
"testing"
8+
"time"
79

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
811
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
912
"github.com/hashicorp/terraform-plugin-testing/terraform"
1013
vpcSDK "github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
@@ -171,30 +174,32 @@ func testAccCheckVPCExists(tt *acctest.TestTools, n string) resource.TestCheckFu
171174

172175
func testAccCheckVPCDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
173176
return func(state *terraform.State) error {
174-
for _, rs := range state.RootModule().Resources {
175-
if rs.Type != "scaleway_vpc" {
176-
continue
177+
ctx := context.Background()
178+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
179+
for _, rs := range state.RootModule().Resources {
180+
if rs.Type != "scaleway_vpc" {
181+
continue
182+
}
183+
184+
vpcAPI, region, id, err := vpc.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
185+
if err != nil {
186+
return retry.NonRetryableError(err)
187+
}
188+
189+
_, err = vpcAPI.GetVPC(&vpcSDK.GetVPCRequest{
190+
Region: region,
191+
VpcID: id,
192+
})
193+
switch {
194+
case err == nil:
195+
return retry.RetryableError(fmt.Errorf("VPC (%s) still exists", rs.Primary.ID))
196+
case httperrors.Is404(err):
197+
continue
198+
default:
199+
return retry.NonRetryableError(err)
200+
}
177201
}
178-
179-
vpcAPI, region, ID, err := vpc.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
180-
if err != nil {
181-
return err
182-
}
183-
184-
_, err = vpcAPI.GetVPC(&vpcSDK.GetVPCRequest{
185-
VpcID: ID,
186-
Region: region,
187-
})
188-
189-
if err == nil {
190-
return fmt.Errorf("VPC (%s) still exists", rs.Primary.ID)
191-
}
192-
193-
if !httperrors.Is404(err) {
194-
return err
195-
}
196-
}
197-
198-
return nil
202+
return nil
203+
})
199204
}
200205
}

0 commit comments

Comments
 (0)