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