From dc54d6a85231c604402954778d6b78f7c0e8f23f Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 15 Apr 2015 10:26:39 -0500 Subject: [PATCH] refactor out a refresh fun that wasn't really used --- .../providers/aws/resource_aws_vpn_gateway.go | 51 +++++++------------ .../aws/resource_aws_vpn_gateway_test.go | 2 +- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/builtin/providers/aws/resource_aws_vpn_gateway.go b/builtin/providers/aws/resource_aws_vpn_gateway.go index a6aaaa9ff..bdaf7c61c 100644 --- a/builtin/providers/aws/resource_aws_vpn_gateway.go +++ b/builtin/providers/aws/resource_aws_vpn_gateway.go @@ -62,17 +62,26 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2SDKconn - vpnGatewayRaw, _, err := vpnGatewayStateRefreshFunc(conn, d.Id())() + resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{ + VPNGatewayIDs: []*string{aws.String(d.Id())}, + }) if err != nil { - return err + if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" { + d.SetId("") + return nil + } else { + log.Printf("[ERROR] Error finding VpnGateway: %s", err) + return err + } } - if vpnGatewayRaw == nil { + + vpnGateway := resp.VPNGateways[0] + if vpnGateway == nil { // Seems we have lost our VPN gateway d.SetId("") return nil } - vpnGateway := vpnGatewayRaw.(*ec2.VPNGateway) if len(vpnGateway.VPCAttachments) == 0 { // Gateway exists but not attached to the VPC d.Set("vpc_id", "") @@ -176,7 +185,7 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error stateConf := &resource.StateChangeConf{ Pending: []string{"detached", "attaching"}, Target: "available", - Refresh: VpnGatewayAttachStateRefreshFunc(conn, d.Id(), "available"), + Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "available"), Timeout: 1 * time.Minute, } if _, err := stateConf.WaitForState(); err != nil { @@ -237,7 +246,7 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error stateConf := &resource.StateChangeConf{ Pending: []string{"attached", "detaching", "available"}, Target: "detached", - Refresh: VpnGatewayAttachStateRefreshFunc(conn, d.Id(), "detached"), + Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "detached"), Timeout: 1 * time.Minute, } if _, err := stateConf.WaitForState(); err != nil { @@ -249,35 +258,9 @@ func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error return nil } -// vpnGatewayStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch a VPNGateway. -func vpnGatewayStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{ - VPNGatewayIDs: []*string{aws.String(id)}, - }) - if err != nil { - if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound" { - resp = nil - } else { - log.Printf("[ERROR] Error on VpnGatewayStateRefresh: %s", err) - return nil, "", err - } - } - - if resp == nil { - // Sometimes AWS just has consistency issues and doesn't see - // our instance yet. Return an empty state. - return nil, "", nil - } - - vpnGateway := resp.VPNGateways[0] - return vpnGateway, "", nil - } -} - -// VpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch +// vpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // the state of a VPN gateway's attachment -func VpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc { +func vpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc { var start time.Time return func() (interface{}, string, error) { if start.IsZero() { diff --git a/builtin/providers/aws/resource_aws_vpn_gateway_test.go b/builtin/providers/aws/resource_aws_vpn_gateway_test.go index 6a5bf2b01..31e2cae12 100644 --- a/builtin/providers/aws/resource_aws_vpn_gateway_test.go +++ b/builtin/providers/aws/resource_aws_vpn_gateway_test.go @@ -10,7 +10,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSVpnGateway(t *testing.T) { +func TestAccAWSVpnGateway_basic(t *testing.T) { var v, v2 ec2.VPNGateway testNotEqual := func(*terraform.State) error {