github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSVPCPeeringConnection_normal(t *testing.T) {
    13  	var conf ec2.Address
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccVpcPeeringConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &conf),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
    31  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "aws_vpc_peering_connection" {
    35  			continue
    36  		}
    37  
    38  		describe, err := conn.DescribeVpcPeeringConnection([]string{rs.Primary.ID}, ec2.NewFilter())
    39  
    40  		if err == nil {
    41  			if len(describe.VpcPeeringConnections) != 0 {
    42  				return fmt.Errorf("vpc peering connection still exists")
    43  			}
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func testAccCheckAWSVpcPeeringConnectionExists(n string, res *ec2.Address) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No vpc peering connection id is set")
    59  		}
    60  
    61  		return nil
    62  	}
    63  }
    64  
    65  const testAccVpcPeeringConfig = `
    66  resource "aws_vpc" "foo" {
    67      cidr_block = "10.0.0.0/16"
    68  }
    69  
    70  resource "aws_vpc" "bar" {
    71      cidr_block = "10.0.1.0/16"
    72  }
    73  
    74  resource "aws_vpc_peering_connection" "foo" {
    75      peer_owner_id = "12345"
    76      vpc_id = "${aws_vpc.foo.id}"
    77      peer_vpc_id = "${aws_vpc.bar.id}"
    78  }
    79  `