2015-02-03 20:11:05 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-02-03 20:11:05 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsMainRouteTableAssociation() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsMainRouteTableAssociationCreate,
|
|
|
|
Read: resourceAwsMainRouteTableAssociationRead,
|
|
|
|
Update: resourceAwsMainRouteTableAssociationUpdate,
|
|
|
|
Delete: resourceAwsMainRouteTableAssociationDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"route_table_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// We use this field to record the main route table that is automatically
|
|
|
|
// created when the VPC is created. We need this to be able to "destroy"
|
|
|
|
// our main route table association, which we do by returning this route
|
|
|
|
// table to its original place as the Main Route Table for the VPC.
|
|
|
|
"original_route_table_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-03 20:11:05 +01:00
|
|
|
vpcId := d.Get("vpc_id").(string)
|
|
|
|
routeTableId := d.Get("route_table_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Creating main route table association: %s => %s", vpcId, routeTableId)
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
mainAssociation, err := findMainRouteTableAssociation(conn, vpcId)
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
|
2015-03-09 22:15:25 +01:00
|
|
|
AssociationID: mainAssociation.RouteTableAssociationID,
|
|
|
|
RouteTableID: aws.String(routeTableId),
|
|
|
|
})
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:15:25 +01:00
|
|
|
d.Set("original_route_table_id", mainAssociation.RouteTableID)
|
|
|
|
d.SetId(*resp.NewAssociationID)
|
2015-02-03 20:11:05 +01:00
|
|
|
log.Printf("[INFO] New main route table association ID: %s", d.Id())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-03 20:11:05 +01:00
|
|
|
|
|
|
|
mainAssociation, err := findMainRouteTableAssociation(
|
2015-04-13 17:01:56 +02:00
|
|
|
conn,
|
2015-02-03 20:11:05 +01:00
|
|
|
d.Get("vpc_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-06 03:07:22 +02:00
|
|
|
if mainAssociation == nil || *mainAssociation.RouteTableAssociationID != d.Id() {
|
2015-02-03 20:11:05 +01:00
|
|
|
// It seems it doesn't exist anymore, so clear the ID
|
|
|
|
d.SetId("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is almost exactly like Create, except we want to retain the
|
|
|
|
// original_route_table_id - this needs to stay recorded as the AWS-created
|
|
|
|
// table from VPC creation.
|
|
|
|
func resourceAwsMainRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-03 20:11:05 +01:00
|
|
|
vpcId := d.Get("vpc_id").(string)
|
|
|
|
routeTableId := d.Get("route_table_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Updating main route table association: %s => %s", vpcId, routeTableId)
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
|
2015-03-09 22:15:25 +01:00
|
|
|
AssociationID: aws.String(d.Id()),
|
|
|
|
RouteTableID: aws.String(routeTableId),
|
|
|
|
})
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:15:25 +01:00
|
|
|
d.SetId(*resp.NewAssociationID)
|
2015-02-03 20:11:05 +01:00
|
|
|
log.Printf("[INFO] New main route table association ID: %s", d.Id())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-03 20:11:05 +01:00
|
|
|
vpcId := d.Get("vpc_id").(string)
|
|
|
|
originalRouteTableId := d.Get("original_route_table_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting main route table association by resetting Main Route Table for VPC: %s to its original Route Table: %s",
|
|
|
|
vpcId,
|
|
|
|
originalRouteTableId)
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
resp, err := conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationInput{
|
2015-03-09 22:15:25 +01:00
|
|
|
AssociationID: aws.String(d.Id()),
|
|
|
|
RouteTableID: aws.String(originalRouteTableId),
|
|
|
|
})
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-09 22:15:25 +01:00
|
|
|
log.Printf("[INFO] Resulting Association ID: %s", *resp.NewAssociationID)
|
2015-02-03 20:11:05 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
func findMainRouteTableAssociation(conn *ec2.EC2, vpcId string) (*ec2.RouteTableAssociation, error) {
|
|
|
|
mainRouteTable, err := findMainRouteTable(conn, vpcId)
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-06 03:07:22 +02:00
|
|
|
if mainRouteTable == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-02-03 20:11:05 +01:00
|
|
|
|
|
|
|
for _, a := range mainRouteTable.Associations {
|
2015-03-09 22:15:25 +01:00
|
|
|
if *a.Main {
|
2015-04-13 17:01:56 +02:00
|
|
|
return a, nil
|
2015-02-03 20:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Could not find main routing table association for VPC: %s", vpcId)
|
|
|
|
}
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
func findMainRouteTable(conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) {
|
|
|
|
mainFilter := &ec2.Filter{
|
|
|
|
Name: aws.String("association.main"),
|
|
|
|
Values: []*string{aws.String("true")},
|
2015-03-09 22:15:25 +01:00
|
|
|
}
|
2015-04-13 17:01:56 +02:00
|
|
|
vpcFilter := &ec2.Filter{
|
|
|
|
Name: aws.String("vpc-id"),
|
|
|
|
Values: []*string{aws.String(vpcId)},
|
2015-03-09 22:15:25 +01:00
|
|
|
}
|
2015-04-13 17:01:56 +02:00
|
|
|
routeResp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
|
|
Filters: []*ec2.Filter{mainFilter, vpcFilter},
|
2015-03-09 22:15:25 +01:00
|
|
|
})
|
2015-02-03 20:11:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(routeResp.RouteTables) != 1 {
|
2015-05-06 03:07:22 +02:00
|
|
|
return nil, nil
|
2015-02-03 20:11:05 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:01:56 +02:00
|
|
|
return routeResp.RouteTables[0], nil
|
2015-02-03 20:11:05 +01:00
|
|
|
}
|