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