Skip to content

Commit

Permalink
Add resource tag support for aws_beanstalk_application_version resource
Browse files Browse the repository at this point in the history
  • Loading branch information
teraken0509 committed May 13, 2019
1 parent 6d337f5 commit ba7aff6
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
18 changes: 18 additions & 0 deletions aws/resource_aws_elastic_beanstalk_application_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func resourceAwsElasticBeanstalkApplicationVersion() *schema.Resource {
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -48,6 +52,7 @@ func resourceAwsElasticBeanstalkApplicationVersion() *schema.Resource {
Optional: true,
Default: false,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -71,6 +76,7 @@ func resourceAwsElasticBeanstalkApplicationVersionCreate(d *schema.ResourceData,
Description: aws.String(description),
SourceBundle: &s3Location,
VersionLabel: aws.String(name),
Tags: tagsFromMapBeanstalk(d.Get("tags").(map[string]interface{})),
}

log.Printf("[DEBUG] Elastic Beanstalk Application Version create opts: %s", createOpts)
Expand Down Expand Up @@ -111,6 +117,14 @@ func resourceAwsElasticBeanstalkApplicationVersionRead(d *schema.ResourceData, m
return err
}

if err := d.Set("arn", resp.ApplicationVersions[0].ApplicationVersionArn); err != nil {
return err
}

if err := saveTagsBeanstalk(conn, d, aws.StringValue(resp.ApplicationVersions[0].ApplicationVersionArn)); err != nil {
return fmt.Errorf("error saving tags for %s: %s", d.Id(), err)
}

return nil
}

Expand All @@ -123,6 +137,10 @@ func resourceAwsElasticBeanstalkApplicationVersionUpdate(d *schema.ResourceData,
}
}

if err := setTagsBeanstalk(conn, d, d.Get("arn").(string)); err != nil {
return fmt.Errorf("error setting tags for %s: %s", d.Id(), err)
}

return resourceAwsElasticBeanstalkApplicationVersionRead(d, meta)

}
Expand Down
113 changes: 113 additions & 0 deletions aws/resource_aws_elastic_beanstalk_application_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,56 @@ func TestAccAWSBeanstalkAppVersion_duplicateLabels(t *testing.T) {
})
}

func TestAccAWSBeanstalkAppVersion_tags(t *testing.T) {
var appVersion elasticbeanstalk.ApplicationVersionDescription
resourceName := "aws_elastic_beanstalk_application_version.default"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckApplicationVersionDestroy,
Steps: []resource.TestStep{
{
Config: testAccBeanstalkApplicationVersionConfigWithTags(acctest.RandInt(), "test1", "test2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckApplicationVersionExists(resourceName, &appVersion),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "test1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "test2"),
),
},
{
Config: testAccBeanstalkApplicationVersionConfigWithTags(acctest.RandInt(), "updateTest1", "updateTest2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckApplicationVersionExists(resourceName, &appVersion),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
),
},
{
Config: testAccBeanstalkApplicationVersionConfigWithAddTags(acctest.RandInt(), "updateTest1", "updateTest2", "addTest3"),
Check: resource.ComposeTestCheckFunc(
testAccCheckApplicationVersionExists(resourceName, &appVersion),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
resource.TestCheckResourceAttr(resourceName, "tags.thirdTag", "addTest3"),
),
},
{
Config: testAccBeanstalkApplicationVersionConfigWithTags(acctest.RandInt(), "updateTest1", "updateTest2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckApplicationVersionExists(resourceName, &appVersion),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
),
},
},
})
}

func testAccCheckApplicationVersionDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn

Expand Down Expand Up @@ -179,3 +229,66 @@ resource "aws_elastic_beanstalk_application_version" "second" {
}
`, randInt, randInt, randInt, randInt, randInt)
}

func testAccBeanstalkApplicationVersionConfigWithTags(randInt int, tag1, tag2 string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "default" {
bucket = "tftest.applicationversion.bucket-%[1]d"
}
resource "aws_s3_bucket_object" "default" {
bucket = "${aws_s3_bucket.default.id}"
key = "beanstalk/python-v1.zip"
source = "test-fixtures/python-v1.zip"
}
resource "aws_elastic_beanstalk_application" "default" {
name = "tf-test-name-%[1]d"
description = "tf-test-desc"
}
resource "aws_elastic_beanstalk_application_version" "default" {
application = "${aws_elastic_beanstalk_application.default.name}"
name = "tf-test-version-label-%[1]d"
bucket = "${aws_s3_bucket.default.id}"
key = "${aws_s3_bucket_object.default.id}"
tags = {
firstTag = "%[2]s"
secondTag = "%[3]s"
}
}
`, randInt, tag1, tag2)
}

func testAccBeanstalkApplicationVersionConfigWithAddTags(randInt int, tag1, tag2, tag3 string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "default" {
bucket = "tftest.applicationversion.bucket-%[1]d"
}
resource "aws_s3_bucket_object" "default" {
bucket = "${aws_s3_bucket.default.id}"
key = "beanstalk/python-v1.zip"
source = "test-fixtures/python-v1.zip"
}
resource "aws_elastic_beanstalk_application" "default" {
name = "tf-test-name-%[1]d"
description = "tf-test-desc"
}
resource "aws_elastic_beanstalk_application_version" "default" {
application = "${aws_elastic_beanstalk_application.default.name}"
name = "tf-test-version-label-%[1]d"
bucket = "${aws_s3_bucket.default.id}"
key = "${aws_s3_bucket_object.default.id}"
tags = {
firstTag = "%[2]s"
secondTag = "%[3]s"
thirdTag = "%[4]s"
}
}
`, randInt, tag1, tag2, tag3)
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ The following arguments are supported:
* `key` - (Required) S3 object that is the Application Version source bundle.
* `force_delete` - (Optional) On delete, force an Application Version to be deleted when it may be in use
by multiple Elastic Beanstalk Environments.
* `tags` - Key-value mapping of tags for the Elastic Beanstalk Application Version.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `name` - The Application Version name.
* `arn` - The ARN assigned by AWS for this Elastic Beanstalk Application.

0 comments on commit ba7aff6

Please sign in to comment.