2015-04-30 01:05:16 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-22 04:43: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-04 00:35:01 +02:00
|
|
|
|
2015-04-30 01:05:16 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSCustomerGateway_basic(t *testing.T) {
|
2015-04-30 01:05:16 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:59:19 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_customer_gateway.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCustomerGatewayDestroy,
|
2015-04-30 01:05:16 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCustomerGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCustomerGateway(
|
|
|
|
"aws_customer_gateway.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2015-05-04 00:35:01 +02:00
|
|
|
Config: testAccCustomerGatewayConfigUpdateTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCustomerGateway(
|
|
|
|
"aws_customer_gateway.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCustomerGatewayConfigForceReplace,
|
2015-04-30 01:05:16 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCustomerGateway(
|
2015-05-04 00:35:01 +02:00
|
|
|
"aws_customer_gateway.foo",
|
2015-04-30 01:05:16 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCustomerGatewayDestroy(s *terraform.State) error {
|
2015-12-22 04:43:42 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_customer_gatewah" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gatewayFilter := &ec2.Filter{
|
|
|
|
Name: aws.String("customer-gateway-id"),
|
|
|
|
Values: []*string{aws.String(rs.Primary.ID)},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
|
|
|
|
Filters: []*ec2.Filter{gatewayFilter},
|
|
|
|
})
|
|
|
|
|
|
|
|
if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidCustomerGatewayID.NotFound" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(resp.CustomerGateways) > 0 {
|
|
|
|
return fmt.Errorf("Customer gateway still exists: %v", resp.CustomerGateways)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-04-30 01:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCustomerGateway(gatewayResource string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[gatewayResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", gatewayResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
gateway, ok := s.RootModule().Resources[gatewayResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", gatewayResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
gatewayFilter := &ec2.Filter{
|
|
|
|
Name: aws.String("customer-gateway-id"),
|
|
|
|
Values: []*string{aws.String(gateway.Primary.ID)},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ec2conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
|
|
|
|
Filters: []*ec2.Filter{gatewayFilter},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCustomerGatewayConfig = `
|
|
|
|
resource "aws_customer_gateway" "foo" {
|
2016-06-08 15:46:56 +02:00
|
|
|
bgp_asn = 65000
|
2015-04-30 01:05:16 +02:00
|
|
|
ip_address = "172.0.0.1"
|
2015-05-04 00:35:01 +02:00
|
|
|
type = "ipsec.1"
|
2015-04-30 01:05:16 +02:00
|
|
|
tags {
|
|
|
|
Name = "foo-gateway"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-05-04 00:35:01 +02:00
|
|
|
// Add the Another: "tag" tag.
|
|
|
|
const testAccCustomerGatewayConfigUpdateTags = `
|
2015-04-30 01:05:16 +02:00
|
|
|
resource "aws_customer_gateway" "foo" {
|
2016-06-08 15:46:56 +02:00
|
|
|
bgp_asn = 65000
|
2015-04-30 01:05:16 +02:00
|
|
|
ip_address = "172.0.0.1"
|
2015-05-04 00:35:01 +02:00
|
|
|
type = "ipsec.1"
|
2015-04-30 01:05:16 +02:00
|
|
|
tags {
|
|
|
|
Name = "foo-gateway"
|
2015-05-04 00:35:01 +02:00
|
|
|
Another = "tag"
|
2015-04-30 01:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 00:35:01 +02:00
|
|
|
`
|
2015-04-30 01:05:16 +02:00
|
|
|
|
2015-05-04 00:35:01 +02:00
|
|
|
// Change the ip_address.
|
|
|
|
const testAccCustomerGatewayConfigForceReplace = `
|
|
|
|
resource "aws_customer_gateway" "foo" {
|
2016-06-08 15:46:56 +02:00
|
|
|
bgp_asn = 65000
|
2015-05-04 00:35:01 +02:00
|
|
|
ip_address = "172.10.10.1"
|
|
|
|
type = "ipsec.1"
|
2015-04-30 01:05:16 +02:00
|
|
|
tags {
|
|
|
|
Name = "foo-gateway"
|
2015-05-04 00:35:01 +02:00
|
|
|
Another = "tag"
|
2015-04-30 01:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|