forked from fortinetdev/terraform-provider-fortios
/
resource_switchcontroller_nacdevice.go
476 lines (385 loc) · 13.1 KB
/
resource_switchcontroller_nacdevice.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2020 Fortinet, Inc. All rights reserved.
// Author: Frank Shen (@frankshen01), Hongbin Lu (@fgtdev-hblu)
// Documentation:
// Frank Shen (@frankshen01), Hongbin Lu (@fgtdev-hblu),
// Xing Li (@lix-fortinet), Yue Wang (@yuew-ftnt), Yuffie Zhu (@yuffiezhu)
// Description: Configure/list NAC devices learned on the managed FortiSwitch ports which matches NAC policy.
package fortios
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceSwitchControllerNacDevice() *schema.Resource {
return &schema.Resource{
Create: resourceSwitchControllerNacDeviceCreate,
Read: resourceSwitchControllerNacDeviceRead,
Update: resourceSwitchControllerNacDeviceUpdate,
Delete: resourceSwitchControllerNacDeviceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"vdomparam": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"fosid": &schema.Schema{
Type: schema.TypeInt,
ForceNew: true,
Optional: true,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 127),
Optional: true,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"mac": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"last_known_switch": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 63),
Optional: true,
Computed: true,
},
"last_known_port": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 35),
Optional: true,
Computed: true,
},
"matched_nac_policy": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 63),
Optional: true,
Computed: true,
},
"port_policy": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 63),
Optional: true,
Computed: true,
},
"mac_policy": &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 63),
Optional: true,
Computed: true,
},
"last_seen": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
}
func resourceSwitchControllerNacDeviceCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*FortiClient).Client
c.Retries = 1
vdomparam := ""
if v, ok := d.GetOk("vdomparam"); ok {
if s, ok := v.(string); ok {
vdomparam = s
}
}
obj, err := getObjectSwitchControllerNacDevice(d, c.Fv)
if err != nil {
return fmt.Errorf("Error creating SwitchControllerNacDevice resource while getting object: %v", err)
}
o, err := c.CreateSwitchControllerNacDevice(obj, vdomparam)
if err != nil {
return fmt.Errorf("Error creating SwitchControllerNacDevice resource: %v", err)
}
if o["mkey"] != nil && o["mkey"] != "" {
d.SetId(strconv.Itoa(int(o["mkey"].(float64))))
} else {
d.SetId("SwitchControllerNacDevice")
}
return resourceSwitchControllerNacDeviceRead(d, m)
}
func resourceSwitchControllerNacDeviceUpdate(d *schema.ResourceData, m interface{}) error {
mkey := d.Id()
c := m.(*FortiClient).Client
c.Retries = 1
vdomparam := ""
if v, ok := d.GetOk("vdomparam"); ok {
if s, ok := v.(string); ok {
vdomparam = s
}
}
obj, err := getObjectSwitchControllerNacDevice(d, c.Fv)
if err != nil {
return fmt.Errorf("Error updating SwitchControllerNacDevice resource while getting object: %v", err)
}
o, err := c.UpdateSwitchControllerNacDevice(obj, mkey, vdomparam)
if err != nil {
return fmt.Errorf("Error updating SwitchControllerNacDevice resource: %v", err)
}
log.Printf(strconv.Itoa(c.Retries))
if o["mkey"] != nil && o["mkey"] != "" {
d.SetId(strconv.Itoa(int(o["mkey"].(float64))))
} else {
d.SetId("SwitchControllerNacDevice")
}
return resourceSwitchControllerNacDeviceRead(d, m)
}
func resourceSwitchControllerNacDeviceDelete(d *schema.ResourceData, m interface{}) error {
mkey := d.Id()
c := m.(*FortiClient).Client
c.Retries = 1
vdomparam := ""
if v, ok := d.GetOk("vdomparam"); ok {
if s, ok := v.(string); ok {
vdomparam = s
}
}
err := c.DeleteSwitchControllerNacDevice(mkey, vdomparam)
if err != nil {
return fmt.Errorf("Error deleting SwitchControllerNacDevice resource: %v", err)
}
d.SetId("")
return nil
}
func resourceSwitchControllerNacDeviceRead(d *schema.ResourceData, m interface{}) error {
mkey := d.Id()
c := m.(*FortiClient).Client
c.Retries = 1
vdomparam := ""
if v, ok := d.GetOk("vdomparam"); ok {
if s, ok := v.(string); ok {
vdomparam = s
}
}
o, err := c.ReadSwitchControllerNacDevice(mkey, vdomparam)
if err != nil {
return fmt.Errorf("Error reading SwitchControllerNacDevice resource: %v", err)
}
if o == nil {
log.Printf("[WARN] resource (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
err = refreshObjectSwitchControllerNacDevice(d, o, c.Fv)
if err != nil {
return fmt.Errorf("Error reading SwitchControllerNacDevice resource from API: %v", err)
}
return nil
}
func flattenSwitchControllerNacDeviceId(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceDescription(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceStatus(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceMac(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceLastKnownSwitch(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceLastKnownPort(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceMatchedNacPolicy(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDevicePortPolicy(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceMacPolicy(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func flattenSwitchControllerNacDeviceLastSeen(v interface{}, d *schema.ResourceData, pre string, sv string) interface{} {
return v
}
func refreshObjectSwitchControllerNacDevice(d *schema.ResourceData, o map[string]interface{}, sv string) error {
var err error
if err = d.Set("fosid", flattenSwitchControllerNacDeviceId(o["id"], d, "fosid", sv)); err != nil {
if !fortiAPIPatch(o["id"]) {
return fmt.Errorf("Error reading fosid: %v", err)
}
}
if err = d.Set("description", flattenSwitchControllerNacDeviceDescription(o["description"], d, "description", sv)); err != nil {
if !fortiAPIPatch(o["description"]) {
return fmt.Errorf("Error reading description: %v", err)
}
}
if err = d.Set("status", flattenSwitchControllerNacDeviceStatus(o["status"], d, "status", sv)); err != nil {
if !fortiAPIPatch(o["status"]) {
return fmt.Errorf("Error reading status: %v", err)
}
}
if err = d.Set("mac", flattenSwitchControllerNacDeviceMac(o["mac"], d, "mac", sv)); err != nil {
if !fortiAPIPatch(o["mac"]) {
return fmt.Errorf("Error reading mac: %v", err)
}
}
if err = d.Set("last_known_switch", flattenSwitchControllerNacDeviceLastKnownSwitch(o["last-known-switch"], d, "last_known_switch", sv)); err != nil {
if !fortiAPIPatch(o["last-known-switch"]) {
return fmt.Errorf("Error reading last_known_switch: %v", err)
}
}
if err = d.Set("last_known_port", flattenSwitchControllerNacDeviceLastKnownPort(o["last-known-port"], d, "last_known_port", sv)); err != nil {
if !fortiAPIPatch(o["last-known-port"]) {
return fmt.Errorf("Error reading last_known_port: %v", err)
}
}
if err = d.Set("matched_nac_policy", flattenSwitchControllerNacDeviceMatchedNacPolicy(o["matched-nac-policy"], d, "matched_nac_policy", sv)); err != nil {
if !fortiAPIPatch(o["matched-nac-policy"]) {
return fmt.Errorf("Error reading matched_nac_policy: %v", err)
}
}
if err = d.Set("port_policy", flattenSwitchControllerNacDevicePortPolicy(o["port-policy"], d, "port_policy", sv)); err != nil {
if !fortiAPIPatch(o["port-policy"]) {
return fmt.Errorf("Error reading port_policy: %v", err)
}
}
if err = d.Set("mac_policy", flattenSwitchControllerNacDeviceMacPolicy(o["mac-policy"], d, "mac_policy", sv)); err != nil {
if !fortiAPIPatch(o["mac-policy"]) {
return fmt.Errorf("Error reading mac_policy: %v", err)
}
}
if err = d.Set("last_seen", flattenSwitchControllerNacDeviceLastSeen(o["last-seen"], d, "last_seen", sv)); err != nil {
if !fortiAPIPatch(o["last-seen"]) {
return fmt.Errorf("Error reading last_seen: %v", err)
}
}
return nil
}
func flattenSwitchControllerNacDeviceFortiTestDebug(d *schema.ResourceData, fosdebugsn int, fosdebugbeg int, fosdebugend int) {
log.Printf(strconv.Itoa(fosdebugsn))
e := validation.IntBetween(fosdebugbeg, fosdebugend)
log.Printf("ER List: %v, %v", strings.Split("FortiOS Ver", " "), e)
}
func expandSwitchControllerNacDeviceId(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceDescription(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceStatus(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceMac(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceLastKnownSwitch(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceLastKnownPort(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceMatchedNacPolicy(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDevicePortPolicy(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceMacPolicy(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func expandSwitchControllerNacDeviceLastSeen(d *schema.ResourceData, v interface{}, pre string, sv string) (interface{}, error) {
return v, nil
}
func getObjectSwitchControllerNacDevice(d *schema.ResourceData, sv string) (*map[string]interface{}, error) {
obj := make(map[string]interface{})
if v, ok := d.GetOkExists("fosid"); ok {
t, err := expandSwitchControllerNacDeviceId(d, v, "fosid", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["id"] = t
}
}
if v, ok := d.GetOk("description"); ok {
t, err := expandSwitchControllerNacDeviceDescription(d, v, "description", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["description"] = t
}
}
if v, ok := d.GetOk("status"); ok {
t, err := expandSwitchControllerNacDeviceStatus(d, v, "status", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["status"] = t
}
}
if v, ok := d.GetOk("mac"); ok {
t, err := expandSwitchControllerNacDeviceMac(d, v, "mac", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["mac"] = t
}
}
if v, ok := d.GetOk("last_known_switch"); ok {
t, err := expandSwitchControllerNacDeviceLastKnownSwitch(d, v, "last_known_switch", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["last-known-switch"] = t
}
}
if v, ok := d.GetOk("last_known_port"); ok {
t, err := expandSwitchControllerNacDeviceLastKnownPort(d, v, "last_known_port", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["last-known-port"] = t
}
}
if v, ok := d.GetOk("matched_nac_policy"); ok {
t, err := expandSwitchControllerNacDeviceMatchedNacPolicy(d, v, "matched_nac_policy", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["matched-nac-policy"] = t
}
}
if v, ok := d.GetOk("port_policy"); ok {
t, err := expandSwitchControllerNacDevicePortPolicy(d, v, "port_policy", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["port-policy"] = t
}
}
if v, ok := d.GetOk("mac_policy"); ok {
t, err := expandSwitchControllerNacDeviceMacPolicy(d, v, "mac_policy", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["mac-policy"] = t
}
}
if v, ok := d.GetOkExists("last_seen"); ok {
t, err := expandSwitchControllerNacDeviceLastSeen(d, v, "last_seen", sv)
if err != nil {
return &obj, err
} else if t != nil {
obj["last-seen"] = t
}
}
return &obj, nil
}