provider/aws Add aws_vpc_endpoint_service data source (#10261)

* provider/aws Add aws_vpc_endpoint_services data source.

* Rename 'aws_vpc_endpoint_services' to 'aws_vpc_endpoint_service'.
This commit is contained in:
Kit Ewbank 2016-12-13 05:40:35 -05:00 committed by Paul Stack
parent 3ceee01e9f
commit d3633398a7
5 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package aws
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsVpcEndpointService() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsVpcEndpointServiceRead,
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Required: true,
},
"service_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
service := d.Get("service").(string)
log.Printf("[DEBUG] Reading VPC Endpoint Services.")
request := &ec2.DescribeVpcEndpointServicesInput{}
resp, err := conn.DescribeVpcEndpointServices(request)
if err != nil {
return fmt.Errorf("Error fetching VPC Endpoint Services: %s", err)
}
names := aws.StringValueSlice(resp.ServiceNames)
for _, name := range names {
if strings.HasSuffix(name, "."+service) {
d.SetId(strconv.Itoa(hashcode.String(name)))
d.Set("service_name", name)
return nil
}
}
return fmt.Errorf("VPC Endpoint Service (%s) not found", service)
}

View File

@ -0,0 +1,52 @@
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDataSourceAwsVpcEndpointService(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsVpcEndpointServiceConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsVpcEndpointServiceCheck("data.aws_vpc_endpoint_service.s3"),
),
},
},
})
}
func testAccDataSourceAwsVpcEndpointServiceCheck(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("root module has no resource called %s", name)
}
attr := rs.Primary.Attributes
name := attr["service_name"]
if name != "com.amazonaws.us-west-2.s3" {
return fmt.Errorf("bad service name %s", name)
}
return nil
}
}
const testAccDataSourceAwsVpcEndpointServiceConfig = `
provider "aws" {
region = "us-west-2"
}
data "aws_vpc_endpoint_service" "s3" {
service = "s3"
}
`

View File

@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider {
"aws_subnet": dataSourceAwsSubnet(),
"aws_security_group": dataSourceAwsSecurityGroup(),
"aws_vpc": dataSourceAwsVpc(),
"aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(),
},
ResourcesMap: map[string]*schema.Resource{

View File

@ -0,0 +1,46 @@
---
layout: "aws"
page_title: "AWS: aws_vpc_endpoint_service"
sidebar_current: "docs-aws-datasource-vpc-endpoint-service"
description: |-
Provides details about a specific AWS service that can be specified when creating a VPC endpoint.
---
# aws\_vpc\_endpoint\_service
The VPC Endpoint Service data source allows access to a specific AWS
service that can be specified when creating a VPC endpoint within the region
configured in the provider.
## Example Usage
```
# Declare the data source
data "aws_vpc_endpoint_service" "s3" {
service = "s3"
}
# Create a VPC
resource "aws_vpc" "foo" {
cidr_block = "10.0.0.0/16"
}
# Create a VPC endpoint
resource "aws_vpc_endpoint" "ep" {
vpc_id = "${aws_vpc.foo.id}"
service_name = "${data.aws_vpc_endpoint_service.s3.service_name}"
}
```
## 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` - (Required) The common name of the AWS service (e.g. `s3`).
## Attributes Reference
The following attributes are exported:
* `service_name` - The service name of the AWS service that can be specified when creating a VPC endpoint.

View File

@ -84,6 +84,9 @@
<li<%= sidebar_current("docs-aws-datasource-vpc") %>>
<a href="/docs/providers/aws/d/vpc.html">aws_vpc</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-vpc-endpoint-service") %>>
<a href="/docs/providers/aws/d/vpc.html">aws_vpc_endpoint_service</a>
</li>
</ul>
</li>