github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_main_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/awserr" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAWSMainRouteTableAssociation_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckMainRouteTableAssociationDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccMainRouteTableAssociationConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckMainRouteTableAssociation( 22 "aws_main_route_table_association.foo", 23 "aws_vpc.foo", 24 "aws_route_table.foo", 25 ), 26 ), 27 }, 28 resource.TestStep{ 29 Config: testAccMainRouteTableAssociationConfigUpdate, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckMainRouteTableAssociation( 32 "aws_main_route_table_association.foo", 33 "aws_vpc.foo", 34 "aws_route_table.bar", 35 ), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckMainRouteTableAssociationDestroy(s *terraform.State) error { 43 conn := testAccProvider.Meta().(*AWSClient).ec2conn 44 45 for _, rs := range s.RootModule().Resources { 46 if rs.Type != "aws_main_route_table_association" { 47 continue 48 } 49 50 mainAssociation, err := findMainRouteTableAssociation( 51 conn, 52 rs.Primary.Attributes["vpc_id"], 53 ) 54 if err != nil { 55 // Verify the error is what we want 56 if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" { 57 continue 58 } 59 return err 60 } 61 62 if mainAssociation != nil { 63 return fmt.Errorf("still exists") 64 } 65 } 66 67 return nil 68 } 69 70 func testAccCheckMainRouteTableAssociation( 71 mainRouteTableAssociationResource string, 72 vpcResource string, 73 routeTableResource string) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 rs, ok := s.RootModule().Resources[mainRouteTableAssociationResource] 76 if !ok { 77 return fmt.Errorf("Not found: %s", mainRouteTableAssociationResource) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No ID is set") 82 } 83 84 vpc, ok := s.RootModule().Resources[vpcResource] 85 if !ok { 86 return fmt.Errorf("Not found: %s", vpcResource) 87 } 88 89 conn := testAccProvider.Meta().(*AWSClient).ec2conn 90 mainAssociation, err := findMainRouteTableAssociation(conn, vpc.Primary.ID) 91 if err != nil { 92 return err 93 } 94 95 if *mainAssociation.RouteTableAssociationId != rs.Primary.ID { 96 return fmt.Errorf("Found wrong main association: %s", 97 *mainAssociation.RouteTableAssociationId) 98 } 99 100 return nil 101 } 102 } 103 104 const testAccMainRouteTableAssociationConfig = ` 105 resource "aws_vpc" "foo" { 106 cidr_block = "10.1.0.0/16" 107 } 108 109 resource "aws_subnet" "foo" { 110 vpc_id = "${aws_vpc.foo.id}" 111 cidr_block = "10.1.1.0/24" 112 } 113 114 resource "aws_internet_gateway" "foo" { 115 vpc_id = "${aws_vpc.foo.id}" 116 } 117 118 resource "aws_route_table" "foo" { 119 vpc_id = "${aws_vpc.foo.id}" 120 route { 121 cidr_block = "10.0.0.0/8" 122 gateway_id = "${aws_internet_gateway.foo.id}" 123 } 124 } 125 126 resource "aws_main_route_table_association" "foo" { 127 vpc_id = "${aws_vpc.foo.id}" 128 route_table_id = "${aws_route_table.foo.id}" 129 } 130 ` 131 132 const testAccMainRouteTableAssociationConfigUpdate = ` 133 resource "aws_vpc" "foo" { 134 cidr_block = "10.1.0.0/16" 135 } 136 137 resource "aws_subnet" "foo" { 138 vpc_id = "${aws_vpc.foo.id}" 139 cidr_block = "10.1.1.0/24" 140 } 141 142 resource "aws_internet_gateway" "foo" { 143 vpc_id = "${aws_vpc.foo.id}" 144 } 145 146 // Need to keep the old route table around when we update the 147 // main_route_table_association, otherwise Terraform will try to destroy the 148 // route table too early, and will fail because it's still the main one 149 resource "aws_route_table" "foo" { 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" "bar" { 158 vpc_id = "${aws_vpc.foo.id}" 159 route { 160 cidr_block = "10.0.0.0/8" 161 gateway_id = "${aws_internet_gateway.foo.id}" 162 } 163 } 164 165 resource "aws_main_route_table_association" "foo" { 166 vpc_id = "${aws_vpc.foo.id}" 167 route_table_id = "${aws_route_table.bar.id}" 168 } 169 `