builtin/providers/aws: remove unreachable code and skip unnecessary remote call

When DeleteInternetGateway is successful it returns a nil error value.
However, for a nil error value, the RetryFunc returns an error yielding a
unnecessary second call to DeleteInternetGateway in the retry logic.

The logic works because DeleteInternetGateway eventually returns an ec2.Error
with error code InvalidInternetGatewayID.NotFound since the internet gateway
has been deleted in the previous call. The return value of nil breaks the
retry logic and the deletion is deemed successful.

Fix the unnecessary second call to DeleteInternetGateway by short circuiting
with a nil error value when deletion of the internet gateway is successful on
the first try.

Add an acceptance test for internet gateway deletion and remove unreachable
code while here.
This commit is contained in:
Emil Hessman 2014-12-18 06:21:30 +01:00
parent 15937fb275
commit c738bf0477
2 changed files with 51 additions and 31 deletions

View File

@ -97,7 +97,10 @@ func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{})
return resource.Retry(5*time.Minute, func() error {
_, err := ec2conn.DeleteInternetGateway(d.Id())
if err != nil {
if err == nil {
return nil
}
ec2err, ok := err.(*ec2.Error)
if !ok {
return err
@ -108,29 +111,10 @@ func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{})
return nil
case "DependencyViolation":
return err // retry
default:
}
return resource.RetryError{err}
}
}
return fmt.Errorf("Error deleting internet gateway: %s", err)
})
// Wait for the internet gateway to actually delete
log.Printf("[DEBUG] Waiting for internet gateway (%s) to delete", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"available"},
Target: "",
Refresh: IGStateRefreshFunc(ec2conn, d.Id()),
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for internet gateway (%s) to destroy: %s",
d.Id(), err)
}
return nil
}
func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) error {

View File

@ -54,6 +54,36 @@ func TestAccAWSInternetGateway(t *testing.T) {
})
}
func TestAccAWSInternetGateway_delete(t *testing.T) {
var ig ec2.InternetGateway
testDeleted := func(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[r]
if ok {
return fmt.Errorf("Internet Gateway %q should have been deleted", r)
}
return nil
}
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInternetGatewayDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &ig)),
},
resource.TestStep{
Config: testAccNoInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(testDeleted("aws_internet_gateway.foo")),
},
},
})
}
func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
@ -113,6 +143,12 @@ func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resour
}
}
const testAccNoInternetGatewayConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
`
const testAccInternetGatewayConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"