github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_route_table_association_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/ec2" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSRouteTableAssociation(t *testing.T) { 14 var v, v2 ec2.RouteTable 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckRouteTableAssociationDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccRouteTableAssociationConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckRouteTableAssociationExists( 25 "aws_route_table_association.foo", &v), 26 ), 27 }, 28 29 resource.TestStep{ 30 Config: testAccRouteTableAssociationConfigChange, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckRouteTableAssociationExists( 33 "aws_route_table_association.foo", &v2), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error { 41 conn := testAccProvider.Meta().(*AWSClient).ec2conn 42 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "aws_route_table_association" { 45 continue 46 } 47 48 // Try to find the resource 49 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{ 50 RouteTableIDs: []string{rs.Primary.Attributes["route_table_id"]}, 51 }) 52 if err != nil { 53 // Verify the error is what we want 54 ec2err, ok := err.(aws.APIError) 55 if !ok { 56 return err 57 } 58 if ec2err.Code != "InvalidRouteTableID.NotFound" { 59 return err 60 } 61 return nil 62 } 63 64 rt := resp.RouteTables[0] 65 if len(rt.Associations) > 0 { 66 return fmt.Errorf( 67 "route table %s has associations", *rt.RouteTableID) 68 69 } 70 } 71 72 return nil 73 } 74 75 func testAccCheckRouteTableAssociationExists(n string, v *ec2.RouteTable) resource.TestCheckFunc { 76 return func(s *terraform.State) error { 77 rs, ok := s.RootModule().Resources[n] 78 if !ok { 79 return fmt.Errorf("Not found: %s", n) 80 } 81 82 if rs.Primary.ID == "" { 83 return fmt.Errorf("No ID is set") 84 } 85 86 conn := testAccProvider.Meta().(*AWSClient).ec2conn 87 resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesRequest{ 88 RouteTableIDs: []string{rs.Primary.Attributes["route_table_id"]}, 89 }) 90 if err != nil { 91 return err 92 } 93 if len(resp.RouteTables) == 0 { 94 return fmt.Errorf("RouteTable not found") 95 } 96 97 *v = resp.RouteTables[0] 98 99 if len(v.Associations) == 0 { 100 return fmt.Errorf("no associations") 101 } 102 103 return nil 104 } 105 } 106 107 const testAccRouteTableAssociationConfig = ` 108 resource "aws_vpc" "foo" { 109 cidr_block = "10.1.0.0/16" 110 } 111 112 resource "aws_subnet" "foo" { 113 vpc_id = "${aws_vpc.foo.id}" 114 cidr_block = "10.1.1.0/24" 115 } 116 117 resource "aws_internet_gateway" "foo" { 118 vpc_id = "${aws_vpc.foo.id}" 119 } 120 121 resource "aws_route_table" "foo" { 122 vpc_id = "${aws_vpc.foo.id}" 123 route { 124 cidr_block = "10.0.0.0/8" 125 gateway_id = "${aws_internet_gateway.foo.id}" 126 } 127 } 128 129 resource "aws_route_table_association" "foo" { 130 route_table_id = "${aws_route_table.foo.id}" 131 subnet_id = "${aws_subnet.foo.id}" 132 } 133 ` 134 135 const testAccRouteTableAssociationConfigChange = ` 136 resource "aws_vpc" "foo" { 137 cidr_block = "10.1.0.0/16" 138 } 139 140 resource "aws_subnet" "foo" { 141 vpc_id = "${aws_vpc.foo.id}" 142 cidr_block = "10.1.1.0/24" 143 } 144 145 resource "aws_internet_gateway" "foo" { 146 vpc_id = "${aws_vpc.foo.id}" 147 } 148 149 resource "aws_route_table" "bar" { 150 vpc_id = "${aws_vpc.foo.id}" 151 route { 152 cidr_block = "10.0.0.0/8" 153 gateway_id = "${aws_internet_gateway.foo.id}" 154 } 155 } 156 157 resource "aws_route_table_association" "foo" { 158 route_table_id = "${aws_route_table.bar.id}" 159 subnet_id = "${aws_subnet.foo.id}" 160 } 161 `