github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/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/aws-sdk-go/gen/ec2"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    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.DescribeVPCPeeringConnections(
    39  			&ec2.DescribeVPCPeeringConnectionsRequest{
    40  				VPCPeeringConnectionIDs: []string{rs.Primary.ID},
    41  			})
    42  
    43  		if err == nil {
    44  			if len(describe.VPCPeeringConnections) != 0 {
    45  				return fmt.Errorf("vpc peering connection still exists")
    46  			}
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func testAccCheckAWSVpcPeeringConnectionExists(n string, res *ec2.Address) resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		rs, ok := s.RootModule().Resources[n]
    56  		if !ok {
    57  			return fmt.Errorf("Not found: %s", n)
    58  		}
    59  
    60  		if rs.Primary.ID == "" {
    61  			return fmt.Errorf("No vpc peering connection id is set")
    62  		}
    63  
    64  		return nil
    65  	}
    66  }
    67  
    68  const testAccVpcPeeringConfig = `
    69  resource "aws_vpc" "foo" {
    70      cidr_block = "10.0.0.0/16"
    71  }
    72  
    73  resource "aws_vpc" "bar" {
    74      cidr_block = "10.1.0.0/16"
    75  }
    76  
    77  resource "aws_vpc_peering_connection" "foo" {
    78      vpc_id = "${aws_vpc.foo.id}"
    79      peer_vpc_id = "${aws_vpc.bar.id}"
    80  }
    81  `