2014-07-08 01:12:03 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2014-07-08 01:12:03 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-08 01:12:03 +02:00
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGateway() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsInternetGatewayCreate,
|
|
|
|
Read: resourceAwsInternetGatewayRead,
|
|
|
|
Update: resourceAwsInternetGatewayUpdate,
|
|
|
|
Delete: resourceAwsInternetGatewayDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2014-12-14 12:20:59 +01:00
|
|
|
Optional: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
2014-12-28 05:05:15 +01:00
|
|
|
"tags": tagsSchema(),
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-08 01:12:03 +02:00
|
|
|
|
|
|
|
// Create the gateway
|
|
|
|
log.Printf("[DEBUG] Creating internet gateway")
|
2015-04-07 16:44:00 +02:00
|
|
|
resp, err := conn.CreateInternetGateway(nil)
|
2014-07-08 01:12:03 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return fmt.Errorf("Error creating internet gateway: %s", err)
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
2015-04-07 16:44:00 +02:00
|
|
|
ig := *resp.InternetGateway
|
2015-03-03 23:08:52 +01:00
|
|
|
d.SetId(*ig.InternetGatewayID)
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[INFO] InternetGateway ID: %s", d.Id())
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
err = setTagsSDK(conn, d)
|
2015-03-11 00:01:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Attach the new gateway to the correct vpc
|
|
|
|
return resourceAwsInternetGatewayAttach(d, meta)
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-08 05:51:45 +02:00
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
igRaw, _, err := IGStateRefreshFunc(conn, d.Id())()
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if igRaw == nil {
|
|
|
|
// Seems we have lost our internet gateway
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-07-08 05:51:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
ig := igRaw.(*ec2.InternetGateway)
|
2014-12-14 07:14:23 +01:00
|
|
|
if len(ig.Attachments) == 0 {
|
2014-12-14 23:05:38 +01:00
|
|
|
// Gateway exists but not attached to the VPC
|
|
|
|
d.Set("vpc_id", "")
|
2014-12-14 07:14:23 +01:00
|
|
|
} else {
|
2015-03-03 23:08:52 +01:00
|
|
|
d.Set("vpc_id", ig.Attachments[0].VPCID)
|
2014-12-14 07:14:23 +01:00
|
|
|
}
|
2014-07-08 06:00:46 +02:00
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
d.Set("tags", tagsToMapSDK(ig.Tags))
|
2014-12-28 05:05:15 +01:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-08 06:00:46 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-12-14 12:20:59 +01:00
|
|
|
if d.HasChange("vpc_id") {
|
2014-12-14 12:32:17 +01:00
|
|
|
// If we're already attached, detach it first
|
2014-12-14 12:20:59 +01:00
|
|
|
if err := resourceAwsInternetGatewayDetach(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the gateway to the new vpc
|
|
|
|
if err := resourceAwsInternetGatewayAttach(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-08 05:51:45 +02:00
|
|
|
}
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-12-28 05:05:15 +01:00
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
if err := setTagsSDK(conn, d); err != nil {
|
2014-12-28 05:05:15 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("tags")
|
|
|
|
|
2014-12-14 12:20:59 +01:00
|
|
|
return nil
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
// Detach if it is attached
|
2014-11-21 17:58:34 +01:00
|
|
|
if err := resourceAwsInternetGatewayDetach(d, meta); err != nil {
|
2014-07-08 06:00:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[INFO] Deleting Internet Gateway: %s", d.Id())
|
|
|
|
|
2014-10-18 10:47:33 +02:00
|
|
|
return resource.Retry(5*time.Minute, func() error {
|
2015-04-07 16:44:00 +02:00
|
|
|
_, err := conn.DeleteInternetGateway(&ec2.DeleteInternetGatewayInput{
|
2015-03-03 23:08:52 +01:00
|
|
|
InternetGatewayID: aws.String(d.Id()),
|
|
|
|
})
|
2014-12-18 06:21:30 +01:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
2014-07-08 02:07:31 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 23:08:52 +01:00
|
|
|
ec2err, ok := err.(aws.APIError)
|
2014-12-18 06:21:30 +01:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2014-12-18 06:21:30 +01:00
|
|
|
switch ec2err.Code {
|
|
|
|
case "InvalidInternetGatewayID.NotFound":
|
|
|
|
return nil
|
|
|
|
case "DependencyViolation":
|
|
|
|
return err // retry
|
|
|
|
}
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2015-02-18 19:26:59 +01:00
|
|
|
return resource.RetryError{Err: err}
|
2014-12-18 06:21:30 +01:00
|
|
|
})
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGatewayAttach(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2014-12-14 12:20:59 +01:00
|
|
|
if d.Get("vpc_id").(string) == "" {
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Not attaching Internet Gateway '%s' as no VPC ID is set",
|
|
|
|
d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
log.Printf(
|
|
|
|
"[INFO] Attaching Internet Gateway '%s' to VPC '%s'",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(),
|
|
|
|
d.Get("vpc_id").(string))
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
_, err := conn.AttachInternetGateway(&ec2.AttachInternetGatewayInput{
|
2015-03-03 23:08:52 +01:00
|
|
|
InternetGatewayID: aws.String(d.Id()),
|
|
|
|
VPCID: aws.String(d.Get("vpc_id").(string)),
|
|
|
|
})
|
2014-07-08 06:00:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +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.
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
// Wait for it to be fully attached before continuing
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to attach", d.Id())
|
2014-07-08 06:00:46 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"detached", "attaching"},
|
|
|
|
Target: "available",
|
2015-04-07 16:44:00 +02:00
|
|
|
Refresh: IGAttachStateRefreshFunc(conn, d.Id(), "available"),
|
2014-07-08 06:00:46 +02:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for internet gateway (%s) to attach: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(), err)
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInternetGatewayDetach(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-08 06:00:46 +02:00
|
|
|
|
2014-12-14 12:20:59 +01:00
|
|
|
// Get the old VPC ID to detach from
|
2014-12-28 05:05:15 +01:00
|
|
|
vpcID, _ := d.GetChange("vpc_id")
|
2014-12-14 12:20:59 +01:00
|
|
|
|
2014-12-28 05:05:15 +01:00
|
|
|
if vpcID.(string) == "" {
|
2014-12-14 12:20:59 +01:00
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Not detaching Internet Gateway '%s' as no VPC ID is set",
|
|
|
|
d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
log.Printf(
|
|
|
|
"[INFO] Detaching Internet Gateway '%s' from VPC '%s'",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(),
|
2014-12-28 05:05:15 +01:00
|
|
|
vpcID.(string))
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
// Wait for it to be fully detached before continuing
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", d.Id())
|
2014-07-08 06:00:46 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
2015-03-27 17:35:10 +01:00
|
|
|
Pending: []string{"detaching"},
|
2014-07-08 06:00:46 +02:00
|
|
|
Target: "detached",
|
2015-04-07 16:44:00 +02:00
|
|
|
Refresh: detachIGStateRefreshFunc(conn, d.Id(), vpcID.(string)),
|
2015-03-27 17:35:10 +01:00
|
|
|
Timeout: 2 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for internet gateway (%s) to detach: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(), err)
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-27 17:35:10 +01:00
|
|
|
// 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) {
|
2015-04-07 16:44:00 +02:00
|
|
|
_, err := conn.DetachInternetGateway(&ec2.DetachInternetGatewayInput{
|
2015-03-27 17:35:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// an internet gateway.
|
2015-04-07 16:44:00 +02:00
|
|
|
func IGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2014-07-08 01:12:03 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2015-04-07 16:44:00 +02:00
|
|
|
resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
|
|
|
|
InternetGatewayIDs: []*string{aws.String(id)},
|
2015-03-03 23:08:52 +01:00
|
|
|
})
|
2014-07-08 01:12:03 +02:00
|
|
|
if err != nil {
|
2015-03-03 23:08:52 +01:00
|
|
|
ec2err, ok := err.(aws.APIError)
|
2014-07-08 02:12:22 +02:00
|
|
|
if ok && ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
2014-07-08 01:12:03 +02:00
|
|
|
resp = nil
|
|
|
|
} else {
|
2014-07-08 02:12:22 +02:00
|
|
|
log.Printf("[ERROR] Error on IGStateRefresh: %s", err)
|
2014-07-08 01:12:03 +02:00
|
|
|
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-07 16:44:00 +02:00
|
|
|
ig := resp.InternetGateways[0]
|
2014-07-08 01:14:08 +02:00
|
|
|
return ig, "available", nil
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-08 05:51:45 +02:00
|
|
|
|
|
|
|
// IGAttachStateRefreshFunc returns a resource.StateRefreshFunc that is used
|
|
|
|
// watch the state of an internet gateway's attachment.
|
2015-04-07 16:44:00 +02:00
|
|
|
func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
|
2014-07-11 00:33:50 +02:00
|
|
|
var start time.Time
|
2014-07-08 05:51:45 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2014-07-11 00:33:50 +02:00
|
|
|
if start.IsZero() {
|
|
|
|
start = time.Now()
|
|
|
|
}
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
|
|
|
|
InternetGatewayIDs: []*string{aws.String(id)},
|
2015-03-03 23:08:52 +01:00
|
|
|
})
|
2014-07-08 05:51:45 +02:00
|
|
|
if err != nil {
|
2015-03-03 23:08:52 +01:00
|
|
|
ec2err, ok := err.(aws.APIError)
|
2014-07-08 05:51:45 +02:00
|
|
|
if ok && ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
|
|
|
resp = nil
|
|
|
|
} else {
|
|
|
|
log.Printf("[ERROR] Error on IGStateRefresh: %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-07 16:44:00 +02:00
|
|
|
ig := resp.InternetGateways[0]
|
2014-07-08 05:51:45 +02:00
|
|
|
|
|
|
|
if len(ig.Attachments) == 0 {
|
|
|
|
// No attachments, we're detached
|
|
|
|
return ig, "detached", nil
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:08:52 +01:00
|
|
|
return ig, *ig.Attachments[0].State, nil
|
2014-07-08 05:51:45 +02:00
|
|
|
}
|
|
|
|
}
|