github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSVPCPeeringConnection_basic(t *testing.T) {
    16  	var connection ec2.VpcPeeringConnection
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck: func() {
    20  			testAccPreCheck(t)
    21  			if os.Getenv("AWS_ACCOUNT_ID") == "" {
    22  				t.Fatal("AWS_ACCOUNT_ID must be set")
    23  			}
    24  		},
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: testAccVpcPeeringConfig,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccAWSVPCPeeringConnection_plan(t *testing.T) {
    39  	var connection ec2.VpcPeeringConnection
    40  
    41  	// reach out and DELETE the VPC Peering connection outside of Terraform
    42  	testDestroy := func(*terraform.State) error {
    43  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    44  		log.Printf("[DEBUG] Test deleting VPC Peering connection")
    45  		_, err := conn.DeleteVpcPeeringConnection(
    46  			&ec2.DeleteVpcPeeringConnectionInput{
    47  				VpcPeeringConnectionId: connection.VpcPeeringConnectionId,
    48  			})
    49  		if err != nil {
    50  			return err
    51  		}
    52  		return nil
    53  	}
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck: func() {
    57  			testAccPreCheck(t)
    58  			if os.Getenv("AWS_ACCOUNT_ID") == "" {
    59  				t.Fatal("AWS_ACCOUNT_ID must be set")
    60  			}
    61  		},
    62  		Providers:    testAccProviders,
    63  		CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy,
    64  		Steps: []resource.TestStep{
    65  			resource.TestStep{
    66  				Config: testAccVpcPeeringConfig,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
    69  					testDestroy,
    70  				),
    71  				ExpectNonEmptyPlan: true,
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccAWSVPCPeeringConnection_tags(t *testing.T) {
    78  	var connection ec2.VpcPeeringConnection
    79  	peerId := os.Getenv("TF_PEER_ID")
    80  	if peerId == "" {
    81  		t.Skip("Error: TestAccAWSVPCPeeringConnection_tags requires a peer id to be set")
    82  	}
    83  
    84  	resource.Test(t, resource.TestCase{
    85  		PreCheck:     func() { testAccPreCheck(t) },
    86  		Providers:    testAccProviders,
    87  		CheckDestroy: testAccCheckVpcDestroy,
    88  		Steps: []resource.TestStep{
    89  			resource.TestStep{
    90  				Config: fmt.Sprintf(testAccVpcPeeringConfigTags, peerId),
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
    93  					testAccCheckTags(&connection.Tags, "foo", "bar"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
   101  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   102  
   103  	for _, rs := range s.RootModule().Resources {
   104  		if rs.Type != "aws_vpc_peering_connection" {
   105  			continue
   106  		}
   107  
   108  		describe, err := conn.DescribeVpcPeeringConnections(
   109  			&ec2.DescribeVpcPeeringConnectionsInput{
   110  				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
   111  			})
   112  
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		var pc *ec2.VpcPeeringConnection
   118  		for _, c := range describe.VpcPeeringConnections {
   119  			if rs.Primary.ID == *c.VpcPeeringConnectionId {
   120  				pc = c
   121  			}
   122  		}
   123  
   124  		if pc == nil {
   125  			// not found
   126  			return nil
   127  		}
   128  
   129  		if pc.Status != nil {
   130  			if *pc.Status.Code == "deleted" {
   131  				return nil
   132  			}
   133  			return fmt.Errorf("Found vpc peering connection in unexpected state: %s", pc)
   134  		}
   135  
   136  		// return error here; we've found the vpc_peering object we want, however
   137  		// it's not in an expected state
   138  		return fmt.Errorf("Fall through error for testAccCheckAWSVpcPeeringConnectionDestroy")
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc {
   145  	return func(s *terraform.State) error {
   146  		rs, ok := s.RootModule().Resources[n]
   147  		if !ok {
   148  			return fmt.Errorf("Not found: %s", n)
   149  		}
   150  
   151  		if rs.Primary.ID == "" {
   152  			return fmt.Errorf("No vpc peering connection id is set")
   153  		}
   154  
   155  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   156  		resp, err := conn.DescribeVpcPeeringConnections(
   157  			&ec2.DescribeVpcPeeringConnectionsInput{
   158  				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
   159  			})
   160  		if err != nil {
   161  			return err
   162  		}
   163  		if len(resp.VpcPeeringConnections) == 0 {
   164  			return fmt.Errorf("VPC peering connection not found")
   165  		}
   166  
   167  		*connection = *resp.VpcPeeringConnections[0]
   168  
   169  		return nil
   170  	}
   171  }
   172  
   173  const testAccVpcPeeringConfig = `
   174  resource "aws_vpc" "foo" {
   175  		cidr_block = "10.0.0.0/16"
   176  		tags {
   177  			Name = "TestAccAWSVPCPeeringConnection_basic"
   178  		}
   179  }
   180  
   181  resource "aws_vpc" "bar" {
   182  		cidr_block = "10.1.0.0/16"
   183  }
   184  
   185  resource "aws_vpc_peering_connection" "foo" {
   186  		vpc_id = "${aws_vpc.foo.id}"
   187  		peer_vpc_id = "${aws_vpc.bar.id}"
   188  		auto_accept = true
   189  }
   190  `
   191  
   192  const testAccVpcPeeringConfigTags = `
   193  resource "aws_vpc" "foo" {
   194  		cidr_block = "10.0.0.0/16"
   195  		tags {
   196  			Name = "TestAccAWSVPCPeeringConnection_tags"
   197  		}
   198  }
   199  
   200  resource "aws_vpc" "bar" {
   201  		cidr_block = "10.1.0.0/16"
   202  }
   203  
   204  resource "aws_vpc_peering_connection" "foo" {
   205  		vpc_id = "${aws_vpc.foo.id}"
   206  		peer_vpc_id = "${aws_vpc.bar.id}"
   207  		peer_owner_id = "%s"
   208  		tags {
   209  			foo = "bar"
   210  		}
   211  }
   212  `