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