2015-05-01 18:23:16 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2017-03-31 13:40:37 +02:00
|
|
|
"time"
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-22 19:53:42 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2017-03-29 23:10:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-05-01 18:23:16 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-12-09 00:48:57 +01:00
|
|
|
func TestAccAWSVpnConnection_basic(t *testing.T) {
|
2017-03-29 23:10:21 +02:00
|
|
|
rInt := acctest.RandInt()
|
2017-04-03 19:17:59 +02:00
|
|
|
rBgpAsn := acctest.RandIntRange(64512, 65534)
|
2017-03-31 13:40:37 +02:00
|
|
|
var vpn ec2.VpnConnection
|
|
|
|
|
2015-05-01 18:23:16 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-20 21:36:56 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_vpn_connection.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccAwsVpnConnectionDestroy,
|
2015-05-01 18:23:16 +02:00
|
|
|
Steps: []resource.TestStep{
|
2016-11-07 17:12:41 +01:00
|
|
|
{
|
2017-04-03 19:17:59 +02:00
|
|
|
Config: testAccAwsVpnConnectionConfig(rBgpAsn),
|
2015-05-01 18:23:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccAwsVpnConnection(
|
|
|
|
"aws_vpc.vpc",
|
|
|
|
"aws_vpn_gateway.vpn_gateway",
|
|
|
|
"aws_customer_gateway.customer_gateway",
|
|
|
|
"aws_vpn_connection.foo",
|
2017-03-31 13:40:37 +02:00
|
|
|
&vpn,
|
2015-05-01 18:23:16 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2016-11-07 17:12:41 +01:00
|
|
|
{
|
2017-04-03 19:17:59 +02:00
|
|
|
Config: testAccAwsVpnConnectionConfigUpdate(rInt, rBgpAsn),
|
2015-05-01 18:23:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccAwsVpnConnection(
|
|
|
|
"aws_vpc.vpc",
|
|
|
|
"aws_vpn_gateway.vpn_gateway",
|
|
|
|
"aws_customer_gateway.customer_gateway",
|
2015-05-04 00:40:10 +02:00
|
|
|
"aws_vpn_connection.foo",
|
2017-03-31 13:40:37 +02:00
|
|
|
&vpn,
|
2015-05-01 18:23:16 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-07 17:12:41 +01:00
|
|
|
func TestAccAWSVpnConnection_withoutStaticRoutes(t *testing.T) {
|
2017-03-29 23:10:21 +02:00
|
|
|
rInt := acctest.RandInt()
|
2017-04-03 19:17:59 +02:00
|
|
|
rBgpAsn := acctest.RandIntRange(64512, 65534)
|
2017-03-31 13:40:37 +02:00
|
|
|
var vpn ec2.VpnConnection
|
2016-11-07 17:12:41 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_vpn_connection.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccAwsVpnConnectionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-04-03 19:17:59 +02:00
|
|
|
Config: testAccAwsVpnConnectionConfigUpdate(rInt, rBgpAsn),
|
2016-11-07 17:12:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccAwsVpnConnection(
|
|
|
|
"aws_vpc.vpc",
|
|
|
|
"aws_vpn_gateway.vpn_gateway",
|
|
|
|
"aws_customer_gateway.customer_gateway",
|
|
|
|
"aws_vpn_connection.foo",
|
2017-03-31 13:40:37 +02:00
|
|
|
&vpn,
|
2016-11-07 17:12:41 +01:00
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr("aws_vpn_connection.foo", "static_routes_only", "false"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:40:37 +02:00
|
|
|
func TestAccAWSVpnConnection_disappears(t *testing.T) {
|
2017-04-03 21:27:43 +02:00
|
|
|
rBgpAsn := acctest.RandIntRange(64512, 65534)
|
2017-03-31 13:40:37 +02:00
|
|
|
var vpn ec2.VpnConnection
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccAwsVpnConnectionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-04-03 21:27:43 +02:00
|
|
|
Config: testAccAwsVpnConnectionConfig(rBgpAsn),
|
2017-03-31 13:40:37 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccAwsVpnConnection(
|
|
|
|
"aws_vpc.vpc",
|
|
|
|
"aws_vpn_gateway.vpn_gateway",
|
|
|
|
"aws_customer_gateway.customer_gateway",
|
|
|
|
"aws_vpn_connection.foo",
|
|
|
|
&vpn,
|
|
|
|
),
|
|
|
|
testAccAWSVpnConnectionDisappears(&vpn),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSVpnConnectionDisappears(connection *ec2.VpnConnection) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
_, err := conn.DeleteVpnConnection(&ec2.DeleteVpnConnectionInput{
|
|
|
|
VpnConnectionId: connection.VpnConnectionId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource.Retry(40*time.Minute, func() *resource.RetryError {
|
|
|
|
opts := &ec2.DescribeVpnConnectionsInput{
|
|
|
|
VpnConnectionIds: []*string{connection.VpnConnectionId},
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeVpnConnections(opts)
|
|
|
|
if err != nil {
|
|
|
|
cgw, ok := err.(awserr.Error)
|
|
|
|
if ok && cgw.Code() == "InvalidVpnConnectionID.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ok && cgw.Code() == "IncorrectState" {
|
|
|
|
return resource.RetryableError(fmt.Errorf(
|
|
|
|
"Waiting for VPN Connection to be in the correct state: %v", connection.VpnConnectionId))
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(
|
|
|
|
fmt.Errorf("Error retrieving VPN Connection: %s", err))
|
|
|
|
}
|
|
|
|
if *resp.VpnConnections[0].State == "deleted" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return resource.RetryableError(fmt.Errorf(
|
|
|
|
"Waiting for VPN Connection: %v", connection.VpnConnectionId))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 18:23:16 +02:00
|
|
|
func testAccAwsVpnConnectionDestroy(s *terraform.State) error {
|
2015-12-22 19:53:42 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_vpn_connection" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
|
|
|
|
VpnConnectionIds: []*string{aws.String(rs.Primary.ID)},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" {
|
|
|
|
// not found
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vpn *ec2.VpnConnection
|
|
|
|
for _, v := range resp.VpnConnections {
|
|
|
|
if v.VpnConnectionId != nil && *v.VpnConnectionId == rs.Primary.ID {
|
|
|
|
vpn = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vpn == nil {
|
|
|
|
// vpn connection not found
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if vpn.State != nil && *vpn.State == "deleted" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 18:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAwsVpnConnection(
|
|
|
|
vpcResource string,
|
|
|
|
vpnGatewayResource string,
|
|
|
|
customerGatewayResource string,
|
2017-03-31 13:40:37 +02:00
|
|
|
vpnConnectionResource string,
|
|
|
|
vpnConnection *ec2.VpnConnection) resource.TestCheckFunc {
|
2015-05-01 18:23:16 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[vpnConnectionResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", vpnConnectionResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
connection, ok := s.RootModule().Resources[vpnConnectionResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", vpnConnectionResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
|
2017-03-31 13:40:37 +02:00
|
|
|
resp, err := ec2conn.DescribeVpnConnections(&ec2.DescribeVpnConnectionsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
VpnConnectionIds: []*string{aws.String(connection.Primary.ID)},
|
2015-05-01 18:23:16 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:40:37 +02:00
|
|
|
*vpnConnection = *resp.VpnConnections[0]
|
|
|
|
|
2015-05-01 18:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:21:03 +01:00
|
|
|
func TestAWSVpnConnection_xmlconfig(t *testing.T) {
|
2016-09-02 16:24:17 +02:00
|
|
|
tunnelInfo, err := xmlConfigToTunnelInfo(testAccAwsVpnTunnelInfoXML)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error unmarshalling XML: %s", err)
|
|
|
|
}
|
2016-02-06 23:00:26 +01:00
|
|
|
if tunnelInfo.Tunnel1Address != "FIRST_ADDRESS" {
|
|
|
|
t.Fatalf("First address from tunnel XML was incorrect.")
|
|
|
|
}
|
2017-05-31 20:03:13 +02:00
|
|
|
if tunnelInfo.Tunnel1CgwInsideAddress != "FIRST_CGW_INSIDE_ADDRESS" {
|
|
|
|
t.Fatalf("First Customer Gateway inside address from tunnel" +
|
|
|
|
" XML was incorrect.")
|
|
|
|
}
|
|
|
|
if tunnelInfo.Tunnel1VgwInsideAddress != "FIRST_VGW_INSIDE_ADDRESS" {
|
|
|
|
t.Fatalf("First VPN Gateway inside address from tunnel " +
|
|
|
|
" XML was incorrect.")
|
|
|
|
}
|
2016-02-06 23:00:26 +01:00
|
|
|
if tunnelInfo.Tunnel1PreSharedKey != "FIRST_KEY" {
|
|
|
|
t.Fatalf("First key from tunnel XML was incorrect.")
|
|
|
|
}
|
|
|
|
if tunnelInfo.Tunnel2Address != "SECOND_ADDRESS" {
|
|
|
|
t.Fatalf("Second address from tunnel XML was incorrect.")
|
|
|
|
}
|
2017-05-31 20:03:13 +02:00
|
|
|
if tunnelInfo.Tunnel2CgwInsideAddress != "SECOND_CGW_INSIDE_ADDRESS" {
|
|
|
|
t.Fatalf("Second Customer Gateway inside address from tunnel" +
|
|
|
|
" XML was incorrect.")
|
|
|
|
}
|
|
|
|
if tunnelInfo.Tunnel2VgwInsideAddress != "SECOND_VGW_INSIDE_ADDRESS" {
|
|
|
|
t.Fatalf("Second VPN Gateway inside address from tunnel " +
|
|
|
|
" XML was incorrect.")
|
|
|
|
}
|
2016-02-06 23:00:26 +01:00
|
|
|
if tunnelInfo.Tunnel2PreSharedKey != "SECOND_KEY" {
|
|
|
|
t.Fatalf("Second key from tunnel XML was incorrect.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 19:17:59 +02:00
|
|
|
func testAccAwsVpnConnectionConfig(rBgpAsn int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpn_gateway" "vpn_gateway" {
|
|
|
|
tags {
|
|
|
|
Name = "vpn_gateway"
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2017-04-03 19:17:59 +02:00
|
|
|
resource "aws_customer_gateway" "customer_gateway" {
|
|
|
|
bgp_asn = %d
|
|
|
|
ip_address = "178.0.0.1"
|
|
|
|
type = "ipsec.1"
|
|
|
|
tags {
|
|
|
|
Name = "main-customer-gateway"
|
|
|
|
}
|
2017-03-29 23:10:21 +02:00
|
|
|
}
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2017-04-03 19:17:59 +02:00
|
|
|
resource "aws_vpn_connection" "foo" {
|
|
|
|
vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
|
|
|
|
customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
|
|
|
|
type = "ipsec.1"
|
|
|
|
static_routes_only = true
|
|
|
|
}
|
|
|
|
`, rBgpAsn)
|
|
|
|
}
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2015-05-04 00:40:10 +02:00
|
|
|
// Change static_routes_only to be false, forcing a refresh.
|
2017-04-03 19:17:59 +02:00
|
|
|
func testAccAwsVpnConnectionConfigUpdate(rInt, rBgpAsn int) string {
|
2017-03-29 23:10:21 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpn_gateway" "vpn_gateway" {
|
|
|
|
tags {
|
|
|
|
Name = "vpn_gateway"
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2017-03-29 23:10:21 +02:00
|
|
|
resource "aws_customer_gateway" "customer_gateway" {
|
2017-04-03 19:17:59 +02:00
|
|
|
bgp_asn = %d
|
2017-03-29 23:10:21 +02:00
|
|
|
ip_address = "178.0.0.1"
|
|
|
|
type = "ipsec.1"
|
|
|
|
tags {
|
|
|
|
Name = "main-customer-gateway-%d"
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 18:23:16 +02:00
|
|
|
|
2017-03-29 23:10:21 +02:00
|
|
|
resource "aws_vpn_connection" "foo" {
|
|
|
|
vpn_gateway_id = "${aws_vpn_gateway.vpn_gateway.id}"
|
|
|
|
customer_gateway_id = "${aws_customer_gateway.customer_gateway.id}"
|
|
|
|
type = "ipsec.1"
|
|
|
|
static_routes_only = false
|
|
|
|
}
|
2017-04-03 19:17:59 +02:00
|
|
|
`, rBgpAsn, rInt)
|
2015-05-01 18:23:16 +02:00
|
|
|
}
|
2016-02-06 23:00:26 +01:00
|
|
|
|
|
|
|
// Test our VPN tunnel config XML parsing
|
|
|
|
const testAccAwsVpnTunnelInfoXML = `
|
|
|
|
<vpn_connection id="vpn-abc123">
|
|
|
|
<ipsec_tunnel>
|
2017-05-31 20:03:13 +02:00
|
|
|
<customer_gateway>
|
|
|
|
<tunnel_outside_address>
|
|
|
|
<ip_address>123.123.123.123</ip_address>
|
|
|
|
</tunnel_outside_address>
|
|
|
|
<tunnel_inside_address>
|
|
|
|
<ip_address>SECOND_CGW_INSIDE_ADDRESS</ip_address>
|
|
|
|
<network_mask>255.255.255.252</network_mask>
|
|
|
|
<network_cidr>30</network_cidr>
|
|
|
|
</tunnel_inside_address>
|
|
|
|
</customer_gateway>
|
2016-02-06 23:00:26 +01:00
|
|
|
<vpn_gateway>
|
|
|
|
<tunnel_outside_address>
|
|
|
|
<ip_address>SECOND_ADDRESS</ip_address>
|
|
|
|
</tunnel_outside_address>
|
2017-05-31 20:03:13 +02:00
|
|
|
<tunnel_inside_address>
|
|
|
|
<ip_address>SECOND_VGW_INSIDE_ADDRESS</ip_address>
|
|
|
|
<network_mask>255.255.255.252</network_mask>
|
|
|
|
<network_cidr>30</network_cidr>
|
|
|
|
</tunnel_inside_address>
|
2016-02-06 23:00:26 +01:00
|
|
|
</vpn_gateway>
|
|
|
|
<ike>
|
|
|
|
<pre_shared_key>SECOND_KEY</pre_shared_key>
|
|
|
|
</ike>
|
|
|
|
</ipsec_tunnel>
|
|
|
|
<ipsec_tunnel>
|
2017-05-31 20:03:13 +02:00
|
|
|
<customer_gateway>
|
|
|
|
<tunnel_outside_address>
|
|
|
|
<ip_address>123.123.123.123</ip_address>
|
|
|
|
</tunnel_outside_address>
|
|
|
|
<tunnel_inside_address>
|
|
|
|
<ip_address>FIRST_CGW_INSIDE_ADDRESS</ip_address>
|
|
|
|
<network_mask>255.255.255.252</network_mask>
|
|
|
|
<network_cidr>30</network_cidr>
|
|
|
|
</tunnel_inside_address>
|
|
|
|
</customer_gateway>
|
2016-02-06 23:00:26 +01:00
|
|
|
<vpn_gateway>
|
|
|
|
<tunnel_outside_address>
|
|
|
|
<ip_address>FIRST_ADDRESS</ip_address>
|
|
|
|
</tunnel_outside_address>
|
2017-05-31 20:03:13 +02:00
|
|
|
<tunnel_inside_address>
|
|
|
|
<ip_address>FIRST_VGW_INSIDE_ADDRESS</ip_address>
|
|
|
|
<network_mask>255.255.255.252</network_mask>
|
|
|
|
<network_cidr>30</network_cidr>
|
|
|
|
</tunnel_inside_address>
|
2016-02-06 23:00:26 +01:00
|
|
|
</vpn_gateway>
|
|
|
|
<ike>
|
|
|
|
<pre_shared_key>FIRST_KEY</pre_shared_key>
|
|
|
|
</ike>
|
|
|
|
</ipsec_tunnel>
|
|
|
|
</vpn_connection>
|
|
|
|
`
|