Skip to content

Commit

Permalink
data-source/aws_vpc_endpoint_service: Switch service_type filtering f…
Browse files Browse the repository at this point in the history
…rom client-side to API (#17641)

* data-soruce/aws_vpc_endpoint_service: Switch service_type filtering from client-side to API

Reference: #17417

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccDataSourceAwsVpcEndpointService_interface (12.25s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Interface (12.65s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Gateway (12.65s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_gateway (12.65s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom (217.41s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter (219.87s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter_tags (228.91s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccDataSourceAwsVpcEndpointService_gateway (16.05s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_interface (16.25s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Interface (16.26s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Gateway (16.26s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter_tags (229.07s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom (229.30s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter (229.52s)
```

* Update CHANGELOG for #17641

* data-source/aws_vpc_endpoint_service: Validate service_type argument, update CHANGELOG for #17641

Reference: #17419

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccDataSourceAwsVpcEndpointService_interface (21.03s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Interface (22.39s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Gateway (22.48s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_gateway (23.76s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter (230.43s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter_tags (235.33s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom (238.79s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccDataSourceAwsVpcEndpointService_interface (30.12s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Interface (30.15s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_gateway (30.16s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_ServiceType_Gateway (30.16s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter_tags (249.96s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom (259.01s)
--- PASS: TestAccDataSourceAwsVpcEndpointService_custom_filter (261.24s)
```
  • Loading branch information
bflad committed Mar 18, 2021
1 parent d0f1b05 commit e1df570
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changelog/17641.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:note
data-source/aws_vpc_endpoint_service: The `service_type` argument filtering has been switched from client-side to new EC2 API functionality
```

```release-note:bug
data-source/aws_vpc_endpoint_service: Prevent panic with incorrect `service_type` argument values
```
38 changes: 15 additions & 23 deletions aws/data_source_aws_vpc_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)
Expand Down Expand Up @@ -66,9 +67,10 @@ func dataSourceAwsVpcEndpointService() *schema.Resource {
ConflictsWith: []string{"service"},
},
"service_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice(ec2.ServiceType_Values(), false),
},
"tags": tagsSchemaComputed(),
"vpc_endpoint_policy_supported": {
Expand Down Expand Up @@ -104,6 +106,14 @@ func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{
if serviceNameOk {
req.ServiceNames = aws.StringSlice([]string{serviceName})
}

if v, ok := d.GetOk("service_type"); ok {
req.Filters = append(req.Filters, &ec2.Filter{
Name: aws.String("service-type"),
Values: aws.StringSlice([]string{v.(string)}),
})
}

if tagsOk {
req.Filters = append(req.Filters, ec2TagFiltersFromMap(tags.(map[string]interface{}))...)
}
Expand Down Expand Up @@ -134,29 +144,11 @@ func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("no matching VPC Endpoint Service found")
}

var serviceDetails []*ec2.ServiceDetail

// Client-side filtering. When the EC2 API supports this functionality
// server-side it should be moved.
for _, serviceDetail := range resp.ServiceDetails {
if serviceDetail == nil {
continue
}

if v, ok := d.GetOk("service_type"); ok {
if len(serviceDetail.ServiceType) > 0 && serviceDetail.ServiceType[0] != nil && v.(string) != aws.StringValue(serviceDetail.ServiceType[0].ServiceType) {
continue
}
}

serviceDetails = append(serviceDetails, serviceDetail)
}

if len(serviceDetails) > 1 {
if len(resp.ServiceDetails) > 1 {
return fmt.Errorf("multiple VPC Endpoint Services matched; use additional constraints to reduce matches to a single VPC Endpoint Service")
}

sd := serviceDetails[0]
sd := resp.ServiceDetails[0]
serviceId := aws.StringValue(sd.ServiceId)
serviceName = aws.StringValue(sd.ServiceName)

Expand Down

0 comments on commit e1df570

Please sign in to comment.