github.com/kwoods/terraform@v0.6.11-0.20160809170336-13497db7138e/builtin/providers/aws/resource_aws_vpn_gateway_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 TestAccAWSVpnGateway_basic(t *testing.T) { 15 var v, v2 ec2.VpnGateway 16 17 testNotEqual := func(*terraform.State) error { 18 if len(v.VpcAttachments) == 0 { 19 return fmt.Errorf("VPN Gateway A is not attached") 20 } 21 if len(v2.VpcAttachments) == 0 { 22 return fmt.Errorf("VPN Gateway B is not attached") 23 } 24 25 id1 := v.VpcAttachments[0].VpcId 26 id2 := v2.VpcAttachments[0].VpcId 27 if id1 == id2 { 28 return fmt.Errorf("Both attachment IDs are the same") 29 } 30 31 return nil 32 } 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 IDRefreshName: "aws_vpn_gateway.foo", 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckVpnGatewayDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccVpnGatewayConfig, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckVpnGatewayExists( 44 "aws_vpn_gateway.foo", &v), 45 ), 46 }, 47 48 resource.TestStep{ 49 Config: testAccVpnGatewayConfigChangeVPC, 50 Check: resource.ComposeTestCheckFunc( 51 testAccCheckVpnGatewayExists( 52 "aws_vpn_gateway.foo", &v2), 53 testNotEqual, 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccAWSVpnGateway_reattach(t *testing.T) { 61 var vpc1, vpc2 ec2.Vpc 62 var vgw1, vgw2 ec2.VpnGateway 63 64 testAttachmentFunc := func(vgw *ec2.VpnGateway, vpc *ec2.Vpc) func(*terraform.State) error { 65 return func(*terraform.State) error { 66 if len(vgw.VpcAttachments) == 0 { 67 return fmt.Errorf("VPN Gateway %q has no VPC attachments.", 68 *vgw.VpnGatewayId) 69 } 70 71 if len(vgw.VpcAttachments) > 1 { 72 count := 0 73 for _, v := range vgw.VpcAttachments { 74 if *v.State == "attached" { 75 count += 1 76 } 77 } 78 if count > 1 { 79 return fmt.Errorf( 80 "VPN Gateway %q has an unexpected number of VPC attachments (more than 1): %#v", 81 *vgw.VpnGatewayId, vgw.VpcAttachments) 82 } 83 } 84 85 if *vgw.VpcAttachments[0].State != "attached" { 86 return fmt.Errorf("Expected VPN Gateway %q to be attached.", 87 *vgw.VpnGatewayId) 88 } 89 90 if *vgw.VpcAttachments[0].VpcId != *vpc.VpcId { 91 return fmt.Errorf("Expected VPN Gateway %q to be attached to VPC %q, but got: %q", 92 *vgw.VpnGatewayId, *vpc.VpcId, *vgw.VpcAttachments[0].VpcId) 93 } 94 return nil 95 } 96 } 97 98 resource.Test(t, resource.TestCase{ 99 PreCheck: func() { testAccPreCheck(t) }, 100 IDRefreshName: "aws_vpn_gateway.foo", 101 Providers: testAccProviders, 102 CheckDestroy: testAccCheckVpnGatewayDestroy, 103 Steps: []resource.TestStep{ 104 resource.TestStep{ 105 Config: testAccCheckVpnGatewayConfigReattach, 106 Check: resource.ComposeTestCheckFunc( 107 testAccCheckVpcExists("aws_vpc.foo", &vpc1), 108 testAccCheckVpcExists("aws_vpc.bar", &vpc2), 109 testAccCheckVpnGatewayExists( 110 "aws_vpn_gateway.foo", &vgw1), 111 testAccCheckVpnGatewayExists( 112 "aws_vpn_gateway.bar", &vgw2), 113 testAttachmentFunc(&vgw1, &vpc1), 114 testAttachmentFunc(&vgw2, &vpc2), 115 ), 116 }, 117 resource.TestStep{ 118 Config: testAccCheckVpnGatewayConfigReattachChange, 119 Check: resource.ComposeTestCheckFunc( 120 testAccCheckVpnGatewayExists( 121 "aws_vpn_gateway.foo", &vgw1), 122 testAccCheckVpnGatewayExists( 123 "aws_vpn_gateway.bar", &vgw2), 124 testAttachmentFunc(&vgw2, &vpc1), 125 testAttachmentFunc(&vgw1, &vpc2), 126 ), 127 }, 128 resource.TestStep{ 129 Config: testAccCheckVpnGatewayConfigReattach, 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckVpnGatewayExists( 132 "aws_vpn_gateway.foo", &vgw1), 133 testAccCheckVpnGatewayExists( 134 "aws_vpn_gateway.bar", &vgw2), 135 testAttachmentFunc(&vgw1, &vpc1), 136 testAttachmentFunc(&vgw2, &vpc2), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func TestAccAWSVpnGateway_delete(t *testing.T) { 144 var vpnGateway ec2.VpnGateway 145 146 testDeleted := func(r string) resource.TestCheckFunc { 147 return func(s *terraform.State) error { 148 _, ok := s.RootModule().Resources[r] 149 if ok { 150 return fmt.Errorf("VPN Gateway %q should have been deleted.", r) 151 } 152 return nil 153 } 154 } 155 156 resource.Test(t, resource.TestCase{ 157 PreCheck: func() { testAccPreCheck(t) }, 158 IDRefreshName: "aws_vpn_gateway.foo", 159 Providers: testAccProviders, 160 CheckDestroy: testAccCheckVpnGatewayDestroy, 161 Steps: []resource.TestStep{ 162 resource.TestStep{ 163 Config: testAccVpnGatewayConfig, 164 Check: resource.ComposeTestCheckFunc( 165 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &vpnGateway)), 166 }, 167 resource.TestStep{ 168 Config: testAccNoVpnGatewayConfig, 169 Check: resource.ComposeTestCheckFunc(testDeleted("aws_vpn_gateway.foo")), 170 }, 171 }, 172 }) 173 } 174 175 func TestAccAWSVpnGateway_tags(t *testing.T) { 176 var v ec2.VpnGateway 177 178 resource.Test(t, resource.TestCase{ 179 PreCheck: func() { testAccPreCheck(t) }, 180 IDRefreshName: "aws_vpn_gateway.foo", 181 Providers: testAccProviders, 182 CheckDestroy: testAccCheckVpnGatewayDestroy, 183 Steps: []resource.TestStep{ 184 resource.TestStep{ 185 Config: testAccCheckVpnGatewayConfigTags, 186 Check: resource.ComposeTestCheckFunc( 187 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v), 188 testAccCheckTags(&v.Tags, "foo", "bar"), 189 ), 190 }, 191 resource.TestStep{ 192 Config: testAccCheckVpnGatewayConfigTagsUpdate, 193 Check: resource.ComposeTestCheckFunc( 194 testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v), 195 testAccCheckTags(&v.Tags, "foo", ""), 196 testAccCheckTags(&v.Tags, "bar", "baz"), 197 ), 198 }, 199 }, 200 }) 201 } 202 203 func testAccCheckVpnGatewayDestroy(s *terraform.State) error { 204 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 205 206 for _, rs := range s.RootModule().Resources { 207 if rs.Type != "aws_vpn_gateway" { 208 continue 209 } 210 211 // Try to find the resource 212 resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ 213 VpnGatewayIds: []*string{aws.String(rs.Primary.ID)}, 214 }) 215 if err == nil { 216 var v *ec2.VpnGateway 217 for _, g := range resp.VpnGateways { 218 if *g.VpnGatewayId == rs.Primary.ID { 219 v = g 220 } 221 } 222 223 if v == nil { 224 // wasn't found 225 return nil 226 } 227 228 if *v.State != "deleted" { 229 return fmt.Errorf("Expected VPN Gateway to be in deleted state, but was not: %s", v) 230 } 231 return nil 232 } 233 234 // Verify the error is what we want 235 ec2err, ok := err.(awserr.Error) 236 if !ok { 237 return err 238 } 239 if ec2err.Code() != "InvalidVpnGatewayID.NotFound" { 240 return err 241 } 242 } 243 244 return nil 245 } 246 247 func testAccCheckVpnGatewayExists(n string, ig *ec2.VpnGateway) resource.TestCheckFunc { 248 return func(s *terraform.State) error { 249 rs, ok := s.RootModule().Resources[n] 250 if !ok { 251 return fmt.Errorf("Not found: %s", n) 252 } 253 254 if rs.Primary.ID == "" { 255 return fmt.Errorf("No ID is set") 256 } 257 258 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 259 resp, err := ec2conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{ 260 VpnGatewayIds: []*string{aws.String(rs.Primary.ID)}, 261 }) 262 if err != nil { 263 return err 264 } 265 if len(resp.VpnGateways) == 0 { 266 return fmt.Errorf("VPN Gateway not found") 267 } 268 269 *ig = *resp.VpnGateways[0] 270 271 return nil 272 } 273 } 274 275 const testAccNoVpnGatewayConfig = ` 276 resource "aws_vpc" "foo" { 277 cidr_block = "10.1.0.0/16" 278 } 279 ` 280 281 const testAccVpnGatewayConfig = ` 282 resource "aws_vpc" "foo" { 283 cidr_block = "10.1.0.0/16" 284 } 285 286 resource "aws_vpn_gateway" "foo" { 287 vpc_id = "${aws_vpc.foo.id}" 288 } 289 ` 290 291 const testAccVpnGatewayConfigChangeVPC = ` 292 resource "aws_vpc" "bar" { 293 cidr_block = "10.2.0.0/16" 294 } 295 296 resource "aws_vpn_gateway" "foo" { 297 vpc_id = "${aws_vpc.bar.id}" 298 } 299 ` 300 301 const testAccCheckVpnGatewayConfigTags = ` 302 resource "aws_vpc" "foo" { 303 cidr_block = "10.1.0.0/16" 304 } 305 306 resource "aws_vpn_gateway" "foo" { 307 vpc_id = "${aws_vpc.foo.id}" 308 tags { 309 foo = "bar" 310 } 311 } 312 ` 313 314 const testAccCheckVpnGatewayConfigTagsUpdate = ` 315 resource "aws_vpc" "foo" { 316 cidr_block = "10.1.0.0/16" 317 } 318 319 resource "aws_vpn_gateway" "foo" { 320 vpc_id = "${aws_vpc.foo.id}" 321 tags { 322 bar = "baz" 323 } 324 } 325 ` 326 327 const testAccCheckVpnGatewayConfigReattach = ` 328 resource "aws_vpc" "foo" { 329 cidr_block = "10.1.0.0/16" 330 } 331 332 resource "aws_vpc" "bar" { 333 cidr_block = "10.2.0.0/16" 334 } 335 336 resource "aws_vpn_gateway" "foo" { 337 vpc_id = "${aws_vpc.foo.id}" 338 } 339 340 resource "aws_vpn_gateway" "bar" { 341 vpc_id = "${aws_vpc.bar.id}" 342 } 343 ` 344 345 const testAccCheckVpnGatewayConfigReattachChange = ` 346 resource "aws_vpc" "foo" { 347 cidr_block = "10.1.0.0/16" 348 } 349 350 resource "aws_vpc" "bar" { 351 cidr_block = "10.2.0.0/16" 352 } 353 354 resource "aws_vpn_gateway" "foo" { 355 vpc_id = "${aws_vpc.bar.id}" 356 } 357 358 resource "aws_vpn_gateway" "bar" { 359 vpc_id = "${aws_vpc.foo.id}" 360 } 361 `