Merge pull request #1171 from hashicorp/aws-go-route-table-assoc

provider/aws: Convert AWS Route Table Association to aws-sdk-go
This commit is contained in:
Clint 2015-03-10 17:01:21 -05:00
commit 263395a9f0
3 changed files with 43 additions and 60 deletions

View File

@ -87,7 +87,7 @@ func resourceAwsRouteTableCreate(d *schema.ResourceData, meta interface{}) error
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"pending"}, Pending: []string{"pending"},
Target: "ready", Target: "ready",
Refresh: resourceAwsRouteTableStateRefreshFuncSDK(ec2conn, d.Id()), Refresh: resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id()),
Timeout: 1 * time.Minute, Timeout: 1 * time.Minute,
} }
if _, err := stateConf.WaitForState(); err != nil { if _, err := stateConf.WaitForState(); err != nil {
@ -102,7 +102,7 @@ func resourceAwsRouteTableCreate(d *schema.ResourceData, meta interface{}) error
func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).awsEC2conn ec2conn := meta.(*AWSClient).awsEC2conn
rtRaw, _, err := resourceAwsRouteTableStateRefreshFuncSDK(ec2conn, d.Id())() rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id())()
if err != nil { if err != nil {
return err return err
} }
@ -217,7 +217,7 @@ func resourceAwsRouteTableDelete(d *schema.ResourceData, meta interface{}) error
// First request the routing table since we'll have to disassociate // First request the routing table since we'll have to disassociate
// all the subnets first. // all the subnets first.
rtRaw, _, err := resourceAwsRouteTableStateRefreshFuncSDK(ec2conn, d.Id())() rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id())()
if err != nil { if err != nil {
return err return err
} }
@ -259,7 +259,7 @@ func resourceAwsRouteTableDelete(d *schema.ResourceData, meta interface{}) error
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"ready"}, Pending: []string{"ready"},
Target: "", Target: "",
Refresh: resourceAwsRouteTableStateRefreshFuncSDK(ec2conn, d.Id()), Refresh: resourceAwsRouteTableStateRefreshFunc(ec2conn, d.Id()),
Timeout: 1 * time.Minute, Timeout: 1 * time.Minute,
} }
if _, err := stateConf.WaitForState(); err != nil { if _, err := stateConf.WaitForState(); err != nil {
@ -291,9 +291,9 @@ func resourceAwsRouteTableHash(v interface{}) int {
return hashcode.String(buf.String()) return hashcode.String(buf.String())
} }
// resourceAwsRouteTableStateRefreshFuncSDK returns a resource.StateRefreshFunc that is used to watch // resourceAwsRouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a RouteTable. // a RouteTable.
func resourceAwsRouteTableStateRefreshFuncSDK(conn *ec2.EC2, id string) resource.StateRefreshFunc { func resourceAwsRouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{ resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{
RouteTableIDs: []string{id}, RouteTableIDs: []string{id},

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/hashicorp/terraform/helper/resource" "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 resourceAwsRouteTableAssociation() *schema.Resource { func resourceAwsRouteTableAssociation() *schema.Resource {
@ -32,30 +32,31 @@ func resourceAwsRouteTableAssociation() *schema.Resource {
} }
func resourceAwsRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
log.Printf( log.Printf(
"[INFO] Creating route table association: %s => %s", "[INFO] Creating route table association: %s => %s",
d.Get("subnet_id").(string), d.Get("subnet_id").(string),
d.Get("route_table_id").(string)) d.Get("route_table_id").(string))
resp, err := ec2conn.AssociateRouteTable( resp, err := ec2conn.AssociateRouteTable(&ec2.AssociateRouteTableRequest{
d.Get("route_table_id").(string), RouteTableID: aws.String(d.Get("route_table_id").(string)),
d.Get("subnet_id").(string)) SubnetID: aws.String(d.Get("subnet_id").(string)),
})
if err != nil { if err != nil {
return err return err
} }
// Set the ID and return // Set the ID and return
d.SetId(resp.AssociationId) d.SetId(*resp.AssociationID)
log.Printf("[INFO] Association ID: %s", d.Id()) log.Printf("[INFO] Association ID: %s", d.Id())
return nil return nil
} }
func resourceAwsRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
// Get the routing table that this association belongs to // Get the routing table that this association belongs to
rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc( rtRaw, _, err := resourceAwsRouteTableStateRefreshFunc(
@ -71,9 +72,9 @@ func resourceAwsRouteTableAssociationRead(d *schema.ResourceData, meta interface
// Inspect that the association exists // Inspect that the association exists
found := false found := false
for _, a := range rt.Associations { for _, a := range rt.Associations {
if a.AssociationId == d.Id() { if *a.RouteTableAssociationID == d.Id() {
found = true found = true
d.Set("subnet_id", a.SubnetId) d.Set("subnet_id", *a.SubnetID)
break break
} }
} }
@ -87,19 +88,21 @@ func resourceAwsRouteTableAssociationRead(d *schema.ResourceData, meta interface
} }
func resourceAwsRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
log.Printf( log.Printf(
"[INFO] Creating route table association: %s => %s", "[INFO] Creating route table association: %s => %s",
d.Get("subnet_id").(string), d.Get("subnet_id").(string),
d.Get("route_table_id").(string)) d.Get("route_table_id").(string))
resp, err := ec2conn.ReassociateRouteTable( req := &ec2.ReplaceRouteTableAssociationRequest{
d.Id(), AssociationID: aws.String(d.Id()),
d.Get("route_table_id").(string)) RouteTableID: aws.String(d.Get("route_table_id").(string)),
}
resp, err := ec2conn.ReplaceRouteTableAssociation(req)
if err != nil { if err != nil {
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(aws.APIError)
if ok && ec2err.Code == "InvalidAssociationID.NotFound" { if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
// Not found, so just create a new one // Not found, so just create a new one
return resourceAwsRouteTableAssociationCreate(d, meta) return resourceAwsRouteTableAssociationCreate(d, meta)
@ -109,18 +112,21 @@ func resourceAwsRouteTableAssociationUpdate(d *schema.ResourceData, meta interfa
} }
// Update the ID // Update the ID
d.SetId(resp.AssociationId) d.SetId(*resp.NewAssociationID)
log.Printf("[INFO] Association ID: %s", d.Id()) log.Printf("[INFO] Association ID: %s", d.Id())
return nil return nil
} }
func resourceAwsRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
log.Printf("[INFO] Deleting route table association: %s", d.Id()) log.Printf("[INFO] Deleting route table association: %s", d.Id())
if _, err := ec2conn.DisassociateRouteTable(d.Id()); err != nil { err := ec2conn.DisassociateRouteTable(&ec2.DisassociateRouteTableRequest{
ec2err, ok := err.(*ec2.Error) AssociationID: aws.String(d.Id()),
})
if err != nil {
ec2err, ok := err.(aws.APIError)
if ok && ec2err.Code == "InvalidAssociationID.NotFound" { if ok && ec2err.Code == "InvalidAssociationID.NotFound" {
return nil return nil
} }
@ -130,29 +136,3 @@ func resourceAwsRouteTableAssociationDelete(d *schema.ResourceData, meta interfa
return nil return nil
} }
// TODO: remove this method when converting to aws-sdk-go
// resourceAwsRouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a RouteTable.
func resourceAwsRouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
resp, err := conn.DescribeRouteTables([]string{id}, ec2.NewFilter())
if err != nil {
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" {
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
}
rt := &resp.RouteTables[0]
return rt, "ready", nil
}
}

View File

@ -4,9 +4,10 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
) )
func TestAccAWSRouteTableAssociation(t *testing.T) { func TestAccAWSRouteTableAssociation(t *testing.T) {
@ -37,7 +38,7 @@ func TestAccAWSRouteTableAssociation(t *testing.T) {
} }
func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error { func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route_table_association" { if rs.Type != "aws_route_table_association" {
@ -45,11 +46,12 @@ func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error {
} }
// Try to find the resource // Try to find the resource
resp, err := conn.DescribeRouteTables( resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{
[]string{rs.Primary.Attributes["route_table_Id"]}, ec2.NewFilter()) RouteTableIDs: []string{rs.Primary.Attributes["route_table_id"]},
})
if err != nil { if err != nil {
// Verify the error is what we want // Verify the error is what we want
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(aws.APIError)
if !ok { if !ok {
return err return err
} }
@ -62,7 +64,7 @@ func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error {
rt := resp.RouteTables[0] rt := resp.RouteTables[0]
if len(rt.Associations) > 0 { if len(rt.Associations) > 0 {
return fmt.Errorf( return fmt.Errorf(
"route table %s has associations", rt.RouteTableId) "route table %s has associations", *rt.RouteTableID)
} }
} }
@ -81,9 +83,10 @@ func testAccCheckRouteTableAssociationExists(n string, v *ec2.RouteTable) resour
return fmt.Errorf("No ID is set") return fmt.Errorf("No ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
resp, err := conn.DescribeRouteTables( resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{
[]string{rs.Primary.Attributes["route_table_id"]}, ec2.NewFilter()) RouteTableIDs: []string{rs.Primary.Attributes["route_table_id"]},
})
if err != nil { if err != nil {
return err return err
} }