Skip to content

Commit 869a656

Browse files
committed
test(iam): harden destroy checks with RetryContext
1 parent 0a07e13 commit 869a656

10 files changed

+1300
-2897
lines changed

internal/services/iam/api_key_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package iam_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
7+
"time"
68

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
710
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
811
"github.com/hashicorp/terraform-plugin-testing/terraform"
912
iamSDK "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
@@ -247,28 +250,30 @@ func testAccCheckIamAPIKeyExists(tt *acctest.TestTools, name string) resource.Te
247250

248251
func testAccCheckIamAPIKeyDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
249252
return func(s *terraform.State) error {
250-
for _, rs := range s.RootModule().Resources {
251-
if rs.Type != "scaleway_iam_api_key" {
252-
continue
253-
}
254-
255-
iamAPI := iam.NewAPI(tt.Meta)
253+
api := iam.NewAPI(tt.Meta)
254+
ctx := context.Background()
256255

257-
_, err := iamAPI.GetAPIKey(&iamSDK.GetAPIKeyRequest{
258-
AccessKey: rs.Primary.ID,
259-
})
256+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
257+
for _, rs := range s.RootModule().Resources {
258+
if rs.Type != "scaleway_iam_api_key" {
259+
continue
260+
}
260261

261-
// If no error resource still exist
262-
if err == nil {
263-
return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID)
264-
}
262+
_, err := api.GetAPIKey(&iamSDK.GetAPIKeyRequest{
263+
AccessKey: rs.Primary.ID,
264+
})
265265

266-
// Unexpected api error we return it
267-
if !httperrors.Is404(err) {
268-
return err
266+
switch {
267+
case err == nil:
268+
return retry.RetryableError(fmt.Errorf("IAM API key (%s) still exists", rs.Primary.ID))
269+
case httperrors.Is404(err):
270+
continue
271+
default:
272+
return retry.NonRetryableError(err)
273+
}
269274
}
270-
}
271275

272-
return nil
276+
return nil
277+
})
273278
}
274279
}

internal/services/iam/application_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package iam_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
7+
"time"
68

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
710
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
811
"github.com/hashicorp/terraform-plugin-testing/terraform"
912
iamSDK "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
@@ -109,28 +112,30 @@ func testAccCheckIamApplicationExists(tt *acctest.TestTools, name string) resour
109112

110113
func testAccCheckIamApplicationDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
111114
return func(s *terraform.State) error {
112-
for _, rs := range s.RootModule().Resources {
113-
if rs.Type != "scaleway_iam_application" {
114-
continue
115-
}
116-
117-
iamAPI := iam.NewAPI(tt.Meta)
115+
api := iam.NewAPI(tt.Meta)
116+
ctx := context.Background()
118117

119-
_, err := iamAPI.GetApplication(&iamSDK.GetApplicationRequest{
120-
ApplicationID: rs.Primary.ID,
121-
})
118+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
119+
for _, rs := range s.RootModule().Resources {
120+
if rs.Type != "scaleway_iam_application" {
121+
continue
122+
}
122123

123-
// If no error resource still exist
124-
if err == nil {
125-
return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID)
126-
}
124+
_, err := api.GetApplication(&iamSDK.GetApplicationRequest{
125+
ApplicationID: rs.Primary.ID,
126+
})
127127

128-
// Unexpected api error we return it
129-
if !httperrors.Is404(err) {
130-
return err
128+
switch {
129+
case err == nil:
130+
return retry.RetryableError(fmt.Errorf("IAM application (%s) still exists", rs.Primary.ID))
131+
case httperrors.Is404(err):
132+
continue
133+
default:
134+
return retry.NonRetryableError(err)
135+
}
131136
}
132-
}
133137

134-
return nil
138+
return nil
139+
})
135140
}
136141
}

internal/services/iam/group_test.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package iam_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
7+
"time"
68

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
710
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
811
"github.com/hashicorp/terraform-plugin-testing/terraform"
912
iamSDK "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
@@ -478,28 +481,30 @@ func testAccCheckIamGroupExists(tt *acctest.TestTools, name string) resource.Tes
478481

479482
func testAccCheckIamGroupDestroy(tt *acctest.TestTools) resource.TestCheckFunc {
480483
return func(s *terraform.State) error {
481-
for _, rs := range s.RootModule().Resources {
482-
if rs.Type != "scaleway_iam_group" {
483-
continue
484-
}
485-
486-
iamAPI := iam.NewAPI(tt.Meta)
484+
api := iam.NewAPI(tt.Meta)
485+
ctx := context.Background()
487486

488-
_, err := iamAPI.GetGroup(&iamSDK.GetGroupRequest{
489-
GroupID: rs.Primary.ID,
490-
})
487+
return retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
488+
for _, rs := range s.RootModule().Resources {
489+
if rs.Type != "scaleway_iam_group" {
490+
continue
491+
}
491492

492-
// If no error resource still exist
493-
if err == nil {
494-
return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID)
495-
}
493+
_, err := api.GetGroup(&iamSDK.GetGroupRequest{
494+
GroupID: rs.Primary.ID,
495+
})
496496

497-
// Unexpected api error we return it
498-
if !httperrors.Is404(err) {
499-
return err
497+
switch {
498+
case err == nil:
499+
return retry.RetryableError(fmt.Errorf("IAM group (%s) still exists", rs.Primary.ID))
500+
case httperrors.Is404(err):
501+
continue
502+
default:
503+
return retry.NonRetryableError(err)
504+
}
500505
}
501-
}
502506

503-
return nil
507+
return nil
508+
})
504509
}
505510
}

0 commit comments

Comments
 (0)