github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_vpc_endpoint.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsVpcEndpoint() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsVpcEndpointRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"id": {
    19  				Type:     schema.TypeString,
    20  				Optional: true,
    21  				Computed: true,
    22  			},
    23  			"state": {
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				Computed: true,
    27  			},
    28  			"vpc_id": {
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  				Computed: true,
    32  			},
    33  			"service_name": {
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				Computed: true,
    37  			},
    38  			"policy": {
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  			"route_table_ids": &schema.Schema{
    43  				Type:     schema.TypeSet,
    44  				Computed: true,
    45  				Elem:     &schema.Schema{Type: schema.TypeString},
    46  				Set:      schema.HashString,
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func dataSourceAwsVpcEndpointRead(d *schema.ResourceData, meta interface{}) error {
    53  	conn := meta.(*AWSClient).ec2conn
    54  
    55  	log.Printf("[DEBUG] Reading VPC Endpoints.")
    56  
    57  	req := &ec2.DescribeVpcEndpointsInput{}
    58  
    59  	if id, ok := d.GetOk("id"); ok {
    60  		req.VpcEndpointIds = aws.StringSlice([]string{id.(string)})
    61  	}
    62  
    63  	req.Filters = buildEC2AttributeFilterList(
    64  		map[string]string{
    65  			"vpc-endpoint-state": d.Get("state").(string),
    66  			"vpc-id":             d.Get("vpc_id").(string),
    67  			"service-name":       d.Get("service_name").(string),
    68  		},
    69  	)
    70  	if len(req.Filters) == 0 {
    71  		// Don't send an empty filters list; the EC2 API won't accept it.
    72  		req.Filters = nil
    73  	}
    74  
    75  	resp, err := conn.DescribeVpcEndpoints(req)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	if resp == nil || len(resp.VpcEndpoints) == 0 {
    80  		return fmt.Errorf("no matching VPC endpoint found")
    81  	}
    82  	if len(resp.VpcEndpoints) > 1 {
    83  		return fmt.Errorf("multiple VPC endpoints matched; use additional constraints to reduce matches to a single VPC endpoint")
    84  	}
    85  
    86  	vpce := resp.VpcEndpoints[0]
    87  	policy, err := normalizeJsonString(*vpce.PolicyDocument)
    88  	if err != nil {
    89  		return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
    90  	}
    91  
    92  	d.SetId(aws.StringValue(vpce.VpcEndpointId))
    93  	d.Set("id", vpce.VpcEndpointId)
    94  	d.Set("state", vpce.State)
    95  	d.Set("vpc_id", vpce.VpcId)
    96  	d.Set("service_name", vpce.ServiceName)
    97  	d.Set("policy", policy)
    98  	if err := d.Set("route_table_ids", aws.StringValueSlice(vpce.RouteTableIds)); err != nil {
    99  		return err
   100  	}
   101  
   102  	return nil
   103  }