Skip to content

Commit

Permalink
Merge pull request #4887 from jessegonzalez/feature/s3_cors_delete
Browse files Browse the repository at this point in the history
Updating S3 CORS planning when CORS is deleted via API or console.
  • Loading branch information
bflad authored Jun 21, 2018
2 parents 8020938 + efb3f71 commit b93a790
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("cors_rule", rules); err != nil {
return err
}
} else {
log.Printf("[DEBUG] S3 bucket: %s, read CORS: %v", d.Id(), cors)
if err := d.Set("cors_rule", make([]map[string]interface{}, 0)); err != nil {
return err
}
}

// Read the website configuration
Expand Down
40 changes: 39 additions & 1 deletion aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,26 @@ func TestAccAWSS3Bucket_Cors(t *testing.T) {
}
}

deleteBucketCors := func(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

conn := testAccProvider.Meta().(*AWSClient).s3conn
_, err := conn.DeleteBucketCors(&s3.DeleteBucketCorsInput{
Bucket: aws.String(rs.Primary.ID),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchCORSConfiguration" {
return err
}
}
return nil
}
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down Expand Up @@ -600,6 +620,22 @@ func TestAccAWSS3Bucket_Cors(t *testing.T) {
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSS3BucketConfigWithCORS(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
deleteBucketCors("aws_s3_bucket.bucket"),
),
ExpectNonEmptyPlan: true,
},
},
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down Expand Up @@ -1286,7 +1322,9 @@ func testAccCheckAWSS3BucketCors(n string, corsRules []*s3.CORSRule) resource.Te
})

if err != nil {
return fmt.Errorf("GetBucketCors error: %v", err)
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchCORSConfiguration" {
return fmt.Errorf("GetBucketCors error: %v", err)
}
}

if !reflect.DeepEqual(out.CORSRules, corsRules) {
Expand Down

0 comments on commit b93a790

Please sign in to comment.