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

r/ssm_maintainance_window_target - remove from state if deleted #16478

Merged
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
45 changes: 29 additions & 16 deletions aws/resource_aws_ssm_maintenance_window_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {
},

"resource_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
ssm.MaintenanceWindowResourceTypeInstance,
ssm.MaintenanceWindowResourceTypeResourceGroup,
}, true),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ssm.MaintenanceWindowResourceType_Values(), true),
},

"targets": {
Expand All @@ -56,10 +53,15 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {
"key": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.All(
validation.StringMatch(regexp.MustCompile(`^[\p{L}\p{Z}\p{N}_.:/=\-@]*$|resource-groups:ResourceTypeFilters|resource-groups:Name$`), ""),
validation.StringLenBetween(1, 163),
),
},
"values": {
Type: schema.TypeList,
Required: true,
MaxItems: 50,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand All @@ -81,8 +83,9 @@ func resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {
},

"owner_information": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
},
}
Expand Down Expand Up @@ -116,16 +119,17 @@ func resourceAwsSsmMaintenanceWindowTargetCreate(d *schema.ResourceData, meta in
return err
}

d.SetId(*resp.WindowTargetId)
d.SetId(aws.StringValue(resp.WindowTargetId))

return resourceAwsSsmMaintenanceWindowTargetRead(d, meta)
}

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

windowID := d.Get("window_id").(string)
params := &ssm.DescribeMaintenanceWindowTargetsInput{
WindowId: aws.String(d.Get("window_id").(string)),
WindowId: aws.String(windowID),
Filters: []*ssm.MaintenanceWindowFilter{
{
Key: aws.String("WindowTargetId"),
Expand All @@ -135,13 +139,18 @@ func resourceAwsSsmMaintenanceWindowTargetRead(d *schema.ResourceData, meta inte
}

resp, err := ssmconn.DescribeMaintenanceWindowTargets(params)
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
log.Printf("[WARN] Maintenance Window (%s) Target (%s) not found, removing from state", windowID, d.Id())
d.SetId("")
return nil
}
if err != nil {
return err
return fmt.Errorf("Error getting Maintenance Window (%s) Target (%s): %w", windowID, d.Id(), err)
}

found := false
for _, t := range resp.Targets {
if *t.WindowTargetId == d.Id() {
if aws.StringValue(t.WindowTargetId) == d.Id() {
found = true

d.Set("owner_information", t.OwnerInformation)
Expand All @@ -151,7 +160,7 @@ func resourceAwsSsmMaintenanceWindowTargetRead(d *schema.ResourceData, meta inte
d.Set("description", t.Description)

if err := d.Set("targets", flattenAwsSsmTargets(t.Targets)); err != nil {
return fmt.Errorf("Error setting targets error: %#v", err)
return fmt.Errorf("Error setting targets: %w", err)
}
}
}
Expand Down Expand Up @@ -190,7 +199,7 @@ func resourceAwsSsmMaintenanceWindowTargetUpdate(d *schema.ResourceData, meta in

_, err := ssmconn.UpdateMaintenanceWindowTarget(params)
if err != nil {
return fmt.Errorf("error updating SSM Maintenance Window Target (%s): %s", d.Id(), err)
return fmt.Errorf("error updating SSM Maintenance Window Target (%s): %w", d.Id(), err)
}

return nil
Expand All @@ -207,8 +216,12 @@ func resourceAwsSsmMaintenanceWindowTargetDelete(d *schema.ResourceData, meta in
}

_, err := ssmconn.DeregisterTargetFromMaintenanceWindow(params)
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
return nil
}

if err != nil {
return fmt.Errorf("error deregistering SSM Maintenance Window Target (%s): %s", d.Id(), err)
return fmt.Errorf("error deregistering SSM Maintenance Window Target (%s): %w", d.Id(), err)
}

return nil
Expand Down
39 changes: 23 additions & 16 deletions aws/resource_aws_ssm_maintenance_window_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,28 @@ func TestAccAWSSSMMaintenanceWindowTarget_disappears(t *testing.T) {
Config: testAccAWSSSMMaintenanceWindowTargetBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowTargetExists(resourceName, &maint),
testAccCheckAWSSSMMaintenanceWindowTargetDisappears(&maint),
testAccCheckResourceDisappears(testAccProvider, resourceAwsSsmMaintenanceWindowTarget(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSSSMMaintenanceWindowTarget_disappears_window(t *testing.T) {
var maint ssm.MaintenanceWindowTarget
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ssm_maintenance_window_target.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMMaintenanceWindowTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMMaintenanceWindowTargetBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowTargetExists(resourceName, &maint),
testAccCheckResourceDisappears(testAccProvider, resourceAwsSsmMaintenanceWindow(), "aws_ssm_maintenance_window.test"),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -239,7 +260,7 @@ func testAccCheckAWSSSMMaintenanceWindowTargetExists(n string, mWindTarget *ssm.
}

for _, i := range resp.Targets {
if *i.WindowTargetId == rs.Primary.ID {
if aws.StringValue(i.WindowTargetId) == rs.Primary.ID {
*mWindTarget = *resp.Targets[0]
return nil
}
Expand All @@ -249,20 +270,6 @@ func testAccCheckAWSSSMMaintenanceWindowTargetExists(n string, mWindTarget *ssm.
}
}

func testAccCheckAWSSSMMaintenanceWindowTargetDisappears(mWindTarget *ssm.MaintenanceWindowTarget) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ssmconn
input := &ssm.DeregisterTargetFromMaintenanceWindowInput{
WindowId: mWindTarget.WindowId,
WindowTargetId: mWindTarget.WindowTargetId,
}

_, err := conn.DeregisterTargetFromMaintenanceWindow(input)

return err
}
}

func testAccCheckAWSSSMMaintenanceWindowTargetDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ssmconn

Expand Down