github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 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 TestAccAWSVPCPeeringConnection_basic(t *testing.T) { 15 var connection ec2.VPCPeeringConnection 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { 19 testAccPreCheck(t) 20 if os.Getenv("AWS_ACCOUNT_ID") == "" { 21 t.Fatal("AWS_ACCOUNT_ID must be set") 22 } 23 }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccVpcPeeringConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSVPCPeeringConnection_tags(t *testing.T) { 38 var connection ec2.VPCPeeringConnection 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckVpcDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccVpcPeeringConfigTags, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection), 49 testAccCheckTags(&connection.Tags, "foo", "bar"), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error { 57 conn := testAccProvider.Meta().(*AWSClient).ec2conn 58 59 for _, rs := range s.RootModule().Resources { 60 if rs.Type != "aws_vpc_peering_connection" { 61 continue 62 } 63 64 describe, err := conn.DescribeVPCPeeringConnections( 65 &ec2.DescribeVPCPeeringConnectionsInput{ 66 VPCPeeringConnectionIDs: []*string{aws.String(rs.Primary.ID)}, 67 }) 68 69 if err == nil { 70 if len(describe.VPCPeeringConnections) != 0 { 71 return fmt.Errorf("vpc peering connection still exists") 72 } 73 } 74 } 75 76 return nil 77 } 78 79 func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VPCPeeringConnection) resource.TestCheckFunc { 80 return func(s *terraform.State) error { 81 rs, ok := s.RootModule().Resources[n] 82 if !ok { 83 return fmt.Errorf("Not found: %s", n) 84 } 85 86 if rs.Primary.ID == "" { 87 return fmt.Errorf("No vpc peering connection id is set") 88 } 89 90 conn := testAccProvider.Meta().(*AWSClient).ec2conn 91 resp, err := conn.DescribeVPCPeeringConnections( 92 &ec2.DescribeVPCPeeringConnectionsInput{ 93 VPCPeeringConnectionIDs: []*string{aws.String(rs.Primary.ID)}, 94 }) 95 if err != nil { 96 return err 97 } 98 if len(resp.VPCPeeringConnections) == 0 { 99 return fmt.Errorf("VPC peering connection not found") 100 } 101 102 *connection = *resp.VPCPeeringConnections[0] 103 104 return nil 105 } 106 } 107 108 const testAccVpcPeeringConfig = ` 109 resource "aws_vpc" "foo" { 110 cidr_block = "10.0.0.0/16" 111 } 112 113 resource "aws_vpc" "bar" { 114 cidr_block = "10.1.0.0/16" 115 } 116 117 resource "aws_vpc_peering_connection" "foo" { 118 vpc_id = "${aws_vpc.foo.id}" 119 peer_vpc_id = "${aws_vpc.bar.id}" 120 } 121 ` 122 123 const testAccVpcPeeringConfigTags = ` 124 resource "aws_vpc" "foo" { 125 cidr_block = "10.0.0.0/16" 126 } 127 128 resource "aws_vpc" "bar" { 129 cidr_block = "10.1.0.0/16" 130 } 131 132 resource "aws_vpc_peering_connection" "foo" { 133 vpc_id = "${aws_vpc.foo.id}" 134 peer_vpc_id = "${aws_vpc.bar.id}" 135 tags { 136 foo = "bar" 137 } 138 } 139 `