-
Notifications
You must be signed in to change notification settings - Fork 45
/
resource_metal_port_vlan_attachment.go
276 lines (255 loc) · 7.88 KB
/
resource_metal_port_vlan_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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package equinix
import (
"fmt"
"log"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/mutexkv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/packethost/packngo"
)
func resourceMetalPortVlanAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceMetalPortVlanAttachmentCreate,
Read: resourceMetalPortVlanAttachmentRead,
Delete: resourceMetalPortVlanAttachmentDelete,
Update: resourceMetalPortVlanAttachmentUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"force_bond": {
Type: schema.TypeBool,
Description: "Add port back to the bond when this resource is removed. Default is false",
Optional: true,
Default: false,
ForceNew: true,
},
"device_id": {
Type: schema.TypeString,
Description: "ID of device to be assigned to the VLAN",
Required: true,
ForceNew: true,
},
"port_name": {
Type: schema.TypeString,
Description: "Name of network port to be assigned to the VLAN",
Required: true,
ForceNew: true,
},
"vlan_vnid": {
Type: schema.TypeInt,
Description: "VXLAN Network Identifier, integer",
Required: true,
ForceNew: true,
},
"vlan_id": {
Type: schema.TypeString,
Description: "UUID of VLAN API resource",
Computed: true,
},
"port_id": {
Type: schema.TypeString,
Description: "UUID of device port",
Computed: true,
},
"native": {
Type: schema.TypeBool,
Description: "Mark this VLAN a native VLAN on the port. This can be used only if this assignment assigns second or further VLAN to the port. To ensure that this attachment is not first on a port, you can use depends_on pointing to another equinix_metal_port_vlan_attachment, just like in the layer2-individual example above",
Optional: true,
Default: false,
},
},
}
}
func resourceMetalPortVlanAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
meta.(*config.Config).AddModuleToMetalUserAgent(d)
client := meta.(*config.Config).Metal
deviceID := d.Get("device_id").(string)
pName := d.Get("port_name").(string)
vlanVNID := d.Get("vlan_vnid").(int)
dev, _, err := client.Devices.Get(deviceID, &packngo.GetOptions{
Includes: []string{"virtual_networks,project,native_virtual_network"},
})
if err != nil {
return err
}
portFound := false
vlanFound := false
vlanID := ""
var port packngo.Port
for _, p := range dev.NetworkPorts {
if p.Name == pName {
portFound = true
port = p
for _, n := range p.AttachedVirtualNetworks {
if vlanVNID == n.VXLAN {
vlanFound = true
vlanID = n.ID
break
}
}
break
}
}
if !portFound {
return fmt.Errorf("Device %s doesn't have port %s", deviceID, pName)
}
par := &packngo.PortAssignRequest{PortID: port.ID}
if vlanFound {
log.Printf("Port %s already has VLAN %d assigned", pName, vlanVNID)
par.VirtualNetworkID = vlanID
} else {
projectID := dev.Project.ID
deviceMetro := dev.Metro.Code
deviceFacility := dev.Facility.Code
vlans, _, err := client.ProjectVirtualNetworks.List(projectID, nil)
if err != nil {
return err
}
for _, n := range vlans.VirtualNetworks {
// looking up vlan with given vxlan, in the same location as
// the device - either in the same faclility or metro or both
vlanMetro := n.MetroCode
vlanFacility := n.FacilityCode
if n.VXLAN == vlanVNID {
facilitiesMatch := deviceFacility == vlanFacility
metrosMatch := deviceMetro == vlanMetro
if metrosMatch || facilitiesMatch {
vlanID = n.ID
break
}
}
}
if len(vlanID) == 0 {
return fmt.Errorf("VLAN with VNID %d doesn't exist in project %s", vlanVNID, projectID)
}
par.VirtualNetworkID = vlanID
// Equinix Metal doesn't allow multiple VLANs to be assigned
// to the same port at the same time
lockId := "vlan-attachment-" + port.ID
mutexkv.Metal.Lock(lockId)
defer mutexkv.Metal.Unlock(lockId)
_, _, err = client.DevicePorts.Assign(par)
if err != nil {
return err
}
}
d.SetId(port.ID + ":" + vlanID)
native := d.Get("native").(bool)
if native {
_, _, err = client.DevicePorts.AssignNative(par)
if err != nil {
return err
}
}
return resourceMetalPortVlanAttachmentRead(d, meta)
}
func resourceMetalPortVlanAttachmentRead(d *schema.ResourceData, meta interface{}) error {
meta.(*config.Config).AddModuleToMetalUserAgent(d)
client := meta.(*config.Config).Metal
deviceID := d.Get("device_id").(string)
pName := d.Get("port_name").(string)
vlanVNID := d.Get("vlan_vnid").(int)
dev, _, err := client.Devices.Get(deviceID, &packngo.GetOptions{Includes: []string{"virtual_networks,project,native_virtual_network"}})
if err != nil {
err = equinix_errors.FriendlyError(err)
if equinix_errors.IsNotFound(err) {
log.Printf("[WARN] Device (%s) for Port Vlan Attachment not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
portFound := false
vlanFound := false
portID := ""
vlanID := ""
vlanNative := false
for _, p := range dev.NetworkPorts {
if p.Name == pName {
portFound = true
portID = p.ID
for _, n := range p.AttachedVirtualNetworks {
if vlanVNID == n.VXLAN {
vlanFound = true
vlanID = n.ID
if p.NativeVirtualNetwork != nil {
vlanNative = vlanID == p.NativeVirtualNetwork.ID
}
break
}
}
break
}
}
if !portFound {
// TODO(displague) should we clear state if the port is unexpectedly
// gone? Can we treat this like a deletion?
return fmt.Errorf("Device %s doesn't have port %s", deviceID, pName)
}
if !vlanFound {
d.SetId("")
}
d.Set("port_id", portID)
d.Set("vlan_id", vlanID)
d.Set("native", vlanNative)
return nil
}
func resourceMetalPortVlanAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
meta.(*config.Config).AddModuleToMetalUserAgent(d)
client := meta.(*config.Config).Metal
if d.HasChange("native") {
native := d.Get("native").(bool)
portID := d.Get("port_id").(string)
if native {
vlanID := d.Get("vlan_id").(string)
par := &packngo.PortAssignRequest{PortID: portID, VirtualNetworkID: vlanID}
_, _, err := client.DevicePorts.AssignNative(par)
if err != nil {
return err
}
} else {
_, _, err := client.DevicePorts.UnassignNative(portID)
if err != nil {
return err
}
}
}
return resourceMetalPortVlanAttachmentRead(d, meta)
}
func resourceMetalPortVlanAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
meta.(*config.Config).AddModuleToMetalUserAgent(d)
client := meta.(*config.Config).Metal
pID := d.Get("port_id").(string)
vlanID := d.Get("vlan_id").(string)
native := d.Get("native").(bool)
if native {
_, resp, err := client.DevicePorts.UnassignNative(pID)
if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil {
return err
}
}
par := &packngo.PortAssignRequest{PortID: pID, VirtualNetworkID: vlanID}
lockId := "vlan-detachment-" + pID
mutexkv.Metal.Lock(lockId)
defer mutexkv.Metal.Unlock(lockId)
portPtr, resp, err := client.DevicePorts.Unassign(par)
if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound, equinix_errors.IsNotAssigned)(resp, err) != nil {
return err
}
forceBond := d.Get("force_bond").(bool)
if forceBond && (len(portPtr.AttachedVirtualNetworks) == 0) {
deviceID := d.Get("device_id").(string)
portName := d.Get("port_name").(string)
port, err := client.DevicePorts.GetPortByName(deviceID, portName)
if err != nil {
return equinix_errors.FriendlyError(err)
}
_, _, err = client.DevicePorts.Bond(port, false)
if err != nil {
return equinix_errors.FriendlyError(err)
}
}
return nil
}