2015-07-10 21:15:29 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-22 19:05:01 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-07-10 21:15:29 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSVpcEndpoint_basic(t *testing.T) {
|
2015-08-17 20:27:16 +02:00
|
|
|
var endpoint ec2.VpcEndpoint
|
2015-07-10 21:15:29 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcEndpointDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-12-22 19:05:01 +01:00
|
|
|
Config: testAccVpcEndpointWithRouteTableAndPolicyConfig,
|
2015-07-10 21:15:29 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-12-22 19:05:01 +01:00
|
|
|
testAccCheckVpcEndpointExists("aws_vpc_endpoint.second-private-s3", &endpoint),
|
2015-07-10 21:15:29 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSVpcEndpoint_withRouteTableAndPolicy(t *testing.T) {
|
2015-08-17 20:27:16 +02:00
|
|
|
var endpoint ec2.VpcEndpoint
|
2015-07-10 21:15:29 +02:00
|
|
|
var routeTable ec2.RouteTable
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVpcEndpointDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcEndpointWithRouteTableAndPolicyConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcEndpointExists("aws_vpc_endpoint.second-private-s3", &endpoint),
|
|
|
|
testAccCheckRouteTableExists("aws_route_table.default", &routeTable),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccVpcEndpointWithRouteTableAndPolicyConfigModified,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVpcEndpointExists("aws_vpc_endpoint.second-private-s3", &endpoint),
|
|
|
|
testAccCheckRouteTableExists("aws_route_table.default", &routeTable),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckVpcEndpointDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_vpc_endpoint" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the VPC
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.DescribeVpcEndpointsInput{
|
|
|
|
VpcEndpointIds: []*string{aws.String(rs.Primary.ID)},
|
2015-07-10 21:15:29 +02:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.DescribeVpcEndpoints(input)
|
2015-12-22 19:05:01 +01:00
|
|
|
if err != nil {
|
|
|
|
// Verify the error is what we want
|
|
|
|
if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidVpcEndpointId.NotFound" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.VpcEndpoints) > 0 {
|
2015-07-10 21:15:29 +02:00
|
|
|
return fmt.Errorf("VPC Endpoints still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
func testAccCheckVpcEndpointExists(n string, endpoint *ec2.VpcEndpoint) resource.TestCheckFunc {
|
2015-07-10 21:15:29 +02: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 VPC Endpoint ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.DescribeVpcEndpointsInput{
|
|
|
|
VpcEndpointIds: []*string{aws.String(rs.Primary.ID)},
|
2015-07-10 21:15:29 +02:00
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.DescribeVpcEndpoints(input)
|
2015-07-10 21:15:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.VpcEndpoints) == 0 {
|
2015-07-10 21:15:29 +02:00
|
|
|
return fmt.Errorf("VPC Endpoint not found")
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
*endpoint = *resp.VpcEndpoints[0]
|
2015-07-10 21:15:29 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccVpcEndpointWithRouteTableAndPolicyConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc_endpoint" "second-private-s3" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
service_name = "com.amazonaws.us-west-2.s3"
|
|
|
|
route_table_ids = ["${aws_route_table.default.id}"]
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
2015-11-12 16:32:45 +01:00
|
|
|
"Version": "2012-10-17",
|
2015-07-10 21:15:29 +02:00
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid":"AllowAll",
|
|
|
|
"Effect":"Allow",
|
|
|
|
"Principal":"*",
|
|
|
|
"Action":"*",
|
|
|
|
"Resource":"*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "default" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "main" {
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
route_table_id = "${aws_route_table.default.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccVpcEndpointWithRouteTableAndPolicyConfigModified = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
cidr_block = "10.0.1.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc_endpoint" "second-private-s3" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
service_name = "com.amazonaws.us-west-2.s3"
|
|
|
|
route_table_ids = ["${aws_route_table.default.id}"]
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
2015-11-12 16:32:45 +01:00
|
|
|
"Version": "2012-10-17",
|
2015-07-10 21:15:29 +02:00
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid":"AllowAll",
|
|
|
|
"Effect":"Allow",
|
|
|
|
"Principal":"*",
|
|
|
|
"Action":"*",
|
|
|
|
"Resource":"*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "default" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
|
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "main" {
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
route_table_id = "${aws_route_table.default.id}"
|
|
|
|
}
|
|
|
|
`
|