Skip to content

Commit

Permalink
resource/aws_ssm_association: Allow updating targets (#2807)
Browse files Browse the repository at this point in the history
* Update ssm_association

* rebase & update

* Fix casing + typo
  • Loading branch information
atsushi-ishibashi authored and radeksimko committed Feb 12, 2018
1 parent c574356 commit 00dc965
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 62 deletions.
5 changes: 4 additions & 1 deletion aws/resource_aws_ssm_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func resourceAwsSsmAssociation() *schema.Resource {
"targets": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Computed: true,
MaxItems: 5,
Elem: &schema.Resource{
Expand Down Expand Up @@ -213,6 +212,10 @@ func resourceAwsSsmAssocationUpdate(d *schema.ResourceData, meta interface{}) er
associationInput.OutputLocation = expandSSMAssociationOutputLocation(d.Get("output_location").([]interface{}))
}

if d.HasChange("targets") {
associationInput.Targets = expandAwsSsmTargets(d)
}

_, err := ssmconn.UpdateAssociation(associationInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating SSM association: {{err}}", err)
Expand Down
99 changes: 38 additions & 61 deletions aws/resource_aws_ssm_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,65 @@ func TestAccAWSSSMAssociation_basic(t *testing.T) {

func TestAccAWSSSMAssociation_withTargets(t *testing.T) {
name := acctest.RandString(10)
oneTarget := `
targets {
key = "tag:Name"
values = ["acceptanceTest"]
}`
twoTargets := `
targets {
key = "tag:Name"
values = ["acceptanceTest"]
}
targets {
key = "tag:ExtraName"
values = ["acceptanceTest"]
}`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMAssociationBasicConfigWithTargets(name),
Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, oneTarget),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.#", "1"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.key", "tag:Name"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"),
),
},
},
})
}

func TestAccAWSSSMAssociation_withMultipleTargets(t *testing.T) {
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMAssociationBasicConfigWithMultipleTargets(name),
Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, twoTargets),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.#", "2"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.key", "tag:Name"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.1.key", "tag:Environment"),
"aws_ssm_association.foo", "targets.1.key", "tag:ExtraName"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.1.values.0", "acceptanceTest"),
),
},
{
Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, oneTarget),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.#", "1"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.key", "tag:Name"),
resource.TestCheckResourceAttr(
"aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"),
),
},
},
})
}
Expand Down Expand Up @@ -359,10 +378,10 @@ resource "aws_ssm_association" "foo" {
}`, rName)
}

func testAccAWSSSMAssociationBasicConfigWithTargets(rName string) string {
func testAccAWSSSMAssociationBasicConfigWithTargets(rName, targetsStr string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo_document" {
name = "test_document_association-%s",
name = "test_document_association-%s"
document_type = "Command"
content = <<DOC
{
Expand All @@ -386,51 +405,9 @@ DOC
}
resource "aws_ssm_association" "foo" {
name = "${aws_ssm_document.foo_document.name}",
targets {
key = "tag:Name"
values = ["acceptanceTest"]
}
}`, rName)
}

func testAccAWSSSMAssociationBasicConfigWithMultipleTargets(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo_document" {
name = "test_document_association-%s",
document_type = "Command"
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
}
resource "aws_ssm_association" "foo" {
name = "${aws_ssm_document.foo_document.name}",
targets {
key = "tag:Name"
values = ["acceptanceTest"]
}
targets {
key = "tag:Environment"
values = ["acceptanceTest"]
}
}`, rName)
name = "${aws_ssm_document.foo_document.name}"
%s
}`, rName, targetsStr)
}

func testAccAWSSSMAssociationBasicConfig(rName string) string {
Expand Down

0 comments on commit 00dc965

Please sign in to comment.