github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_main_route_table_association.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/ec2" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAwsMainRouteTableAssociation() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsMainRouteTableAssociationCreate, 15 Read: resourceAwsMainRouteTableAssociationRead, 16 Update: resourceAwsMainRouteTableAssociationUpdate, 17 Delete: resourceAwsMainRouteTableAssociationDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "vpc_id": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 }, 24 25 "route_table_id": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 }, 29 30 // We use this field to record the main route table that is automatically 31 // created when the VPC is created. We need this to be able to "destroy" 32 // our main route table association, which we do by returning this route 33 // table to its original place as the Main Route Table for the VPC. 34 "original_route_table_id": &schema.Schema{ 35 Type: schema.TypeString, 36 Computed: true, 37 }, 38 }, 39 } 40 } 41 42 func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error { 43 ec2conn := meta.(*AWSClient).ec2conn 44 vpcId := d.Get("vpc_id").(string) 45 routeTableId := d.Get("route_table_id").(string) 46 47 log.Printf("[INFO] Creating main route table association: %s => %s", vpcId, routeTableId) 48 49 mainAssociation, err := findMainRouteTableAssociation(ec2conn, vpcId) 50 if err != nil { 51 return err 52 } 53 54 resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{ 55 AssociationID: mainAssociation.RouteTableAssociationID, 56 RouteTableID: aws.String(routeTableId), 57 }) 58 if err != nil { 59 return err 60 } 61 62 d.Set("original_route_table_id", mainAssociation.RouteTableID) 63 d.SetId(*resp.NewAssociationID) 64 log.Printf("[INFO] New main route table association ID: %s", d.Id()) 65 66 return nil 67 } 68 69 func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error { 70 ec2conn := meta.(*AWSClient).ec2conn 71 72 mainAssociation, err := findMainRouteTableAssociation( 73 ec2conn, 74 d.Get("vpc_id").(string)) 75 if err != nil { 76 return err 77 } 78 79 if *mainAssociation.RouteTableAssociationID != d.Id() { 80 // It seems it doesn't exist anymore, so clear the ID 81 d.SetId("") 82 } 83 84 return nil 85 } 86 87 // Update is almost exactly like Create, except we want to retain the 88 // original_route_table_id - this needs to stay recorded as the AWS-created 89 // table from VPC creation. 90 func resourceAwsMainRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error { 91 ec2conn := meta.(*AWSClient).ec2conn 92 vpcId := d.Get("vpc_id").(string) 93 routeTableId := d.Get("route_table_id").(string) 94 95 log.Printf("[INFO] Updating main route table association: %s => %s", vpcId, routeTableId) 96 97 resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{ 98 AssociationID: aws.String(d.Id()), 99 RouteTableID: aws.String(routeTableId), 100 }) 101 if err != nil { 102 return err 103 } 104 105 d.SetId(*resp.NewAssociationID) 106 log.Printf("[INFO] New main route table association ID: %s", d.Id()) 107 108 return nil 109 } 110 111 func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error { 112 ec2conn := meta.(*AWSClient).ec2conn 113 vpcId := d.Get("vpc_id").(string) 114 originalRouteTableId := d.Get("original_route_table_id").(string) 115 116 log.Printf("[INFO] Deleting main route table association by resetting Main Route Table for VPC: %s to its original Route Table: %s", 117 vpcId, 118 originalRouteTableId) 119 120 resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{ 121 AssociationID: aws.String(d.Id()), 122 RouteTableID: aws.String(originalRouteTableId), 123 }) 124 if err != nil { 125 return err 126 } 127 128 log.Printf("[INFO] Resulting Association ID: %s", *resp.NewAssociationID) 129 130 return nil 131 } 132 133 func findMainRouteTableAssociation(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTableAssociation, error) { 134 mainRouteTable, err := findMainRouteTable(ec2conn, vpcId) 135 if err != nil { 136 return nil, err 137 } 138 139 for _, a := range mainRouteTable.Associations { 140 if *a.Main { 141 return &a, nil 142 } 143 } 144 return nil, fmt.Errorf("Could not find main routing table association for VPC: %s", vpcId) 145 } 146 147 func findMainRouteTable(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) { 148 mainFilter := ec2.Filter{ 149 aws.String("association.main"), 150 []string{"true"}, 151 } 152 vpcFilter := ec2.Filter{ 153 aws.String("vpc-id"), 154 []string{vpcId}, 155 } 156 routeResp, err := ec2conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{ 157 Filters: []ec2.Filter{mainFilter, vpcFilter}, 158 }) 159 if err != nil { 160 return nil, err 161 } else if len(routeResp.RouteTables) != 1 { 162 return nil, fmt.Errorf( 163 "Expected to find a single main routing table for VPC: %s, but found %d", 164 vpcId, 165 len(routeResp.RouteTables)) 166 } 167 168 return &routeResp.RouteTables[0], nil 169 }