provider/aws: Convert Main Route Table assoc. to aws-sdk-go

This commit is contained in:
Clint Shryock 2015-03-09 16:15:25 -05:00
parent d2c6ae0741
commit 5a13ac9bc9
2 changed files with 38 additions and 24 deletions

View File

@ -4,8 +4,9 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2"
) )
func resourceAwsMainRouteTableAssociation() *schema.Resource { func resourceAwsMainRouteTableAssociation() *schema.Resource {
@ -39,7 +40,7 @@ func resourceAwsMainRouteTableAssociation() *schema.Resource {
} }
func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
vpcId := d.Get("vpc_id").(string) vpcId := d.Get("vpc_id").(string)
routeTableId := d.Get("route_table_id").(string) routeTableId := d.Get("route_table_id").(string)
@ -50,23 +51,23 @@ func resourceAwsMainRouteTableAssociationCreate(d *schema.ResourceData, meta int
return err return err
} }
resp, err := ec2conn.ReassociateRouteTable( resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{
mainAssociation.AssociationId, AssociationID: mainAssociation.RouteTableAssociationID,
routeTableId, RouteTableID: aws.String(routeTableId),
) })
if err != nil { if err != nil {
return err return err
} }
d.Set("original_route_table_id", mainAssociation.RouteTableId) d.Set("original_route_table_id", mainAssociation.RouteTableID)
d.SetId(resp.AssociationId) d.SetId(*resp.NewAssociationID)
log.Printf("[INFO] New main route table association ID: %s", d.Id()) log.Printf("[INFO] New main route table association ID: %s", d.Id())
return nil return nil
} }
func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
mainAssociation, err := findMainRouteTableAssociation( mainAssociation, err := findMainRouteTableAssociation(
ec2conn, ec2conn,
@ -75,7 +76,7 @@ func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta inter
return err return err
} }
if mainAssociation.AssociationId != d.Id() { if *mainAssociation.RouteTableAssociationID != d.Id() {
// It seems it doesn't exist anymore, so clear the ID // It seems it doesn't exist anymore, so clear the ID
d.SetId("") d.SetId("")
} }
@ -87,25 +88,28 @@ func resourceAwsMainRouteTableAssociationRead(d *schema.ResourceData, meta inter
// original_route_table_id - this needs to stay recorded as the AWS-created // original_route_table_id - this needs to stay recorded as the AWS-created
// table from VPC creation. // table from VPC creation.
func resourceAwsMainRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsMainRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
vpcId := d.Get("vpc_id").(string) vpcId := d.Get("vpc_id").(string)
routeTableId := d.Get("route_table_id").(string) routeTableId := d.Get("route_table_id").(string)
log.Printf("[INFO] Updating main route table association: %s => %s", vpcId, routeTableId) log.Printf("[INFO] Updating main route table association: %s => %s", vpcId, routeTableId)
resp, err := ec2conn.ReassociateRouteTable(d.Id(), routeTableId) resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{
AssociationID: aws.String(d.Id()),
RouteTableID: aws.String(routeTableId),
})
if err != nil { if err != nil {
return err return err
} }
d.SetId(resp.AssociationId) d.SetId(*resp.NewAssociationID)
log.Printf("[INFO] New main route table association ID: %s", d.Id()) log.Printf("[INFO] New main route table association ID: %s", d.Id())
return nil return nil
} }
func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
vpcId := d.Get("vpc_id").(string) vpcId := d.Get("vpc_id").(string)
originalRouteTableId := d.Get("original_route_table_id").(string) originalRouteTableId := d.Get("original_route_table_id").(string)
@ -113,12 +117,15 @@ func resourceAwsMainRouteTableAssociationDelete(d *schema.ResourceData, meta int
vpcId, vpcId,
originalRouteTableId) originalRouteTableId)
resp, err := ec2conn.ReassociateRouteTable(d.Id(), originalRouteTableId) resp, err := ec2conn.ReplaceRouteTableAssociation(&ec2.ReplaceRouteTableAssociationRequest{
AssociationID: aws.String(d.Id()),
RouteTableID: aws.String(originalRouteTableId),
})
if err != nil { if err != nil {
return err return err
} }
log.Printf("[INFO] Resulting Association ID: %s", resp.AssociationId) log.Printf("[INFO] Resulting Association ID: %s", *resp.NewAssociationID)
return nil return nil
} }
@ -130,7 +137,7 @@ func findMainRouteTableAssociation(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTa
} }
for _, a := range mainRouteTable.Associations { for _, a := range mainRouteTable.Associations {
if a.Main { if *a.Main {
return &a, nil return &a, nil
} }
} }
@ -138,10 +145,17 @@ func findMainRouteTableAssociation(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTa
} }
func findMainRouteTable(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) { func findMainRouteTable(ec2conn *ec2.EC2, vpcId string) (*ec2.RouteTable, error) {
filter := ec2.NewFilter() mainFilter := ec2.Filter{
filter.Add("association.main", "true") aws.String("association.main"),
filter.Add("vpc-id", vpcId) []string{"true"},
routeResp, err := ec2conn.DescribeRouteTables(nil, filter) }
vpcFilter := ec2.Filter{
aws.String("vpc-id"),
[]string{vpcId},
}
routeResp, err := ec2conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{
Filters: []ec2.Filter{mainFilter, vpcFilter},
})
if err != nil { if err != nil {
return nil, err return nil, err
} else if len(routeResp.RouteTables) != 1 { } else if len(routeResp.RouteTables) != 1 {

View File

@ -65,15 +65,15 @@ func testAccCheckMainRouteTableAssociation(
return fmt.Errorf("Not found: %s", vpcResource) return fmt.Errorf("Not found: %s", vpcResource)
} }
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
mainAssociation, err := findMainRouteTableAssociation(conn, vpc.Primary.ID) mainAssociation, err := findMainRouteTableAssociation(conn, vpc.Primary.ID)
if err != nil { if err != nil {
return err return err
} }
if mainAssociation.AssociationId != rs.Primary.ID { if *mainAssociation.RouteTableAssociationID != rs.Primary.ID {
return fmt.Errorf("Found wrong main association: %s", return fmt.Errorf("Found wrong main association: %s",
mainAssociation.AssociationId) *mainAssociation.RouteTableAssociationID)
} }
return nil return nil