Skip to content

Commit

Permalink
'acctest.PreCheckSkipError' check both AWS SDK for Go v1 and v2 errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Sep 4, 2024
1 parent 8deb6e4 commit 6c6476d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
19 changes: 10 additions & 9 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv1 "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv2 "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
Expand Down Expand Up @@ -1821,31 +1822,31 @@ func PreCheckSkipError(err error) bool {
// GovCloud has endpoints that respond with (no message provided after the error code):
// AccessDeniedException:
// Ignore these API endpoints that exist but are not officially enabled
if tfawserr.ErrCodeEquals(err, "AccessDeniedException") {
if tfawserr_sdkv1.ErrCodeEquals(err, "AccessDeniedException") || tfawserr_sdkv2.ErrCodeEquals(err, "AccessDeniedException") {
return true
}
// Ignore missing API endpoints
if tfawserr.ErrMessageContains(err, "RequestError", "send request failed") {
if tfawserr_sdkv1.ErrMessageContains(err, "RequestError", "send request failed") || tfawserr_sdkv2.ErrMessageContains(err, "RequestError", "send request failed") {
return true
}
// Ignore unsupported API calls
if tfawserr.ErrCodeEquals(err, "UnknownOperationException") {
if tfawserr_sdkv1.ErrCodeEquals(err, "UnknownOperationException") || tfawserr_sdkv2.ErrCodeEquals(err, "UnknownOperationException") {
return true
}
if tfawserr.ErrCodeEquals(err, "UnsupportedOperation") {
if tfawserr_sdkv1.ErrCodeEquals(err, "UnsupportedOperation") || tfawserr_sdkv2.ErrCodeEquals(err, "UnsupportedOperation") {
return true
}
if tfawserr.ErrMessageContains(err, "InvalidInputException", "Unknown operation") {
if tfawserr_sdkv1.ErrMessageContains(err, "InvalidInputException", "Unknown operation") || tfawserr_sdkv2.ErrMessageContains(err, "InvalidInputException", "Unknown operation") {
return true
}
if tfawserr.ErrMessageContains(err, "InvalidAction", "is not valid") {
if tfawserr_sdkv1.ErrMessageContains(err, "InvalidAction", "is not valid") || tfawserr_sdkv2.ErrMessageContains(err, "InvalidAction", "is not valid") {
return true
}
if tfawserr.ErrMessageContains(err, "InvalidAction", "Unavailable Operation") {
if tfawserr_sdkv1.ErrMessageContains(err, "InvalidAction", "Unavailable Operation") || tfawserr_sdkv2.ErrMessageContains(err, "InvalidAction", "Unavailable Operation") {
return true
}
// ignore when not authorized to call API from account
if tfawserr.ErrCodeEquals(err, "ForbiddenException") {
if tfawserr_sdkv1.ErrCodeEquals(err, "ForbiddenException") || tfawserr_sdkv2.ErrCodeEquals(err, "ForbiddenException") {
return true
}
// Ignore missing API endpoints
Expand Down
6 changes: 3 additions & 3 deletions internal/errs/unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package errs

import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv1 "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv2 "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -106,9 +106,9 @@ func IsUnsupportedOperationInPartitionError(partition string, err error) bool {
}

func errCodeContains(err error, code string) bool {
return tfawserr.ErrCodeContains(err, code) || tfawserr_sdkv2.ErrCodeContains(err, code)
return tfawserr_sdkv1.ErrCodeContains(err, code) || tfawserr_sdkv2.ErrCodeContains(err, code)
}

func errMessageContains(err error, code, message string) bool {
return tfawserr.ErrMessageContains(err, code, message) || tfawserr_sdkv2.ErrMessageContains(err, code, message)
return tfawserr_sdkv1.ErrMessageContains(err, code, message) || tfawserr_sdkv2.ErrMessageContains(err, code, message)
}
12 changes: 6 additions & 6 deletions internal/tfresource/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
"time"

"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv1 "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
tfawserr_sdkv2 "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
Expand Down Expand Up @@ -96,7 +96,7 @@ func RetryGWhen[T any](ctx context.Context, timeout time.Duration, f func() (T,
// RetryWhenAWSErrCodeEquals retries the specified function when it returns one of the specified AWS error codes.
func RetryWhenAWSErrCodeEquals(ctx context.Context, timeout time.Duration, f func() (interface{}, error), codes ...string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhen(ctx, timeout, f, func(err error) (bool, error) {
if tfawserr.ErrCodeEquals(err, codes...) || tfawserr_sdkv2.ErrCodeEquals(err, codes...) {
if tfawserr_sdkv1.ErrCodeEquals(err, codes...) || tfawserr_sdkv2.ErrCodeEquals(err, codes...) {
return true, err
}

Expand All @@ -107,7 +107,7 @@ func RetryWhenAWSErrCodeEquals(ctx context.Context, timeout time.Duration, f fun
// RetryWhenAWSErrCodeContains retries the specified function when it returns an AWS error containing the specified code.
func RetryWhenAWSErrCodeContains(ctx context.Context, timeout time.Duration, f func() (interface{}, error), code string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhen(ctx, timeout, f, func(err error) (bool, error) {
if tfawserr.ErrCodeContains(err, code) || tfawserr_sdkv2.ErrCodeContains(err, code) {
if tfawserr_sdkv1.ErrCodeContains(err, code) || tfawserr_sdkv2.ErrCodeContains(err, code) {
return true, err
}

Expand All @@ -118,7 +118,7 @@ func RetryWhenAWSErrCodeContains(ctx context.Context, timeout time.Duration, f f
// RetryWhenAWSErrMessageContains retries the specified function when it returns an AWS error containing the specified message.
func RetryWhenAWSErrMessageContains(ctx context.Context, timeout time.Duration, f func() (interface{}, error), code, message string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhen(ctx, timeout, f, func(err error) (bool, error) {
if tfawserr.ErrMessageContains(err, code, message) || tfawserr_sdkv2.ErrMessageContains(err, code, message) {
if tfawserr_sdkv1.ErrMessageContains(err, code, message) || tfawserr_sdkv2.ErrMessageContains(err, code, message) {
return true, err
}

Expand All @@ -130,7 +130,7 @@ func RetryWhenAWSErrMessageContains(ctx context.Context, timeout time.Duration,
func RetryWhenMessageContains(ctx context.Context, timeout time.Duration, f func() (interface{}, error), codes []string, messages []string) (interface{}, error) {
return RetryWhen(ctx, timeout, f, func(err error) (bool, error) {
for i, message := range messages {
if tfawserr.ErrMessageContains(err, codes[i], message) || tfawserr_sdkv2.ErrMessageContains(err, codes[i], message) {
if tfawserr_sdkv1.ErrMessageContains(err, codes[i], message) || tfawserr_sdkv2.ErrMessageContains(err, codes[i], message) {
return true, err
}
}
Expand All @@ -143,7 +143,7 @@ func RetryWhenMessageContains(ctx context.Context, timeout time.Duration, f func
func RetryGWhenMessageContains[T any](ctx context.Context, timeout time.Duration, f func() (T, error), codes []string, messages []string) (T, error) {
return RetryGWhen(ctx, timeout, f, func(err error) (bool, error) {
for i, message := range messages {
if tfawserr.ErrMessageContains(err, codes[i], message) || tfawserr_sdkv2.ErrMessageContains(err, codes[i], message) {
if tfawserr_sdkv1.ErrMessageContains(err, codes[i], message) || tfawserr_sdkv2.ErrMessageContains(err, codes[i], message) {
return true, err
}
}
Expand Down

0 comments on commit 6c6476d

Please sign in to comment.