github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_vpc_endpoint_route_table_association.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 resourceAwsVpcEndpointRouteTableAssociation() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsVPCEndpointRouteTableAssociationCreate, 17 Read: resourceAwsVPCEndpointRouteTableAssociationRead, 18 Delete: resourceAwsVPCEndpointRouteTableAssociationDelete, 19 Importer: &schema.ResourceImporter{ 20 State: schema.ImportStatePassthrough, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "vpc_endpoint_id": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "route_table_id": { 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 }, 35 } 36 } 37 38 func resourceAwsVPCEndpointRouteTableAssociationCreate(d *schema.ResourceData, meta interface{}) error { 39 conn := meta.(*AWSClient).ec2conn 40 endpointId := d.Get("vpc_endpoint_id").(string) 41 rtId := d.Get("route_table_id").(string) 42 43 _, err := findResourceVPCEndpoint(conn, endpointId) 44 if err != nil { 45 return err 46 } 47 48 log.Printf( 49 "[INFO] Creating VPC Endpoint/Route Table association: %s => %s", 50 endpointId, rtId) 51 52 input := &ec2.ModifyVpcEndpointInput{ 53 VpcEndpointId: aws.String(endpointId), 54 AddRouteTableIds: aws.StringSlice([]string{rtId}), 55 } 56 57 _, err = conn.ModifyVpcEndpoint(input) 58 if err != nil { 59 return fmt.Errorf("Error creating VPC Endpoint/Route Table association: %s", err.Error()) 60 } 61 id := vpcEndpointIdRouteTableIdHash(endpointId, rtId) 62 log.Printf("[DEBUG] VPC Endpoint/Route Table association %q created.", id) 63 64 d.SetId(id) 65 66 return resourceAwsVPCEndpointRouteTableAssociationRead(d, meta) 67 } 68 69 func resourceAwsVPCEndpointRouteTableAssociationRead(d *schema.ResourceData, meta interface{}) error { 70 conn := meta.(*AWSClient).ec2conn 71 endpointId := d.Get("vpc_endpoint_id").(string) 72 rtId := d.Get("route_table_id").(string) 73 74 vpce, err := findResourceVPCEndpoint(conn, endpointId) 75 if err, ok := err.(awserr.Error); ok && err.Code() == "InvalidVpcEndpointId.NotFound" { 76 d.SetId("") 77 return nil 78 } 79 80 found := false 81 for _, id := range vpce.RouteTableIds { 82 if id != nil && *id == rtId { 83 found = true 84 break 85 } 86 } 87 if !found { 88 // The association no longer exists. 89 d.SetId("") 90 return nil 91 } 92 93 id := vpcEndpointIdRouteTableIdHash(endpointId, rtId) 94 log.Printf("[DEBUG] Computed VPC Endpoint/Route Table ID %s", id) 95 d.SetId(id) 96 97 return nil 98 } 99 100 func resourceAwsVPCEndpointRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error { 101 conn := meta.(*AWSClient).ec2conn 102 endpointId := d.Get("vpc_endpoint_id").(string) 103 rtId := d.Get("route_table_id").(string) 104 105 input := &ec2.ModifyVpcEndpointInput{ 106 VpcEndpointId: aws.String(endpointId), 107 RemoveRouteTableIds: aws.StringSlice([]string{rtId}), 108 } 109 110 _, err := conn.ModifyVpcEndpoint(input) 111 if err != nil { 112 ec2err, ok := err.(awserr.Error) 113 if !ok { 114 return fmt.Errorf("Error deleting VPC Endpoint/Route Table association: %s", err.Error()) 115 } 116 117 switch ec2err.Code() { 118 case "InvalidVpcEndpointId.NotFound": 119 fallthrough 120 case "InvalidRouteTableId.NotFound": 121 fallthrough 122 case "InvalidParameter": 123 log.Printf("[DEBUG] VPC Endpoint/Route Table association is already gone") 124 default: 125 return fmt.Errorf("Error deleting VPC Endpoint/Route Table association: %s", err.Error()) 126 } 127 } 128 129 log.Printf("[DEBUG] VPC Endpoint/Route Table association %q deleted", d.Id()) 130 d.SetId("") 131 132 return nil 133 } 134 135 func findResourceVPCEndpoint(conn *ec2.EC2, id string) (*ec2.VpcEndpoint, error) { 136 input := &ec2.DescribeVpcEndpointsInput{ 137 VpcEndpointIds: aws.StringSlice([]string{id}), 138 } 139 140 log.Printf("[DEBUG] Reading VPC Endpoint: %q", id) 141 output, err := conn.DescribeVpcEndpoints(input) 142 if err != nil { 143 return nil, err 144 } 145 146 return output.VpcEndpoints[0], nil 147 } 148 149 func vpcEndpointIdRouteTableIdHash(endpointId, rtId string) string { 150 return fmt.Sprintf("a-%s%d", endpointId, hashcode.String(rtId)) 151 }