diff --git a/aws/resource_aws_ssm_association.go b/aws/resource_aws_ssm_association.go index b65dd858453..e459ab878a1 100644 --- a/aws/resource_aws_ssm_association.go +++ b/aws/resource_aws_ssm_association.go @@ -122,7 +122,7 @@ func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) e } if _, ok := d.GetOk("targets"); ok { - assosciationInput.Targets = expandAwsSsmTargets(d) + assosciationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{})) } if v, ok := d.GetOk("output_location"); ok { @@ -216,7 +216,7 @@ func resourceAwsSsmAssocationUpdate(d *schema.ResourceData, meta interface{}) er } if d.HasChange("targets") { - associationInput.Targets = expandAwsSsmTargets(d) + associationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{})) } _, err := ssmconn.UpdateAssociation(associationInput) diff --git a/aws/resource_aws_ssm_maintenance_window_target.go b/aws/resource_aws_ssm_maintenance_window_target.go index 20fbd3bc768..868323835a0 100644 --- a/aws/resource_aws_ssm_maintenance_window_target.go +++ b/aws/resource_aws_ssm_maintenance_window_target.go @@ -13,6 +13,7 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource { return &schema.Resource{ Create: resourceAwsSsmMaintenanceWindowTargetCreate, Read: resourceAwsSsmMaintenanceWindowTargetRead, + Update: resourceAwsSsmMaintenanceWindowTargetUpdate, Delete: resourceAwsSsmMaintenanceWindowTargetDelete, Schema: map[string]*schema.Schema{ @@ -31,7 +32,6 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource { "targets": { Type: schema.TypeList, Required: true, - ForceNew: true, MaxItems: 5, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -50,7 +50,6 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource { "owner_information": { Type: schema.TypeString, - ForceNew: true, Optional: true, }, }, @@ -65,7 +64,7 @@ func resourceAwsSsmMaintenanceWindowTargetCreate(d *schema.ResourceData, meta in params := &ssm.RegisterTargetWithMaintenanceWindowInput{ WindowId: aws.String(d.Get("window_id").(string)), ResourceType: aws.String(d.Get("resource_type").(string)), - Targets: expandAwsSsmTargets(d), + Targets: expandAwsSsmTargets(d.Get("targets").([]interface{})), } if v, ok := d.GetOk("owner_information"); ok { @@ -124,6 +123,29 @@ func resourceAwsSsmMaintenanceWindowTargetRead(d *schema.ResourceData, meta inte return nil } +func resourceAwsSsmMaintenanceWindowTargetUpdate(d *schema.ResourceData, meta interface{}) error { + ssmconn := meta.(*AWSClient).ssmconn + + log.Printf("[INFO] Updating SSM Maintenance Window Target: %s", d.Id()) + + params := &ssm.UpdateMaintenanceWindowTargetInput{ + Targets: expandAwsSsmTargets(d.Get("targets").([]interface{})), + WindowId: aws.String(d.Get("window_id").(string)), + WindowTargetId: aws.String(d.Id()), + } + + if d.HasChange("owner_information") { + params.OwnerInformation = aws.String(d.Get("owner_information").(string)) + } + + _, err := ssmconn.UpdateMaintenanceWindowTarget(params) + if err != nil { + return err + } + + return nil +} + func resourceAwsSsmMaintenanceWindowTargetDelete(d *schema.ResourceData, meta interface{}) error { ssmconn := meta.(*AWSClient).ssmconn diff --git a/aws/resource_aws_ssm_maintenance_window_target_test.go b/aws/resource_aws_ssm_maintenance_window_target_test.go index 5ff21733b6e..f14c56e07fe 100644 --- a/aws/resource_aws_ssm_maintenance_window_target_test.go +++ b/aws/resource_aws_ssm_maintenance_window_target_test.go @@ -24,7 +24,49 @@ func TestAccAWSSSMMaintenanceWindowTarget_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSSSMMaintenanceWindowTargetExists("aws_ssm_maintenance_window_target.target"), resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.key", "tag:Name"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.#", "1"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.0", "acceptance_test"), resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.key", "tag:Name2"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.#", "2"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.0", "acceptance_test"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.1", "acceptance_test2"), + ), + }, + }, + }) +} + +func TestAccAWSSSMMaintenanceWindowTarget_update(t *testing.T) { + name := acctest.RandString(10) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSSMMaintenanceWindowTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSSMMaintenanceWindowTargetBasicConfig(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSSMMaintenanceWindowTargetExists("aws_ssm_maintenance_window_target.target"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.key", "tag:Name"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.#", "1"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.0", "acceptance_test"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.key", "tag:Name2"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.#", "2"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.0", "acceptance_test"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.1", "acceptance_test2"), + ), + }, + { + Config: testAccAWSSSMMaintenanceWindowTargetBasicConfigUpdated(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSSMMaintenanceWindowTargetExists("aws_ssm_maintenance_window_target.target"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "owner_information", "something"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.key", "tag:Name"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.#", "1"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.0.values.0", "acceptance_test"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.key", "tag:Updated"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.#", "1"), + resource.TestCheckResourceAttr("aws_ssm_maintenance_window_target.target", "targets.1.values.0", "new-value"), ), }, }, @@ -126,3 +168,28 @@ resource "aws_ssm_maintenance_window_target" "target" { } `, rName) } + +func testAccAWSSSMMaintenanceWindowTargetBasicConfigUpdated(rName string) string { + return fmt.Sprintf(` +resource "aws_ssm_maintenance_window" "foo" { + name = "maintenance-window-%s" + schedule = "cron(0 16 ? * TUE *)" + duration = 3 + cutoff = 1 +} + +resource "aws_ssm_maintenance_window_target" "target" { + window_id = "${aws_ssm_maintenance_window.foo.id}" + resource_type = "INSTANCE" + owner_information = "something" + targets { + key = "tag:Name" + values = ["acceptance_test"] + } + targets { + key = "tag:Updated" + values = ["new-value"] + } +} +`, rName) +} diff --git a/aws/resource_aws_ssm_maintenance_window_task.go b/aws/resource_aws_ssm_maintenance_window_task.go index a45aa7db843..072b5877c80 100644 --- a/aws/resource_aws_ssm_maintenance_window_task.go +++ b/aws/resource_aws_ssm_maintenance_window_task.go @@ -191,7 +191,7 @@ func resourceAwsSsmMaintenanceWindowTaskCreate(d *schema.ResourceData, meta inte TaskType: aws.String(d.Get("task_type").(string)), ServiceRoleArn: aws.String(d.Get("service_role_arn").(string)), TaskArn: aws.String(d.Get("task_arn").(string)), - Targets: expandAwsSsmTargets(d), + Targets: expandAwsSsmTargets(d.Get("targets").([]interface{})), } if v, ok := d.GetOk("priority"); ok { diff --git a/aws/structure.go b/aws/structure.go index 4cf4a123270..079388eb35f 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -3156,12 +3156,10 @@ func sliceContainsMap(l []interface{}, m map[string]interface{}) (int, bool) { return -1, false } -func expandAwsSsmTargets(d *schema.ResourceData) []*ssm.Target { +func expandAwsSsmTargets(in []interface{}) []*ssm.Target { targets := make([]*ssm.Target, 0) - targetConfig := d.Get("targets").([]interface{}) - - for _, tConfig := range targetConfig { + for _, tConfig := range in { config := tConfig.(map[string]interface{}) target := &ssm.Target{