forked from hashicorp/terraform-provider-aws
/
resource_aws_vpc_endpoint_route_table_association.go
143 lines (118 loc) · 3.82 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
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
}
_, err = conn.ModifyVpcEndpoint(&ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(endpointId),
AddRouteTableIds: aws.StringSlice([]string{rtId}),
})
if err != nil {
return fmt.Errorf("Error creating VPC Endpoint/Route Table association: %s", err.Error())
}
d.SetId(vpcEndpointIdRouteTableIdHash(endpointId, rtId))
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 isAWSErr(err, "InvalidVpcEndpointId.NotFound", "") {
log.Printf("[WARN] VPC Endpoint (%s) not found, removing VPC Endpoint/Route Table association (%s) from state", endpointId, d.Id())
d.SetId("")
return nil
}
return err
}
found := false
for _, id := range vpce.RouteTableIds {
if aws.StringValue(id) == rtId {
found = true
break
}
}
if !found {
log.Printf("[WARN] VPC Endpoint/Route Table association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
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)
_, err := conn.ModifyVpcEndpoint(&ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(endpointId),
RemoveRouteTableIds: aws.StringSlice([]string{rtId}),
})
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())
}
}
return nil
}
func findResourceVpcEndpoint(conn *ec2.EC2, id string) (*ec2.VpcEndpoint, error) {
resp, err := conn.DescribeVpcEndpoints(&ec2.DescribeVpcEndpointsInput{
VpcEndpointIds: aws.StringSlice([]string{id}),
})
if err != nil {
return nil, err
}
if resp.VpcEndpoints == nil || len(resp.VpcEndpoints) == 0 {
return nil, fmt.Errorf("No VPC Endpoints were found for %s", id)
}
return resp.VpcEndpoints[0], nil
}
func vpcEndpointIdRouteTableIdHash(endpointId, rtId string) string {
return fmt.Sprintf("a-%s%d", endpointId, hashcode.String(rtId))
}