From 043a4848ee88fe06c58235a1f07d5787a11a993f Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Fri, 27 Mar 2015 11:35:10 -0500 Subject: [PATCH] provider/aws: Fix dependency violation when deleting Internet Gateways --- .../aws/resource_aws_internet_gateway.go | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/builtin/providers/aws/resource_aws_internet_gateway.go b/builtin/providers/aws/resource_aws_internet_gateway.go index 9546ffb5c..e4270d6bc 100644 --- a/builtin/providers/aws/resource_aws_internet_gateway.go +++ b/builtin/providers/aws/resource_aws_internet_gateway.go @@ -199,39 +199,14 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) d.Id(), vpcID.(string)) - wait := true - err := ec2conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{ - InternetGatewayID: aws.String(d.Id()), - VPCID: aws.String(vpcID.(string)), - }) - if err != nil { - ec2err, ok := err.(aws.APIError) - if ok { - if ec2err.Code == "InvalidInternetGatewayID.NotFound" { - err = nil - wait = false - } else if ec2err.Code == "Gateway.NotAttached" { - err = nil - wait = false - } - } - - if err != nil { - return err - } - } - - if !wait { - return nil - } - // Wait for it to be fully detached before continuing log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", d.Id()) stateConf := &resource.StateChangeConf{ - Pending: []string{"attached", "detaching", "available"}, + Pending: []string{"detaching"}, Target: "detached", - Refresh: IGAttachStateRefreshFunc(ec2conn, d.Id(), "detached"), - Timeout: 1 * time.Minute, + Refresh: detachIGStateRefreshFunc(ec2conn, d.Id(), vpcID.(string)), + Timeout: 2 * time.Minute, + Delay: 10 * time.Second, } if _, err := stateConf.WaitForState(); err != nil { return fmt.Errorf( @@ -242,6 +217,32 @@ func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) return nil } +// InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch +// an EC2 instance. +func detachIGStateRefreshFunc(conn *ec2.EC2, instanceID, vpcID string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + err := conn.DetachInternetGateway(&ec2.DetachInternetGatewayRequest{ + InternetGatewayID: aws.String(instanceID), + VPCID: aws.String(vpcID), + }) + if err != nil { + ec2err, ok := err.(aws.APIError) + if ok { + if ec2err.Code == "InvalidInternetGatewayID.NotFound" { + return nil, "Not Found", err + } else if ec2err.Code == "Gateway.NotAttached" { + return "detached", "detached", nil + } else if ec2err.Code == "DependencyViolation" { + return nil, "detaching", nil + } + } + } + // DetachInternetGateway only returns an error, so if it's nil, assume we're + // detached + return "detached", "detached", nil + } +} + // IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // an internet gateway. func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc { @@ -300,10 +301,6 @@ func IGAttachStateRefreshFunc(ec2conn *ec2.EC2, id string, expected string) reso ig := &resp.InternetGateways[0] - if time.Now().Sub(start) > 10*time.Second { - return ig, expected, nil - } - if len(ig.Attachments) == 0 { // No attachments, we're detached return ig, "detached", nil