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

Updating S3 CORS planning when CORS is deleted via API or console. #4887

Merged
merged 1 commit into from
Jun 21, 2018
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: The logic can be simpler if rules was instantiated before the if statement and the d.Set() was moved outside the if:

rules := make([]map[string]interface{}, 0)
if cors.CORSRules != nil {
  for _, ruleObject := range cors.CORSRules {
    // ...
  }
  // remove d.Set() here
}
if err := d.Set("cors_rule", rules); err != nil {
  return fmt.Errorf("error setting cors_rule: %s", err)
}

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