2015-03-05 05:40:45 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2015-03-05 05:40:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsVpnGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsVpnGatewayCreate,
|
|
|
|
Read: resourceAwsVpnGatewayRead,
|
|
|
|
Update: resourceAwsVpnGatewayUpdate,
|
|
|
|
Delete: resourceAwsVpnGatewayDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"availability_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-03-05 07:24:14 +01:00
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
"tags": tagsSchema(),
|
2015-03-05 05:40:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-10 01:30:29 +01:00
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
createOpts := &ec2.CreateVPNGatewayInput{
|
2015-03-10 01:30:29 +01:00
|
|
|
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
|
|
|
|
Type: aws.String("ipsec.1"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the VPN gateway
|
|
|
|
log.Printf("[DEBUG] Creating VPN gateway")
|
2015-04-14 22:45:48 +02:00
|
|
|
resp, err := conn.CreateVPNGateway(createOpts)
|
2015-03-10 01:30:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating VPN gateway: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
|
|
|
vpnGateway := resp.VPNGateway
|
|
|
|
d.SetId(*vpnGateway.VPNGatewayID)
|
|
|
|
log.Printf("[INFO] VPN Gateway ID: %s", *vpnGateway.VPNGatewayID)
|
|
|
|
|
|
|
|
// Attach the VPN gateway to the correct VPC
|
|
|
|
return resourceAwsVpnGatewayUpdate(d, meta)
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-04-15 17:26:39 +02:00
|
|
|
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
|
|
|
VPNGatewayIDs: []*string{aws.String(d.Id())},
|
|
|
|
})
|
2015-03-05 23:47:29 +01:00
|
|
|
if err != nil {
|
2015-04-15 17:26:39 +02:00
|
|
|
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
|
|
|
|
}
|
2015-03-05 23:47:29 +01:00
|
|
|
}
|
2015-04-15 17:26:39 +02:00
|
|
|
|
|
|
|
vpnGateway := resp.VPNGateways[0]
|
|
|
|
if vpnGateway == nil {
|
2015-03-05 23:47:29 +01:00
|
|
|
// Seems we have lost our VPN gateway
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-05 23:47:29 +01:00
|
|
|
if len(vpnGateway.VPCAttachments) == 0 {
|
|
|
|
// Gateway exists but not attached to the VPC
|
|
|
|
d.Set("vpc_id", "")
|
|
|
|
} else {
|
|
|
|
d.Set("vpc_id", vpnGateway.VPCAttachments[0].VPCID)
|
|
|
|
}
|
2015-03-10 01:30:29 +01:00
|
|
|
d.Set("availability_zone", vpnGateway.AvailabilityZone)
|
2015-04-14 22:45:48 +02:00
|
|
|
d.Set("tags", tagsToMapSDK(vpnGateway.Tags))
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
return nil
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-03-10 01:30:29 +01:00
|
|
|
if d.HasChange("vpc_id") {
|
|
|
|
// If we're already attached, detach it first
|
|
|
|
if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the VPN gateway to the new vpc
|
|
|
|
if err := resourceAwsVpnGatewayAttach(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
if err := setTagsSDK(conn, d); err != nil {
|
2015-03-10 01:30:29 +01:00
|
|
|
return err
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
d.SetPartial("tags")
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
return resourceAwsVpnGatewayRead(d, meta)
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-10 01:30:29 +01:00
|
|
|
|
|
|
|
// Detach if it is attached
|
|
|
|
if err := resourceAwsVpnGatewayDetach(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting VPN gateway: %s", d.Id())
|
|
|
|
|
|
|
|
return resource.Retry(5*time.Minute, func() error {
|
2015-04-14 22:45:48 +02:00
|
|
|
_, err := conn.DeleteVPNGateway(&ec2.DeleteVPNGatewayInput{
|
2015-03-10 01:30:29 +01:00
|
|
|
VPNGatewayID: aws.String(d.Id()),
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2err, ok := err.(aws.APIError)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ec2err.Code {
|
|
|
|
case "InvalidVpnGatewayID.NotFound":
|
|
|
|
return nil
|
|
|
|
case "IncorrectState":
|
|
|
|
return err // retry
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.RetryError{Err: err}
|
|
|
|
})
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayAttach(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-10 01:30:29 +01:00
|
|
|
|
|
|
|
if d.Get("vpc_id").(string) == "" {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Not attaching VPN Gateway '%s' as no VPC ID is set",
|
|
|
|
d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Attaching VPN Gateway '%s' to VPC '%s'",
|
|
|
|
d.Id(),
|
|
|
|
d.Get("vpc_id").(string))
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
_, err := conn.AttachVPNGateway(&ec2.AttachVPNGatewayInput{
|
2015-03-10 01:30:29 +01:00
|
|
|
VPNGatewayID: aws.String(d.Id()),
|
|
|
|
VPCID: aws.String(d.Get("vpc_id").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-05 23:47:29 +01:00
|
|
|
// 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
|
2015-03-10 01:30:29 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for VPN gateway (%s) to attach", d.Id())
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"detached", "attaching"},
|
|
|
|
Target: "available",
|
2015-04-15 17:26:39 +02:00
|
|
|
Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "available"),
|
2015-03-10 01:30:29 +01:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for VPN gateway (%s) to attach: %s",
|
|
|
|
d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVpnGatewayDetach(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-03-10 01:30:29 +01:00
|
|
|
|
|
|
|
// Get the old VPC ID to detach from
|
|
|
|
vpcID, _ := d.GetChange("vpc_id")
|
|
|
|
|
|
|
|
if vpcID.(string) == "" {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Not detaching VPN Gateway '%s' as no VPC ID is set",
|
|
|
|
d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Detaching VPN Gateway '%s' from VPC '%s'",
|
|
|
|
d.Id(),
|
|
|
|
vpcID.(string))
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-03-10 01:30:29 +01:00
|
|
|
wait := true
|
2015-04-14 22:45:48 +02:00
|
|
|
_, err := conn.DetachVPNGateway(&ec2.DetachVPNGatewayInput{
|
2015-03-10 01:30:29 +01:00
|
|
|
VPNGatewayID: aws.String(d.Id()),
|
2015-05-06 18:44:50 +02:00
|
|
|
VPCID: aws.String(vpcID.(string)),
|
2015-03-10 01:30:29 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(aws.APIError)
|
|
|
|
if ok {
|
|
|
|
if ec2err.Code == "InvalidVpnGatewayID.NotFound" {
|
|
|
|
err = nil
|
|
|
|
wait = false
|
|
|
|
} else if ec2err.Code == "InvalidVpnGatewayAttachment.NotFound" {
|
|
|
|
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 VPN gateway (%s) to detach", d.Id())
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"attached", "detaching", "available"},
|
|
|
|
Target: "detached",
|
2015-04-15 17:26:39 +02:00
|
|
|
Refresh: vpnGatewayAttachStateRefreshFunc(conn, d.Id(), "detached"),
|
2015-03-10 01:30:29 +01:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for vpn gateway (%s) to detach: %s",
|
|
|
|
d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
|
2015-04-15 17:26:39 +02:00
|
|
|
// vpnGatewayAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
2015-03-05 05:40:45 +01:00
|
|
|
// the state of a VPN gateway's attachment
|
2015-04-15 17:26:39 +02:00
|
|
|
func vpnGatewayAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
|
2015-03-10 01:30:29 +01:00
|
|
|
var start time.Time
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
if start.IsZero() {
|
|
|
|
start = time.Now()
|
|
|
|
}
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
resp, err := conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
|
|
|
VPNGatewayIDs: []*string{aws.String(id)},
|
2015-03-10 01:30:29 +01:00
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
vpnGateway := resp.VPNGateways[0]
|
2015-03-10 01:30:29 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpnGateway, *vpnGateway.VPCAttachments[0].State, nil
|
|
|
|
}
|
2015-03-05 05:40:45 +01:00
|
|
|
}
|