github.com/gwilym/terraform@v0.3.8-0.20151231151641-c7573de75b19/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  			if len(describe.VpcPeeringConnections) != 0 {
    75  				return fmt.Errorf("vpc peering connection still exists")
    76  			}
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc {
    84  	return func(s *terraform.State) error {
    85  		rs, ok := s.RootModule().Resources[n]
    86  		if !ok {
    87  			return fmt.Errorf("Not found: %s", n)
    88  		}
    89  
    90  		if rs.Primary.ID == "" {
    91  			return fmt.Errorf("No vpc peering connection id is set")
    92  		}
    93  
    94  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    95  		resp, err := conn.DescribeVpcPeeringConnections(
    96  			&ec2.DescribeVpcPeeringConnectionsInput{
    97  				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
    98  			})
    99  		if err != nil {
   100  			return err
   101  		}
   102  		if len(resp.VpcPeeringConnections) == 0 {
   103  			return fmt.Errorf("VPC peering connection not found")
   104  		}
   105  
   106  		*connection = *resp.VpcPeeringConnections[0]
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  const testAccVpcPeeringConfig = `
   113  resource "aws_vpc" "foo" {
   114  		cidr_block = "10.0.0.0/16"
   115  }
   116  
   117  resource "aws_vpc" "bar" {
   118  		cidr_block = "10.1.0.0/16"
   119  }
   120  
   121  resource "aws_vpc_peering_connection" "foo" {
   122  		vpc_id = "${aws_vpc.foo.id}"
   123  		peer_vpc_id = "${aws_vpc.bar.id}"
   124  		auto_accept = true
   125  }
   126  `
   127  
   128  const testAccVpcPeeringConfigTags = `
   129  resource "aws_vpc" "foo" {
   130  		cidr_block = "10.0.0.0/16"
   131  }
   132  
   133  resource "aws_vpc" "bar" {
   134  		cidr_block = "10.1.0.0/16"
   135  }
   136  
   137  resource "aws_vpc_peering_connection" "foo" {
   138  		vpc_id = "${aws_vpc.foo.id}"
   139  		peer_vpc_id = "${aws_vpc.bar.id}"
   140  		peer_owner_id = "%s"
   141  		tags {
   142  			foo = "bar"
   143  		}
   144  }
   145  `