From 82578d4a738d03325fae5e8c65fbe0526ab64695 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 28 Nov 2020 21:00:42 +0200 Subject: [PATCH 1/2] remove from state if removed --- ...ource_aws_ssm_maintenance_window_target.go | 45 ++++++++++++------- ..._aws_ssm_maintenance_window_target_test.go | 39 +++++++++------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/aws/resource_aws_ssm_maintenance_window_target.go b/aws/resource_aws_ssm_maintenance_window_target.go index f552dfc6f32..987a1558cab 100644 --- a/aws/resource_aws_ssm_maintenance_window_target.go +++ b/aws/resource_aws_ssm_maintenance_window_target.go @@ -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": { @@ -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}, }, }, @@ -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), }, }, } @@ -116,7 +119,7 @@ func resourceAwsSsmMaintenanceWindowTargetCreate(d *schema.ResourceData, meta in return err } - d.SetId(*resp.WindowTargetId) + d.SetId(aws.StringValue(resp.WindowTargetId)) return resourceAwsSsmMaintenanceWindowTargetRead(d, meta) } @@ -124,8 +127,9 @@ func resourceAwsSsmMaintenanceWindowTargetCreate(d *schema.ResourceData, meta in 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"), @@ -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) @@ -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 error: %w", err) } } } @@ -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 @@ -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 diff --git a/aws/resource_aws_ssm_maintenance_window_target_test.go b/aws/resource_aws_ssm_maintenance_window_target_test.go index 58deea89b5c..0112b2b50ee 100644 --- a/aws/resource_aws_ssm_maintenance_window_target_test.go +++ b/aws/resource_aws_ssm_maintenance_window_target_test.go @@ -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, }, @@ -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 } @@ -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 From 3971204a9ac1f96c1f58f573d4f038800e55ccee Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 30 Nov 2020 21:02:54 +0200 Subject: [PATCH 2/2] Update aws/resource_aws_ssm_maintenance_window_target.go Co-authored-by: Kit Ewbank --- aws/resource_aws_ssm_maintenance_window_target.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_ssm_maintenance_window_target.go b/aws/resource_aws_ssm_maintenance_window_target.go index 987a1558cab..3f06c414d1e 100644 --- a/aws/resource_aws_ssm_maintenance_window_target.go +++ b/aws/resource_aws_ssm_maintenance_window_target.go @@ -160,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: %w", err) + return fmt.Errorf("Error setting targets: %w", err) } } }