github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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/awslabs/aws-sdk-go/aws"
     9  	"github.com/awslabs/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSVPCPeeringConnection_normal(t *testing.T) {
    15  	var conf ec2.Address
    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", &conf),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
    38  	conn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
    39  
    40  	for _, rs := range s.RootModule().Resources {
    41  		if rs.Type != "aws_vpc_peering_connection" {
    42  			continue
    43  		}
    44  
    45  		describe, err := conn.DescribeVPCPeeringConnections(
    46  			&ec2.DescribeVPCPeeringConnectionsInput{
    47  				VPCPeeringConnectionIDs: []*string{aws.String(rs.Primary.ID)},
    48  			})
    49  
    50  		if err == nil {
    51  			if len(describe.VPCPeeringConnections) != 0 {
    52  				return fmt.Errorf("vpc peering connection still exists")
    53  			}
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func testAccCheckAWSVpcPeeringConnectionExists(n string, res *ec2.Address) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No vpc peering connection id is set")
    69  		}
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  const testAccVpcPeeringConfig = `
    76  resource "aws_vpc" "foo" {
    77      cidr_block = "10.0.0.0/16"
    78  }
    79  
    80  resource "aws_vpc" "bar" {
    81      cidr_block = "10.1.0.0/16"
    82  }
    83  
    84  resource "aws_vpc_peering_connection" "foo" {
    85      vpc_id = "${aws_vpc.foo.id}"
    86      peer_vpc_id = "${aws_vpc.bar.id}"
    87  }
    88  `