Skip to content

Commit

Permalink
Merge pull request #5416 from saravanan30erd/issue-5412
Browse files Browse the repository at this point in the history
issue #5412 AWS SSM DoesNotExistException for missing Maintenance Window
  • Loading branch information
bflad authored Aug 2, 2018
2 parents 4013f34 + 1389092 commit 7b490dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
10 changes: 10 additions & 0 deletions aws/resource_aws_ssm_maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ func resourceAwsSsmMaintenanceWindowUpdate(d *schema.ResourceData, meta interfac

_, err := ssmconn.UpdateMaintenanceWindow(params)
if err != nil {
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
log.Printf("[WARN] Maintenance Window %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

Expand All @@ -117,6 +122,11 @@ func resourceAwsSsmMaintenanceWindowRead(d *schema.ResourceData, meta interface{

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

Expand Down
52 changes: 46 additions & 6 deletions aws/resource_aws_ssm_maintenance_window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

func TestAccAWSSSMMaintenanceWindow_basic(t *testing.T) {
var winId ssm.MaintenanceWindowIdentity
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -21,7 +23,7 @@ func TestAccAWSSSMMaintenanceWindow_basic(t *testing.T) {
{
Config: testAccAWSSSMMaintenanceWindowBasicConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowExists("aws_ssm_maintenance_window.foo"),
testAccCheckAWSSSMMaintenanceWindowExists("aws_ssm_maintenance_window.foo", &winId),
resource.TestCheckResourceAttr(
"aws_ssm_maintenance_window.foo", "schedule", "cron(0 16 ? * TUE *)"),
resource.TestCheckResourceAttr(
Expand All @@ -37,7 +39,7 @@ func TestAccAWSSSMMaintenanceWindow_basic(t *testing.T) {
{
Config: testAccAWSSSMMaintenanceWindowBasicConfigUpdated(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowExists("aws_ssm_maintenance_window.foo"),
testAccCheckAWSSSMMaintenanceWindowExists("aws_ssm_maintenance_window.foo", &winId),
resource.TestCheckResourceAttr(
"aws_ssm_maintenance_window.foo", "schedule", "cron(0 16 ? * WED *)"),
resource.TestCheckResourceAttr(
Expand All @@ -54,7 +56,29 @@ func TestAccAWSSSMMaintenanceWindow_basic(t *testing.T) {
})
}

func testAccCheckAWSSSMMaintenanceWindowExists(n string) resource.TestCheckFunc {
func TestAccAWSSSMMaintenanceWindow_disappears(t *testing.T) {
var winId ssm.MaintenanceWindowIdentity
name := acctest.RandString(10)
resourceName := "aws_ssm_maintenance_window.foo"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMMaintenanceWindowDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMMaintenanceWindowBasicConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowExists(resourceName, &winId),
testAccCheckAWSSSMMaintenanceWindowDisappears(&winId),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAWSSSMMaintenanceWindowExists(n string, res *ssm.MaintenanceWindowIdentity) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -75,20 +99,36 @@ func testAccCheckAWSSSMMaintenanceWindowExists(n string) resource.TestCheckFunc
},
},
})
if err != nil {
return err
}

for _, i := range resp.WindowIdentities {
if *i.WindowId == rs.Primary.ID {
*res = *i
return nil
}
}
if err != nil {
return err
}

return fmt.Errorf("No AWS SSM Maintenance window found")
}
}

func testAccCheckAWSSSMMaintenanceWindowDisappears(maintenanceWindowIdentity *ssm.MaintenanceWindowIdentity) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ssmconn

id := aws.StringValue(maintenanceWindowIdentity.WindowId)
_, err := conn.DeleteMaintenanceWindow(&ssm.DeleteMaintenanceWindowInput{
WindowId: aws.String(id),
})
if err != nil {
return fmt.Errorf("error deleting maintenance window %s: %s", id, err)
}
return nil
}
}

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

Expand Down

0 comments on commit 7b490dd

Please sign in to comment.