provider/aws: Convert AWS Route Table Association to aws-sdk-go
This commit is contained in:
parent
357ef9f313
commit
b038e5f720
|
@ -4,9 +4,10 @@ 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/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"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 +33,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 +73,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 +89,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 +113,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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue