Skip to content

Commit

Permalink
Merge pull request #9421 from terraform-providers/rfd-retry-custgw
Browse files Browse the repository at this point in the history
Final retry after timeout deleting customer gateway
  • Loading branch information
ryndaniels authored Jul 24, 2019
2 parents 3077dfb + 461ea12 commit baf2034
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions aws/resource_aws_customer_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ func resourceAwsCustomerGatewayDelete(d *schema.ResourceData, meta interface{})
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCustomerGatewayID.NotFound" {
return nil
} else {
log.Printf("[ERROR] Error deleting CustomerGateway: %s", err)
return err
return fmt.Errorf("[ERROR] Error deleting CustomerGateway: %s", err)
}
}

Expand All @@ -248,31 +247,53 @@ func resourceAwsCustomerGatewayDelete(d *schema.ResourceData, meta interface{})
Values: []*string{aws.String(d.Id())},
}

input := &ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{gatewayFilter},
}
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{gatewayFilter},
})
resp, err := conn.DescribeCustomerGateways(input)

if err != nil {
if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "InvalidCustomerGatewayID.NotFound" {
if isAWSErr(err, "InvalidCustomerGatewayID.NotFound", "") {
return nil
}
return resource.NonRetryableError(err)
}

if len(resp.CustomerGateways) != 1 {
return resource.RetryableError(fmt.Errorf("Error finding CustomerGateway for delete: %s", d.Id()))
err = checkGatewayDeleteResponse(resp, d.Id())
if err != nil {
return resource.RetryableError(err)
}
return nil
})

switch *resp.CustomerGateways[0].State {
case "pending", "available", "deleting":
return resource.RetryableError(fmt.Errorf("Gateway (%s) in state (%s), retrying", d.Id(), *resp.CustomerGateways[0].State))
case "deleted":
return nil
default:
return resource.RetryableError(fmt.Errorf("Unrecognized state (%s) for Customer Gateway delete on (%s)", *resp.CustomerGateways[0].State, d.Id()))
if isResourceTimeoutError(err) {
var resp *ec2.DescribeCustomerGatewaysOutput
resp, err = conn.DescribeCustomerGateways(input)

if err != nil {
return checkGatewayDeleteResponse(resp, d.Id())
}
})
}

if err != nil {
return fmt.Errorf("Error deleting customer gateway: %s", err)
}
return nil

}

func checkGatewayDeleteResponse(resp *ec2.DescribeCustomerGatewaysOutput, id string) error {
if len(resp.CustomerGateways) != 1 {
return fmt.Errorf("Error finding CustomerGateway for delete: %s", id)
}

return err
switch *resp.CustomerGateways[0].State {
case "pending", "available", "deleting":
return fmt.Errorf("Gateway (%s) in state (%s), retrying", id, *resp.CustomerGateways[0].State)
case "deleted":
return nil
default:
return fmt.Errorf("Unrecognized state (%s) for Customer Gateway delete on (%s)", *resp.CustomerGateways[0].State, id)
}
}

0 comments on commit baf2034

Please sign in to comment.