diff --git a/aws/resource_aws_media_package_channel.go b/aws/resource_aws_media_package_channel.go index 356d3241ac0..81481a8000b 100644 --- a/aws/resource_aws_media_package_channel.go +++ b/aws/resource_aws_media_package_channel.go @@ -66,6 +66,7 @@ func resourceAwsMediaPackageChannel() *schema.Resource { }, }, }, + "tags": tagsSchema(), }, } } @@ -78,6 +79,10 @@ func resourceAwsMediaPackageChannelCreate(d *schema.ResourceData, meta interface Description: aws.String(d.Get("description").(string)), } + if attr, ok := d.GetOk("tags"); ok { + input.Tags = tagsFromMapGeneric(attr.(map[string]interface{})) + } + _, err := conn.CreateChannel(input) if err != nil { return fmt.Errorf("error creating MediaPackage Channel: %s", err) @@ -105,6 +110,10 @@ func resourceAwsMediaPackageChannelRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("error setting hls_ingest: %s", err) } + if err := d.Set("tags", tagsToMapGeneric(resp.Tags)); err != nil { + return fmt.Errorf("error setting tags: %s", err) + } + return nil } @@ -121,6 +130,10 @@ func resourceAwsMediaPackageChannelUpdate(d *schema.ResourceData, meta interface return fmt.Errorf("error updating MediaPackage Channel: %s", err) } + if err := setTagsMediaPackage(conn, d, d.Get("arn").(string)); err != nil { + return fmt.Errorf("error updating MediaPackage Channel (%s) tags: %s", d.Id(), err) + } + return resourceAwsMediaPackageChannelRead(d, meta) } diff --git a/aws/resource_aws_media_package_channel_test.go b/aws/resource_aws_media_package_channel_test.go index 89e9a73fbd2..537ff0b6d86 100644 --- a/aws/resource_aws_media_package_channel_test.go +++ b/aws/resource_aws_media_package_channel_test.go @@ -74,6 +74,49 @@ func TestAccAWSMediaPackageChannel_description(t *testing.T) { }) } +func TestAccAWSMediaPackageChannel_tags(t *testing.T) { + resourceName := "aws_media_package_channel.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsMediaPackageChannelDestroy, + Steps: []resource.TestStep{ + { + Config: testAccMediaPackageChannelConfigWithTags(rName, "Environment", "test"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsMediaPackageChannelExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.Name", rName), + resource.TestCheckResourceAttr(resourceName, "tags.Environment", "test"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccMediaPackageChannelConfigWithTags(rName, "Environment", "test1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsMediaPackageChannelExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.Environment", "test1"), + ), + }, + { + Config: testAccMediaPackageChannelConfigWithTags(rName, "Update", "true"), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsMediaPackageChannelExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.Update", "true"), + ), + }, + }, + }) +} + func testAccCheckAwsMediaPackageChannelDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).mediapackageconn @@ -132,3 +175,15 @@ resource "aws_media_package_channel" "test" { description = %q }`, rName, description) } + +func testAccMediaPackageChannelConfigWithTags(rName, key, value string) string { + return fmt.Sprintf(` +resource "aws_media_package_channel" "test" { + channel_id = "%[1]s" + + tags = { + Name = "%[1]s" + %[2]s = "%[3]s" + } +}`, rName, key, value) +} diff --git a/aws/tagsMediapackage.go b/aws/tagsMediapackage.go new file mode 100644 index 00000000000..790933098e9 --- /dev/null +++ b/aws/tagsMediapackage.go @@ -0,0 +1,47 @@ +package aws + +import ( + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/mediapackage" + "github.com/hashicorp/terraform/helper/schema" +) + +func setTagsMediaPackage(conn *mediapackage.MediaPackage, 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{}) + create, remove := diffTagsGeneric(o, n) + + // Set tags + if len(remove) > 0 { + log.Printf("[DEBUG] Removing tags: %#v", remove) + keys := make([]*string, 0, len(remove)) + for k := range remove { + keys = append(keys, aws.String(k)) + } + + _, err := conn.UntagResource(&mediapackage.UntagResourceInput{ + ResourceArn: aws.String(arn), + TagKeys: keys, + }) + if err != nil { + return err + } + } + if len(create) > 0 { + log.Printf("[DEBUG] Creating tags: %#v", create) + _, err := conn.TagResource(&mediapackage.TagResourceInput{ + ResourceArn: aws.String(arn), + Tags: create, + }) + if err != nil { + return err + } + } + } + + return nil +} diff --git a/website/docs/r/media_package_channel.html.markdown b/website/docs/r/media_package_channel.html.markdown index 3b67820d440..09bbdfaa449 100644 --- a/website/docs/r/media_package_channel.html.markdown +++ b/website/docs/r/media_package_channel.html.markdown @@ -25,6 +25,7 @@ The following arguments are supported: * `channel_id` - (Required) A unique identifier describing the channel * `description` - (Optional) A description of the channel +* `tags` - (Optional) A mapping of tags to assign to the resource. ## Attributes Reference