github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "reflect" 7 "regexp" 8 "testing" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSVPCPeeringConnection_basic(t *testing.T) { 17 var connection ec2.VpcPeeringConnection 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 IDRefreshName: "aws_vpc_peering_connection.foo", 22 IDRefreshIgnore: []string{"auto_accept"}, 23 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccVpcPeeringConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSVpcPeeringConnectionExists( 31 "aws_vpc_peering_connection.foo", 32 &connection), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAWSVPCPeeringConnection_plan(t *testing.T) { 40 var connection ec2.VpcPeeringConnection 41 42 // reach out and DELETE the VPC Peering connection outside of Terraform 43 testDestroy := func(*terraform.State) error { 44 conn := testAccProvider.Meta().(*AWSClient).ec2conn 45 log.Printf("[DEBUG] Test deleting the VPC Peering Connection.") 46 _, err := conn.DeleteVpcPeeringConnection( 47 &ec2.DeleteVpcPeeringConnectionInput{ 48 VpcPeeringConnectionId: connection.VpcPeeringConnectionId, 49 }) 50 if err != nil { 51 return err 52 } 53 return nil 54 } 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 IDRefreshIgnore: []string{"auto_accept"}, 59 Providers: testAccProviders, 60 CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, 61 Steps: []resource.TestStep{ 62 resource.TestStep{ 63 Config: testAccVpcPeeringConfig, 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckAWSVpcPeeringConnectionExists( 66 "aws_vpc_peering_connection.foo", 67 &connection), 68 testDestroy, 69 ), 70 ExpectNonEmptyPlan: true, 71 }, 72 }, 73 }) 74 } 75 76 func TestAccAWSVPCPeeringConnection_tags(t *testing.T) { 77 var connection ec2.VpcPeeringConnection 78 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 IDRefreshName: "aws_vpc_peering_connection.foo", 82 IDRefreshIgnore: []string{"auto_accept"}, 83 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckVpcDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: testAccVpcPeeringConfigTags, 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckAWSVpcPeeringConnectionExists( 91 "aws_vpc_peering_connection.foo", 92 &connection), 93 testAccCheckTags(&connection.Tags, "foo", "bar"), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccAWSVPCPeeringConnection_options(t *testing.T) { 101 var connection ec2.VpcPeeringConnection 102 103 testAccepterChange := func(*terraform.State) error { 104 conn := testAccProvider.Meta().(*AWSClient).ec2conn 105 log.Printf("[DEBUG] Test change to the VPC Peering Connection Options.") 106 107 _, err := conn.ModifyVpcPeeringConnectionOptions( 108 &ec2.ModifyVpcPeeringConnectionOptionsInput{ 109 VpcPeeringConnectionId: connection.VpcPeeringConnectionId, 110 AccepterPeeringConnectionOptions: &ec2.PeeringConnectionOptionsRequest{ 111 AllowDnsResolutionFromRemoteVpc: aws.Bool(false), 112 }, 113 }) 114 if err != nil { 115 return err 116 } 117 return nil 118 } 119 120 resource.Test(t, resource.TestCase{ 121 PreCheck: func() { testAccPreCheck(t) }, 122 IDRefreshName: "aws_vpc_peering_connection.foo", 123 IDRefreshIgnore: []string{"auto_accept"}, 124 125 Providers: testAccProviders, 126 CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, 127 Steps: []resource.TestStep{ 128 resource.TestStep{ 129 Config: testAccVpcPeeringConfigOptions, 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckAWSVpcPeeringConnectionExists( 132 "aws_vpc_peering_connection.foo", 133 &connection), 134 resource.TestCheckResourceAttr( 135 "aws_vpc_peering_connection.foo", 136 "accepter.#", "1"), 137 resource.TestCheckResourceAttr( 138 "aws_vpc_peering_connection.foo", 139 "accepter.1102046665.allow_remote_vpc_dns_resolution", "true"), 140 testAccCheckAWSVpcPeeringConnectionOptions( 141 "aws_vpc_peering_connection.foo", "accepter", 142 &ec2.VpcPeeringConnectionOptionsDescription{ 143 AllowDnsResolutionFromRemoteVpc: aws.Bool(true), 144 AllowEgressFromLocalClassicLinkToRemoteVpc: aws.Bool(false), 145 AllowEgressFromLocalVpcToRemoteClassicLink: aws.Bool(false), 146 }), 147 resource.TestCheckResourceAttr( 148 "aws_vpc_peering_connection.foo", 149 "requester.#", "1"), 150 resource.TestCheckResourceAttr( 151 "aws_vpc_peering_connection.foo", 152 "requester.41753983.allow_classic_link_to_remote_vpc", "true"), 153 resource.TestCheckResourceAttr( 154 "aws_vpc_peering_connection.foo", 155 "requester.41753983.allow_vpc_to_remote_classic_link", "true"), 156 testAccCheckAWSVpcPeeringConnectionOptions( 157 "aws_vpc_peering_connection.foo", "requester", 158 &ec2.VpcPeeringConnectionOptionsDescription{ 159 AllowDnsResolutionFromRemoteVpc: aws.Bool(false), 160 AllowEgressFromLocalClassicLinkToRemoteVpc: aws.Bool(true), 161 AllowEgressFromLocalVpcToRemoteClassicLink: aws.Bool(true), 162 }, 163 ), 164 testAccepterChange, 165 ), 166 ExpectNonEmptyPlan: true, 167 }, 168 resource.TestStep{ 169 Config: testAccVpcPeeringConfigOptions, 170 Check: resource.ComposeTestCheckFunc( 171 testAccCheckAWSVpcPeeringConnectionExists( 172 "aws_vpc_peering_connection.foo", 173 &connection), 174 resource.TestCheckResourceAttr( 175 "aws_vpc_peering_connection.foo", 176 "accepter.#", "1"), 177 resource.TestCheckResourceAttr( 178 "aws_vpc_peering_connection.foo", 179 "accepter.1102046665.allow_remote_vpc_dns_resolution", "true"), 180 testAccCheckAWSVpcPeeringConnectionOptions( 181 "aws_vpc_peering_connection.foo", "accepter", 182 &ec2.VpcPeeringConnectionOptionsDescription{ 183 AllowDnsResolutionFromRemoteVpc: aws.Bool(true), 184 AllowEgressFromLocalClassicLinkToRemoteVpc: aws.Bool(false), 185 AllowEgressFromLocalVpcToRemoteClassicLink: aws.Bool(false), 186 }, 187 ), 188 ), 189 }, 190 }, 191 }) 192 } 193 194 func TestAccAWSVPCPeeringConnection_failedState(t *testing.T) { 195 resource.Test(t, resource.TestCase{ 196 PreCheck: func() { testAccPreCheck(t) }, 197 IDRefreshIgnore: []string{"auto_accept"}, 198 Providers: testAccProviders, 199 CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, 200 Steps: []resource.TestStep{ 201 resource.TestStep{ 202 Config: testAccVpcPeeringConfigFailedState, 203 ExpectError: regexp.MustCompile(`.*Error waiting.*\(pcx-\w+\).*incorrect.*VPC-ID.*`), 204 }, 205 }, 206 }) 207 } 208 209 func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error { 210 conn := testAccProvider.Meta().(*AWSClient).ec2conn 211 212 for _, rs := range s.RootModule().Resources { 213 if rs.Type != "aws_vpc_peering_connection" { 214 continue 215 } 216 217 describe, err := conn.DescribeVpcPeeringConnections( 218 &ec2.DescribeVpcPeeringConnectionsInput{ 219 VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)}, 220 }) 221 222 if err != nil { 223 return err 224 } 225 226 var pc *ec2.VpcPeeringConnection 227 for _, c := range describe.VpcPeeringConnections { 228 if rs.Primary.ID == *c.VpcPeeringConnectionId { 229 pc = c 230 } 231 } 232 233 if pc == nil { 234 // not found 235 return nil 236 } 237 238 if pc.Status != nil { 239 if *pc.Status.Code == "deleted" { 240 return nil 241 } 242 return fmt.Errorf("Found the VPC Peering Connection in an unexpected state: %s", pc) 243 } 244 245 // return error here; we've found the vpc_peering object we want, however 246 // it's not in an expected state 247 return fmt.Errorf("Fall through error for testAccCheckAWSVpcPeeringConnectionDestroy.") 248 } 249 250 return nil 251 } 252 253 func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc { 254 return func(s *terraform.State) error { 255 rs, ok := s.RootModule().Resources[n] 256 if !ok { 257 return fmt.Errorf("Not found: %s", n) 258 } 259 260 if rs.Primary.ID == "" { 261 return fmt.Errorf("No VPC Peering Connection ID is set.") 262 } 263 264 conn := testAccProvider.Meta().(*AWSClient).ec2conn 265 resp, err := conn.DescribeVpcPeeringConnections( 266 &ec2.DescribeVpcPeeringConnectionsInput{ 267 VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)}, 268 }) 269 if err != nil { 270 return err 271 } 272 if len(resp.VpcPeeringConnections) == 0 { 273 return fmt.Errorf("VPC Peering Connection could not be found") 274 } 275 276 *connection = *resp.VpcPeeringConnections[0] 277 278 return nil 279 } 280 } 281 282 func testAccCheckAWSVpcPeeringConnectionOptions(n, block string, options *ec2.VpcPeeringConnectionOptionsDescription) resource.TestCheckFunc { 283 return func(s *terraform.State) error { 284 rs, ok := s.RootModule().Resources[n] 285 if !ok { 286 return fmt.Errorf("Not found: %s", n) 287 } 288 289 if rs.Primary.ID == "" { 290 return fmt.Errorf("No VPC Peering Connection ID is set.") 291 } 292 293 conn := testAccProvider.Meta().(*AWSClient).ec2conn 294 resp, err := conn.DescribeVpcPeeringConnections( 295 &ec2.DescribeVpcPeeringConnectionsInput{ 296 VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)}, 297 }) 298 if err != nil { 299 return err 300 } 301 302 pc := resp.VpcPeeringConnections[0] 303 304 o := pc.AccepterVpcInfo 305 if block == "requester" { 306 o = pc.RequesterVpcInfo 307 } 308 309 if !reflect.DeepEqual(o.PeeringOptions, options) { 310 return fmt.Errorf("Expected the VPC Peering Connection Options to be %#v, got %#v", 311 options, o.PeeringOptions) 312 } 313 314 return nil 315 } 316 } 317 318 const testAccVpcPeeringConfig = ` 319 resource "aws_vpc" "foo" { 320 cidr_block = "10.0.0.0/16" 321 tags { 322 Name = "TestAccAWSVPCPeeringConnection_basic" 323 } 324 } 325 326 resource "aws_vpc" "bar" { 327 cidr_block = "10.1.0.0/16" 328 } 329 330 resource "aws_vpc_peering_connection" "foo" { 331 vpc_id = "${aws_vpc.foo.id}" 332 peer_vpc_id = "${aws_vpc.bar.id}" 333 auto_accept = true 334 } 335 ` 336 337 const testAccVpcPeeringConfigTags = ` 338 resource "aws_vpc" "foo" { 339 cidr_block = "10.0.0.0/16" 340 tags { 341 Name = "TestAccAWSVPCPeeringConnection_tags" 342 } 343 } 344 345 resource "aws_vpc" "bar" { 346 cidr_block = "10.1.0.0/16" 347 } 348 349 resource "aws_vpc_peering_connection" "foo" { 350 vpc_id = "${aws_vpc.foo.id}" 351 peer_vpc_id = "${aws_vpc.bar.id}" 352 auto_accept = true 353 tags { 354 foo = "bar" 355 } 356 } 357 ` 358 359 const testAccVpcPeeringConfigOptions = ` 360 resource "aws_vpc" "foo" { 361 cidr_block = "10.0.0.0/16" 362 tags { 363 Name = "TestAccAWSVPCPeeringConnection_options" 364 } 365 } 366 367 resource "aws_vpc" "bar" { 368 cidr_block = "10.1.0.0/16" 369 enable_dns_hostnames = true 370 } 371 372 resource "aws_vpc_peering_connection" "foo" { 373 vpc_id = "${aws_vpc.foo.id}" 374 peer_vpc_id = "${aws_vpc.bar.id}" 375 auto_accept = true 376 377 accepter { 378 allow_remote_vpc_dns_resolution = true 379 } 380 381 requester { 382 allow_vpc_to_remote_classic_link = true 383 allow_classic_link_to_remote_vpc = true 384 } 385 } 386 ` 387 388 const testAccVpcPeeringConfigFailedState = ` 389 resource "aws_vpc" "foo" { 390 cidr_block = "10.0.0.0/16" 391 tags { 392 Name = "TestAccAWSVPCPeeringConnection_failedState" 393 } 394 } 395 396 resource "aws_vpc" "bar" { 397 cidr_block = "10.0.0.0/16" 398 } 399 400 resource "aws_vpc_peering_connection" "foo" { 401 vpc_id = "${aws_vpc.foo.id}" 402 peer_vpc_id = "${aws_vpc.bar.id}" 403 } 404 `