Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_vpc_peering_connection: Implement sweeper and refactor deletion StateChangeConf logic into function #9499

Merged
merged 1 commit into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions aws/resource_aws_vpc_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,31 +277,18 @@ func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error
req := &ec2.DeleteVpcPeeringConnectionInput{
VpcPeeringConnectionId: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting VPC Peering Connection: %s", req)

_, err := conn.DeleteVpcPeeringConnection(req)

if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") {
return nil
}

if err != nil {
if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") {
return nil
}
return fmt.Errorf("Error deleting VPC Peering Connection (%s): %s", d.Id(), err)
}

// Wait for the vpc peering connection to delete
log.Printf("[DEBUG] Waiting for VPC Peering Connection (%s) to delete.", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{
ec2.VpcPeeringConnectionStateReasonCodeActive,
ec2.VpcPeeringConnectionStateReasonCodePendingAcceptance,
ec2.VpcPeeringConnectionStateReasonCodeDeleting,
},
Target: []string{
ec2.VpcPeeringConnectionStateReasonCodeRejected,
ec2.VpcPeeringConnectionStateReasonCodeDeleted,
},
Refresh: vpcPeeringConnectionRefreshState(conn, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
}
if _, err := stateConf.WaitForState(); err != nil {
if err := waitForEc2VpcPeeringConnectionDeletion(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("Error waiting for VPC Peering Connection (%s) to be deleted: %s", d.Id(), err)
}

Expand Down Expand Up @@ -394,3 +381,23 @@ func vpcPeeringConnectionWaitUntilAvailable(conn *ec2.EC2, id string, timeout ti
}
return nil
}

func waitForEc2VpcPeeringConnectionDeletion(conn *ec2.EC2, id string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{
ec2.VpcPeeringConnectionStateReasonCodeActive,
ec2.VpcPeeringConnectionStateReasonCodePendingAcceptance,
ec2.VpcPeeringConnectionStateReasonCodeDeleting,
},
Target: []string{
ec2.VpcPeeringConnectionStateReasonCodeRejected,
ec2.VpcPeeringConnectionStateReasonCodeDeleted,
},
Refresh: vpcPeeringConnectionRefreshState(conn, id),
Timeout: timeout,
}

_, err := stateConf.WaitForState()

return err
}
72 changes: 72 additions & 0 deletions aws/resource_aws_vpc_peering_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -14,6 +15,77 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_vpc_peering_connection", &resource.Sweeper{
Name: "aws_vpc_peering_connection",
F: testSweepEc2VpcPeeringConnections,
})
}

func testSweepEc2VpcPeeringConnections(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}

conn := client.(*AWSClient).ec2conn
input := &ec2.DescribeVpcPeeringConnectionsInput{}

err = conn.DescribeVpcPeeringConnectionsPages(input, func(page *ec2.DescribeVpcPeeringConnectionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, vpcPeeringConnection := range page.VpcPeeringConnections {
deletedStatuses := map[string]bool{
ec2.VpcPeeringConnectionStateReasonCodeDeleted: true,
ec2.VpcPeeringConnectionStateReasonCodeExpired: true,
ec2.VpcPeeringConnectionStateReasonCodeFailed: true,
ec2.VpcPeeringConnectionStateReasonCodeRejected: true,
}

if _, ok := deletedStatuses[aws.StringValue(vpcPeeringConnection.Status.Code)]; ok {
continue
}

id := aws.StringValue(vpcPeeringConnection.VpcPeeringConnectionId)
input := &ec2.DeleteVpcPeeringConnectionInput{
VpcPeeringConnectionId: vpcPeeringConnection.VpcPeeringConnectionId,
}

log.Printf("[INFO] Deleting EC2 VPC Peering Connection: %s", id)

_, err := conn.DeleteVpcPeeringConnection(input)

if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") {
continue
}

if err != nil {
log.Printf("[ERROR] Error deleting EC2 VPC Peering Connection (%s): %s", id, err)
continue
}

if err := waitForEc2VpcPeeringConnectionDeletion(conn, id, 5*time.Minute); err != nil {
log.Printf("[ERROR] Error waiting for EC2 VPC Peering Connection (%s) to be deleted: %s", id, err)
}
}

return !lastPage
})

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping EC2 VPC Peering Connection sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("Error describing EC2 VPC Peering Connections: %s", err)
}

return nil
}

func TestAccAWSVPCPeeringConnection_importBasic(t *testing.T) {
resourceName := "aws_vpc_peering_connection.foo"

Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
"aws_route_table",
"aws_security_group",
"aws_subnet",
"aws_vpc_peering_connection",
"aws_vpn_gateway",
},
F: testSweepVPCs,
Expand Down