github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckVpcDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: fmt.Sprintf(testAccVpcPeeringConfigTags, peerId),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
    50  					testAccCheckTags(&connection.Tags, "foo", "bar"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
    58  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    59  
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "aws_vpc_peering_connection" {
    62  			continue
    63  		}
    64  
    65  		describe, err := conn.DescribeVpcPeeringConnections(
    66  			&ec2.DescribeVpcPeeringConnectionsInput{
    67  				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
    68  			})
    69  
    70  		if err == nil {
    71  			if len(describe.VpcPeeringConnections) != 0 {
    72  				return fmt.Errorf("vpc peering connection still exists")
    73  			}
    74  		}
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		rs, ok := s.RootModule().Resources[n]
    83  		if !ok {
    84  			return fmt.Errorf("Not found: %s", n)
    85  		}
    86  
    87  		if rs.Primary.ID == "" {
    88  			return fmt.Errorf("No vpc peering connection id is set")
    89  		}
    90  
    91  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    92  		resp, err := conn.DescribeVpcPeeringConnections(
    93  			&ec2.DescribeVpcPeeringConnectionsInput{
    94  				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
    95  			})
    96  		if err != nil {
    97  			return err
    98  		}
    99  		if len(resp.VpcPeeringConnections) == 0 {
   100  			return fmt.Errorf("VPC peering connection not found")
   101  		}
   102  
   103  		*connection = *resp.VpcPeeringConnections[0]
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  const testAccVpcPeeringConfig = `
   110  resource "aws_vpc" "foo" {
   111  		cidr_block = "10.0.0.0/16"
   112  }
   113  
   114  resource "aws_vpc" "bar" {
   115  		cidr_block = "10.1.0.0/16"
   116  }
   117  
   118  resource "aws_vpc_peering_connection" "foo" {
   119  		vpc_id = "${aws_vpc.foo.id}"
   120  		peer_vpc_id = "${aws_vpc.bar.id}"
   121  		auto_accept = true
   122  }
   123  `
   124  
   125  const testAccVpcPeeringConfigTags = `
   126  resource "aws_vpc" "foo" {
   127  		cidr_block = "10.0.0.0/16"
   128  }
   129  
   130  resource "aws_vpc" "bar" {
   131  		cidr_block = "10.1.0.0/16"
   132  }
   133  
   134  resource "aws_vpc_peering_connection" "foo" {
   135  		vpc_id = "${aws_vpc.foo.id}"
   136  		peer_vpc_id = "${aws_vpc.bar.id}"
   137  		peer_owner_id = "%s"
   138  		tags {
   139  			foo = "bar"
   140  		}
   141  }
   142  `