2014-07-08 01:12:03 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-08 01:12:03 +02:00
|
|
|
|
|
|
|
// Create the gateway
|
|
|
|
log.Printf("[DEBUG] Creating internet gateway")
|
|
|
|
resp, err := ec2conn.CreateInternetGateway(nil)
|
|
|
|
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
|
|
|
|
ig := &resp.InternetGateway
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId(ig.InternetGatewayId)
|
|
|
|
log.Printf("[INFO] InternetGateway ID: %s", d.Id())
|
2014-07-08 01:12:03 +02:00
|
|
|
|
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 {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-08 05:51:45 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
igRaw, _, err := IGStateRefreshFunc(ec2conn, d.Id())()
|
|
|
|
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 {
|
|
|
|
d.Set("vpc_id", ig.Attachments[0].VpcId)
|
|
|
|
}
|
2014-07-08 06:00:46 +02:00
|
|
|
|
2014-12-28 05:05:15 +01:00
|
|
|
d.Set("tags", tagsToMap(ig.Tags))
|
|
|
|
|
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
|
|
|
|
2014-12-28 05:05:15 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
if err := setTags(ec2conn, d); err != nil {
|
|
|
|
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 {
|
|
|
|
ec2conn := 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 {
|
2014-11-21 17:58:34 +01:00
|
|
|
_, err := ec2conn.DeleteInternetGateway(d.Id())
|
2014-12-18 06:21:30 +01:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
2014-07-08 02:07:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 06:21:30 +01:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
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 {
|
|
|
|
ec2conn := 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))
|
|
|
|
|
|
|
|
_, err := ec2conn.AttachInternetGateway(d.Id(), 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",
|
2014-11-21 17:58:34 +01:00
|
|
|
Refresh: IGAttachStateRefreshFunc(ec2conn, 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 {
|
|
|
|
ec2conn := 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-09 18:16:29 +02:00
|
|
|
wait := true
|
2014-12-28 05:05:15 +01:00
|
|
|
_, err := ec2conn.DetachInternetGateway(d.Id(), vpcID.(string))
|
2014-07-08 06:00:46 +02:00
|
|
|
if err != nil {
|
2014-07-09 01:02:01 +02:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
2014-07-09 01:05:08 +02:00
|
|
|
if ok {
|
|
|
|
if ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
|
|
|
err = nil
|
2014-07-09 18:16:29 +02:00
|
|
|
wait = false
|
2014-07-09 01:05:08 +02:00
|
|
|
} else if ec2err.Code == "Gateway.NotAttached" {
|
|
|
|
err = nil
|
2014-07-09 18:16:29 +02:00
|
|
|
wait = false
|
2014-07-09 01:05:08 +02:00
|
|
|
}
|
2014-07-09 01:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 18:16:29 +02:00
|
|
|
if !wait {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Pending: []string{"attached", "detaching", "available"},
|
|
|
|
Target: "detached",
|
2014-11-21 17:58:34 +01:00
|
|
|
Refresh: IGAttachStateRefreshFunc(ec2conn, d.Id(), "detached"),
|
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 detach: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(), err)
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// an internet gateway.
|
2014-11-21 17:58:34 +01:00
|
|
|
func IGStateRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2014-07-08 01:12:03 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2014-11-21 17:58:34 +01:00
|
|
|
resp, err := ec2conn.DescribeInternetGateways([]string{id}, ec2.NewFilter())
|
2014-07-08 01:12:03 +02:00
|
|
|
if err != nil {
|
2014-07-08 02:12:22 +02:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2014-07-11 00:33:50 +02:00
|
|
|
func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resource.StateRefreshFunc {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2014-07-08 05:51:45 +02:00
|
|
|
resp, err := conn.DescribeInternetGateways([]string{id}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ig := &resp.InternetGateways[0]
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
if time.Now().Sub(start) > 10*time.Second {
|
2014-07-11 00:33:50 +02:00
|
|
|
return ig, expected, nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 05:51:45 +02:00
|
|
|
if len(ig.Attachments) == 0 {
|
|
|
|
// No attachments, we're detached
|
|
|
|
return ig, "detached", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ig, ig.Attachments[0].State, nil
|
|
|
|
}
|
|
|
|
}
|