github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  		IDRefreshName: "aws_vpn_connection.foo",
    19  		Providers:     testAccProviders,
    20  		CheckDestroy:  testAccAwsVpnConnectionDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAwsVpnConnectionConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccAwsVpnConnection(
    26  						"aws_vpc.vpc",
    27  						"aws_vpn_gateway.vpn_gateway",
    28  						"aws_customer_gateway.customer_gateway",
    29  						"aws_vpn_connection.foo",
    30  					),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccAwsVpnConnectionConfigUpdate,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccAwsVpnConnection(
    37  						"aws_vpc.vpc",
    38  						"aws_vpn_gateway.vpn_gateway",
    39  						"aws_customer_gateway.customer_gateway",
    40  						"aws_vpn_connection.foo",
    41  					),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccAwsVpnConnectionDestroy(s *terraform.State) error {
    49  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    50  	for _, rs := range s.RootModule().Resources {
    51  		if rs.Type != "aws_vpn_connection" {
    52  			continue
    53  		}
    54  
    55  		resp, err := conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
    56  			VpnConnectionIds: []*string{aws.String(rs.Primary.ID)},
    57  		})
    58  
    59  		if err != nil {
    60  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
    61  				// not found
    62  				return nil
    63  			}
    64  			return err
    65  		}
    66  
    67  		var vpn *ec2.VpnConnection
    68  		for _, v := range resp.VpnConnections {
    69  			if v.VpnConnectionId != nil && *v.VpnConnectionId == rs.Primary.ID {
    70  				vpn = v
    71  			}
    72  		}
    73  
    74  		if vpn == nil {
    75  			// vpn connection not found
    76  			return nil
    77  		}
    78  
    79  		if vpn.State != nil && *vpn.State == "deleted" {
    80  			return nil
    81  		}
    82  
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  func testAccAwsVpnConnection(
    89  	vpcResource string,
    90  	vpnGatewayResource string,
    91  	customerGatewayResource string,
    92  	vpnConnectionResource string) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[vpnConnectionResource]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
    97  		}
    98  
    99  		if rs.Primary.ID == "" {
   100  			return fmt.Errorf("No ID is set")
   101  		}
   102  		connection, ok := s.RootModule().Resources[vpnConnectionResource]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", vpnConnectionResource)
   105  		}
   106  
   107  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   108  
   109  		_, err := ec2conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
   110  			VpnConnectionIds: []*string{aws.String(connection.Primary.ID)},
   111  		})
   112  
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func TestAWSVpnConnection_xmlconfig(t *testing.T) {
   122  	tunnelInfo, err := xmlConfigToTunnelInfo(testAccAwsVpnTunnelInfoXML)
   123  	if err != nil {
   124  		t.Fatalf("Error unmarshalling XML: %s", err)
   125  	}
   126  	if tunnelInfo.Tunnel1Address != "FIRST_ADDRESS" {
   127  		t.Fatalf("First address from tunnel XML was incorrect.")
   128  	}
   129  	if tunnelInfo.Tunnel1PreSharedKey != "FIRST_KEY" {
   130  		t.Fatalf("First key from tunnel XML was incorrect.")
   131  	}
   132  	if tunnelInfo.Tunnel2Address != "SECOND_ADDRESS" {
   133  		t.Fatalf("Second address from tunnel XML was incorrect.")
   134  	}
   135  	if tunnelInfo.Tunnel2PreSharedKey != "SECOND_KEY" {
   136  		t.Fatalf("Second key from tunnel XML was incorrect.")
   137  	}
   138  }
   139  
   140  const testAccAwsVpnConnectionConfig = `
   141  resource "aws_vpn_gateway" "vpn_gateway" {
   142    tags {
   143      Name = "vpn_gateway"
   144    }
   145  }
   146  
   147  resource "aws_customer_gateway" "customer_gateway" {
   148    bgp_asn = 65000
   149    ip_address = "178.0.0.1"
   150    type = "ipsec.1"
   151  }
   152  
   153  resource "aws_vpn_connection" "foo" {
   154    vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   155    customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   156    type = "ipsec.1"
   157    static_routes_only = true
   158  }
   159  `
   160  
   161  // Change static_routes_only to be false, forcing a refresh.
   162  const testAccAwsVpnConnectionConfigUpdate = `
   163  resource "aws_vpn_gateway" "vpn_gateway" {
   164    tags {
   165      Name = "vpn_gateway"
   166    }
   167  }
   168  
   169  resource "aws_customer_gateway" "customer_gateway" {
   170    bgp_asn = 65000
   171    ip_address = "178.0.0.1"
   172    type = "ipsec.1"
   173  }
   174  
   175  resource "aws_vpn_connection" "foo" {
   176    vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   177    customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   178    type = "ipsec.1"
   179    static_routes_only = false
   180  }
   181  `
   182  
   183  // Test our VPN tunnel config XML parsing
   184  const testAccAwsVpnTunnelInfoXML = `
   185  <vpn_connection id="vpn-abc123">
   186    <ipsec_tunnel>
   187      <vpn_gateway>
   188        <tunnel_outside_address>
   189          <ip_address>SECOND_ADDRESS</ip_address>
   190        </tunnel_outside_address>
   191      </vpn_gateway>
   192      <ike>
   193        <pre_shared_key>SECOND_KEY</pre_shared_key>
   194      </ike>
   195    </ipsec_tunnel>
   196    <ipsec_tunnel>
   197      <vpn_gateway>
   198        <tunnel_outside_address>
   199          <ip_address>FIRST_ADDRESS</ip_address>
   200        </tunnel_outside_address>
   201      </vpn_gateway>
   202      <ike>
   203        <pre_shared_key>FIRST_KEY</pre_shared_key>
   204      </ike>
   205    </ipsec_tunnel>
   206  </vpn_connection>
   207  `