2014-07-08 01:12:03 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// Create the gateway
|
|
|
|
log.Printf("[DEBUG] Creating internet gateway")
|
|
|
|
resp, err := ec2conn.CreateInternetGateway(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating subnet: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
|
|
|
ig := &resp.InternetGateway
|
|
|
|
s.ID = ig.InternetGatewayId
|
|
|
|
log.Printf("[INFO] InternetGateway ID: %s", s.ID)
|
|
|
|
|
|
|
|
// Update our attributes and return
|
2014-07-08 05:51:45 +02:00
|
|
|
return resource_aws_internet_gateway_update(s, d, meta)
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_update(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
2014-07-08 05:51:45 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// Merge the diff so we have the latest attributes
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
// 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
|
|
|
// If we're already attached, detach it first
|
|
|
|
if err := resource_aws_internet_gateway_detach(ec2conn, s); err != nil {
|
2014-07-08 05:51:45 +02:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
// Set the VPC ID to empty since we're detached at this point
|
|
|
|
delete(rs.Attributes, "vpc_id")
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["vpc_id"]; ok && attr.New != "" {
|
|
|
|
err := resource_aws_internet_gateway_attach(ec2conn, s, attr.New)
|
|
|
|
if err != nil {
|
2014-07-13 20:16:59 +02:00
|
|
|
return rs, err
|
2014-07-08 06:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rs.Attributes["vpc_id"] = attr.New
|
2014-07-08 05:51:45 +02:00
|
|
|
}
|
2014-07-08 01:12:03 +02:00
|
|
|
|
2014-07-08 06:03:53 +02:00
|
|
|
return resource_aws_internet_gateway_update_state(rs, nil)
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
// Detach if it is attached
|
|
|
|
if err := resource_aws_internet_gateway_detach(ec2conn, s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
log.Printf("[INFO] Deleting Internet Gateway: %s", s.ID)
|
|
|
|
if _, err := ec2conn.DeleteInternetGateway(s.ID); err != nil {
|
2014-07-08 02:07:31 +02:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if ok && ec2err.Code == "InvalidInternetGatewayID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
return fmt.Errorf("Error deleting internet gateway: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the internet gateway to actually delete
|
|
|
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to delete", s.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-07-08 01:14:08 +02:00
|
|
|
Pending: []string{"available"},
|
2014-07-08 01:12:03 +02:00
|
|
|
Target: "",
|
|
|
|
Refresh: IGStateRefreshFunc(ec2conn, s.ID),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for internet gateway (%s) to destroy",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
igRaw, _, err := IGStateRefreshFunc(ec2conn, s.ID)()
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
if igRaw == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ig := igRaw.(*ec2.InternetGateway)
|
|
|
|
return resource_aws_internet_gateway_update_state(s, ig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
b := &diff.ResourceBuilder{
|
2014-07-08 05:51:45 +02:00
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"vpc_id": diff.AttrTypeUpdate,
|
|
|
|
},
|
2014-07-08 01:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:00:46 +02:00
|
|
|
func resource_aws_internet_gateway_attach(
|
|
|
|
ec2conn *ec2.EC2,
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
vpcId string) error {
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Attaching Internet Gateway '%s' to VPC '%s'",
|
|
|
|
s.ID,
|
|
|
|
vpcId)
|
|
|
|
_, err := ec2conn.AttachInternetGateway(s.ID, vpcId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for it to be fully attached before continuing
|
|
|
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to attach", s.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"detached", "attaching"},
|
|
|
|
Target: "available",
|
2014-07-11 00:33:50 +02:00
|
|
|
Refresh: IGAttachStateRefreshFunc(ec2conn, s.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",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_internet_gateway_detach(
|
|
|
|
ec2conn *ec2.EC2,
|
|
|
|
s *terraform.ResourceState) error {
|
|
|
|
if s.Attributes["vpc_id"] == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Detaching Internet Gateway '%s' from VPC '%s'",
|
|
|
|
s.ID,
|
|
|
|
s.Attributes["vpc_id"])
|
2014-07-09 18:16:29 +02:00
|
|
|
wait := true
|
2014-07-08 06:00:46 +02:00
|
|
|
_, err := ec2conn.DetachInternetGateway(s.ID, s.Attributes["vpc_id"])
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.Attributes, "vpc_id")
|
|
|
|
|
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
|
|
|
|
log.Printf("[DEBUG] Waiting for internet gateway (%s) to detach", s.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"attached", "detaching", "available"},
|
|
|
|
Target: "detached",
|
2014-07-11 00:33:50 +02:00
|
|
|
Refresh: IGAttachStateRefreshFunc(ec2conn, s.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",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
func resource_aws_internet_gateway_update_state(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
ig *ec2.InternetGateway) (*terraform.ResourceState, error) {
|
2014-07-08 06:03:53 +02:00
|
|
|
if s.Attributes["vpc_id"] != "" {
|
|
|
|
// We belong to a VPC
|
|
|
|
s.Dependencies = []terraform.ResourceDependency{
|
|
|
|
terraform.ResourceDependency{ID: s.Attributes["vpc_id"]},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:12:03 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// an internet gateway.
|
|
|
|
func IGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.DescribeInternetGateways([]string{id}, ec2.NewFilter())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|