From dfda0cd61d65d45b9c71b59ba589a6c02d1c6c2a Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 16 Mar 2020 12:14:34 +0200 Subject: [PATCH 1/7] add filter support --- aws/data_source_aws_vpc_endpoint_service.go | 21 +++-- ...ta_source_aws_vpc_endpoint_service_test.go | 79 ++++++++++++++++++- .../docs/d/vpc_endpoint_service.html.markdown | 18 ++++- 3 files changed, 110 insertions(+), 8 deletions(-) diff --git a/aws/data_source_aws_vpc_endpoint_service.go b/aws/data_source_aws_vpc_endpoint_service.go index 53af9811123..3c749e07ea3 100644 --- a/aws/data_source_aws_vpc_endpoint_service.go +++ b/aws/data_source_aws_vpc_endpoint_service.go @@ -69,6 +69,7 @@ func dataSourceAwsVpcEndpointService() *schema.Resource { Type: schema.TypeBool, Computed: true, }, + "filter": dataSourceFiltersSchema(), }, } } @@ -76,18 +77,28 @@ func dataSourceAwsVpcEndpointService() *schema.Resource { func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn + filters, filtersOk := d.GetOk("filter") + tags, tagsOk := d.GetOk("tags") + var serviceName string + serviceNameOk := false if v, ok := d.GetOk("service_name"); ok { serviceName = v.(string) + serviceNameOk = true } else if v, ok := d.GetOk("service"); ok { serviceName = fmt.Sprintf("com.amazonaws.%s.%s", meta.(*AWSClient).region, v.(string)) - } else { - return fmt.Errorf( - "One of ['service', 'service_name'] must be set to query VPC Endpoint Services") + serviceNameOk = true } - req := &ec2.DescribeVpcEndpointServicesInput{ - ServiceNames: aws.StringSlice([]string{serviceName}), + req := &ec2.DescribeVpcEndpointServicesInput{} + if filtersOk { + req.Filters = buildAwsDataSourceFilters(filters.(*schema.Set)) + } + if serviceNameOk { + req.ServiceNames = aws.StringSlice([]string{serviceName}) + } + if tagsOk { + req.Filters = append(req.Filters, ec2TagFiltersFromMap(tags.(map[string]interface{}))...) } log.Printf("[DEBUG] Reading VPC Endpoint Service: %s", req) diff --git a/aws/data_source_aws_vpc_endpoint_service_test.go b/aws/data_source_aws_vpc_endpoint_service_test.go index 8c4552d626c..8267cd42526 100644 --- a/aws/data_source_aws_vpc_endpoint_service_test.go +++ b/aws/data_source_aws_vpc_endpoint_service_test.go @@ -86,6 +86,56 @@ func TestAccDataSourceAwsVpcEndpointService_custom(t *testing.T) { }) } +func TestAccDataSourceAwsVpcEndpointService_custom_filter(t *testing.T) { + datasourceName := "data.aws_vpc_endpoint_service.test" + rName := fmt.Sprintf("tf-testacc-vpcesvc-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsVpcEndpointServiceCustomConfigFilter(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(datasourceName, "acceptance_required", "true"), + resource.TestCheckResourceAttr(datasourceName, "availability_zones.#", "2"), + 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), + ), + }, + }, + }) +} + +func TestAccDataSourceAwsVpcEndpointService_custom_filter_tags(t *testing.T) { + datasourceName := "data.aws_vpc_endpoint_service.test" + rName := fmt.Sprintf("tf-testacc-vpcesvc-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsVpcEndpointServiceCustomConfigFilterTags(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(datasourceName, "acceptance_required", "true"), + resource.TestCheckResourceAttr(datasourceName, "availability_zones.#", "2"), + 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 = ` data "aws_availability_zones" "available" {} @@ -100,7 +150,7 @@ data "aws_vpc_endpoint_service" "test" { } ` -func testAccDataSourceAwsVpcEndpointServiceCustomConfig(rName string) string { +func testAccDataSourceAwsVpcEndpointServiceCustomConfigBase(rName string) string { return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" @@ -161,9 +211,34 @@ resource "aws_vpc_endpoint_service" "test" { Name = %[1]q } } +`, rName) +} +func testAccDataSourceAwsVpcEndpointServiceCustomConfig(rName string) string { + return testAccDataSourceAwsVpcEndpointServiceCustomConfigBase(rName) + fmt.Sprintf(` data "aws_vpc_endpoint_service" "test" { service_name = "${aws_vpc_endpoint_service.test.service_name}" } -`, rName) +`) +} + +func testAccDataSourceAwsVpcEndpointServiceCustomConfigFilter(rName string) string { + return testAccDataSourceAwsVpcEndpointServiceCustomConfigBase(rName) + fmt.Sprintf(` +data "aws_vpc_endpoint_service" "test" { + filter { + name = "service-name" + values = ["${aws_vpc_endpoint_service.test.service_name}"] + } +} +`) +} + +func testAccDataSourceAwsVpcEndpointServiceCustomConfigFilterTags(rName string) string { + return testAccDataSourceAwsVpcEndpointServiceCustomConfigBase(rName) + fmt.Sprintf(` +data "aws_vpc_endpoint_service" "test" { + tags = { + Name = "${aws_vpc_endpoint_service.test.tags["Name"]}" + } +} +`) } diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index 165a00a2389..7600e6ea915 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -41,6 +41,17 @@ data "aws_vpc_endpoint_service" "custome" { } ``` +### Filter: + +```hcl +data "aws_vpc_endpoint_service" "test" { + filter { + name = "service-name" + values = ["some-service"] + } +} +``` + ## Argument Reference The arguments of this data source act as filters for querying the available VPC endpoint services. @@ -48,8 +59,11 @@ The given filters must match exactly one VPC endpoint service whose data will be * `service` - (Optional) The common name of an AWS service (e.g. `s3`). * `service_name` - (Optional) The service name that is specified when creating a VPC endpoint. For AWS services the service name is usually in the form `com.amazonaws..` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker..notebook`). +* `filter` - (Optional) One or more name/value pairs to use as filters. There are +several valid keys, for a full reference, check out +[describe-vpc-endpoint-services in the AWS CLI reference][1]. -~> **NOTE:** One of `service` or `service_name` must be specified. Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`. +~> **NOTE:** Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`. ## Attributes Reference @@ -65,3 +79,5 @@ In addition to all arguments above, the following attributes are exported: * `service_type` - The service type, `Gateway` or `Interface`. * `tags` - A mapping of tags assigned to the resource. * `vpc_endpoint_policy_supported` - Whether or not the service supports endpoint policies - `true` or `false`. + +[1]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpc-endpoint-services.html From cafa8960ad221cf2674258fbf4d32d70723681c1 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Mon, 16 Mar 2020 13:39:13 +0200 Subject: [PATCH 2/7] add filter support - docs --- website/docs/d/vpc_endpoint_service.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index 7600e6ea915..bfc8a3dace3 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -13,7 +13,7 @@ can be specified when creating a VPC endpoint within the region configured in th ## Example Usage -### AWS Service +AWS service usage: ```hcl # Declare the data source @@ -33,7 +33,7 @@ resource "aws_vpc_endpoint" "ep" { } ``` -### Non-AWS Service +Non-AWS service usage: ```hcl data "aws_vpc_endpoint_service" "custome" { @@ -41,7 +41,7 @@ data "aws_vpc_endpoint_service" "custome" { } ``` -### Filter: +Filter usage: ```hcl data "aws_vpc_endpoint_service" "test" { @@ -58,12 +58,12 @@ The arguments of this data source act as filters for querying the available VPC The given filters must match exactly one VPC endpoint service whose data will be exported as attributes. * `service` - (Optional) The common name of an AWS service (e.g. `s3`). -* `service_name` - (Optional) The service name that is specified when creating a VPC endpoint. For AWS services the service name is usually in the form `com.amazonaws..` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker..notebook`). +* `service_name` - (Optional) The service name that can be specified when creating a VPC endpoint. * `filter` - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-vpc-endpoint-services in the AWS CLI reference][1]. -~> **NOTE:** Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`. +~> **NOTE:** One of `service` or `service_name` must be specified. ## Attributes Reference From 8d12a357209f59ad9e0853439cbc6fee2f1935e3 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 16 Mar 2020 23:33:48 +0200 Subject: [PATCH 3/7] Update vpc_endpoint_service.html.markdown --- website/docs/d/vpc_endpoint_service.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index bfc8a3dace3..54e90cc04eb 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -58,7 +58,7 @@ The arguments of this data source act as filters for querying the available VPC The given filters must match exactly one VPC endpoint service whose data will be exported as attributes. * `service` - (Optional) The common name of an AWS service (e.g. `s3`). -* `service_name` - (Optional) The service name that can be specified when creating a VPC endpoint. +* `service_name` - (Optional) The service name that is specified when creating a VPC endpoint. For AWS services the service name is usually in the form `com.amazonaws..` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker..notebook`). * `filter` - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-vpc-endpoint-services in the AWS CLI reference][1]. From debad6ea2cf9c5a16f30ab973099be8b0b689fac Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Mon, 16 Mar 2020 23:35:03 +0200 Subject: [PATCH 4/7] Update vpc_endpoint_service.html.markdown --- website/docs/d/vpc_endpoint_service.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index 54e90cc04eb..20f08c9ccd4 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -13,7 +13,7 @@ can be specified when creating a VPC endpoint within the region configured in th ## Example Usage -AWS service usage: +### AWS Service ```hcl # Declare the data source @@ -33,7 +33,7 @@ resource "aws_vpc_endpoint" "ep" { } ``` -Non-AWS service usage: +### Non-AWS Service ```hcl data "aws_vpc_endpoint_service" "custome" { @@ -41,7 +41,7 @@ data "aws_vpc_endpoint_service" "custome" { } ``` -Filter usage: +### Filter ```hcl data "aws_vpc_endpoint_service" "test" { @@ -63,7 +63,7 @@ The given filters must match exactly one VPC endpoint service whose data will be several valid keys, for a full reference, check out [describe-vpc-endpoint-services in the AWS CLI reference][1]. -~> **NOTE:** One of `service` or `service_name` must be specified. +~> **NOTE:** Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`. ## Attributes Reference From b0d7ffc2493e2ace9adaaf424b3d8d1e19a2d9a6 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Tue, 24 Mar 2020 22:09:23 +0200 Subject: [PATCH 5/7] Update vpc_endpoint_service.html.markdown --- website/docs/d/vpc_endpoint_service.html.markdown | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index 20f08c9ccd4..bddf204a7ae 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -59,12 +59,17 @@ The given filters must match exactly one VPC endpoint service whose data will be * `service` - (Optional) The common name of an AWS service (e.g. `s3`). * `service_name` - (Optional) The service name that is specified when creating a VPC endpoint. For AWS services the service name is usually in the form `com.amazonaws..` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker..notebook`). -* `filter` - (Optional) One or more name/value pairs to use as filters. There are -several valid keys, for a full reference, check out -[describe-vpc-endpoint-services in the AWS CLI reference][1]. +* `filter` - (Optional) Configuration block(s) for filtering. Detailed below. ~> **NOTE:** Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`. +### filter Configuration Block + +The following arguments are supported by the `filter` configuration block: + +* `name` - (Required) The name of the filter field. Valid values can be found in the [EC2 DescribeVpcEndpointServices API Reference](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcEndpointServices.html). +* `values` - (Required) Set of values that are accepted for the given filter field. Results will be selected if any given value matches. + ## Attributes Reference In addition to all arguments above, the following attributes are exported: From a664b8f3b9182b8e16ca2a130d4fd995fbe17cc1 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Tue, 24 Mar 2020 22:10:05 +0200 Subject: [PATCH 6/7] Update vpc_endpoint_service.html.markdown --- website/docs/d/vpc_endpoint_service.html.markdown | 2 -- 1 file changed, 2 deletions(-) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index bddf204a7ae..27d30937299 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -84,5 +84,3 @@ In addition to all arguments above, the following attributes are exported: * `service_type` - The service type, `Gateway` or `Interface`. * `tags` - A mapping of tags assigned to the resource. * `vpc_endpoint_policy_supported` - Whether or not the service supports endpoint policies - `true` or `false`. - -[1]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpc-endpoint-services.html From 151f8f5a934b239b715d1032ecff758e94bebfef Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 26 Mar 2020 16:24:37 +0200 Subject: [PATCH 7/7] Update vpc_endpoint_service.html.markdown --- website/docs/d/vpc_endpoint_service.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/d/vpc_endpoint_service.html.markdown b/website/docs/d/vpc_endpoint_service.html.markdown index 27d30937299..ad68e8ed6a9 100644 --- a/website/docs/d/vpc_endpoint_service.html.markdown +++ b/website/docs/d/vpc_endpoint_service.html.markdown @@ -60,6 +60,7 @@ The given filters must match exactly one VPC endpoint service whose data will be * `service` - (Optional) The common name of an AWS service (e.g. `s3`). * `service_name` - (Optional) The service name that is specified when creating a VPC endpoint. For AWS services the service name is usually in the form `com.amazonaws..` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker..notebook`). * `filter` - (Optional) Configuration block(s) for filtering. Detailed below. +* `tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on the desired VPC Endpoint Service. ~> **NOTE:** Specifying `service` will not work for non-AWS services or AWS services that don't follow the standard `service_name` pattern of `com.amazonaws..`.