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
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
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
|
|
|
)
|
|
|
|
|
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,
|
2016-05-13 20:52:52 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourceAwsRouteTableImportState,
|
|
|
|
},
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-14 11:38:25 +01:00
|
|
|
"vpc_id": {
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-12-10 19:33:13 +01:00
|
|
|
"tags": tagsSchema(),
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"propagating_vgws": {
|
2015-04-14 02:02:09 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-04-14 02:02:09 +02:00
|
|
|
},
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"route": {
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeSet,
|
2015-10-15 23:55:47 +02:00
|
|
|
Computed: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-14 11:38:25 +01:00
|
|
|
"cidr_block": {
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeString,
|
2017-03-14 11:38:25 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ipv6_cidr_block": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"egress_only_gateway_id": {
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"gateway_id": {
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-02-10 17:50:29 +01:00
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"instance_id": {
|
2015-12-18 08:20:13 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"nat_gateway_id": {
|
2015-02-10 17:50:29 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-04-17 19:18:46 +02:00
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
"vpc_peering_connection_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"network_interface_id": {
|
2015-04-17 19:18:46 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsRouteTableHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRouteTableCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2014-07-10 02:17:24 +02:00
|
|
|
// Create the routing table
|
2015-04-11 00:09:36 +02:00
|
|
|
createOpts := &ec2.CreateRouteTableInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
VpcId: aws.String(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
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.CreateRouteTable(createOpts)
|
2014-07-09 00:56:19 +02:00
|
|
|
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
|
2015-03-10 16:23:14 +01:00
|
|
|
rt := resp.RouteTable
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*rt.RouteTableId)
|
2014-11-21 17:58:34 +01:00
|
|
|
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"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"ready"},
|
2015-04-11 00:09:36 +02:00
|
|
|
Refresh: resourceAwsRouteTableStateRefreshFunc(conn, d.Id()),
|
2017-05-20 23:40:33 +02:00
|
|
|
Timeout: 10 * time.Minute,
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
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 {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(conn, d.Id())()
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rtRaw == nil {
|
2015-03-20 18:29:11 +01:00
|
|
|
d.SetId("")
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
rt := rtRaw.(*ec2.RouteTable)
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("vpc_id", rt.VpcId)
|
2014-07-09 00:56:19 +02:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
propagatingVGWs := make([]string, 0, len(rt.PropagatingVgws))
|
|
|
|
for _, vgw := range rt.PropagatingVgws {
|
|
|
|
propagatingVGWs = append(propagatingVGWs, *vgw.GatewayId)
|
2015-04-14 02:02:09 +02:00
|
|
|
}
|
|
|
|
d.Set("propagating_vgws", propagatingVGWs)
|
|
|
|
|
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 {
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.GatewayId != nil && *r.GatewayId == "local" {
|
2014-11-24 21:22:18 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-10 16:23:14 +01:00
|
|
|
if r.Origin != nil && *r.Origin == "EnableVgwRoutePropagation" {
|
2015-01-02 09:17:52 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.DestinationPrefixListId != nil {
|
2015-07-10 14:29:03 +02:00
|
|
|
// Skipping because VPC endpoint routes are handled separately
|
|
|
|
// See aws_vpc_endpoint
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-11-24 21:22:18 +01:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.DestinationCidrBlock != nil {
|
|
|
|
m["cidr_block"] = *r.DestinationCidrBlock
|
2015-03-10 16:23:14 +01:00
|
|
|
}
|
2017-03-14 11:38:25 +01:00
|
|
|
if r.DestinationIpv6CidrBlock != nil {
|
|
|
|
m["ipv6_cidr_block"] = *r.DestinationIpv6CidrBlock
|
|
|
|
}
|
|
|
|
if r.EgressOnlyInternetGatewayId != nil {
|
|
|
|
m["egress_only_gateway_id"] = *r.EgressOnlyInternetGatewayId
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.GatewayId != nil {
|
|
|
|
m["gateway_id"] = *r.GatewayId
|
2015-03-10 16:23:14 +01:00
|
|
|
}
|
2015-12-18 08:20:13 +01:00
|
|
|
if r.NatGatewayId != nil {
|
|
|
|
m["nat_gateway_id"] = *r.NatGatewayId
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.InstanceId != nil {
|
|
|
|
m["instance_id"] = *r.InstanceId
|
2015-03-10 16:23:14 +01:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.VpcPeeringConnectionId != nil {
|
|
|
|
m["vpc_peering_connection_id"] = *r.VpcPeeringConnectionId
|
2015-03-10 16:23:14 +01:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if r.NetworkInterfaceId != nil {
|
|
|
|
m["network_interface_id"] = *r.NetworkInterfaceId
|
2015-04-17 19:18:46 +02:00
|
|
|
}
|
2014-11-24 21:22:18 +01:00
|
|
|
|
|
|
|
route.Add(m)
|
|
|
|
}
|
|
|
|
d.Set("route", route)
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2014-12-10 19:33:13 +01:00
|
|
|
// Tags
|
2015-05-12 21:58:10 +02:00
|
|
|
d.Set("tags", tagsToMap(rt.Tags))
|
2014-12-10 19:33:13 +01: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 {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-07-10 02:17:24 +02:00
|
|
|
|
2015-04-14 02:02:09 +02:00
|
|
|
if d.HasChange("propagating_vgws") {
|
|
|
|
o, n := d.GetChange("propagating_vgws")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
remove := os.Difference(ns).List()
|
|
|
|
add := ns.Difference(os).List()
|
|
|
|
|
|
|
|
// Now first loop through all the old propagations and disable any obsolete ones
|
|
|
|
for _, vgw := range remove {
|
|
|
|
id := vgw.(string)
|
|
|
|
|
|
|
|
// Disable the propagation as it no longer exists in the config
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Deleting VGW propagation from %s: %s",
|
|
|
|
d.Id(), id)
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.DisableVgwRoutePropagation(&ec2.DisableVgwRoutePropagationInput{
|
|
|
|
RouteTableId: aws.String(d.Id()),
|
|
|
|
GatewayId: aws.String(id),
|
2015-04-14 02:02:09 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we save the state of the currently configured rules
|
|
|
|
propagatingVGWs := os.Intersection(ns)
|
|
|
|
d.Set("propagating_vgws", propagatingVGWs)
|
|
|
|
|
|
|
|
// Then loop through all the newly configured propagations and enable them
|
|
|
|
for _, vgw := range add {
|
|
|
|
id := vgw.(string)
|
|
|
|
|
2015-05-06 20:09:51 +02:00
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
log.Printf("[INFO] Enabling VGW propagation for %s: %s", d.Id(), id)
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err = conn.EnableVgwRoutePropagation(&ec2.EnableVgwRoutePropagationInput{
|
|
|
|
RouteTableId: aws.String(d.Id()),
|
|
|
|
GatewayId: aws.String(id),
|
2015-05-06 20:09:51 +02:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get a Gateway.NotAttached, it is usually some
|
|
|
|
// eventually consistency stuff. So we have to just wait a
|
|
|
|
// bit...
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if ok && ec2err.Code() == "Gateway.NotAttached" {
|
2015-05-06 20:09:51 +02:00
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 02:02:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
propagatingVGWs.Add(vgw)
|
|
|
|
d.Set("propagating_vgws", propagatingVGWs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{})
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
deleteOpts := &ec2.DeleteRouteInput{
|
|
|
|
RouteTableId: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["ipv6_cidr_block"].(string); s != "" {
|
|
|
|
deleteOpts.DestinationIpv6CidrBlock = aws.String(s)
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Deleting route from %s: %s",
|
|
|
|
d.Id(), m["ipv6_cidr_block"].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["cidr_block"].(string); s != "" {
|
|
|
|
deleteOpts.DestinationCidrBlock = aws.String(s)
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Deleting route from %s: %s",
|
|
|
|
d.Id(), m["cidr_block"].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.DeleteRoute(deleteOpts)
|
2014-11-21 17:58:34 +01:00
|
|
|
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)
|
|
|
|
|
2015-04-24 19:24:59 +02:00
|
|
|
// Then loop through all the newly configured routes and create them
|
2014-11-21 17:58:34 +01:00
|
|
|
for _, route := range nrs.List() {
|
|
|
|
m := route.(map[string]interface{})
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
opts := ec2.CreateRouteInput{
|
2017-03-14 11:38:25 +01:00
|
|
|
RouteTableId: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["vpc_peering_connection_id"].(string); s != "" {
|
|
|
|
opts.VpcPeeringConnectionId = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["network_interface_id"].(string); s != "" {
|
|
|
|
opts.NetworkInterfaceId = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["instance_id"].(string); s != "" {
|
|
|
|
opts.InstanceId = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["ipv6_cidr_block"].(string); s != "" {
|
|
|
|
opts.DestinationIpv6CidrBlock = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["cidr_block"].(string); s != "" {
|
|
|
|
opts.DestinationCidrBlock = aws.String(s)
|
2014-07-13 02:12:41 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
if s := m["gateway_id"].(string); s != "" {
|
|
|
|
opts.GatewayId = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["egress_only_gateway_id"].(string); s != "" {
|
|
|
|
opts.EgressOnlyInternetGatewayId = aws.String(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := m["nat_gateway_id"].(string); s != "" {
|
|
|
|
opts.NatGatewayId = aws.String(s)
|
2016-01-04 22:14:30 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:43:19 +01:00
|
|
|
log.Printf("[INFO] Creating route for %s: %#v", d.Id(), opts)
|
2017-05-10 14:41:49 +02:00
|
|
|
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
|
2016-06-14 22:39:52 +02:00
|
|
|
_, err := conn.CreateRoute(&opts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "InvalidRouteTableID.NotFound" {
|
|
|
|
return resource.RetryableError(awsErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2014-12-10 19:33:13 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := 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.
|
2015-04-11 00:09:36 +02:00
|
|
|
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(conn, 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 {
|
2015-08-17 20:27:16 +02:00
|
|
|
log.Printf("[INFO] Disassociating association: %s", *a.RouteTableAssociationId)
|
2015-04-11 00:09:36 +02:00
|
|
|
_, err := conn.DisassociateRouteTable(&ec2.DisassociateRouteTableInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
AssociationId: a.RouteTableAssociationId,
|
2015-03-10 16:23:14 +01:00
|
|
|
})
|
2015-10-23 15:03:54 +02:00
|
|
|
if err != nil {
|
|
|
|
// First check if the association ID is not found. If this
|
|
|
|
// is the case, then it was already disassociated somehow,
|
|
|
|
// and that is okay.
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
2015-03-10 16:23:14 +01:00
|
|
|
if err != nil {
|
2014-07-11 21:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the route table
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[INFO] Deleting Route Table: %s", d.Id())
|
2015-04-11 00:09:36 +02:00
|
|
|
_, err = conn.DeleteRouteTable(&ec2.DeleteRouteTableInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
RouteTableId: aws.String(d.Id()),
|
2015-03-10 16:23:14 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if ok && ec2err.Code() == "InvalidRouteTableID.NotFound" {
|
2014-07-09 00:56:19 +02:00
|
|
|
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"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{},
|
2015-04-11 00:09:36 +02:00
|
|
|
Refresh: resourceAwsRouteTableStateRefreshFunc(conn, d.Id()),
|
2017-05-10 14:41:49 +02:00
|
|
|
Timeout: 5 * time.Minute,
|
2014-07-11 00:52:56 +02:00
|
|
|
}
|
|
|
|
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
|
2017-04-11 20:45:10 +02:00
|
|
|
m, castOk := v.(map[string]interface{})
|
|
|
|
if !castOk {
|
|
|
|
return 0
|
|
|
|
}
|
2015-06-03 20:14:43 +02:00
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
if v, ok := m["ipv6_cidr_block"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2015-06-03 20:14:43 +02:00
|
|
|
if v, ok := m["cidr_block"]; 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["gateway_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-09 00:56:19 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 11:38:25 +01:00
|
|
|
if v, ok := m["egress_only_gateway_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2015-12-18 08:20:13 +01:00
|
|
|
natGatewaySet := false
|
|
|
|
if v, ok := m["nat_gateway_id"]; ok {
|
|
|
|
natGatewaySet = v.(string) != ""
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2015-05-06 19:32:17 +02:00
|
|
|
instanceSet := false
|
2014-11-21 17:58:34 +01:00
|
|
|
if v, ok := m["instance_id"]; ok {
|
2015-05-06 19:32:17 +02:00
|
|
|
instanceSet = v.(string) != ""
|
2014-11-21 17:58:34 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-10 02:17:24 +02:00
|
|
|
}
|
|
|
|
|
2015-02-10 17:50:29 +01:00
|
|
|
if v, ok := m["vpc_peering_connection_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
|
2015-12-18 08:20:13 +01:00
|
|
|
if v, ok := m["network_interface_id"]; ok && !(instanceSet || natGatewaySet) {
|
2015-04-17 19:18:46 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-03-10 22:52:09 +01:00
|
|
|
// resourceAwsRouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
2014-07-11 00:52:56 +02:00
|
|
|
// a RouteTable.
|
2015-03-10 22:52:09 +01:00
|
|
|
func resourceAwsRouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2014-07-11 00:52:56 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
RouteTableIds: []*string{aws.String(id)},
|
2015-03-10 16:23:14 +01:00
|
|
|
})
|
2014-07-11 00:52:56 +02:00
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidRouteTableID.NotFound" {
|
2014-07-11 00:52:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
rt := resp.RouteTables[0]
|
2014-07-11 00:52:56 +02:00
|
|
|
return rt, "ready", nil
|
|
|
|
}
|
|
|
|
}
|