2014-07-09 00:56:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-07-10 02:17:24 +02:00
|
|
|
"reflect"
|
2014-07-11 00:52:56 +02:00
|
|
|
"time"
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-07-09 00:56:19 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
2014-07-11 00:52:56 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-07-09 00:56:19 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_route_table_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
// Create the routing table
|
2014-07-09 00:56:19 +02:00
|
|
|
createOpts := &ec2.CreateRouteTable{
|
2014-07-10 03:13:11 +02:00
|
|
|
VpcId: d.Attributes["vpc_id"].New,
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] RouteTable create config: %#v", createOpts)
|
|
|
|
resp, err := ec2conn.CreateRouteTable(createOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating route table: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
|
|
|
rt := &resp.RouteTable
|
|
|
|
s.ID = rt.RouteTableId
|
|
|
|
log.Printf("[INFO] Route Table ID: %s", s.ID)
|
|
|
|
|
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",
|
|
|
|
s.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "ready",
|
|
|
|
Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID),
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return s, fmt.Errorf(
|
|
|
|
"Error waiting for route table (%s) to become available: %s",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
// Update our routes
|
|
|
|
return resource_aws_route_table_update(s, d, meta)
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_route_table_update(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
2014-07-10 02:17:24 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// Our resulting state
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
// Get our routes out of the merge
|
|
|
|
oldroutes := flatmap.Expand(s.Attributes, "route")
|
|
|
|
routes := flatmap.Expand(s.MergeDiff(d).Attributes, "route")
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
// Determine the route operations we need to perform
|
|
|
|
ops := routeTableOps(oldroutes, routes)
|
|
|
|
if len(ops) == 0 {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through each operation, performing each one at a time.
|
|
|
|
// We store the updated state on each operation so that if any
|
|
|
|
// individual operation fails, we can return a valid partial state.
|
|
|
|
var err error
|
|
|
|
resultRoutes := make([]map[string]string, 0, len(ops))
|
|
|
|
for _, op := range ops {
|
|
|
|
switch op.Op {
|
|
|
|
case routeTableOpCreate:
|
|
|
|
opts := ec2.CreateRoute{
|
|
|
|
RouteTableId: s.ID,
|
|
|
|
DestinationCidrBlock: op.Route.DestinationCidrBlock,
|
|
|
|
GatewayId: op.Route.GatewayId,
|
|
|
|
InstanceId: op.Route.InstanceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ec2conn.CreateRoute(&opts)
|
|
|
|
case routeTableOpReplace:
|
|
|
|
opts := ec2.ReplaceRoute{
|
|
|
|
RouteTableId: s.ID,
|
|
|
|
DestinationCidrBlock: op.Route.DestinationCidrBlock,
|
|
|
|
GatewayId: op.Route.GatewayId,
|
|
|
|
InstanceId: op.Route.InstanceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ec2conn.ReplaceRoute(&opts)
|
|
|
|
case routeTableOpDelete:
|
|
|
|
_, err = ec2conn.DeleteRoute(
|
|
|
|
s.ID, op.Route.DestinationCidrBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// Exit early so we can return what we've done so far
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:12:41 +02:00
|
|
|
// If we didn't delete the route, append it to the list of routes
|
|
|
|
// we have.
|
|
|
|
if op.Op != routeTableOpDelete {
|
|
|
|
resultMap := map[string]string{"cidr_block": op.Route.DestinationCidrBlock}
|
|
|
|
if op.Route.GatewayId != "" {
|
|
|
|
resultMap["gateway_id"] = op.Route.GatewayId
|
|
|
|
} else if op.Route.InstanceId != "" {
|
|
|
|
resultMap["instance_id"] = op.Route.InstanceId
|
|
|
|
}
|
|
|
|
|
|
|
|
resultRoutes = append(resultRoutes, resultMap)
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update our state with the settings
|
|
|
|
flatmap.Map(rs.Attributes).Merge(flatmap.Flatten(map[string]interface{}{
|
|
|
|
"route": resultRoutes,
|
|
|
|
}))
|
|
|
|
|
2014-07-10 02:37:56 +02:00
|
|
|
return rs, err
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_route_table_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-07-11 21:57:23 +02:00
|
|
|
// First request the routing table since we'll have to disassociate
|
|
|
|
// all the subnets first.
|
|
|
|
rtRaw, _, err := RouteTableStateRefreshFunc(ec2conn, s.ID)()
|
|
|
|
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-07-09 00:56:19 +02:00
|
|
|
log.Printf("[INFO] Deleting Route Table: %s", s.ID)
|
|
|
|
if _, err := ec2conn.DeleteRouteTable(s.ID); err != nil {
|
|
|
|
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",
|
|
|
|
s.ID)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"ready"},
|
|
|
|
Target: "",
|
|
|
|
Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID),
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for route table (%s) to become destroyed: %s",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:56:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_route_table_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
rtRaw, _, err := RouteTableStateRefreshFunc(ec2conn, s.ID)()
|
2014-07-09 00:56:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2014-07-11 00:52:56 +02:00
|
|
|
if rtRaw == nil {
|
2014-07-09 00:56:19 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:52:56 +02:00
|
|
|
rt := rtRaw.(*ec2.RouteTable)
|
2014-07-09 00:56:19 +02:00
|
|
|
return resource_aws_route_table_update_state(s, rt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_route_table_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"vpc_id": diff.AttrTypeCreate,
|
2014-07-09 18:47:13 +02:00
|
|
|
"route": diff.AttrTypeUpdate,
|
2014-07-09 00:56:19 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_route_table_update_state(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
rt *ec2.RouteTable) (*terraform.ResourceState, error) {
|
|
|
|
s.Attributes["vpc_id"] = rt.VpcId
|
|
|
|
|
|
|
|
// We belong to a VPC
|
|
|
|
s.Dependencies = []terraform.ResourceDependency{
|
|
|
|
terraform.ResourceDependency{ID: rt.VpcId},
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
|
|
|
|
// routeTableOp represents a minor operation on the routing table.
|
|
|
|
// This tells us what we should do to the routing table.
|
|
|
|
type routeTableOp struct {
|
|
|
|
Op routeTableOpType
|
|
|
|
Route ec2.Route
|
|
|
|
}
|
|
|
|
|
|
|
|
// routeTableOpType is the type of operation related to a route that
|
|
|
|
// can be operated on a routing table.
|
|
|
|
type routeTableOpType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
routeTableOpCreate routeTableOpType = iota
|
|
|
|
routeTableOpReplace
|
|
|
|
routeTableOpDelete
|
|
|
|
)
|
|
|
|
|
|
|
|
// routeTableOps takes the old and new routes from flatmap.Expand
|
|
|
|
// and returns a set of operations that must be performed in order
|
|
|
|
// to get to the desired state.
|
|
|
|
func routeTableOps(a interface{}, b interface{}) []routeTableOp {
|
|
|
|
// Build up the actual ec2.Route objects
|
|
|
|
oldRoutes := make(map[string]ec2.Route)
|
|
|
|
newRoutes := make(map[string]ec2.Route)
|
2014-07-10 02:37:56 +02:00
|
|
|
for i, raws := range []interface{}{a, b} {
|
2014-07-10 02:17:24 +02:00
|
|
|
result := oldRoutes
|
2014-07-10 02:37:56 +02:00
|
|
|
if i == 1 {
|
2014-07-10 02:17:24 +02:00
|
|
|
result = newRoutes
|
|
|
|
}
|
2014-07-10 03:41:00 +02:00
|
|
|
if raws == nil {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
|
|
|
|
for _, raw := range raws.([]interface{}) {
|
|
|
|
m := raw.(map[string]interface{})
|
|
|
|
r := ec2.Route{
|
|
|
|
DestinationCidrBlock: m["cidr_block"].(string),
|
|
|
|
}
|
|
|
|
if v, ok := m["gateway_id"]; ok {
|
|
|
|
r.GatewayId = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := m["instance_id"]; ok {
|
|
|
|
r.InstanceId = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
result[r.DestinationCidrBlock] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, start building up the ops
|
|
|
|
ops := make([]routeTableOp, 0, len(newRoutes))
|
|
|
|
for n, r := range newRoutes {
|
|
|
|
op := routeTableOpCreate
|
|
|
|
if oldR, ok := oldRoutes[n]; ok {
|
|
|
|
if reflect.DeepEqual(r, oldR) {
|
|
|
|
// No changes!
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
op = routeTableOpReplace
|
|
|
|
}
|
|
|
|
|
|
|
|
ops = append(ops, routeTableOp{
|
|
|
|
Op: op,
|
|
|
|
Route: r,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine what routes we need to delete
|
|
|
|
for _, op := range ops {
|
|
|
|
delete(oldRoutes, op.Route.DestinationCidrBlock)
|
|
|
|
}
|
|
|
|
for _, r := range oldRoutes {
|
|
|
|
ops = append(ops, routeTableOp{
|
|
|
|
Op: routeTableOpDelete,
|
|
|
|
Route: r,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops
|
|
|
|
}
|
2014-07-11 00:52:56 +02:00
|
|
|
|
|
|
|
// RouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a RouteTable.
|
|
|
|
func RouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|