2014-07-11 02:17:01 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-07-11 02:17:01 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSRouteTableAssociation_basic(t *testing.T) {
|
2014-07-11 22:10:09 +02:00
|
|
|
var v, v2 ec2.RouteTable
|
2014-07-11 02:17:01 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRouteTableAssociationDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableAssociationConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableAssociationExists(
|
|
|
|
"aws_route_table_association.foo", &v),
|
|
|
|
),
|
|
|
|
},
|
2014-07-11 22:10:09 +02:00
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRouteTableAssociationConfigChange,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRouteTableAssociationExists(
|
|
|
|
"aws_route_table_association.foo", &v2),
|
|
|
|
),
|
|
|
|
},
|
2014-07-11 02:17:01 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-11 02:17:01 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-11 02:17:01 +02:00
|
|
|
if rs.Type != "aws_route_table_association" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
RouteTableIds: []*string{aws.String(rs.Primary.Attributes["route_table_id"])},
|
2015-03-09 23:11:30 +01:00
|
|
|
})
|
2014-07-11 02:17:01 +02:00
|
|
|
if err != nil {
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-07-11 02:17:01 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidRouteTableID.NotFound" {
|
2014-07-11 02:17:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := resp.RouteTables[0]
|
|
|
|
if len(rt.Associations) > 0 {
|
|
|
|
return fmt.Errorf(
|
2015-08-17 20:27:16 +02:00
|
|
|
"route table %s has associations", *rt.RouteTableId)
|
2014-07-11 02:17:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRouteTableAssociationExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-11 02:17:01 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-11 02:17:01 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-11 00:09:36 +02:00
|
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
RouteTableIds: []*string{aws.String(rs.Primary.Attributes["route_table_id"])},
|
2015-03-09 23:11:30 +01:00
|
|
|
})
|
2014-07-11 02:17:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.RouteTables) == 0 {
|
|
|
|
return fmt.Errorf("RouteTable not found")
|
|
|
|
}
|
|
|
|
|
2015-04-11 00:09:36 +02:00
|
|
|
*v = *resp.RouteTables[0]
|
2014-07-11 02:17:01 +02:00
|
|
|
|
|
|
|
if len(v.Associations) == 0 {
|
|
|
|
return fmt.Errorf("no associations")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccRouteTableAssociationConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
}
|
|
|
|
|
2014-07-28 19:27:55 +02:00
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
2014-07-11 02:17:01 +02:00
|
|
|
resource "aws_route_table" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-07-28 19:27:55 +02:00
|
|
|
route {
|
|
|
|
cidr_block = "10.0.0.0/8"
|
|
|
|
gateway_id = "${aws_internet_gateway.foo.id}"
|
|
|
|
}
|
2014-07-11 02:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "foo" {
|
|
|
|
route_table_id = "${aws_route_table.foo.id}"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
}
|
|
|
|
`
|
2014-07-11 22:10:09 +02:00
|
|
|
|
|
|
|
const testAccRouteTableAssociationConfigChange = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
}
|
|
|
|
|
2014-07-28 19:27:55 +02:00
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:10:09 +02:00
|
|
|
resource "aws_route_table" "bar" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-07-28 19:27:55 +02:00
|
|
|
route {
|
|
|
|
cidr_block = "10.0.0.0/8"
|
|
|
|
gateway_id = "${aws_internet_gateway.foo.id}"
|
|
|
|
}
|
2014-07-11 22:10:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "foo" {
|
|
|
|
route_table_id = "${aws_route_table.bar.id}"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
}
|
|
|
|
`
|