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 tags attribute for resource_aws_media_package_channel #7984

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
13 changes: 13 additions & 0 deletions aws/resource_aws_media_package_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func resourceAwsMediaPackageChannel() *schema.Resource {
},
},
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down
55 changes: 55 additions & 0 deletions aws/resource_aws_media_package_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
47 changes: 47 additions & 0 deletions aws/tagsMediapackage.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions website/docs/r/media_package_channel.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down