From cf5b32617b89995a06865335c3b6ced03ecfb08b Mon Sep 17 00:00:00 2001 From: clint shryock Date: Fri, 20 Nov 2015 11:44:29 -0600 Subject: [PATCH] fix vpn gateway refresh/reattach issue --- .../providers/aws/resource_aws_vpn_gateway.go | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/builtin/providers/aws/resource_aws_vpn_gateway.go b/builtin/providers/aws/resource_aws_vpn_gateway.go index debd0ec91..4d7860dec 100644 --- a/builtin/providers/aws/resource_aws_vpn_gateway.go +++ b/builtin/providers/aws/resource_aws_vpn_gateway.go @@ -168,24 +168,34 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error d.Id(), d.Get("vpc_id").(string)) - _, err := conn.AttachVpnGateway(&ec2.AttachVpnGatewayInput{ + req := &ec2.AttachVpnGatewayInput{ VpnGatewayId: aws.String(d.Id()), VpcId: aws.String(d.Get("vpc_id").(string)), + } + + err := resource.Retry(30*time.Second, func() error { + _, err := conn.AttachVpnGateway(req) + if err != nil { + if ec2err, ok := err.(awserr.Error); ok { + if "InvalidVpnGatewayID.NotFound" == ec2err.Code() { + //retry + return fmt.Errorf("Gateway not found, retry for eventual consistancy") + } + } + return resource.RetryError{Err: err} + } + return nil }) + if err != nil { return err } - // A note on the states below: the AWS docs (as of July, 2014) say - // that the states would be: attached, attaching, detached, detaching, - // but when running, I noticed that the state is usually "available" when - // it is attached. - // Wait for it to be fully attached before continuing log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id()) stateConf := &resource.StateChangeConf{ Pending: []string{"detached", "attaching"}, - Target: "available", + Target: "attached", Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "available"), Timeout: 1 * time.Minute, } @@ -271,6 +281,7 @@ func vpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ VpnGatewayIds: []*string{aws.String(id)}, }) + if err != nil { if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnGatewayID.NotFound" { resp = nil @@ -288,10 +299,6 @@ func vpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) vpnGateway := resp.VpnGateways[0] - if time.Now().Sub(start) > 10*time.Second { - return vpnGateway, expected, nil - } - if len(vpnGateway.VpcAttachments) == 0 { // No attachments, we're detached return vpnGateway, "detached", nil