forked from hashicorp/terraform-provider-aws
/
data_source_aws_vpc_endpoint_service.go
56 lines (45 loc) · 1.25 KB
/
data_source_aws_vpc_endpoint_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
}