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

Add support resource tags for elastic beanstalk resources #8614

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
15 changes: 15 additions & 0 deletions aws/resource_aws_elastic_beanstalk_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func resourceAwsElasticBeanstalkApplication() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -58,6 +62,7 @@ func resourceAwsElasticBeanstalkApplication() *schema.Resource {
},
},
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -74,6 +79,7 @@ func resourceAwsElasticBeanstalkApplicationCreate(d *schema.ResourceData, meta i
req := &elasticbeanstalk.CreateApplicationInput{
ApplicationName: aws.String(name),
Description: aws.String(description),
Tags: tagsFromMapBeanstalk(d.Get("tags").(map[string]interface{})),
}

app, err := beanstalkConn.CreateApplication(req)
Expand Down Expand Up @@ -105,6 +111,10 @@ func resourceAwsElasticBeanstalkApplicationUpdate(d *schema.ResourceData, meta i
}
}

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

return resourceAwsElasticBeanstalkApplicationRead(d, meta)
}

Expand Down Expand Up @@ -223,13 +233,18 @@ func resourceAwsElasticBeanstalkApplicationRead(d *schema.ResourceData, meta int
return err
}

d.Set("arn", app.ApplicationArn)
d.Set("name", app.ApplicationName)
d.Set("description", app.Description)

if app.ResourceLifecycleConfig != nil {
d.Set("appversion_lifecycle", flattenResourceLifecycleConfig(app.ResourceLifecycleConfig))
}

if err := saveTagsBeanstalk(conn, d, aws.StringValue(app.ApplicationArn)); err != nil {
return fmt.Errorf("error saving tags for %s: %s", d.Id(), err)
}

return nil
}

Expand Down
85 changes: 85 additions & 0 deletions aws/resource_aws_elastic_beanstalk_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,62 @@ func TestAccAWSBeanstalkApp_appversionlifecycle(t *testing.T) {
})
}

func TestAccAWSBeanstalkApp_tags(t *testing.T) {
var app elasticbeanstalk.ApplicationDescription
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elastic_beanstalk_application.tftest"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBeanstalkAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccBeanstalkAppConfigWithTags(rName, "test1", "test2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "test1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "test2"),
),
},
{
Config: testAccBeanstalkAppConfigWithTags(rName, "updateTest1", "updateTest2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
),
},
{
Config: testAccBeanstalkAppConfigWithAddTags(rName, "updateTest1", "updateTest2", "addTest3"),
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
resource.TestCheckResourceAttr(resourceName, "tags.thirdTag", "addTest3"),
),
},
{
Config: testAccBeanstalkAppConfigWithTags(rName, "updateTest1", "updateTest2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.firstTag", "updateTest1"),
resource.TestCheckResourceAttr(resourceName, "tags.secondTag", "updateTest2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

Expand Down Expand Up @@ -320,3 +376,32 @@ resource "aws_elastic_beanstalk_application" "tftest" {
}
`, rName)
}

func testAccBeanstalkAppConfigWithTags(rName, tag1, tag2 string) string {
return fmt.Sprintf(`
resource "aws_elastic_beanstalk_application" "tftest" {
name = "%s"
description = "tf-test-desc"

tags = {
firstTag = "%s"
secondTag = "%s"
}
}
`, rName, tag1, tag2)
}

func testAccBeanstalkAppConfigWithAddTags(rName, tag1, tag2, tag3 string) string {
return fmt.Sprintf(`
resource "aws_elastic_beanstalk_application" "tftest" {
name = "%s"
description = "tf-test-desc"

tags = {
firstTag = "%s"
secondTag = "%s"
thirdTag = "%s"
}
}
`, rName, tag1, tag2, tag3)
}
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)
}
39 changes: 39 additions & 0 deletions aws/tagsBeanstalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,47 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/hashicorp/terraform/helper/schema"
)

// saveTagsBeanstalk is a helper to save the tags for a resource. It expects the
// tags field to be named "tags"
func saveTagsBeanstalk(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData, arn string) error {
resp, err := conn.ListTagsForResource(&elasticbeanstalk.ListTagsForResourceInput{
ResourceArn: aws.String(arn),
})
if err != nil {
return err
}

if err := d.Set("tags", tagsToMapBeanstalk(resp.ResourceTags)); err != nil {
return err
}

return nil
}

// setTags is a helper to set the tags for a resource. It expects the
// tags field to be named "tags"
func setTagsBeanstalk(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData, arn string) error {
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
add, remove := diffTagsBeanstalk(tagsFromMapBeanstalk(o), tagsFromMapBeanstalk(n))

if _, err := conn.UpdateTagsForResource(&elasticbeanstalk.UpdateTagsForResourceInput{
ResourceArn: aws.String(arn),
TagsToAdd: add,
TagsToRemove: remove,
}); err != nil {
return err
}
}

return nil
}

// diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/elastic_beanstalk_application.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Application version lifecycle (`appversion_lifecycle`) supports the following se
* `max_count` - (Optional) The maximum number of application versions to retain.
* `max_age_in_days` - (Optional) The number of days to retain an application version.
* `delete_source_from_s3` - (Optional) Set to `true` to delete a version's source bundle from S3 when the application version is deleted.
* `tags` - Key-value mapping of tags for the Elastic Beanstalk Application.

## Attributes Reference

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

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


## Import
Expand Down
Loading