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