github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSVpnConnection_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccAwsVpnConnectionDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAwsVpnConnectionConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccAwsVpnConnection(
    25  						"aws_vpc.vpc",
    26  						"aws_vpn_gateway.vpn_gateway",
    27  						"aws_customer_gateway.customer_gateway",
    28  						"aws_vpn_connection.foo",
    29  					),
    30  				),
    31  			},
    32  			resource.TestStep{
    33  				Config: testAccAwsVpnConnectionConfigUpdate,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccAwsVpnConnection(
    36  						"aws_vpc.vpc",
    37  						"aws_vpn_gateway.vpn_gateway",
    38  						"aws_customer_gateway.customer_gateway",
    39  						"aws_vpn_connection.foo",
    40  					),
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func testAccAwsVpnConnectionDestroy(s *terraform.State) error {
    48  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    49  	for _, rs := range s.RootModule().Resources {
    50  		if rs.Type != "aws_vpn_connection" {
    51  			continue
    52  		}
    53  
    54  		resp, err := conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
    55  			VpnConnectionIds: []*string{aws.String(rs.Primary.ID)},
    56  		})
    57  
    58  		if err != nil {
    59  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
    60  				// not found
    61  				return nil
    62  			}
    63  			return err
    64  		}
    65  
    66  		var vpn *ec2.VpnConnection
    67  		for _, v := range resp.VpnConnections {
    68  			if v.VpnConnectionId != nil && *v.VpnConnectionId == rs.Primary.ID {
    69  				vpn = v
    70  			}
    71  		}
    72  
    73  		if vpn == nil {
    74  			// vpn connection not found
    75  			return nil
    76  		}
    77  
    78  		if vpn.State != nil && *vpn.State == "deleted" {
    79  			return nil
    80  		}
    81  
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func testAccAwsVpnConnection(
    88  	vpcResource string,
    89  	vpnGatewayResource string,
    90  	customerGatewayResource string,
    91  	vpnConnectionResource string) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources[vpnConnectionResource]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("No ID is set")
   100  		}
   101  		connection, ok := s.RootModule().Resources[vpnConnectionResource]
   102  		if !ok {
   103  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
   104  		}
   105  
   106  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   107  
   108  		_, err := ec2conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
   109  			VpnConnectionIds: []*string{aws.String(connection.Primary.ID)},
   110  		})
   111  
   112  		if err != nil {
   113  			return err
   114  		}
   115  
   116  		return nil
   117  	}
   118  }
   119  
   120  const testAccAwsVpnConnectionConfig = `
   121  resource "aws_vpn_gateway" "vpn_gateway" {
   122  	tags {
   123  		Name = "vpn_gateway"
   124  	}
   125  }
   126  
   127  resource "aws_customer_gateway" "customer_gateway" {
   128  	bgp_asn = 60000
   129  	ip_address = "178.0.0.1"
   130  	type = "ipsec.1"
   131  }
   132  
   133  resource "aws_vpn_connection" "foo" {
   134  	vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   135  	customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   136  	type = "ipsec.1"
   137  	static_routes_only = true
   138  }
   139  `
   140  
   141  // Change static_routes_only to be false, forcing a refresh.
   142  const testAccAwsVpnConnectionConfigUpdate = `
   143  resource "aws_vpn_gateway" "vpn_gateway" {
   144  	tags {
   145  		Name = "vpn_gateway"
   146  	}
   147  }
   148  
   149  resource "aws_customer_gateway" "customer_gateway" {
   150  	bgp_asn = 60000
   151  	ip_address = "178.0.0.1"
   152  	type = "ipsec.1"
   153  }
   154  
   155  resource "aws_vpn_connection" "foo" {
   156  	vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   157  	customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   158  	type = "ipsec.1"
   159  	static_routes_only = false
   160  }
   161  `