From 549ce3614c2052d3ea8a9c9c01fe1e3286aafa86 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 3 Aug 2018 11:50:55 +0400 Subject: [PATCH 1/2] remove resource id from state if it doesn't exists --- aws/resource_aws_ssm_patch_baseline.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/aws/resource_aws_ssm_patch_baseline.go b/aws/resource_aws_ssm_patch_baseline.go index bbcf19fc704..c271c84ae40 100644 --- a/aws/resource_aws_ssm_patch_baseline.go +++ b/aws/resource_aws_ssm_patch_baseline.go @@ -218,6 +218,11 @@ func resourceAwsSsmPatchBaselineUpdate(d *schema.ResourceData, meta interface{}) _, err := ssmconn.UpdatePatchBaseline(params) if err != nil { + if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") { + log.Printf("[WARN] Patch Baseline %s not found, removing from state", d.Id()) + d.SetId("") + return nil + } return err } @@ -232,6 +237,11 @@ func resourceAwsSsmPatchBaselineRead(d *schema.ResourceData, meta interface{}) e resp, err := ssmconn.GetPatchBaseline(params) if err != nil { + if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") { + log.Printf("[WARN] Patch Baseline %s not found, removing from state", d.Id()) + d.SetId("") + return nil + } return err } From 2099ec6915118996bdcc0fe5a02b033032ad54a3 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Fri, 3 Aug 2018 12:06:48 +0400 Subject: [PATCH 2/2] add acceptance test --- aws/resource_aws_ssm_patch_baseline_test.go | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/aws/resource_aws_ssm_patch_baseline_test.go b/aws/resource_aws_ssm_patch_baseline_test.go index 8ad5ff8c28c..3ac36b80977 100644 --- a/aws/resource_aws_ssm_patch_baseline_test.go +++ b/aws/resource_aws_ssm_patch_baseline_test.go @@ -63,6 +63,28 @@ func TestAccAWSSSMPatchBaseline_basic(t *testing.T) { }) } +func TestAccAWSSSMPatchBaseline_disappears(t *testing.T) { + var identity ssm.PatchBaselineIdentity + name := acctest.RandString(10) + resourceName := "aws_ssm_patch_baseline.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSSMPatchBaselineDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSSSMPatchBaselineBasicConfig(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSSMPatchBaselineExists(resourceName, &identity), + testAccCheckAWSSSMPatchBaselineDisappears(&identity), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func TestAccAWSSSMPatchBaselineWithOperatingSystem(t *testing.T) { var before, after ssm.PatchBaselineIdentity name := acctest.RandString(10) @@ -156,6 +178,24 @@ func testAccCheckAWSSSMPatchBaselineExists(n string, patch *ssm.PatchBaselineIde } } +func testAccCheckAWSSSMPatchBaselineDisappears(patch *ssm.PatchBaselineIdentity) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).ssmconn + + id := aws.StringValue(patch.BaselineId) + params := &ssm.DeletePatchBaselineInput{ + BaselineId: aws.String(id), + } + + _, err := conn.DeletePatchBaseline(params) + if err != nil { + return fmt.Errorf("error deleting Patch Baseline %s: %s", id, err) + } + + return nil + } +} + func testAccCheckAWSSSMPatchBaselineDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ssmconn