github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 != nil { 76 if err, ok := err.(awserr.Error); ok && err.Code() == "InvalidVpcEndpointId.NotFound" { 77 d.SetId("") 78 return nil 79 } 80 81 return err 82 } 83 84 found := false 85 for _, id := range vpce.RouteTableIds { 86 if id != nil && *id == rtId { 87 found = true 88 break 89 } 90 } 91 if !found { 92 // The association no longer exists. 93 d.SetId("") 94 return nil 95 } 96 97 id := vpcEndpointIdRouteTableIdHash(endpointId, rtId) 98 log.Printf("[DEBUG] Computed VPC Endpoint/Route Table ID %s", id) 99 d.SetId(id) 100 101 return nil 102 } 103 104 func resourceAwsVPCEndpointRouteTableAssociationDelete(d *schema.ResourceData, meta interface{}) error { 105 conn := meta.(*AWSClient).ec2conn 106 endpointId := d.Get("vpc_endpoint_id").(string) 107 rtId := d.Get("route_table_id").(string) 108 109 input := &ec2.ModifyVpcEndpointInput{ 110 VpcEndpointId: aws.String(endpointId), 111 RemoveRouteTableIds: aws.StringSlice([]string{rtId}), 112 } 113 114 _, err := conn.ModifyVpcEndpoint(input) 115 if err != nil { 116 ec2err, ok := err.(awserr.Error) 117 if !ok { 118 return fmt.Errorf("Error deleting VPC Endpoint/Route Table association: %s", err.Error()) 119 } 120 121 switch ec2err.Code() { 122 case "InvalidVpcEndpointId.NotFound": 123 fallthrough 124 case "InvalidRouteTableId.NotFound": 125 fallthrough 126 case "InvalidParameter": 127 log.Printf("[DEBUG] VPC Endpoint/Route Table association is already gone") 128 default: 129 return fmt.Errorf("Error deleting VPC Endpoint/Route Table association: %s", err.Error()) 130 } 131 } 132 133 log.Printf("[DEBUG] VPC Endpoint/Route Table association %q deleted", d.Id()) 134 d.SetId("") 135 136 return nil 137 } 138 139 func findResourceVPCEndpoint(conn *ec2.EC2, id string) (*ec2.VpcEndpoint, error) { 140 input := &ec2.DescribeVpcEndpointsInput{ 141 VpcEndpointIds: aws.StringSlice([]string{id}), 142 } 143 144 log.Printf("[DEBUG] Reading VPC Endpoint: %q", id) 145 output, err := conn.DescribeVpcEndpoints(input) 146 if err != nil { 147 return nil, err 148 } 149 150 if output.VpcEndpoints == nil { 151 return nil, fmt.Errorf("No VPC Endpoints were found for %q", id) 152 } 153 154 return output.VpcEndpoints[0], nil 155 } 156 157 func vpcEndpointIdRouteTableIdHash(endpointId, rtId string) string { 158 return fmt.Sprintf("a-%s%d", endpointId, hashcode.String(rtId)) 159 }