From efb3f71b9e4f172b9c3aef1418f7e0b18a6292ca Mon Sep 17 00:00:00 2001 From: Jesse Gonzalez Date: Tue, 19 Jun 2018 16:34:30 -0500 Subject: [PATCH] Updating S3 CORS planning when CORS is deleted via API or console. --- aws/resource_aws_s3_bucket.go | 5 ++++ aws/resource_aws_s3_bucket_test.go | 40 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_s3_bucket.go b/aws/resource_aws_s3_bucket.go index da66a97c2af..8b159cc6359 100644 --- a/aws/resource_aws_s3_bucket.go +++ b/aws/resource_aws_s3_bucket.go @@ -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 diff --git a/aws/resource_aws_s3_bucket_test.go b/aws/resource_aws_s3_bucket_test.go index 717d784ce8a..bacd0abe53e 100644 --- a/aws/resource_aws_s3_bucket_test.go +++ b/aws/resource_aws_s3_bucket_test.go @@ -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, @@ -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, @@ -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) {