github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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  func TestAWSVpnConnection_xmlconfig(t *testing.T) {
   121  	tunnelInfo := xmlConfigToTunnelInfo(testAccAwsVpnTunnelInfoXML)
   122  	if tunnelInfo.Tunnel1Address != "FIRST_ADDRESS" {
   123  		t.Fatalf("First address from tunnel XML was incorrect.")
   124  	}
   125  	if tunnelInfo.Tunnel1PreSharedKey != "FIRST_KEY" {
   126  		t.Fatalf("First key from tunnel XML was incorrect.")
   127  	}
   128  	if tunnelInfo.Tunnel2Address != "SECOND_ADDRESS" {
   129  		t.Fatalf("Second address from tunnel XML was incorrect.")
   130  	}
   131  	if tunnelInfo.Tunnel2PreSharedKey != "SECOND_KEY" {
   132  		t.Fatalf("Second key from tunnel XML was incorrect.")
   133  	}
   134  }
   135  
   136  const testAccAwsVpnConnectionConfig = `
   137  resource "aws_vpn_gateway" "vpn_gateway" {
   138    tags {
   139      Name = "vpn_gateway"
   140    }
   141  }
   142  
   143  resource "aws_customer_gateway" "customer_gateway" {
   144    bgp_asn = 60000
   145    ip_address = "178.0.0.1"
   146    type = "ipsec.1"
   147  }
   148  
   149  resource "aws_vpn_connection" "foo" {
   150    vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   151    customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   152    type = "ipsec.1"
   153    static_routes_only = true
   154  }
   155  `
   156  
   157  // Change static_routes_only to be false, forcing a refresh.
   158  const testAccAwsVpnConnectionConfigUpdate = `
   159  resource "aws_vpn_gateway" "vpn_gateway" {
   160    tags {
   161      Name = "vpn_gateway"
   162    }
   163  }
   164  
   165  resource "aws_customer_gateway" "customer_gateway" {
   166    bgp_asn = 60000
   167    ip_address = "178.0.0.1"
   168    type = "ipsec.1"
   169  }
   170  
   171  resource "aws_vpn_connection" "foo" {
   172    vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
   173    customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
   174    type = "ipsec.1"
   175    static_routes_only = false
   176  }
   177  `
   178  
   179  // Test our VPN tunnel config XML parsing
   180  const testAccAwsVpnTunnelInfoXML = `
   181  <vpn_connection id="vpn-abc123">
   182    <ipsec_tunnel>
   183      <vpn_gateway>
   184        <tunnel_outside_address>
   185          <ip_address>SECOND_ADDRESS</ip_address>
   186        </tunnel_outside_address>
   187      </vpn_gateway>
   188      <ike>
   189        <pre_shared_key>SECOND_KEY</pre_shared_key>
   190      </ike>
   191    </ipsec_tunnel>
   192    <ipsec_tunnel>
   193      <vpn_gateway>
   194        <tunnel_outside_address>
   195          <ip_address>FIRST_ADDRESS</ip_address>
   196        </tunnel_outside_address>
   197      </vpn_gateway>
   198      <ike>
   199        <pre_shared_key>FIRST_KEY</pre_shared_key>
   200      </ike>
   201    </ipsec_tunnel>
   202  </vpn_connection>
   203  `