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_maintenance_window_target: Make resource updatable #4074

Merged
merged 2 commits into from
Apr 5, 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
4 changes: 2 additions & 2 deletions aws/resource_aws_ssm_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 25 additions & 3 deletions aws/resource_aws_ssm_maintenance_window_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmMaintenanceWindowTargetCreate,
Read: resourceAwsSsmMaintenanceWindowTargetRead,
Update: resourceAwsSsmMaintenanceWindowTargetUpdate,
Delete: resourceAwsSsmMaintenanceWindowTargetDelete,

Schema: map[string]*schema.Schema{
Expand All @@ -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{
Expand All @@ -50,7 +50,6 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {

"owner_information": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
},
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
67 changes: 67 additions & 0 deletions aws/resource_aws_ssm_maintenance_window_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion aws/resource_aws_ssm_maintenance_window_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

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{
Expand Down