Skip to content

Commit

Permalink
Merge pull request #5020 from terraform-providers/f-aws_ssm_document-…
Browse files Browse the repository at this point in the history
…tags

resource/aws_ssm_document: Support tagging
  • Loading branch information
bflad authored Jun 29, 2018
2 parents 0cd9653 + 37249a5 commit 217c7ff
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aws/resource_aws_ssm_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func resourceAwsSsmDocument() *schema.Resource {
},
},
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -181,6 +182,10 @@ func resourceAwsSsmDocumentCreate(d *schema.ResourceData, meta interface{}) erro
log.Printf("[DEBUG] Not setting permissions for %q", d.Id())
}

if err := setTagsSSM(ssmconn, d, d.Id(), ssm.ResourceTypeForTaggingDocument); err != nil {
return fmt.Errorf("error setting SSM Document tags: %s", err)
}

return resourceAwsSsmDocumentRead(d, meta)
}

Expand Down Expand Up @@ -271,10 +276,26 @@ func resourceAwsSsmDocumentRead(d *schema.ResourceData, meta interface{}) error
return err
}

tagList, err := ssmconn.ListTagsForResource(&ssm.ListTagsForResourceInput{
ResourceId: aws.String(d.Id()),
ResourceType: aws.String(ssm.ResourceTypeForTaggingDocument),
})
if err != nil {
return fmt.Errorf("error listing SSM Document tags for %s: %s", d.Id(), err)
}
d.Set("tags", tagsToMapSSM(tagList.TagList))

return nil
}

func resourceAwsSsmDocumentUpdate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

if d.HasChange("tags") {
if err := setTagsSSM(ssmconn, d, d.Id(), ssm.ResourceTypeForTaggingDocument); err != nil {
return fmt.Errorf("error setting SSM Document tags: %s", err)
}
}

if _, ok := d.GetOk("permissions"); ok {
if err := setDocumentPermissions(d, meta); err != nil {
Expand Down
106 changes: 106 additions & 0 deletions aws/resource_aws_ssm_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccAWSSSMDocument_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_ssm_document.foo", "document_format", "JSON"),
resource.TestMatchResourceAttr("aws_ssm_document.foo", "arn",
regexp.MustCompile(`^arn:aws:ssm:[a-z]{2}-[a-z]+-\d{1}:\d{12}:document/.*$`)),
resource.TestCheckResourceAttr("aws_ssm_document.foo", "tags.%", "0"),
),
},
},
Expand Down Expand Up @@ -184,6 +185,44 @@ mainSteps:
})
}

func TestAccAWSSSMDocument_Tags(t *testing.T) {
rName := acctest.RandString(10)
resourceName := "aws_ssm_document.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMDocumentConfig_Tags_Single(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Config: testAccAWSSSMDocumentConfig_Tags_Multiple(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSSSMDocumentConfig_Tags_Single(rName, "key2", "value2updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2updated"),
),
},
},
})
}

func testAccCheckAWSSSMDocumentExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -518,3 +557,70 @@ DOC
}
`, rName, content)
}

func testAccAWSSSMDocumentConfig_Tags_Single(rName, key1, value1 string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
document_type = "Command"
name = "test_document-%s"
content = <<DOC
{
"schemaVersion": "1.2",
"description": "Check ip configuration of a Linux instance.",
"parameters": {
},
"runtimeConfig": {
"aws:runShellScript": {
"properties": [
{
"id": "0.aws:runShellScript",
"runCommand": ["ifconfig"]
}
]
}
}
}
DOC
tags {
%s = %q
}
}
`, rName, key1, value1)
}

func testAccAWSSSMDocumentConfig_Tags_Multiple(rName, key1, value1, key2, value2 string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
document_type = "Command"
name = "test_document-%s"
content = <<DOC
{
"schemaVersion": "1.2",
"description": "Check ip configuration of a Linux instance.",
"parameters": {
},
"runtimeConfig": {
"aws:runShellScript": {
"properties": [
{
"id": "0.aws:runShellScript",
"runCommand": ["ifconfig"]
}
]
}
}
}
DOC
tags {
%s = %q
%s = %q
}
}
`, rName, key1, value1, key2, value2)
}
1 change: 1 addition & 0 deletions website/docs/r/ssm_document.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The following arguments are supported:
* `document_format` - (Optional, defaults to JSON) The format of the document. Valid document types include: `JSON` and `YAML`
* `document_type` - (Required) The type of the document. Valid document types include: `Command`, `Policy` and `Automation`
* `permissions` - (Optional) Additional Permissions to attach to the document. See [Permissions](#permissions) below for details.
* `tags` - (Optional) A mapping of tags to assign to the object.

## Attributes Reference

Expand Down

0 comments on commit 217c7ff

Please sign in to comment.