github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/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 peerId := os.Getenv("TF_PEER_ID") 40 if peerId == "" { 41 t.Skip("Error: TestAccAWSVPCPeeringConnection_tags requires a peer id to be set") 42 } 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckVpcDestroy, 48 Steps: []resource.TestStep{ 49 resource.TestStep{ 50 Config: fmt.Sprintf(testAccVpcPeeringConfigTags, peerId), 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection), 53 testAccCheckTags(&connection.Tags, "foo", "bar"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error { 61 conn := testAccProvider.Meta().(*AWSClient).ec2conn 62 63 for _, rs := range s.RootModule().Resources { 64 if rs.Type != "aws_vpc_peering_connection" { 65 continue 66 } 67 68 describe, err := conn.DescribeVpcPeeringConnections( 69 &ec2.DescribeVpcPeeringConnectionsInput{ 70 VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)}, 71 }) 72 73 if err != nil { 74 return err 75 } 76 77 var pc *ec2.VpcPeeringConnection 78 for _, c := range describe.VpcPeeringConnections { 79 if rs.Primary.ID == *c.VpcPeeringConnectionId { 80 pc = c 81 } 82 } 83 84 if pc == nil { 85 // not found 86 return nil 87 } 88 89 if pc.Status != nil { 90 if *pc.Status.Code == "deleted" { 91 return nil 92 } 93 return fmt.Errorf("Found vpc peering connection in unexpected state: %s", pc) 94 } 95 96 } 97 98 return fmt.Errorf("Fall through error for testAccCheckAWSVpcPeeringConnectionDestroy") 99 } 100 101 func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc { 102 return func(s *terraform.State) error { 103 rs, ok := s.RootModule().Resources[n] 104 if !ok { 105 return fmt.Errorf("Not found: %s", n) 106 } 107 108 if rs.Primary.ID == "" { 109 return fmt.Errorf("No vpc peering connection id is set") 110 } 111 112 conn := testAccProvider.Meta().(*AWSClient).ec2conn 113 resp, err := conn.DescribeVpcPeeringConnections( 114 &ec2.DescribeVpcPeeringConnectionsInput{ 115 VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)}, 116 }) 117 if err != nil { 118 return err 119 } 120 if len(resp.VpcPeeringConnections) == 0 { 121 return fmt.Errorf("VPC peering connection not found") 122 } 123 124 *connection = *resp.VpcPeeringConnections[0] 125 126 return nil 127 } 128 } 129 130 const testAccVpcPeeringConfig = ` 131 resource "aws_vpc" "foo" { 132 cidr_block = "10.0.0.0/16" 133 } 134 135 resource "aws_vpc" "bar" { 136 cidr_block = "10.1.0.0/16" 137 } 138 139 resource "aws_vpc_peering_connection" "foo" { 140 vpc_id = "${aws_vpc.foo.id}" 141 peer_vpc_id = "${aws_vpc.bar.id}" 142 auto_accept = true 143 } 144 ` 145 146 const testAccVpcPeeringConfigTags = ` 147 resource "aws_vpc" "foo" { 148 cidr_block = "10.0.0.0/16" 149 } 150 151 resource "aws_vpc" "bar" { 152 cidr_block = "10.1.0.0/16" 153 } 154 155 resource "aws_vpc_peering_connection" "foo" { 156 vpc_id = "${aws_vpc.foo.id}" 157 peer_vpc_id = "${aws_vpc.bar.id}" 158 peer_owner_id = "%s" 159 tags { 160 foo = "bar" 161 } 162 } 163 `