Skip to content

Commit

Permalink
Tagging for VPC Endpoints and VPC Endpoint Services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kit Ewbank authored and Kit Ewbank committed Jun 10, 2019
1 parent ad7341e commit 864285b
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 375 deletions.
45 changes: 25 additions & 20 deletions aws/data_source_aws_vpc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,6 @@ func dataSourceAwsVpcEndpoint() *schema.Resource {
Read: dataSourceAwsVpcEndpointRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cidr_blocks": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -56,6 +36,11 @@ func dataSourceAwsVpcEndpoint() *schema.Resource {
},
},
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"network_interface_ids": {
Type: schema.TypeSet,
Computed: true,
Expand Down Expand Up @@ -90,16 +75,32 @@ func dataSourceAwsVpcEndpoint() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"tags": tagsSchemaComputed(),
"vpc_endpoint_type": {
Type: schema.TypeString,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -194,6 +195,10 @@ func dataSourceAwsVpcEndpointRead(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return fmt.Errorf("error setting subnet_ids: %s", err)
}
err = d.Set("tags", tagsToMap(vpce.Tags))
if err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
// VPC endpoints don't have types in GovCloud, so set type to default if empty
if vpceType := aws.StringValue(vpce.VpcEndpointType); vpceType == "" {
d.Set("vpc_endpoint_type", ec2.VpcEndpointTypeGateway)
Expand Down
32 changes: 21 additions & 11 deletions aws/data_source_aws_vpc_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ func dataSourceAwsVpcEndpointService() *schema.Resource {
Read: dataSourceAwsVpcEndpointServiceRead,

Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"service_name"},
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"service"},
},
"acceptance_required": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -55,10 +44,26 @@ func dataSourceAwsVpcEndpointService() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"service": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"service_name"},
},
"service_id": {
Type: schema.TypeString,
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"service"},
},
"service_type": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"vpc_endpoint_policy_supported": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -130,7 +135,12 @@ func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{
d.Set("manages_vpc_endpoints", sd.ManagesVpcEndpoints)
d.Set("owner", sd.Owner)
d.Set("private_dns_name", sd.PrivateDnsName)
d.Set("service_id", sd.ServiceId)
d.Set("service_type", sd.ServiceType[0].ServiceType)
err = d.Set("tags", tagsToMap(sd.Tags))
if err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("vpc_endpoint_policy_supported", sd.VpcEndpointPolicySupported)

return nil
Expand Down
81 changes: 37 additions & 44 deletions aws/data_source_aws_vpc_endpoint_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

func TestAccDataSourceAwsVpcEndpointService_gateway(t *testing.T) {
datasourceName := "data.aws_vpc_endpoint_service.s3"
datasourceName := "data.aws_vpc_endpoint_service.test"
region := testAccGetRegion()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -18,28 +19,25 @@ func TestAccDataSourceAwsVpcEndpointService_gateway(t *testing.T) {
{
Config: testAccDataSourceAwsVpcEndpointServiceGatewayConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "service_name", "com.amazonaws.us-west-2.s3"),
resource.TestCheckResourceAttr(datasourceName, "service_name", fmt.Sprintf("com.amazonaws.%s.s3", region)),
resource.TestCheckResourceAttr(datasourceName, "acceptance_required", "false"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.#", "4"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.2487133097", "us-west-2a"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.221770259", "us-west-2b"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.2050015877", "us-west-2c"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.3830732582", "us-west-2d"),
resource.TestCheckResourceAttrPair(datasourceName, "availability_zones.#", "data.aws_availability_zones.available", "names.#"),
resource.TestCheckResourceAttr(datasourceName, "base_endpoint_dns_names.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "base_endpoint_dns_names.3003388505", "s3.us-west-2.amazonaws.com"),
resource.TestCheckResourceAttr(datasourceName, "manages_vpc_endpoints", "false"),
resource.TestCheckResourceAttr(datasourceName, "owner", "amazon"),
resource.TestCheckResourceAttr(datasourceName, "private_dns_name", ""),
resource.TestCheckResourceAttr(datasourceName, "service_type", "Gateway"),
resource.TestCheckResourceAttr(datasourceName, "vpc_endpoint_policy_supported", "true"),
resource.TestCheckResourceAttr(datasourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAwsVpcEndpointService_interface(t *testing.T) {
datasourceName := "data.aws_vpc_endpoint_service.ec2"
datasourceName := "data.aws_vpc_endpoint_service.test"
region := testAccGetRegion()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -48,27 +46,23 @@ func TestAccDataSourceAwsVpcEndpointService_interface(t *testing.T) {
{
Config: testAccDataSourceAwsVpcEndpointServiceInterfaceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "service_name", "com.amazonaws.us-west-2.ec2"),
resource.TestCheckResourceAttr(datasourceName, "service_name", fmt.Sprintf("com.amazonaws.%s.ec2", region)),
resource.TestCheckResourceAttr(datasourceName, "acceptance_required", "false"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.#", "3"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.2487133097", "us-west-2a"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.221770259", "us-west-2b"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.2050015877", "us-west-2c"),
resource.TestCheckResourceAttr(datasourceName, "base_endpoint_dns_names.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "base_endpoint_dns_names.1880016359", "ec2.us-west-2.vpce.amazonaws.com"),
resource.TestCheckResourceAttr(datasourceName, "manages_vpc_endpoints", "false"),
resource.TestCheckResourceAttr(datasourceName, "owner", "amazon"),
resource.TestCheckResourceAttr(datasourceName, "private_dns_name", "ec2.us-west-2.amazonaws.com"),
resource.TestCheckResourceAttr(datasourceName, "private_dns_name", fmt.Sprintf("ec2.%s.amazonaws.com", region)),
resource.TestCheckResourceAttr(datasourceName, "service_type", "Interface"),
resource.TestCheckResourceAttr(datasourceName, "vpc_endpoint_policy_supported", "false"),
resource.TestCheckResourceAttr(datasourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAwsVpcEndpointService_custom(t *testing.T) {
datasourceName := "data.aws_vpc_endpoint_service.foo"
datasourceName := "data.aws_vpc_endpoint_service.test"
rName := fmt.Sprintf("tf-testacc-vpcesvc-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum))

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -80,55 +74,48 @@ func TestAccDataSourceAwsVpcEndpointService_custom(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "acceptance_required", "true"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.#", "2"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.2487133097", "us-west-2a"),
resource.TestCheckResourceAttr(datasourceName, "availability_zones.221770259", "us-west-2b"),
resource.TestCheckResourceAttr(datasourceName, "manages_vpc_endpoints", "false"),
testAccCheckResourceAttrAccountID(datasourceName, "owner"),
resource.TestCheckResourceAttr(datasourceName, "service_type", "Interface"),
resource.TestCheckResourceAttr(datasourceName, "vpc_endpoint_policy_supported", "false"),
resource.TestCheckResourceAttr(datasourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(datasourceName, "tags.Name", rName),
),
},
},
})
}

const testAccDataSourceAwsVpcEndpointServiceGatewayConfig = `
provider "aws" {
region = "us-west-2"
}
data "aws_availability_zones" "available" {}
data "aws_vpc_endpoint_service" "s3" {
data "aws_vpc_endpoint_service" "test" {
service = "s3"
}
`

const testAccDataSourceAwsVpcEndpointServiceInterfaceConfig = `
provider "aws" {
region = "us-west-2"
}
data "aws_vpc_endpoint_service" "ec2" {
data "aws_vpc_endpoint_service" "test" {
service = "ec2"
}
`

func testAccDataSourceAwsVpcEndpointServiceCustomConfig(rName string) string {
return fmt.Sprintf(
`
resource "aws_vpc" "nlb_test" {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_lb" "nlb_test_1" {
resource "aws_lb" "test" {
name = %[1]q
subnets = [
"${aws_subnet.nlb_test_1.id}",
"${aws_subnet.nlb_test_2.id}",
"${aws_subnet.test1.id}",
"${aws_subnet.test2.id}",
]
load_balancer_type = "network"
Expand All @@ -141,36 +128,42 @@ resource "aws_lb" "nlb_test_1" {
}
}
resource "aws_subnet" "nlb_test_1" {
vpc_id = "${aws_vpc.nlb_test.id}"
data "aws_availability_zones" "available" {}
resource "aws_subnet" "test1" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
tags = {
Name = %[1]q
}
}
resource "aws_subnet" "nlb_test_2" {
vpc_id = "${aws_vpc.nlb_test.id}"
resource "aws_subnet" "test2" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2b"
availability_zone = "${data.aws_availability_zones.available.names[1]}"
tags = {
Name = %[1]q
}
}
resource "aws_vpc_endpoint_service" "foo" {
resource "aws_vpc_endpoint_service" "test" {
acceptance_required = true
network_load_balancer_arns = [
"${aws_lb.nlb_test_1.id}",
"${aws_lb.test.id}",
]
tags = {
Name = %[1]q
}
}
data "aws_vpc_endpoint_service" "foo" {
service_name = "${aws_vpc_endpoint_service.foo.service_name}"
data "aws_vpc_endpoint_service" "test" {
service_name = "${aws_vpc_endpoint_service.test.service_name}"
}
`, rName)
}
Loading

0 comments on commit 864285b

Please sign in to comment.