generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 117
/
resource_netbox_interface.go
253 lines (220 loc) · 7.69 KB
/
resource_netbox_interface.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
package netbox
import (
"context"
"strconv"
"strings"
"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/virtualization"
"github.com/fbreckle/go-netbox/netbox/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
var resourceNetboxInterfaceModeOptions = []string{"access", "tagged", "tagged-all"}
func resourceNetboxInterface() *schema.Resource {
return &schema.Resource{
CreateContext: resourceNetboxInterfaceCreate,
ReadContext: resourceNetboxInterfaceRead,
UpdateContext: resourceNetboxInterfaceUpdate,
DeleteContext: resourceNetboxInterfaceDelete,
Description: `:meta:subcategory:Virtualization:From the [official documentation](https://docs.netbox.dev/en/stable/features/virtualization/#interfaces):
> Virtual machine interfaces behave similarly to device interfaces, and can be assigned to VRFs, and may have IP addresses, VLANs, and services attached to them. However, given their virtual nature, they lack properties pertaining to physical attributes. For example, VM interfaces do not have a physical type and cannot have cables attached to them.`,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"virtual_machine_id": {
Type: schema.TypeInt,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"mac_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsMACAddress,
// Netbox converts MAC addresses always to uppercase
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
},
},
"mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(resourceNetboxInterfaceModeOptions, false),
Description: buildValidValueDescription(resourceNetboxInterfaceModeOptions),
},
"mtu": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 65536),
},
"type": {
Type: schema.TypeString,
Optional: true,
Deprecated: "This attribute is not supported by netbox any longer. It will be removed in future versions of this provider.",
},
tagsKey: tagsSchema,
"tagged_vlans": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"untagged_vlan": {
Type: schema.TypeInt,
Optional: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceNetboxInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*client.NetBoxAPI)
var diags diag.Diagnostics
name := d.Get("name").(string)
description := d.Get("description").(string)
enabled := d.Get("enabled").(bool)
mode := d.Get("mode").(string)
tags, diagnostics := getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
if diagnostics != nil {
diags = append(diags, diagnostics...)
}
taggedVlans := toInt64List(d.Get("tagged_vlans"))
virtualMachineID := int64(d.Get("virtual_machine_id").(int))
data := models.WritableVMInterface{
Name: &name,
Description: description,
Enabled: enabled,
Mode: mode,
Tags: tags,
TaggedVlans: taggedVlans,
VirtualMachine: &virtualMachineID,
}
if macAddress := d.Get("mac_address").(string); macAddress != "" {
data.MacAddress = &macAddress
}
if mtu, ok := d.Get("mtu").(int); ok && mtu != 0 {
data.Mtu = int64ToPtr(int64(mtu))
}
if untaggedVlan, ok := d.Get("untagged_vlan").(int); ok && untaggedVlan != 0 {
data.UntaggedVlan = int64ToPtr(int64(untaggedVlan))
}
params := virtualization.NewVirtualizationInterfacesCreateParams().WithData(&data)
res, err := api.Virtualization.VirtualizationInterfacesCreate(params, nil)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatInt(res.GetPayload().ID, 10))
return diags
}
func resourceNetboxInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*client.NetBoxAPI)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
var diags diag.Diagnostics
params := virtualization.NewVirtualizationInterfacesReadParams().WithID(id)
res, err := api.Virtualization.VirtualizationInterfacesRead(params, nil)
if err != nil {
if errresp, ok := err.(*virtualization.VirtualizationInterfacesReadDefault); ok {
errorcode := errresp.Code()
if errorcode == 404 {
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
iface := res.GetPayload()
d.Set("name", iface.Name)
d.Set("description", iface.Description)
d.Set("enabled", iface.Enabled)
d.Set("mac_address", iface.MacAddress)
d.Set("mtu", iface.Mtu)
d.Set(tagsKey, getTagListFromNestedTagList(iface.Tags))
d.Set("tagged_vlans", getIDsFromNestedVLAN(iface.TaggedVlans))
d.Set("virtual_machine_id", iface.VirtualMachine.ID)
if iface.Mode != nil {
d.Set("mode", iface.Mode.Value)
}
if iface.UntaggedVlan != nil {
d.Set("untagged_vlan", iface.UntaggedVlan.ID)
}
return diags
}
func resourceNetboxInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*client.NetBoxAPI)
var diags diag.Diagnostics
id, _ := strconv.ParseInt(d.Id(), 10, 64)
name := d.Get("name").(string)
description := d.Get("description").(string)
enabled := d.Get("enabled").(bool)
mode := d.Get("mode").(string)
tags, diagnostics := getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
if diagnostics != nil {
diags = append(diags, diagnostics...)
}
taggedVlans := toInt64List(d.Get("tagged_vlans"))
virtualMachineID := int64(d.Get("virtual_machine_id").(int))
data := models.WritableVMInterface{
Name: &name,
Description: description,
Enabled: enabled,
Mode: mode,
Tags: tags,
TaggedVlans: taggedVlans,
VirtualMachine: &virtualMachineID,
}
if d.HasChange("mac_address") {
macAddress := d.Get("mac_address").(string)
data.MacAddress = &macAddress
}
if d.HasChange("mtu") {
mtu := int64(d.Get("mtu").(int))
data.Mtu = &mtu
}
if d.HasChange("untagged_vlan") {
untaggedvlan := int64(d.Get("untagged_vlan").(int))
data.UntaggedVlan = &untaggedvlan
}
params := virtualization.NewVirtualizationInterfacesPartialUpdateParams().WithID(id).WithData(&data)
_, err := api.Virtualization.VirtualizationInterfacesPartialUpdate(params, nil)
if err != nil {
return diag.FromErr(err)
}
return diags
}
func resourceNetboxInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*client.NetBoxAPI)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
params := virtualization.NewVirtualizationInterfacesDeleteParams().WithID(id)
_, err := api.Virtualization.VirtualizationInterfacesDelete(params, nil)
if err != nil {
if errresp, ok := err.(*virtualization.VirtualizationInterfacesDeleteDefault); ok {
if errresp.Code() == 404 {
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
return nil
}
func getIDsFromNestedVLAN(nestedvlans []*models.NestedVLAN) []int64 {
var vlans []int64
for _, vlan := range nestedvlans {
vlans = append(vlans, vlan.ID)
}
return vlans
}