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

resource/aws_ssm_association: Allow updating targets #2807

Merged
merged 3 commits into from
Feb 12, 2018
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
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