From 53681390d4ccd51aad97b0d6894597d13930920c Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 7 Jul 2016 13:45:49 +0100 Subject: [PATCH] provider/aws: Remove `aws_codedeploy_deployment_group` from state on 404 Fixes #4802 The manual removal will now force Terraform to remove the resource from state and then report it needs recreated --- ...esource_aws_codedeploy_deployment_group.go | 6 +++ ...ce_aws_codedeploy_deployment_group_test.go | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/builtin/providers/aws/resource_aws_codedeploy_deployment_group.go b/builtin/providers/aws/resource_aws_codedeploy_deployment_group.go index f1bfa49ba..72e019311 100644 --- a/builtin/providers/aws/resource_aws_codedeploy_deployment_group.go +++ b/builtin/providers/aws/resource_aws_codedeploy_deployment_group.go @@ -238,6 +238,12 @@ func resourceAwsCodeDeployDeploymentGroupRead(d *schema.ResourceData, meta inter DeploymentGroupName: aws.String(d.Get("deployment_group_name").(string)), }) if err != nil { + if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DeploymentGroupDoesNotExistException" { + log.Printf("[INFO] CodeDeployment DeploymentGroup %s not found", d.Get("deployment_group_name").(string)) + d.SetId("") + return nil + } + return err } diff --git a/builtin/providers/aws/resource_aws_codedeploy_deployment_group_test.go b/builtin/providers/aws/resource_aws_codedeploy_deployment_group_test.go index 3ce37f1a4..4e8955b22 100644 --- a/builtin/providers/aws/resource_aws_codedeploy_deployment_group_test.go +++ b/builtin/providers/aws/resource_aws_codedeploy_deployment_group_test.go @@ -6,6 +6,7 @@ import ( "regexp" "sort" "testing" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -107,6 +108,27 @@ func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) { }) } +func TestAccAWSCodeDeployDeploymentGroup_disappears(t *testing.T) { + var group codedeploy.DeploymentGroupInfo + rName := acctest.RandString(5) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSCodeDeployDeploymentGroup(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group), + testAccAWSCodeDeployDeploymentGroupDisappears(&group), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T) { var group codedeploy.DeploymentGroupInfo @@ -407,6 +429,36 @@ func testAccCheckAWSCodeDeployDeploymentGroupDestroy(s *terraform.State) error { return nil } +func testAccAWSCodeDeployDeploymentGroupDisappears(group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).codedeployconn + opts := &codedeploy.DeleteDeploymentGroupInput{ + ApplicationName: group.ApplicationName, + DeploymentGroupName: group.DeploymentGroupName, + } + if _, err := conn.DeleteDeploymentGroup(opts); err != nil { + return err + } + return resource.Retry(40*time.Minute, func() *resource.RetryError { + opts := &codedeploy.GetDeploymentGroupInput{ + ApplicationName: group.ApplicationName, + DeploymentGroupName: group.DeploymentGroupName, + } + _, err := conn.GetDeploymentGroup(opts) + if err != nil { + codedeploy, ok := err.(awserr.Error) + if ok && codedeploy.Code() == "DeploymentGroupDoesNotExistException" { + return nil + } + return resource.NonRetryableError( + fmt.Errorf("Error retrieving CodeDeploy Deployment Group: %s", err)) + } + return resource.RetryableError(fmt.Errorf( + "Waiting for CodeDeploy Deployment Group: %v", group.DeploymentGroupName)) + }) + } +} + func testAccCheckAWSCodeDeployDeploymentGroupExists(name string, group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name]