github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_vpc_endpoint_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSVpcEndpoint_basic(t *testing.T) {
    15  	var endpoint ec2.VpcEndpoint
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckVpcEndpointDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccVpcEndpointConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckVpcEndpointExists("aws_vpc_endpoint.private-s3", &endpoint),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSVpcEndpoint_withRouteTableAndPolicy(t *testing.T) {
    33  	var endpoint ec2.VpcEndpoint
    34  	var routeTable ec2.RouteTable
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckVpcEndpointDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccVpcEndpointWithRouteTableAndPolicyConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckVpcEndpointExists("aws_vpc_endpoint.second-private-s3", &endpoint),
    45  					testAccCheckRouteTableExists("aws_route_table.default", &routeTable),
    46  				),
    47  			},
    48  			resource.TestStep{
    49  				Config: testAccVpcEndpointWithRouteTableAndPolicyConfigModified,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckVpcEndpointExists("aws_vpc_endpoint.second-private-s3", &endpoint),
    52  					testAccCheckRouteTableExists("aws_route_table.default", &routeTable),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func testAccCheckVpcEndpointDestroy(s *terraform.State) error {
    60  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    61  
    62  	for _, rs := range s.RootModule().Resources {
    63  		if rs.Type != "aws_vpc_endpoint" {
    64  			continue
    65  		}
    66  
    67  		// Try to find the VPC
    68  		input := &ec2.DescribeVpcEndpointsInput{
    69  			VpcEndpointIds: []*string{aws.String(rs.Primary.ID)},
    70  		}
    71  		resp, err := conn.DescribeVpcEndpoints(input)
    72  
    73  		if len(resp.VpcEndpoints) > 0 {
    74  			return fmt.Errorf("VPC Endpoints still exist.")
    75  		}
    76  
    77  		return err
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func testAccCheckVpcEndpointExists(n string, endpoint *ec2.VpcEndpoint) resource.TestCheckFunc {
    84  	return func(s *terraform.State) error {
    85  		rs, ok := s.RootModule().Resources[n]
    86  		if !ok {
    87  			return fmt.Errorf("Not found: %s", n)
    88  		}
    89  
    90  		if rs.Primary.ID == "" {
    91  			return fmt.Errorf("No VPC Endpoint ID is set")
    92  		}
    93  
    94  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    95  		input := &ec2.DescribeVpcEndpointsInput{
    96  			VpcEndpointIds: []*string{aws.String(rs.Primary.ID)},
    97  		}
    98  		resp, err := conn.DescribeVpcEndpoints(input)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		if len(resp.VpcEndpoints) == 0 {
   103  			return fmt.Errorf("VPC Endpoint not found")
   104  		}
   105  
   106  		*endpoint = *resp.VpcEndpoints[0]
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  const testAccVpcEndpointConfig = `
   113  resource "aws_vpc" "foo" {
   114      cidr_block = "10.1.0.0/16"
   115  }
   116  
   117  resource "aws_vpc_endpoint" "private-s3" {
   118      vpc_id = "${aws_vpc.foo.id}"
   119      service_name = "com.amazonaws.us-west-2.s3"
   120  }
   121  `
   122  
   123  const testAccVpcEndpointWithRouteTableAndPolicyConfig = `
   124  resource "aws_vpc" "foo" {
   125      cidr_block = "10.0.0.0/16"
   126  }
   127  
   128  resource "aws_subnet" "foo" {
   129  	vpc_id = "${aws_vpc.foo.id}"
   130      cidr_block = "10.0.1.0/24"
   131  }
   132  
   133  resource "aws_vpc_endpoint" "second-private-s3" {
   134      vpc_id = "${aws_vpc.foo.id}"
   135      service_name = "com.amazonaws.us-west-2.s3"
   136      route_table_ids = ["${aws_route_table.default.id}"]
   137      policy = <<POLICY
   138  {
   139  	"Version": "2012-10-17",
   140  	"Statement": [
   141  		{
   142  			"Sid":"AllowAll",
   143  			"Effect":"Allow",
   144  			"Principal":"*",
   145  			"Action":"*",
   146  			"Resource":"*"
   147  		}
   148  	]
   149  }
   150  POLICY
   151  }
   152  
   153  resource "aws_route_table" "default" {
   154      vpc_id = "${aws_vpc.foo.id}"
   155  }
   156  
   157  resource "aws_route_table_association" "main" {
   158      subnet_id = "${aws_subnet.foo.id}"
   159      route_table_id = "${aws_route_table.default.id}"
   160  }
   161  `
   162  
   163  const testAccVpcEndpointWithRouteTableAndPolicyConfigModified = `
   164  resource "aws_vpc" "foo" {
   165      cidr_block = "10.0.0.0/16"
   166  }
   167  
   168  resource "aws_subnet" "foo" {
   169  	vpc_id = "${aws_vpc.foo.id}"
   170      cidr_block = "10.0.1.0/24"
   171  }
   172  
   173  resource "aws_vpc_endpoint" "second-private-s3" {
   174      vpc_id = "${aws_vpc.foo.id}"
   175      service_name = "com.amazonaws.us-west-2.s3"
   176      route_table_ids = ["${aws_route_table.default.id}"]
   177      policy = <<POLICY
   178  {
   179  	"Version": "2012-10-17",
   180  	"Statement": [
   181  		{
   182  			"Sid":"AllowAll",
   183  			"Effect":"Allow",
   184  			"Principal":"*",
   185  			"Action":"*",
   186  			"Resource":"*"
   187  		}
   188  	]
   189  }
   190  POLICY
   191  }
   192  
   193  resource "aws_internet_gateway" "gw" {
   194      vpc_id = "${aws_vpc.foo.id}"
   195  }
   196  
   197  resource "aws_route_table" "default" {
   198      vpc_id = "${aws_vpc.foo.id}"
   199  
   200      route {
   201          cidr_block = "0.0.0.0/0"
   202          gateway_id = "${aws_internet_gateway.gw.id}"
   203      }
   204  }
   205  
   206  resource "aws_route_table_association" "main" {
   207      subnet_id = "${aws_subnet.foo.id}"
   208      route_table_id = "${aws_route_table.default.id}"
   209  }
   210  `