Skip to content

Commit

Permalink
provider/aws: fix CheckDestroy on a bunch of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Dec 22, 2015
1 parent 1d5c65f commit 7d6b980
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
22 changes: 19 additions & 3 deletions builtin/providers/aws/resource_aws_codecommit_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codecommit"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -86,9 +87,24 @@ func testAccCheckCodeCommitRepositoryExists(name string) resource.TestCheckFunc
}

func testAccCheckCodeCommitRepositoryDestroy(s *terraform.State) error {
if len(s.RootModule().Resources) > 0 {
return fmt.Errorf("Expected all resources to be gone, but found: %#v",
s.RootModule().Resources)
conn := testAccProvider.Meta().(*AWSClient).codecommitconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_codecommit_repository" {
continue
}

_, err := conn.GetRepository(&codecommit.GetRepositoryInput{
RepositoryName: aws.String(rs.Primary.ID),
})

if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" {
continue
}
if err == nil {
return fmt.Errorf("Repository still exists: %s", rs.Primary.ID)
}
return err
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion builtin/providers/aws/resource_aws_codedeploy_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func testAccCheckAWSCodeDeployAppDestroy(s *terraform.State) error {
}

resp, err := conn.GetApplication(&codedeploy.GetApplicationInput{
ApplicationName: aws.String(rs.Primary.ID),
ApplicationName: aws.String(rs.Primary.Attributes["name"]),
})

if err == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -45,6 +46,10 @@ func testAccCheckAWSCodeDeployDeploymentGroupDestroy(s *terraform.State) error {
DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]),
})

if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" {
continue
}

if err == nil {
if resp.DeploymentGroupInfo.DeploymentGroupName != nil {
return fmt.Errorf("CodeDeploy deployment group still exists:\n%#v", *resp.DeploymentGroupInfo.DeploymentGroupName)
Expand Down
30 changes: 28 additions & 2 deletions builtin/providers/aws/resource_aws_customer_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -46,8 +47,33 @@ func TestAccAWSCustomerGateway_basic(t *testing.T) {
}

func testAccCheckCustomerGatewayDestroy(s *terraform.State) error {
if len(s.RootModule().Resources) > 0 {
return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
conn := testAccProvider.Meta().(*AWSClient).ec2conn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_customer_gatewah" {
continue
}

gatewayFilter := &ec2.Filter{
Name: aws.String("customer-gateway-id"),
Values: []*string{aws.String(rs.Primary.ID)},
}

resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{gatewayFilter},
})

if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidCustomerGatewayID.NotFound" {
continue
}

if err == nil {
if len(resp.CustomerGateways) > 0 {
return fmt.Errorf("Customer gateway still exists: %v", resp.CustomerGateways)
}
}

return err
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions builtin/providers/aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
DBInstanceIdentifier: aws.String(rs.Primary.ID),
})

if ae, ok := err.(awserr.Error); ok && ae.Code() == "DBInstanceNotFound" {
continue
}

if err == nil {
if len(resp.DBInstances) != 0 &&
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
Expand Down
5 changes: 4 additions & 1 deletion builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ func testAccCheckDHCPOptionsDestroy(s *terraform.State) error {
aws.String(rs.Primary.ID),
},
})
if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidDhcpOptionID.NotFound" {
continue
}
if err == nil {
if len(resp.DhcpOptions) > 0 {
return fmt.Errorf("still exist.")
return fmt.Errorf("still exists")
}

return nil
Expand Down

0 comments on commit 7d6b980

Please sign in to comment.