2015-07-10 14:29:03 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-09-21 20:22:40 +02:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-07-10 14:29:03 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsVpcEndpoint() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsVPCEndpointCreate,
|
|
|
|
Read: resourceAwsVPCEndpointRead,
|
|
|
|
Update: resourceAwsVPCEndpointUpdate,
|
|
|
|
Delete: resourceAwsVPCEndpointDelete,
|
2016-06-23 03:48:59 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
2015-07-10 14:29:03 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"policy": &schema.Schema{
|
2016-09-15 00:29:29 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: validateJsonString,
|
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
json, _ := normalizeJsonString(v)
|
|
|
|
return json
|
|
|
|
},
|
2015-07-10 14:29:03 +02:00
|
|
|
},
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"service_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"route_table_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-07-10 14:29:03 +02:00
|
|
|
},
|
2016-06-06 12:02:07 +02:00
|
|
|
"prefix_list_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-07-10 14:29:03 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVPCEndpointCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.CreateVpcEndpointInput{
|
2016-10-13 19:41:38 +02:00
|
|
|
VpcId: aws.String(d.Get("vpc_id").(string)),
|
|
|
|
ServiceName: aws.String(d.Get("service_name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("route_table_ids"); ok {
|
|
|
|
list := v.(*schema.Set).List()
|
|
|
|
if len(list) > 0 {
|
|
|
|
input.RouteTableIds = expandStringList(list)
|
|
|
|
}
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("policy"); ok {
|
2016-09-21 20:22:40 +02:00
|
|
|
policy, err := normalizeJsonString(v)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
|
|
|
|
}
|
2015-07-10 14:29:03 +02:00
|
|
|
input.PolicyDocument = aws.String(policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating VPC Endpoint: %#v", input)
|
2015-08-17 20:27:16 +02:00
|
|
|
output, err := conn.CreateVpcEndpoint(input)
|
2015-07-10 14:29:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating VPC Endpoint: %s", err)
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
log.Printf("[DEBUG] VPC Endpoint %q created.", *output.VpcEndpoint.VpcEndpointId)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*output.VpcEndpoint.VpcEndpointId)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
|
|
|
return resourceAwsVPCEndpointRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVPCEndpointRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.DescribeVpcEndpointsInput{
|
|
|
|
VpcEndpointIds: []*string{aws.String(d.Id())},
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading VPC Endpoint: %q", d.Id())
|
2015-08-17 20:27:16 +02:00
|
|
|
output, err := conn.DescribeVpcEndpoints(input)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
|
2016-10-24 21:17:58 +02:00
|
|
|
log.Printf("[WARN] VPC Endpoint (%s) not found, removing from state", d.Id())
|
|
|
|
d.SetId("")
|
2015-07-10 14:29:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(output.VpcEndpoints) != 1 {
|
2015-07-10 14:29:03 +02:00
|
|
|
return fmt.Errorf("There's no unique VPC Endpoint, but %d endpoints: %#v",
|
2015-08-17 20:27:16 +02:00
|
|
|
len(output.VpcEndpoints), output.VpcEndpoints)
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
vpce := output.VpcEndpoints[0]
|
2015-07-10 14:29:03 +02:00
|
|
|
|
2016-06-06 12:02:07 +02:00
|
|
|
// A VPC Endpoint is associated with exactly one prefix list name (also called Service Name).
|
|
|
|
// The prefix list ID can be used in security groups, so retrieve it to support that capability.
|
|
|
|
prefixListServiceName := *vpce.ServiceName
|
|
|
|
prefixListInput := &ec2.DescribePrefixListsInput{
|
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
{Name: aws.String("prefix-list-name"), Values: []*string{aws.String(prefixListServiceName)}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading VPC Endpoint prefix list: %s", prefixListServiceName)
|
|
|
|
prefixListsOutput, err := conn.DescribePrefixLists(prefixListInput)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error reading VPC Endpoint prefix list: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(prefixListsOutput.PrefixLists) != 1 {
|
|
|
|
return fmt.Errorf("There are multiple prefix lists associated with the service name '%s'. Unexpected", prefixListServiceName)
|
|
|
|
}
|
|
|
|
|
2016-09-21 20:22:40 +02:00
|
|
|
policy, err := normalizeJsonString(*vpce.PolicyDocument)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
|
|
|
|
}
|
2016-09-15 00:29:29 +02:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("vpc_id", vpce.VpcId)
|
2016-09-15 00:29:29 +02:00
|
|
|
d.Set("policy", policy)
|
2015-07-10 14:29:03 +02:00
|
|
|
d.Set("service_name", vpce.ServiceName)
|
2015-12-18 21:18:30 +01:00
|
|
|
if err := d.Set("route_table_ids", aws.StringValueSlice(vpce.RouteTableIds)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-06 12:02:07 +02:00
|
|
|
d.Set("prefix_list_id", prefixListsOutput.PrefixLists[0].PrefixListId)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVPCEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.ModifyVpcEndpointInput{
|
|
|
|
VpcEndpointId: aws.String(d.Id()),
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("route_table_ids") {
|
|
|
|
o, n := d.GetChange("route_table_ids")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
2015-12-11 17:15:54 +01:00
|
|
|
add := expandStringList(ns.Difference(os).List())
|
2015-07-10 14:29:03 +02:00
|
|
|
if len(add) > 0 {
|
2015-08-17 20:27:16 +02:00
|
|
|
input.AddRouteTableIds = add
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
2015-12-11 17:15:54 +01:00
|
|
|
remove := expandStringList(os.Difference(ns).List())
|
2015-07-10 14:29:03 +02:00
|
|
|
if len(remove) > 0 {
|
2015-08-17 20:27:16 +02:00
|
|
|
input.RemoveRouteTableIds = remove
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("policy") {
|
2016-09-21 20:22:40 +02:00
|
|
|
policy, err := normalizeJsonString(d.Get("policy"))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
|
|
|
|
}
|
2015-07-10 14:29:03 +02:00
|
|
|
input.PolicyDocument = aws.String(policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating VPC Endpoint: %#v", input)
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.ModifyVpcEndpoint(input)
|
2015-07-10 14:29:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating VPC Endpoint: %s", err)
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
log.Printf("[DEBUG] VPC Endpoint %q updated", input.VpcEndpointId)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
2015-12-18 21:18:30 +01:00
|
|
|
return resourceAwsVPCEndpointRead(d, meta)
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsVPCEndpointDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-08-17 20:27:16 +02:00
|
|
|
input := &ec2.DeleteVpcEndpointsInput{
|
|
|
|
VpcEndpointIds: []*string{aws.String(d.Id())},
|
2015-07-10 14:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleting VPC Endpoint: %#v", input)
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.DeleteVpcEndpoints(input)
|
2015-07-10 14:29:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
|
|
|
|
log.Printf("[DEBUG] VPC Endpoint %q is already gone", d.Id())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] VPC Endpoint %q deleted", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|