github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_vpc_endpoint_service.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/hashcode"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func dataSourceAwsVpcEndpointService() *schema.Resource {
    16  	return &schema.Resource{
    17  		Read: dataSourceAwsVpcEndpointServiceRead,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"service": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"service_name": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func dataSourceAwsVpcEndpointServiceRead(d *schema.ResourceData, meta interface{}) error {
    33  	conn := meta.(*AWSClient).ec2conn
    34  
    35  	service := d.Get("service").(string)
    36  
    37  	log.Printf("[DEBUG] Reading VPC Endpoint Services.")
    38  
    39  	request := &ec2.DescribeVpcEndpointServicesInput{}
    40  
    41  	resp, err := conn.DescribeVpcEndpointServices(request)
    42  	if err != nil {
    43  		return fmt.Errorf("Error fetching VPC Endpoint Services: %s", err)
    44  	}
    45  
    46  	names := aws.StringValueSlice(resp.ServiceNames)
    47  	for _, name := range names {
    48  		if strings.HasSuffix(name, "."+service) {
    49  			d.SetId(strconv.Itoa(hashcode.String(name)))
    50  			d.Set("service_name", name)
    51  			return nil
    52  		}
    53  	}
    54  
    55  	return fmt.Errorf("VPC Endpoint Service (%s) not found", service)
    56  }