forked from hashicorp/terraform-provider-aws
/
resource_aws_vpc_endpoint_subnet_association.go
156 lines (130 loc) · 4.17 KB
/
resource_aws_vpc_endpoint_subnet_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
package aws
import (
"fmt"
"log"
"time"
"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/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsVpcEndpointSubnetAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVpcEndpointSubnetAssociationCreate,
Read: resourceAwsVpcEndpointSubnetAssociationRead,
Delete: resourceAwsVpcEndpointSubnetAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"vpc_endpoint_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
}
}
func resourceAwsVpcEndpointSubnetAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
endpointId := d.Get("vpc_endpoint_id").(string)
snId := d.Get("subnet_id").(string)
_, err := findResourceVpcEndpoint(conn, endpointId)
if err != nil {
return err
}
// See https://github.com/terraform-providers/terraform-provider-aws/issues/3382.
// Prevent concurrent subnet association requests and delay between requests.
mk := "vpc_endpoint_subnet_association_" + endpointId
awsMutexKV.Lock(mk)
defer awsMutexKV.Unlock(mk)
c := &resource.StateChangeConf{
Delay: 1 * time.Minute,
Timeout: 3 * time.Minute,
Target: []string{"ok"},
Refresh: func() (interface{}, string, error) {
res, err := conn.ModifyVpcEndpoint(&ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(endpointId),
AddSubnetIds: aws.StringSlice([]string{snId}),
})
return res, "ok", err
},
}
_, err = c.WaitForState()
if err != nil {
return fmt.Errorf("Error creating Vpc Endpoint/Subnet association: %s", err)
}
d.SetId(vpcEndpointSubnetAssociationId(endpointId, snId))
if err := vpcEndpointWaitUntilAvailable(conn, endpointId, d.Timeout(schema.TimeoutCreate)); err != nil {
return err
}
return resourceAwsVpcEndpointSubnetAssociationRead(d, meta)
}
func resourceAwsVpcEndpointSubnetAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
endpointId := d.Get("vpc_endpoint_id").(string)
snId := d.Get("subnet_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/Subnet association (%s) from state", endpointId, d.Id())
d.SetId("")
return nil
}
return err
}
found := false
for _, id := range vpce.SubnetIds {
if aws.StringValue(id) == snId {
found = true
break
}
}
if !found {
log.Printf("[WARN] Vpc Endpoint/Subnet association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return nil
}
func resourceAwsVpcEndpointSubnetAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
endpointId := d.Get("vpc_endpoint_id").(string)
snId := d.Get("subnet_id").(string)
_, err := conn.ModifyVpcEndpoint(&ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(endpointId),
RemoveSubnetIds: aws.StringSlice([]string{snId}),
})
if err != nil {
ec2err, ok := err.(awserr.Error)
if !ok {
return fmt.Errorf("Error deleting Vpc Endpoint/Subnet association: %s", err)
}
switch ec2err.Code() {
case "InvalidVpcEndpointId.NotFound":
fallthrough
case "InvalidParameter":
log.Printf("[DEBUG] Vpc Endpoint/Subnet association is already gone")
default:
return fmt.Errorf("Error deleting Vpc Endpoint/Subnet association: %s", err)
}
}
if err := vpcEndpointWaitUntilAvailable(conn, endpointId, d.Timeout(schema.TimeoutDelete)); err != nil {
return err
}
return nil
}
func vpcEndpointSubnetAssociationId(endpointId, snId string) string {
return fmt.Sprintf("a-%s%d", endpointId, hashcode.String(snId))
}