This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
resource_openstack_networking_port_secgroup_associate_v2.go
208 lines (165 loc) · 5.77 KB
/
resource_openstack_networking_port_secgroup_associate_v2.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
206
207
208
package openstack
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
)
func resourceNetworkingPortSecGroupAssociateV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingPortSecGroupAssociateV2Create,
Read: resourceNetworkingPortSecGroupAssociateV2Read,
Update: resourceNetworkingPortSecGroupAssociateV2Update,
Delete: resourceNetworkingPortSecGroupAssociateV2Delete,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"port_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"security_group_ids": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"enforce": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"all_security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func resourceNetworkingPortSecGroupAssociateV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
securityGroups := expandToStringSlice(d.Get("security_group_ids").(*schema.Set).List())
portID := d.Get("port_id").(string)
port, err := ports.Get(networkingClient, portID).Extract()
if err != nil {
return fmt.Errorf("Unable to get %s Port: %s", portID, err)
}
log.Printf("[DEBUG] Retrieved Port %s: %+v", portID, port)
var updateOpts ports.UpdateOpts
var enforce bool
if v, ok := d.GetOkExists("enforce"); ok {
enforce = v.(bool)
}
if enforce {
updateOpts.SecurityGroups = &securityGroups
} else {
// append security groups
sg := sliceUnion(port.SecurityGroups, securityGroups)
updateOpts.SecurityGroups = &sg
}
log.Printf("[DEBUG] Port Security Group Associate Options: %#v", updateOpts.SecurityGroups)
_, err = ports.Update(networkingClient, portID, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error associating %s port with '%s' security groups: %s", portID, strings.Join(securityGroups, ","), err)
}
d.SetId(portID)
return resourceNetworkingPortSecGroupAssociateV2Read(d, meta)
}
func resourceNetworkingPortSecGroupAssociateV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
port, err := ports.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Error fetching port security groups")
}
var enforce bool
if v, ok := d.GetOkExists("enforce"); ok {
enforce = v.(bool)
}
d.Set("all_security_group_ids", port.SecurityGroups)
if enforce {
d.Set("security_group_ids", port.SecurityGroups)
} else {
allSet := d.Get("all_security_group_ids").(*schema.Set)
desiredSet := d.Get("security_group_ids").(*schema.Set)
actualSet := allSet.Intersection(desiredSet)
if !actualSet.Equal(desiredSet) {
d.Set("security_group_ids", expandToStringSlice(actualSet.List()))
}
}
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingPortSecGroupAssociateV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
var updateOpts ports.UpdateOpts
var enforce bool
if v, ok := d.GetOkExists("enforce"); ok {
enforce = v.(bool)
}
if enforce {
securityGroups := expandToStringSlice(d.Get("security_group_ids").(*schema.Set).List())
updateOpts.SecurityGroups = &securityGroups
} else {
allSet := d.Get("all_security_group_ids").(*schema.Set)
oldIDs, newIDs := d.GetChange("security_group_ids")
oldSet, newSet := oldIDs.(*schema.Set), newIDs.(*schema.Set)
allWithoutOld := allSet.Difference(oldSet)
newSecurityGroups := expandToStringSlice(allWithoutOld.Union(newSet).List())
updateOpts.SecurityGroups = &newSecurityGroups
}
if d.HasChange("security_group_ids") || d.HasChange("enforce") {
log.Printf("[DEBUG] Port Security Group Update Options: %#v", updateOpts.SecurityGroups)
_, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Port: %s", err)
}
}
return resourceNetworkingPortSecGroupAssociateV2Read(d, meta)
}
func resourceNetworkingPortSecGroupAssociateV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
var updateOpts ports.UpdateOpts
var enforce bool
if v, ok := d.GetOkExists("enforce"); ok {
enforce = v.(bool)
}
if enforce {
updateOpts.SecurityGroups = &[]string{}
} else {
allSet := d.Get("all_security_group_ids").(*schema.Set)
oldSet := d.Get("security_group_ids").(*schema.Set)
allWithoutOld := allSet.Difference(oldSet)
newSecurityGroups := expandToStringSlice(allWithoutOld.List())
updateOpts.SecurityGroups = &newSecurityGroups
}
log.Printf("[DEBUG] Port security groups disassociation options: %#v", updateOpts.SecurityGroups)
_, err = ports.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return CheckDeleted(d, err, "Error disassociating port security groups")
}
return nil
}