Skip to content

Commit

Permalink
Merge pull request #22977 from hashicorp/f-add-retry-in-get-bucket-cors
Browse files Browse the repository at this point in the history
r/s3_bucket_cors_configuration: add retry around Get method during read
  • Loading branch information
anGie44 committed Feb 7, 2022
2 parents 170943f + 865c3ca commit 9e49415
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
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

0 comments on commit 9e49415

Please sign in to comment.