2015-03-05 07:24:14 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/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-08-17 20:27:16 +02:00
|
|
|
var v, v2 ec2.VpnGateway
|
2015-03-05 07:24:14 +01:00
|
|
|
|
|
|
|
testNotEqual := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(v.VpcAttachments) == 0 {
|
2015-03-05 07:24:14 +01:00
|
|
|
return fmt.Errorf("VPN gateway A is not attached")
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(v2.VpcAttachments) == 0 {
|
2015-03-05 07:24:14 +01:00
|
|
|
return fmt.Errorf("VPN gateway B is not attached")
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
id1 := v.VpcAttachments[0].VpcId
|
|
|
|
id2 := v2.VpcAttachments[0].VpcId
|
2015-03-05 07:24:14 +01:00
|
|
|
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) {
|
2015-08-17 20:27:16 +02:00
|
|
|
var vpnGateway ec2.VpnGateway
|
2015-03-05 07:24:14 +01:00
|
|
|
|
|
|
|
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")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSVpnGateway_tags(t *testing.T) {
|
2015-08-17 20:27:16 +02:00
|
|
|
var v ec2.VpnGateway
|
2015-03-05 07:24:14 +01:00
|
|
|
|
|
|
|
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-08-17 20:27:16 +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 {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.VpnGateways) > 0 {
|
2015-03-05 07:24:14 +01:00
|
|
|
return fmt.Errorf("still exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2015-03-05 07:24:14 +01:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidVpnGatewayID.NotFound" {
|
2015-03-05 07:24:14 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
func testAccCheckVpnGatewayExists(n string, ig *ec2.VpnGateway) resource.TestCheckFunc {
|
2015-03-05 07:24:14 +01:00
|
|
|
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-08-17 20:27:16 +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
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.VpnGateways) == 0 {
|
2015-03-05 07:24:14 +01:00
|
|
|
return fmt.Errorf("VPNGateway not found")
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|