forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_vpc_endpoint.go
205 lines (170 loc) · 5.64 KB
/
resource_aws_vpc_endpoint.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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/schema"
)
func resourceAwsVpcEndpoint() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVPCEndpointCreate,
Read: resourceAwsVPCEndpointRead,
Update: resourceAwsVPCEndpointUpdate,
Delete: resourceAwsVPCEndpointDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
StateFunc: normalizeJson,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"service_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_ids": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"prefix_list_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsVPCEndpointCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
input := &ec2.CreateVpcEndpointInput{
VpcId: aws.String(d.Get("vpc_id").(string)),
RouteTableIds: expandStringList(d.Get("route_table_ids").(*schema.Set).List()),
ServiceName: aws.String(d.Get("service_name").(string)),
}
if v, ok := d.GetOk("policy"); ok {
policy := normalizeJson(v)
input.PolicyDocument = aws.String(policy)
}
log.Printf("[DEBUG] Creating VPC Endpoint: %#v", input)
output, err := conn.CreateVpcEndpoint(input)
if err != nil {
return fmt.Errorf("Error creating VPC Endpoint: %s", err)
}
log.Printf("[DEBUG] VPC Endpoint %q created.", *output.VpcEndpoint.VpcEndpointId)
d.SetId(*output.VpcEndpoint.VpcEndpointId)
return resourceAwsVPCEndpointRead(d, meta)
}
func resourceAwsVPCEndpointRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
input := &ec2.DescribeVpcEndpointsInput{
VpcEndpointIds: []*string{aws.String(d.Id())},
}
log.Printf("[DEBUG] Reading VPC Endpoint: %q", d.Id())
output, err := conn.DescribeVpcEndpoints(input)
if err != nil {
ec2err, ok := err.(awserr.Error)
if !ok {
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
}
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
return nil
}
return fmt.Errorf("Error reading VPC Endpoint: %s", err.Error())
}
if len(output.VpcEndpoints) != 1 {
return fmt.Errorf("There's no unique VPC Endpoint, but %d endpoints: %#v",
len(output.VpcEndpoints), output.VpcEndpoints)
}
vpce := output.VpcEndpoints[0]
// A VPC Endpoint is associated with exactly one prefix list name (also called Service Name).
// The prefix list ID can be used in security groups, so retrieve it to support that capability.
prefixListServiceName := *vpce.ServiceName
prefixListInput := &ec2.DescribePrefixListsInput{
Filters: []*ec2.Filter{
{Name: aws.String("prefix-list-name"), Values: []*string{aws.String(prefixListServiceName)}},
},
}
log.Printf("[DEBUG] Reading VPC Endpoint prefix list: %s", prefixListServiceName)
prefixListsOutput, err := conn.DescribePrefixLists(prefixListInput)
if err != nil {
_, ok := err.(awserr.Error)
if !ok {
return fmt.Errorf("Error reading VPC Endpoint prefix list: %s", err.Error())
}
}
if len(prefixListsOutput.PrefixLists) != 1 {
return fmt.Errorf("There are multiple prefix lists associated with the service name '%s'. Unexpected", prefixListServiceName)
}
d.Set("vpc_id", vpce.VpcId)
d.Set("policy", normalizeJson(*vpce.PolicyDocument))
d.Set("service_name", vpce.ServiceName)
if err := d.Set("route_table_ids", aws.StringValueSlice(vpce.RouteTableIds)); err != nil {
return err
}
d.Set("prefix_list_id", prefixListsOutput.PrefixLists[0].PrefixListId)
return nil
}
func resourceAwsVPCEndpointUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
input := &ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(d.Id()),
}
if d.HasChange("route_table_ids") {
o, n := d.GetChange("route_table_ids")
os := o.(*schema.Set)
ns := n.(*schema.Set)
add := expandStringList(ns.Difference(os).List())
if len(add) > 0 {
input.AddRouteTableIds = add
}
remove := expandStringList(os.Difference(ns).List())
if len(remove) > 0 {
input.RemoveRouteTableIds = remove
}
}
if d.HasChange("policy") {
policy := normalizeJson(d.Get("policy"))
input.PolicyDocument = aws.String(policy)
}
log.Printf("[DEBUG] Updating VPC Endpoint: %#v", input)
_, err := conn.ModifyVpcEndpoint(input)
if err != nil {
return fmt.Errorf("Error updating VPC Endpoint: %s", err)
}
log.Printf("[DEBUG] VPC Endpoint %q updated", input.VpcEndpointId)
return resourceAwsVPCEndpointRead(d, meta)
}
func resourceAwsVPCEndpointDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
input := &ec2.DeleteVpcEndpointsInput{
VpcEndpointIds: []*string{aws.String(d.Id())},
}
log.Printf("[DEBUG] Deleting VPC Endpoint: %#v", input)
_, err := conn.DeleteVpcEndpoints(input)
if err != nil {
ec2err, ok := err.(awserr.Error)
if !ok {
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
}
if ec2err.Code() == "InvalidVpcEndpointId.NotFound" {
log.Printf("[DEBUG] VPC Endpoint %q is already gone", d.Id())
} else {
return fmt.Errorf("Error deleting VPC Endpoint: %s", err.Error())
}
}
log.Printf("[DEBUG] VPC Endpoint %q deleted", d.Id())
d.SetId("")
return nil
}