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/aws_s3_bucket: Include any system tags that Terraform ignores when setting S3 bucket tags #7342

Merged
merged 2 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
74 changes: 74 additions & 0 deletions aws/resource_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,77 @@ func cfStackEventIsStackDeletion(event *cloudformation.StackEvent) bool {
*event.ResourceType == "AWS::CloudFormation::Stack" &&
event.ResourceStatusReason != nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add these functions for use in the new S3 bucket acceptance tests. Future opportunity to use these in the aws_cloudformation_stack resource functionality.

func cfStackStateRefresh(conn *cloudformation.CloudFormation, stackId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: aws.String(stackId),
})
if err != nil {
return nil, "", fmt.Errorf("error describing CloudFormation stacks: %s", err)
}

n := len(resp.Stacks)
switch n {
case 0:
return "", cloudformation.StackStatusDeleteComplete, nil

case 1:
stack := resp.Stacks[0]
return stack, aws.StringValue(stack.StackStatus), nil

default:
return nil, "", fmt.Errorf("found %d CloudFormation stacks for %s, expected 1", n, stackId)
}
}
}

func waitForCloudFormationStackCreation(conn *cloudformation.CloudFormation, stackId string, timeout time.Duration) (string, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{
cloudformation.StackStatusCreateInProgress,
cloudformation.StackStatusDeleteInProgress,
cloudformation.StackStatusRollbackInProgress,
},
Target: []string{
cloudformation.StackStatusCreateComplete,
cloudformation.StackStatusCreateFailed,
cloudformation.StackStatusDeleteComplete,
cloudformation.StackStatusDeleteFailed,
cloudformation.StackStatusRollbackComplete,
cloudformation.StackStatusRollbackFailed,
},
Refresh: cfStackStateRefresh(conn, stackId),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}

v, err := stateConf.WaitForState()
if err != nil {
return "", err
}

return aws.StringValue(v.(*cloudformation.Stack).StackStatus), nil
}

func waitForCloudFormationStackDeletion(conn *cloudformation.CloudFormation, stackId string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{
cloudformation.StackStatusDeleteInProgress,
cloudformation.StackStatusRollbackInProgress,
},
Target: []string{
cloudformation.StackStatusDeleteComplete,
cloudformation.StackStatusDeleteFailed,
},
Refresh: cfStackStateRefresh(conn, stackId),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}

_, err := stateConf.WaitForState()

return err
}
6 changes: 3 additions & 3 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {

func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn
if err := setTagsS3(s3conn, d); err != nil {
if err := setTagsS3Bucket(s3conn, d); err != nil {
return fmt.Errorf("%q: %s", d.Get("bucket").(string), err)
}

Expand Down Expand Up @@ -1222,9 +1222,9 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
}
}

tagSet, err := getTagSetS3(s3conn, d.Id())
tagSet, err := getTagSetS3Bucket(s3conn, d.Id())
if err != nil {
return err
return fmt.Errorf("error getting S3 bucket tags: %s", err)
}

if err := d.Set("tags", tagsToMapS3(tagSet)); err != nil {
Expand Down
243 changes: 243 additions & 0 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -238,6 +239,136 @@ func TestAccAWSS3Bucket_Bucket_EmptyString(t *testing.T) {
})
}

func TestAccAWSS3Bucket_tagsWithNoSystemTags(t *testing.T) {
resourceName := "aws_s3_bucket.bucket"
rInt := acctest.RandInt()
bucketName := fmt.Sprintf("tf-test-bucket-%d", rInt)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSS3BucketConfig_withTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
{
Config: testAccAWSS3BucketConfig_withUpdatedTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "XXX"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "EEE"),
),
},
{

Config: testAccAWSS3BucketConfig_withNoTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Config: testAccAWSS3BucketConfig_withTags(bucketName),
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
),
},
},
})
}

func TestAccAWSS3Bucket_tagsWithSystemTags(t *testing.T) {
resourceName := "aws_s3_bucket.bucket"
rInt := acctest.RandInt()
bucketName := fmt.Sprintf("tf-test-bucket-%d", rInt)
var stackId string

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSS3BucketDestroy,
func(s *terraform.State) error {
// Tear down CF stack.
conn := testAccProvider.Meta().(*AWSClient).cfconn

req := &cloudformation.DeleteStackInput{
StackName: aws.String(stackId),
}

log.Printf("[DEBUG] Deleting CloudFormation stack: %#v", req)
if _, err := conn.DeleteStack(req); err != nil {
return fmt.Errorf("Error deleting CloudFormation stack: %s", err)
}

if err := waitForCloudFormationStackDeletion(conn, stackId, 10*time.Minute); err != nil {
return fmt.Errorf("Error waiting for CloudFormation stack deletion: %s", err)
}

return nil
},
),
Steps: []resource.TestStep{
{
Config: testAccAWSS3BucketConfig_withNoTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
testAccCheckAWSS3DestroyBucket(resourceName),
testAccCheckAWSS3BucketCreateViaCloudFormation(bucketName, &stackId),
),
},
{
Config: testAccAWSS3BucketConfig_withTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "AAA"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "CCC"),
testAccCheckAWSS3BucketTagKeys(resourceName, "aws:cloudformation:stack-name", "aws:cloudformation:stack-id", "aws:cloudformation:logical-id"),
),
},
{
Config: testAccAWSS3BucketConfig_withUpdatedTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "BBB"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "XXX"),
resource.TestCheckResourceAttr(resourceName, "tags.Key4", "DDD"),
resource.TestCheckResourceAttr(resourceName, "tags.Key5", "EEE"),
testAccCheckAWSS3BucketTagKeys(resourceName, "aws:cloudformation:stack-name", "aws:cloudformation:stack-id", "aws:cloudformation:logical-id"),
),
},
{

Config: testAccAWSS3BucketConfig_withNoTags(bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
testAccCheckAWSS3BucketTagKeys(resourceName, "aws:cloudformation:stack-name", "aws:cloudformation:stack-id", "aws:cloudformation:logical-id"),
),
},
},
})
}

func TestAccAWSS3MultiBucket_withTags(t *testing.T) {
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -1739,6 +1870,77 @@ func testAccCheckAWSS3DestroyBucket(n string) resource.TestCheckFunc {
}
}

// Create an S3 bucket via a CF stack so that it has system tags.
func testAccCheckAWSS3BucketCreateViaCloudFormation(n string, stackId *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cfconn
stackName := fmt.Sprintf("tf-acc-test-s3tags-%d", acctest.RandInt())
templateBody := fmt.Sprintf(`
{
"Resources": {
"TfTestBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "%s",
}
}
}
}
`, n)

req := &cloudformation.CreateStackInput{
StackName: aws.String(stackName),
TemplateBody: aws.String(templateBody),
}

log.Printf("[DEBUG] Creating CloudFormation stack: %#v", req)
resp, err := conn.CreateStack(req)
if err != nil {
return fmt.Errorf("Error creating CloudFormation stack: %s", err)
}

status, err := waitForCloudFormationStackCreation(conn, aws.StringValue(resp.StackId), 10*time.Minute)
if err != nil {
return fmt.Errorf("Error waiting for CloudFormation stack creation: %s", err)
}
if status != cloudformation.StackStatusCreateComplete {
return fmt.Errorf("Invalid CloudFormation stack creation status: %s", status)
}

*stackId = aws.StringValue(resp.StackId)
return nil
}
}

func testAccCheckAWSS3BucketTagKeys(n string, keys ...string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]
conn := testAccProvider.Meta().(*AWSClient).s3conn

resp, err := conn.GetBucketTagging(&s3.GetBucketTaggingInput{
Bucket: aws.String(rs.Primary.ID),
})
if err != nil {
return err
}

for _, key := range keys {
ok := false
for _, tag := range resp.TagSet {
if key == aws.StringValue(tag.Key) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("Key %s not found in bucket's tag set", key)
}
}

return nil
}
}

func testAccCheckAWSS3BucketPolicy(n string, policy string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]
Expand Down Expand Up @@ -2094,6 +2296,47 @@ resource "aws_s3_bucket" "bucket" {
`, randInt)
}

func testAccAWSS3BucketConfig_withNoTags(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = %[1]q
acl = "private"
force_destroy = false
}
`, bucketName)
}

func testAccAWSS3BucketConfig_withTags(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = %[1]q
acl = "private"
force_destroy = false
tags = {
Key1 = "AAA"
Key2 = "BBB"
Key3 = "CCC"
}
}
`, bucketName)
}

func testAccAWSS3BucketConfig_withUpdatedTags(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = %[1]q
acl = "private"
force_destroy = false
tags = {
Key2 = "BBB"
Key3 = "XXX"
Key4 = "DDD"
Key5 = "EEE"
}
}
`, bucketName)
}

func testAccAWSS3MultiBucketConfigWithTags(randInt int) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket1" {
Expand Down
Loading