Match the internet gateway code better.

This commit is contained in:
Dan Everton 2015-03-06 08:47:29 +10:00
parent b741e0c9a3
commit 98d827b6f5
2 changed files with 31 additions and 37 deletions

View File

@ -48,7 +48,7 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error
createOpts := &ec2.CreateVPNGatewayRequest{ createOpts := &ec2.CreateVPNGatewayRequest{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)), AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
Type: aws.String(d.Get("type").(string)), Type: aws.String(d.Get("type").(string)),
} }
// Create the VPN gateway // Create the VPN gateway
@ -61,7 +61,7 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error
// Get the ID and store it // Get the ID and store it
vpnGateway := resp.VPNGateway vpnGateway := resp.VPNGateway
d.SetId(*vpnGateway.VPNGatewayID) d.SetId(*vpnGateway.VPNGatewayID)
log.Printf("[INFO] VPN Gateway ID: %s", vpnGateway.VPNGatewayID) log.Printf("[INFO] VPN Gateway ID: %s", *vpnGateway.VPNGatewayID)
// Attach the VPN gateway to the correct VPC // Attach the VPN gateway to the correct VPC
return resourceAwsVpnGatewayUpdate(d, meta) return resourceAwsVpnGatewayUpdate(d, meta)
@ -70,29 +70,23 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error
func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).awsEC2conn ec2conn := meta.(*AWSClient).awsEC2conn
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{ vpnGatewayRaw, _, err := VpnGatewayStateRefreshFunc(ec2conn, d.Id())()
VPNGatewayIDs: []string{d.Id()}, if err != nil {
}) return err
}
if vpnGatewayRaw == nil {
// Seems we have lost our VPN gateway
d.SetId("")
return nil
}
if err != nil { vpnGateway := vpnGatewayRaw.(*ec2.VPNGateway)
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpnGatewayID.NotFound " { if len(vpnGateway.VPCAttachments) == 0 {
// Update state to indicate the subnet no longer exists. // Gateway exists but not attached to the VPC
d.SetId("") d.Set("vpc_id", "")
return nil } else {
} d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
return err }
}
if resp == nil {
return nil
}
vpnGateway := &resp.VPNGateways[0]
if len(vpnGateway.VPCAttachments) == 0 {
// VPN gateway exists but not attached to the VPC
d.Set("vpc_id", "")
} else {
d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
}
d.Set("availability_zone", vpnGateway.AvailabilityZone) d.Set("availability_zone", vpnGateway.AvailabilityZone)
d.Set("type", vpnGateway.Type) d.Set("type", vpnGateway.Type)
d.Set("tags", tagsToMapSDK(vpnGateway.Tags)) d.Set("tags", tagsToMapSDK(vpnGateway.Tags))
@ -115,17 +109,13 @@ func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error
ec2conn := meta.(*AWSClient).awsEC2conn ec2conn := meta.(*AWSClient).awsEC2conn
d.Partial(true)
if err := setTagsSDK(ec2conn, d); err != nil { if err := setTagsSDK(ec2conn, d); err != nil {
return err return err
} else {
d.SetPartial("tags")
} }
d.Partial(false) d.SetPartial("tags")
return resourceAwsVpnGatewayRead(d, meta) return nil
} }
func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
@ -160,8 +150,6 @@ func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error
return resource.RetryError{Err: err} return resource.RetryError{Err: err}
}) })
return nil
} }
func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error { func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error {
@ -187,6 +175,12 @@ func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error
return err 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()) log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id())
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"detached", "attaching"}, Pending: []string{"detached", "attaching"},

View File

@ -114,7 +114,7 @@ func TestAccVpnGateway_tags(t *testing.T) {
} }
func testAccCheckVpnGatewayDestroy(s *terraform.State) error { func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).awsEC2conn ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_vpn_gateway" { if rs.Type != "aws_vpn_gateway" {
@ -122,7 +122,7 @@ func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
} }
// Try to find the resource // Try to find the resource
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{ resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
VPNGatewayIDs: []string{rs.Primary.ID}, VPNGatewayIDs: []string{rs.Primary.ID},
}) })
if err == nil { if err == nil {
@ -134,7 +134,7 @@ func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
} }
// Verify the error is what we want // Verify the error is what we want
ec2err, ok := err.(*aws.APIError) ec2err, ok := err.(aws.APIError)
if !ok { if !ok {
return err return err
} }
@ -157,8 +157,8 @@ func testAccCheckVpnGatewayExists(n string, ig *ec2.VPNGateway) resource.TestChe
return fmt.Errorf("No ID is set") return fmt.Errorf("No ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).awsEC2conn ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{ resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysRequest{
VPNGatewayIDs: []string{rs.Primary.ID}, VPNGatewayIDs: []string{rs.Primary.ID},
}) })
if err != nil { if err != nil {