forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_network_interface_sg_attachment.go
170 lines (140 loc) · 4.78 KB
/
resource_aws_network_interface_sg_attachment.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
package aws
import (
"fmt"
"log"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsNetworkInterfaceSGAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsNetworkInterfaceSGAttachmentCreate,
Read: resourceAwsNetworkInterfaceSGAttachmentRead,
Delete: resourceAwsNetworkInterfaceSGAttachmentDelete,
Schema: map[string]*schema.Schema{
"security_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"network_interface_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsNetworkInterfaceSGAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
mk := "network_interface_sg_attachment_" + d.Get("network_interface_id").(string)
awsMutexKV.Lock(mk)
defer awsMutexKV.Unlock(mk)
sgID := d.Get("security_group_id").(string)
interfaceID := d.Get("network_interface_id").(string)
conn := meta.(*AWSClient).ec2conn
// Fetch the network interface we will be working with.
iface, err := fetchNetworkInterface(conn, interfaceID)
if err != nil {
return err
}
// Add the security group to the network interface.
log.Printf("[DEBUG] Attaching security group %s to network interface ID %s", sgID, interfaceID)
if sgExistsInENI(sgID, iface) {
return fmt.Errorf("security group %s already attached to interface ID %s", sgID, *iface.NetworkInterfaceId)
}
var groupIDs []string
for _, v := range iface.Groups {
groupIDs = append(groupIDs, *v.GroupId)
}
groupIDs = append(groupIDs, sgID)
params := &ec2.ModifyNetworkInterfaceAttributeInput{
NetworkInterfaceId: iface.NetworkInterfaceId,
Groups: aws.StringSlice(groupIDs),
}
_, err = conn.ModifyNetworkInterfaceAttribute(params)
if err != nil {
return err
}
log.Printf("[DEBUG] Successful attachment of security group %s to network interface ID %s", sgID, interfaceID)
return resourceAwsNetworkInterfaceSGAttachmentRead(d, meta)
}
func resourceAwsNetworkInterfaceSGAttachmentRead(d *schema.ResourceData, meta interface{}) error {
sgID := d.Get("security_group_id").(string)
interfaceID := d.Get("network_interface_id").(string)
log.Printf("[DEBUG] Checking association of security group %s to network interface ID %s", sgID, interfaceID)
conn := meta.(*AWSClient).ec2conn
iface, err := fetchNetworkInterface(conn, interfaceID)
if err != nil {
return err
}
if sgExistsInENI(sgID, iface) {
d.SetId(fmt.Sprintf("%s_%s", sgID, interfaceID))
} else {
// The association does not exist when it should, taint this resource.
log.Printf("[WARN] Security group %s not associated with network interface ID %s, tainting", sgID, interfaceID)
d.SetId("")
}
return nil
}
func resourceAwsNetworkInterfaceSGAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
mk := "network_interface_sg_attachment_" + d.Get("network_interface_id").(string)
awsMutexKV.Lock(mk)
defer awsMutexKV.Unlock(mk)
sgID := d.Get("security_group_id").(string)
interfaceID := d.Get("network_interface_id").(string)
log.Printf("[DEBUG] Removing security group %s from interface ID %s", sgID, interfaceID)
conn := meta.(*AWSClient).ec2conn
iface, err := fetchNetworkInterface(conn, interfaceID)
if err != nil {
return err
}
if err := delSGFromENI(conn, sgID, iface); err != nil {
return err
}
d.SetId("")
return nil
}
// fetchNetworkInterface is a utility function used by Read and Delete to fetch
// the full ENI details for a specific interface ID.
func fetchNetworkInterface(conn *ec2.EC2, ifaceID string) (*ec2.NetworkInterface, error) {
log.Printf("[DEBUG] Fetching information for interface ID %s", ifaceID)
dniParams := &ec2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: aws.StringSlice([]string{ifaceID}),
}
dniResp, err := conn.DescribeNetworkInterfaces(dniParams)
if err != nil {
return nil, err
}
return dniResp.NetworkInterfaces[0], nil
}
func delSGFromENI(conn *ec2.EC2, sgID string, iface *ec2.NetworkInterface) error {
old := iface.Groups
var new []*string
for _, v := range iface.Groups {
if *v.GroupId == sgID {
continue
}
new = append(new, v.GroupId)
}
if reflect.DeepEqual(old, new) {
// The interface already didn't have the security group, nothing to do
return nil
}
params := &ec2.ModifyNetworkInterfaceAttributeInput{
NetworkInterfaceId: iface.NetworkInterfaceId,
Groups: new,
}
_, err := conn.ModifyNetworkInterfaceAttribute(params)
return err
}
// sgExistsInENI is a utility function that can be used to quickly check to
// see if a security group exists in an *ec2.NetworkInterface.
func sgExistsInENI(sgID string, iface *ec2.NetworkInterface) bool {
for _, v := range iface.Groups {
if *v.GroupId == sgID {
return true
}
}
return false
}