Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/s3_bucket_cors_configuration: add retry around Get method during read #22977

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22977.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_s3_bucket_cors_configuration: Retry when `NoSuchCORSConfiguration` errors are returned from the AWS API
```
7 changes: 5 additions & 2 deletions internal/service/s3/bucket_cors_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func resourceBucketCorsConfigurationRead(ctx context.Context, d *schema.Resource
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

output, err := conn.GetBucketCorsWithContext(ctx, input)
corsResponse, err := verify.RetryOnAWSCode(ErrCodeNoSuchCORSConfiguration, func() (interface{}, error) {
return conn.GetBucketCorsWithContext(ctx, input)
})

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, s3.ErrCodeNoSuchBucket, ErrCodeNoSuchCORSConfiguration) {
log.Printf("[WARN] S3 Bucket CORS Configuration (%s) not found, removing from state", d.Id())
Expand All @@ -139,7 +141,8 @@ func resourceBucketCorsConfigurationRead(ctx context.Context, d *schema.Resource
return diag.FromErr(fmt.Errorf("error reading S3 bucket CORS configuration (%s): %w", d.Id(), err))
}

if output == nil {
output, ok := corsResponse.(*s3.GetBucketCorsOutput)
if !ok || output == nil {
if d.IsNewResource() {
return diag.FromErr(fmt.Errorf("error reading S3 bucket CORS configuration (%s): empty output", d.Id()))
}
Expand Down
7 changes: 5 additions & 2 deletions internal/service/s3/bucket_cors_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfs3 "github.com/hashicorp/terraform-provider-aws/internal/service/s3"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func TestAccS3BucketCorsConfiguration_basic(t *testing.T) {
Expand Down Expand Up @@ -283,13 +284,15 @@ func testAccCheckBucketCorsConfigurationExists(resourceName string) resource.Tes
input.ExpectedBucketOwner = aws.String(expectedBucketOwner)
}

output, err := conn.GetBucketCors(input)
corsResponse, err := verify.RetryOnAWSCode(tfs3.ErrCodeNoSuchCORSConfiguration, func() (interface{}, error) {
return conn.GetBucketCors(input)
})

if err != nil {
return fmt.Errorf("error getting S3 Bucket CORS configuration (%s): %w", rs.Primary.ID, err)
}

if output == nil || len(output.CORSRules) == 0 {
if output, ok := corsResponse.(*s3.GetBucketCorsOutput); !ok || output == nil || len(output.CORSRules) == 0 {
return fmt.Errorf("S3 Bucket CORS configuration (%s) not found", rs.Primary.ID)
}

Expand Down