github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_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/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/hashcode"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceAwsVpcEndpoint() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsVPCEndpointCreate,
    17  		Read:   resourceAwsVPCEndpointRead,
    18  		Update: resourceAwsVPCEndpointUpdate,
    19  		Delete: resourceAwsVPCEndpointDelete,
    20  		Schema: map[string]*schema.Schema{
    21  			"policy": &schema.Schema{
    22  				Type:      schema.TypeString,
    23  				Optional:  true,
    24  				Computed:  true,
    25  				StateFunc: normalizeJson,
    26  			},
    27  			"vpc_id": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"service_name": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  			"route_table_ids": &schema.Schema{
    38  				Type:     schema.TypeSet,
    39  				Optional: true,
    40  				Elem:     &schema.Schema{Type: schema.TypeString},
    41  				Set: func(v interface{}) int {
    42  					return hashcode.String(v.(string))
    43  				},
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceAwsVPCEndpointCreate(d *schema.ResourceData, meta interface{}) error {
    50  	conn := meta.(*AWSClient).ec2conn
    51  	input := &ec2.CreateVpcEndpointInput{
    52  		VpcId:         aws.String(d.Get("vpc_id").(string)),
    53  		RouteTableIds: expandStringList(d.Get("route_table_ids").(*schema.Set).List()),
    54  		ServiceName:   aws.String(d.Get("service_name").(string)),
    55  	}
    56  
    57  	if v, ok := d.GetOk("policy"); ok {
    58  		policy := normalizeJson(v)
    59  		input.PolicyDocument = aws.String(policy)
    60  	}
    61  
    62  	log.Printf("[DEBUG] Creating VPC Endpoint: %#v", input)
    63  	output, err := conn.CreateVpcEndpoint(input)
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating VPC Endpoint: %s", err)
    66  	}
    67  	log.Printf("[DEBUG] VPC Endpoint %q created.", *output.VpcEndpoint.VpcEndpointId)
    68  
    69  	d.SetId(*output.VpcEndpoint.VpcEndpointId)
    70  
    71  	return resourceAwsVPCEndpointRead(d, meta)
    72  }
    73  
    74  func resourceAwsVPCEndpointRead(d *schema.ResourceData, meta interface{}) error {
    75  	conn := meta.(*AWSClient).ec2conn
    76  	input := &ec2.DescribeVpcEndpointsInput{
    77  		VpcEndpointIds: []*string{aws.String(d.Id())},
    78  	}
    79  
    80  	log.Printf("[DEBUG] Reading VPC Endpoint: %q", d.Id())
    81  	output, err := conn.DescribeVpcEndpoints(input)
    82  
    83  	if err != nil {
    84  		ec2err, ok := err.(awserr.Error)
    85  		if !ok {
    86  			return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
    87  		}
    88  
    89  		if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
    90  			return nil
    91  		}
    92  
    93  		return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
    94  	}
    95  
    96  	if len(output.VpcEndpoints) != 1 {
    97  		return fmt.Errorf("There's no unique VPC Endpoint, but %d endpoints: %#v",
    98  			len(output.VpcEndpoints), output.VpcEndpoints)
    99  	}
   100  
   101  	vpce := output.VpcEndpoints[0]
   102  
   103  	d.Set("vpc_id", vpce.VpcId)
   104  	d.Set("policy", normalizeJson(*vpce.PolicyDocument))
   105  	d.Set("service_name", vpce.ServiceName)
   106  	d.Set("route_table_ids", vpce.RouteTableIds)
   107  
   108  	return nil
   109  }
   110  
   111  func resourceAwsVPCEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
   112  	conn := meta.(*AWSClient).ec2conn
   113  	input := &ec2.ModifyVpcEndpointInput{
   114  		VpcEndpointId: aws.String(d.Id()),
   115  	}
   116  
   117  	if d.HasChange("route_table_ids") {
   118  		o, n := d.GetChange("route_table_ids")
   119  		os := o.(*schema.Set)
   120  		ns := n.(*schema.Set)
   121  
   122  		add := expandStringList(os.Difference(ns).List())
   123  		if len(add) > 0 {
   124  			input.AddRouteTableIds = add
   125  		}
   126  
   127  		remove := expandStringList(ns.Difference(os).List())
   128  		if len(remove) > 0 {
   129  			input.RemoveRouteTableIds = remove
   130  		}
   131  	}
   132  
   133  	if d.HasChange("policy") {
   134  		policy := normalizeJson(d.Get("policy"))
   135  		input.PolicyDocument = aws.String(policy)
   136  	}
   137  
   138  	log.Printf("[DEBUG] Updating VPC Endpoint: %#v", input)
   139  	_, err := conn.ModifyVpcEndpoint(input)
   140  	if err != nil {
   141  		return fmt.Errorf("Error updating VPC Endpoint: %s", err)
   142  	}
   143  	log.Printf("[DEBUG] VPC Endpoint %q updated", input.VpcEndpointId)
   144  
   145  	return nil
   146  }
   147  
   148  func resourceAwsVPCEndpointDelete(d *schema.ResourceData, meta interface{}) error {
   149  	conn := meta.(*AWSClient).ec2conn
   150  	input := &ec2.DeleteVpcEndpointsInput{
   151  		VpcEndpointIds: []*string{aws.String(d.Id())},
   152  	}
   153  
   154  	log.Printf("[DEBUG] Deleting VPC Endpoint: %#v", input)
   155  	_, err := conn.DeleteVpcEndpoints(input)
   156  
   157  	if err != nil {
   158  		ec2err, ok := err.(awserr.Error)
   159  		if !ok {
   160  			return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
   161  		}
   162  
   163  		if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
   164  			log.Printf("[DEBUG] VPC Endpoint %q is already gone", d.Id())
   165  		} else {
   166  			return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
   167  		}
   168  	}
   169  
   170  	log.Printf("[DEBUG] VPC Endpoint %q deleted", d.Id())
   171  	d.SetId("")
   172  
   173  	return nil
   174  }