2015-03-05 07:24:14 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2015-03-05 07:24:14 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-04-15 17:26:39 +02:00
|
|
|
func TestAccAWSVpnGateway_basic(t *testing.T) {
|
2015-03-05 07:24:14 +01:00
|
|
|
var v, v2 ec2.VPNGateway
|
|
|
|
|
|
|
|
testNotEqual := func(*terraform.State) error {
|
|
|
|
if len(v.VPCAttachments) == 0 {
|
|
|
|
return fmt.Errorf("VPN gateway A is not attached")
|
|
|
|
}
|
|
|
|
if len(v2.VPCAttachments) == 0 {
|
|
|
|
return fmt.Errorf("VPN gateway B is not attached")
|
|
|
|
}
|
|
|
|
|
|
|
|
id1 := v.VPCAttachments[0].VPCID
|
|
|
|
id2 := v2.VPCAttachments[0].VPCID
|
|
|
|
if id1 == id2 {
|
|
|
|
return fmt.Errorf("Both attachment IDs are the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpnGatewayDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpnGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayExists(
|
|
|
|
"aws_vpn_gateway.foo", &v),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpnGatewayConfigChangeVPC,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayExists(
|
|
|
|
"aws_vpn_gateway.foo", &v2),
|
|
|
|
testNotEqual,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSVpnGateway_delete(t *testing.T) {
|
|
|
|
var vpnGateway ec2.VPNGateway
|
|
|
|
|
|
|
|
testDeleted := func(r string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, ok := s.RootModule().Resources[r]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("VPN Gateway %q should have been deleted", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpnGatewayDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpnGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &vpnGateway)),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccNoVpnGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(testDeleted("aws_vpn_gateway.foo")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccVpnGateway_tags(t *testing.T) {
|
|
|
|
var v ec2.VPNGateway
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpnGatewayDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckVpnGatewayConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
2015-03-05 07:24:14 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckVpnGatewayConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpnGatewayExists("aws_vpn_gateway.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
2015-03-05 07:24:14 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckVpnGatewayDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-03-05 07:24:14 +01:00
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_vpn_gateway" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-04-14 22:45:48 +02:00
|
|
|
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
|
|
|
VPNGatewayIDs: []*string{aws.String(rs.Primary.ID)},
|
2015-03-10 01:30:29 +01:00
|
|
|
})
|
2015-03-05 07:24:14 +01:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.VPNGateways) > 0 {
|
|
|
|
return fmt.Errorf("still exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-03-05 23:47:29 +01:00
|
|
|
ec2err, ok := err.(aws.APIError)
|
2015-03-05 07:24:14 +01:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ec2err.Code != "InvalidVpnGatewayID.NotFound" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckVpnGatewayExists(n string, ig *ec2.VPNGateway) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-14 22:45:48 +02:00
|
|
|
resp, err := ec2conn.DescribeVPNGateways(&ec2.DescribeVPNGatewaysInput{
|
|
|
|
VPNGatewayIDs: []*string{aws.String(rs.Primary.ID)},
|
2015-03-10 01:30:29 +01:00
|
|
|
})
|
2015-03-05 07:24:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.VPNGateways) == 0 {
|
|
|
|
return fmt.Errorf("VPNGateway not found")
|
|
|
|
}
|
|
|
|
|
2015-04-14 22:45:48 +02:00
|
|
|
*ig = *resp.VPNGateways[0]
|
2015-03-05 07:24:14 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccNoVpnGatewayConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccVpnGatewayConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpn_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccVpnGatewayConfigChangeVPC = `
|
|
|
|
resource "aws_vpc" "bar" {
|
|
|
|
cidr_block = "10.2.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpn_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.bar.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckVpnGatewayConfigTags = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpn_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckVpnGatewayConfigTagsUpdate = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpn_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|