github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_vpn_connection_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAwsVpnConnection_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccAwsVpnConnectionDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAwsVpnConnectionConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccAwsVpnConnection(
    24  						"aws_vpc.vpc",
    25  						"aws_vpn_gateway.vpn_gateway",
    26  						"aws_customer_gateway.customer_gateway",
    27  						"aws_vpn_connection.foo",
    28  					),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccAwsVpnConnectionConfigUpdate,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccAwsVpnConnection(
    35  						"aws_vpc.vpc",
    36  						"aws_vpn_gateway.vpn_gateway",
    37  						"aws_customer_gateway.customer_gateway",
    38  						"aws_vpn_connection.foo",
    39  					),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func testAccAwsVpnConnectionDestroy(s *terraform.State) error {
    47  	if len(s.RootModule().Resources) > 0 {
    48  		return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func testAccAwsVpnConnection(
    55  	vpcResource string,
    56  	vpnGatewayResource string,
    57  	customerGatewayResource string,
    58  	vpnConnectionResource string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[vpnConnectionResource]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  		connection, ok := s.RootModule().Resources[vpnConnectionResource]
    69  		if !ok {
    70  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
    71  		}
    72  
    73  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
    74  
    75  		_, err := ec2conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
    76  			VpnConnectionIds: []*string{aws.String(connection.Primary.ID)},
    77  		})
    78  
    79  		if err != nil {
    80  			return err
    81  		}
    82  
    83  		return nil
    84  	}
    85  }
    86  
    87  const testAccAwsVpnConnectionConfig = `
    88  resource "aws_vpn_gateway" "vpn_gateway" {
    89  	tags {
    90  		Name = "vpn_gateway"
    91  	}
    92  }
    93  
    94  resource "aws_customer_gateway" "customer_gateway" {
    95  	bgp_asn = 60000
    96  	ip_address = "178.0.0.1"
    97  	type = "ipsec.1"
    98  }
    99  
   100  resource "aws_vpn_connection" "foo" {
   101  	vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   102  	customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   103  	type = "ipsec.1"
   104  	static_routes_only = true
   105  }
   106  `
   107  
   108  // Change static_routes_only to be false, forcing a refresh.
   109  const testAccAwsVpnConnectionConfigUpdate = `
   110  resource "aws_vpn_gateway" "vpn_gateway" {
   111  	tags {
   112  		Name = "vpn_gateway"
   113  	}
   114  }
   115  
   116  resource "aws_customer_gateway" "customer_gateway" {
   117  	bgp_asn = 60000
   118  	ip_address = "178.0.0.1"
   119  	type = "ipsec.1"
   120  }
   121  
   122  resource "aws_vpn_connection" "foo" {
   123  	vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   124  	customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   125  	type = "ipsec.1"
   126  	static_routes_only = false
   127  }
   128  `