2014-07-09 00:56:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-11-21 17:58:34 +01:00
|
|
|
"bytes"
|
2014-07-09 00:56:19 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-07-11 00:52:56 +02:00
|
|
|
"time"
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-07-11 00:52:56 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-09 00:56:19 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTable() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsRouteTableCreate,
|
|
|
|
Read: resourceAwsRouteTableRead,
|
2014-11-24 21:22:18 +01:00
|
|
|
Update: resourceAwsRouteTableUpdate,
|
2014-11-21 17:58:34 +01:00
|
|
|
Delete: resourceAwsRouteTableDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"route": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"cidr_block": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"gateway_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsRouteTableHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRouteTableCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
// Create the routing table
|
2014-07-09 00:56:19 +02:00
|
|
|
createOpts := &ec2.CreateRouteTable{
|
2014-11-21 17:58:34 +01:00
|
|
|
VpcId: d.Get("vpc_id").(string),
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] RouteTable create config: %#v", createOpts)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2014-07-09 00:56:19 +02:00
|
|
|
resp, err := ec2conn.CreateRouteTable(createOpts)
|
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return fmt.Errorf("Error creating route table: %s", err)
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
|
|
|
rt := &resp.RouteTable
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId(rt.RouteTableId)
|
|
|
|
log.Printf("[INFO] Route Table ID: %s", d.Id())
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
// Wait for the route table to become available
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for route table (%s) to become available",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id())
|
2014-07-11 00:52:56 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "ready",
|
2014-11-21 17:58:34 +01:00
|
|
|
Refresh: resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id()),
|
2014-07-11 00:52:56 +02:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return fmt.Errorf(
|
2014-07-11 00:52:56 +02:00
|
|
|
"Error waiting for route table (%s) to become available: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(), err)
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:22:18 +01:00
|
|
|
return resourceAwsRouteTableUpdate(d, meta)
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id())()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rtRaw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
rt := rtRaw.(*ec2.RouteTable)
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Set("vpc_id", rt.VpcId)
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-11-24 21:22:18 +01:00
|
|
|
// Create an empty schema.Set to hold all routes
|
|
|
|
route := &schema.Set{F: resourceAwsRouteTableHash}
|
|
|
|
|
|
|
|
// Loop through the routes and add them to the set
|
|
|
|
for _, r := range rt.Routes {
|
|
|
|
if r.GatewayId == "local" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["cidr_block"] = r.DestinationCidrBlock
|
|
|
|
|
|
|
|
if r.GatewayId != "" {
|
|
|
|
m["gateway_id"] = r.GatewayId
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.InstanceId != "" {
|
|
|
|
m["instance_id"] = r.InstanceId
|
|
|
|
}
|
|
|
|
|
|
|
|
route.Add(m)
|
|
|
|
}
|
|
|
|
d.Set("route", route)
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTableUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Check if the route set as a whole has changed
|
|
|
|
if d.HasChange("route") {
|
|
|
|
o, n := d.GetChange("route")
|
|
|
|
ors := o.(*schema.Set).Difference(n.(*schema.Set))
|
|
|
|
nrs := n.(*schema.Set).Difference(o.(*schema.Set))
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Now first loop through all the old routes and delete any obsolete ones
|
|
|
|
for _, route := range ors.List() {
|
|
|
|
m := route.(map[string]interface{})
|
|
|
|
|
|
|
|
// Delete the route as it no longer exists in the config
|
|
|
|
_, err := ec2conn.DeleteRoute(
|
|
|
|
d.Id(), m["cidr_block"].(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Make sure we save the state of the currently configured rules
|
|
|
|
routes := o.(*schema.Set).Intersection(n.(*schema.Set))
|
|
|
|
d.Set("route", routes)
|
|
|
|
|
|
|
|
// Then loop through al the newly configured routes and create them
|
|
|
|
for _, route := range nrs.List() {
|
|
|
|
m := route.(map[string]interface{})
|
|
|
|
|
|
|
|
opts := ec2.CreateRoute{
|
|
|
|
RouteTableId: d.Id(),
|
|
|
|
DestinationCidrBlock: m["cidr_block"].(string),
|
|
|
|
GatewayId: m["gateway_id"].(string),
|
|
|
|
InstanceId: m["instance_id"].(string),
|
2014-07-13 02:12:41 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
_, err := ec2conn.CreateRoute(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
routes.Add(route)
|
|
|
|
d.Set("route", routes)
|
2014-07-13 02:12:41 +02:00
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return resourceAwsRouteTableRead(d, meta)
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTableDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-11 21:57:23 +02:00
|
|
|
// First request the routing table since we'll have to disassociate
|
|
|
|
// all the subnets first.
|
2014-11-21 17:58:34 +01:00
|
|
|
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id())()
|
2014-07-11 21:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rtRaw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rt := rtRaw.(*ec2.RouteTable)
|
|
|
|
|
|
|
|
// Do all the disassociations
|
|
|
|
for _, a := range rt.Associations {
|
2014-07-11 22:10:09 +02:00
|
|
|
log.Printf("[INFO] Disassociating association: %s", a.AssociationId)
|
2014-07-11 21:57:23 +02:00
|
|
|
if _, err := ec2conn.DisassociateRouteTable(a.AssociationId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the route table
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[INFO] Deleting Route Table: %s", d.Id())
|
|
|
|
if _, err := ec2conn.DeleteRouteTable(d.Id()); err != nil {
|
2014-07-09 00:56:19 +02:00
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if ok && ec2err.Code == "InvalidRouteTableID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error deleting route table: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
// Wait for the route table to really destroy
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for route table (%s) to become destroyed",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id())
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ready"},
|
|
|
|
Target: "",
|
2014-11-21 17:58:34 +01:00
|
|
|
Refresh: resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id()),
|
2014-07-11 00:52:56 +02:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for route table (%s) to become destroyed: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Id(), err)
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 00:56:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTableHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["cidr_block"].(string)))
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if v, ok := m["gateway_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if v, ok := m["instance_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return hashcode.String(buf.String())
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
2014-07-11 00:52:56 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// resourceAwsRouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
2014-07-11 00:52:56 +02:00
|
|
|
// a RouteTable.
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2014-07-11 00:52:56 +02:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.DescribeRouteTables([]string{id}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" {
|
|
|
|
resp = nil
|
|
|
|
} else {
|
|
|
|
log.Printf("Error on RouteTableStateRefresh: %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
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := &resp.RouteTables[0]
|
|
|
|
return rt, "ready", nil
|
|
|
|
}
|
|
|
|
}
|