Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_vpc_endpoint_service - filter support #12404

Merged
merged 7 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions aws/data_source_aws_vpc_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,36 @@ func dataSourceAwsVpcEndpointService() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"filter": dataSourceFiltersSchema(),
},
}
}

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)
Expand Down
79 changes: 77 additions & 2 deletions aws/data_source_aws_vpc_endpoint_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {}

Expand All @@ -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"
Expand Down Expand Up @@ -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"]}"
}
}
`)
}
22 changes: 21 additions & 1 deletion website/docs/d/vpc_endpoint_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,35 @@ 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.
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.<region>.<service>` (the SageMaker Notebook service is an exception to this rule, the service name is in the form `aws.sagemaker.<region>.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.<region>.<service>`.

### filter Configuration Block

The following arguments are supported by the `filter` configuration block:

~> **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.<region>.<service>`.
* `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

Expand Down