forked from hashicorp/terraform-provider-aws
/
resource_aws_vpc_endpoint_subnet_association.go
128 lines (106 loc) · 3.3 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
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 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,
},
},
}
}
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
}
_, err = conn.ModifyVpcEndpoint(&ec2.ModifyVpcEndpointInput{
VpcEndpointId: aws.String(endpointId),
AddSubnetIds: aws.StringSlice([]string{snId}),
})
if err != nil {
return fmt.Errorf("Error creating Vpc Endpoint/Subnet association: %s", err.Error())
}
d.SetId(vpcEndpointIdSubnetIdHash(endpointId, snId))
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.Error())
}
switch ec2err.Code() {
case "InvalidVpcEndpointId.NotFound":
fallthrough
case "InvalidRouteTableId.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.Error())
}
}
return nil
}
func vpcEndpointIdSubnetIdHash(endpointId, snId string) string {
return fmt.Sprintf("a-%s%d", endpointId, hashcode.String(snId))
}