-
Notifications
You must be signed in to change notification settings - Fork 2
/
vlan_interface.go
569 lines (474 loc) · 15.4 KB
/
vlan_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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package aoscxgo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"golang.org/x/exp/slices"
)
type VlanInterface struct {
// Connection properties.
Vlan Vlan `json:"vlan"`
Description string `json:"description"`
Ipv4 []interface{} `json:"ipv4"`
Ipv6 []interface{} `json:"ipv6"`
Vrf string `json:"vrf"`
InterfaceDetails map[string]interface{} `json:"details"`
materialized bool `json:"materialized"`
}
// Create performs POST to create VlanInterface configuration on the given Client object.
func (v *VlanInterface) Create(c *Client) error {
base_uri := "system/interfaces"
vlan_interface_id := fmt.Sprintf("vlan%d", v.Vlan.VlanId)
postMap := map[string]interface{}{}
if v.Vlan.VlanId == 0 {
return &RequestError{
StatusCode: "Missing Required Values VlanId",
Err: errors.New("Create Error"),
}
}
// Retrieve VLAN from sw if existing
tmp_vlan := Vlan{
VlanId: v.Vlan.VlanId,
}
err := tmp_vlan.Get(c)
if err != nil {
return &RequestError{
StatusCode: fmt.Sprintf("Missing VLAN %d - Create Vlan before VlanInterface", v.Vlan.VlanId),
Err: errors.New("Create Error"),
}
}
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
postMap["description"] = v.Description
postMap["admin"] = v.Vlan.AdminState
postMap["name"] = vlan_interface_id
postMap["type"] = "vlan"
postMap["interfaces"] = []string{fmt.Sprintf("/rest/%s/system/vlans/%s", c.Version, strconv.Itoa(v.Vlan.VlanId))}
if v.Vrf == "" {
postMap["vrf"] = "/rest/" + c.Version + "/system/vrfs/" + "default"
} else {
postMap["vrf"] = "/rest/" + c.Version + "/system/vrfs/" + v.Vrf
}
//check if it's ipv6
// Validate ipv4 address
if len(v.Ipv4) == 0 {
postMap["ip4_address"] = nil
postMap["ip4_address_secondary"] = nil
} else if len(v.Ipv4) == 1 {
str_ipv4_1 := fmt.Sprintf("%v", v.Ipv4[0])
if checkIPAddress(str_ipv4_1) {
postMap["ip4_address"] = str_ipv4_1
postMap["ip4_address_secondary"] = nil
} else {
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in same ipv4 format: " + str_ipv4_1
return &RequestError{
StatusCode: status_str,
Err: errors.New("Create Error"),
}
}
} else if len(v.Ipv4) > 1 {
str_ipv4_1 := fmt.Sprintf("%v", v.Ipv4[0])
if checkIPAddress(str_ipv4_1) {
postMap["ip4_address"] = v.Ipv4[0]
} else {
fmt.Println("THIS IS THE VALUE vvv")
fmt.Println(str_ipv4_1)
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in ipv4 format: " + str_ipv4_1
return &RequestError{
StatusCode: status_str,
Err: errors.New("Create Error"),
}
}
var tmp_splice []string
for index := 1; index < len(v.Ipv4); index++ {
str_ipv4_tmp := fmt.Sprintf("%v", v.Ipv4[index])
fmt.Println("THIS IS THE VALUE vvv")
fmt.Println(str_ipv4_tmp)
fmt.Println(v.Ipv4)
if checkIPAddress(str_ipv4_tmp) {
tmp_splice = append(tmp_splice, str_ipv4_tmp)
} else {
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in ipv4 format: " + str_ipv4_tmp
return &RequestError{
StatusCode: status_str,
Err: errors.New("Create Error"),
}
}
}
postMap["ip4_address_secondary"] = tmp_splice
}
if v.Vlan.AdminState == "down" || v.Vlan.AdminState == "" {
postMap["user_config"] = map[string]interface{}{
"admin": "down"}
} else if v.Vlan.AdminState == "up" {
postMap["user_config"] = map[string]interface{}{
"admin": "up"}
}
postBody, _ := json.Marshal(postMap)
json_body := bytes.NewBuffer(postBody)
res := post(c, url, json_body)
if res.StatusCode != http.StatusCreated {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Create Error"),
}
}
// string to track ipv6 Create success
failed_ipv6 := ""
if len(v.Ipv6) == 0 {
// What are default values when no ipv6 but routing enabled
postMap["ip6_addresses"] = nil
} else if len(v.Ipv6) > 0 {
// two values for ipv6? how is that affected
// first create POST to add an ipv6 address Object
//
for _, ip_address := range v.Ipv6 {
if _, ok := ip_address.(string); ok {
str_ipv6 := fmt.Sprintf("%v", ip_address)
if checkIPAddress(str_ipv6) {
ipv6Map := map[string]interface{}{}
ipv6Map["address"] = str_ipv6
ipv6Map["type"] = "global-unicast"
ipv6Map["preferred_lifetime"] = 604800
ipv6Map["valid_lifetime"] = 2592000
ipv6Map["node_address"] = true
ipv6Map["ra_prefix"] = true
ipv6Map["ra_route"] = false
ipv6body, _ := json.Marshal(ipv6Map)
json_body := bytes.NewBuffer(ipv6body)
ip6_url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id + "/" + "ip6_addresses"
res := post(c, ip6_url, json_body)
if res.StatusCode != http.StatusCreated {
failed_ipv6 = fmt.Sprintf("\nip6_addresses failed to create %v\nstatus code %v", v.Ipv6, res.Status)
return &RequestError{
StatusCode: failed_ipv6,
Err: errors.New("Create Error"),
}
}
} else {
status_str := "Invalid Required Value: Ipv6 - ensure addresses are in ipv6 address/mask format:" +
ip_address.(string)
return &RequestError{
StatusCode: status_str,
Err: errors.New("Create Error"),
}
}
}
}
}
v.materialized = true
return nil
}
// Update performs PATCH or PUT to update VlanInterface configuration on the given Client object.
func (v *VlanInterface) Update(c *Client, use_put bool) error {
base_uri := "system/interfaces"
updateMap := map[string]interface{}{}
vlan_interface_id := fmt.Sprintf("vlan%d", v.Vlan.VlanId)
tmp_vlan_int := VlanInterface{Vlan: v.Vlan}
if use_put {
err := tmp_vlan_int.Get(c)
if err != nil {
error_str := "Missing VlanInterface - " + vlan_interface_id
return &RequestError{
StatusCode: error_str,
Err: errors.New("Update Error"),
}
}
for key, value := range tmp_vlan_int.InterfaceDetails {
updateMap[key] = value
}
}
if v.Vrf == "" {
updateMap["vrf"] = "/rest/" + c.Version + "/system/vrfs/" + "default"
} else {
updateMap["vrf"] = "/rest/" + c.Version + "/system/vrfs/" + v.Vrf
}
//check if it's ipv6
// Validate ipv4 address
if len(v.Ipv4) == 0 {
updateMap["ip4_address"] = nil
updateMap["ip4_address_secondary"] = nil
} else if len(v.Ipv4) == 1 {
str_ipv4_1 := fmt.Sprintf("%v", v.Ipv4[0])
if checkIPAddress(str_ipv4_1) {
updateMap["ip4_address"] = str_ipv4_1
updateMap["ip4_address_secondary"] = nil
} else {
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in poop ipv4 format: " + str_ipv4_1
return &RequestError{
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
} else if len(v.Ipv4) > 1 {
str_ipv4_1 := fmt.Sprintf("%v", v.Ipv4[0])
if checkIPAddress(str_ipv4_1) {
updateMap["ip4_address"] = v.Ipv4[0]
} else {
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in butt ipv4 format: \n" + str_ipv4_1
return &RequestError{
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
var tmp_splice []string
for index := 1; index < len(v.Ipv4); index++ {
str_ipv4_tmp := fmt.Sprintf("%v", v.Ipv4[index])
fmt.Println("THIS IS THE VALUE vvv")
fmt.Println(str_ipv4_tmp)
fmt.Println(v.Ipv4)
if checkIPAddress(str_ipv4_tmp) {
tmp_splice = append(tmp_splice, str_ipv4_tmp)
} else {
status_str := "Invalid Required Value: Ipv4 - ensure addresses are in ipv4 fdafdas format: " + str_ipv4_tmp
return &RequestError{
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
}
updateMap["ip4_address_secondary"] = tmp_splice
}
if len(v.Ipv6) == 0 {
// What are default values when no ipv6 but routing enabled
updateMap["ip6_addresses"] = nil
// retrieve what's existing on the switch and remove
ip6_url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id + "/" + "ip6_addresses"
res, body := get(c, ip6_url)
if res.StatusCode != http.StatusOK {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Retrieval Error"),
}
}
for key, _ := range body {
tmp_ip6_str := url.QueryEscape(key)
del_ip6_url := ip6_url + "/" + tmp_ip6_str
res := delete(c, del_ip6_url)
if res.StatusCode != http.StatusNoContent {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Delete Error"),
}
}
}
} else if len(v.Ipv6) > 0 {
// two values for ipv6? how is that affected
// first create POST to add an ipv6 address Object
//
ip6_url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id + "/" + "ip6_addresses"
// execute GET to retrieve current IPs
// iterate over the list of IPs
// delete the ones that aren't provided
res, body := get(c, ip6_url)
if res.StatusCode != http.StatusOK {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Retrieval Error"),
}
}
var ipv6_slice []string
// convert i.Ipv6 from []interface{} to []string
for _, ip_address := range v.Ipv6 {
if _, ok := ip_address.(string); ok {
ipv6_slice = append(ipv6_slice, ip_address.(string))
}
}
var get_ipv6_slice []string
for key, _ := range body {
if !slices.Contains(ipv6_slice, key) {
tmp_ip6_str := url.QueryEscape(key)
del_ip6_url := ip6_url + "/" + tmp_ip6_str
res := delete(c, del_ip6_url)
if res.StatusCode != http.StatusNoContent {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Delete Error"),
}
}
} else {
get_ipv6_slice = append(get_ipv6_slice, key)
}
}
for _, str_ipv6 := range ipv6_slice {
// Only POST ipv6 addresses not existing
if checkIPAddress(str_ipv6) && !slices.Contains(get_ipv6_slice, str_ipv6) {
ipv6Map := map[string]interface{}{}
ipv6Map["address"] = str_ipv6
ipv6Map["type"] = "global-unicast"
ipv6Map["preferred_lifetime"] = 604800
ipv6Map["valid_lifetime"] = 2592000
ipv6Map["node_address"] = true
ipv6Map["ra_prefix"] = true
ipv6Map["ra_route"] = false
ipv6body, _ := json.Marshal(ipv6Map)
json_body := bytes.NewBuffer(ipv6body)
res := post(c, ip6_url, json_body)
// include logic to check if address is existing?
if res.StatusCode != http.StatusCreated {
status_str := "ip6_addresses failed to update " +
str_ipv6 + " status " + res.Status
return &RequestError{
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
} else if !checkIPAddress(str_ipv6) {
status_str := "Invalid Required Value: Ipv6 - ensure addresses are in ipv6 address/mask format:" +
str_ipv6
return &RequestError{
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
}
}
if v.Vlan.AdminState == "down" || v.Vlan.AdminState == "" {
updateMap["user_config"] = map[string]interface{}{
"admin": "down"}
} else if v.Vlan.AdminState == "up" {
updateMap["user_config"] = map[string]interface{}{
"admin": "up"}
}
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id
updateMap["description"] = v.Description
updateMap["admin"] = v.Vlan.AdminState
updateMap["routing"] = true
updateMap["vlan_mode"] = nil
updateMap["vlan_tag"] = nil
if v.Vlan.AdminState == "down" {
updateMap["user_config"] = map[string]interface{}{
"admin": "down"}
}
if v.Vlan.AdminState == "up" {
updateMap["user_config"] = map[string]interface{}{
"admin": "up"}
}
updateBody, _ := json.Marshal(updateMap)
json_body := bytes.NewBuffer(updateBody)
if use_put {
res := put(c, url, json_body)
if res.StatusCode != http.StatusOK {
status_str := url + "||" + string(updateBody) + "PUT status code = " + res.Status
return &RequestError{
//StatusCode: res.Status,
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
} else {
res := patch(c, url, json_body)
if res.StatusCode != http.StatusNoContent {
status_str := string(updateBody) + "PATCH status code = " + res.Status
return &RequestError{
//StatusCode: res.Status,
StatusCode: status_str,
Err: errors.New("Update Error"),
}
}
}
v.materialized = true
return nil
}
// Delete performs DELETE to remove VlanInterface configuration from the given Client object.
func (v *VlanInterface) Delete(c *Client) error {
base_uri := "system/interfaces"
if v.Vlan.VlanId == 0 {
return &RequestError{
StatusCode: "Missing VlanId unable to configure VlanInterface",
Err: errors.New("Delete Error"),
}
}
vlan_interface_id := fmt.Sprintf("vlan%d", v.Vlan.VlanId)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id
res := delete(c, url)
if res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusOK {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Delete Error"),
}
}
return nil
}
// Get performs GET to retrieve VlanInterface configuration from the given Client object.
func (v *VlanInterface) Get(c *Client) error {
base_uri := "system/interfaces"
if v.Vlan.VlanId == 0 {
return &RequestError{
StatusCode: "Missing VlanId unable to configure VlanInterface",
Err: errors.New("Get Error"),
}
}
vlan_interface_id := fmt.Sprintf("vlan%d", v.Vlan.VlanId)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id + "?selector=writable"
res, body := get(c, url)
if res.StatusCode != http.StatusOK || len(body) <= 1 {
v.materialized = false
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Retrieval Error"),
}
}
if v.InterfaceDetails == nil {
v.InterfaceDetails = map[string]interface{}{}
}
for key, value := range body {
v.InterfaceDetails[key] = value
if key == "description" && value != nil {
v.Description = value.(string)
v.Vlan.Description = value.(string)
}
if key == "admin" && value != nil {
v.Vlan.AdminState = value.(string)
}
if key == "ip4_address" && value != nil {
var tmp_splice []interface{}
tmp_splice = append(tmp_splice, value.(string))
if val, ok := body["ip4_address_secondary"]; ok {
tmp_addr := val.([]interface{})
if len(tmp_addr) > 0 {
for index := 0; index < len(tmp_addr); index++ {
tmp_splice = append(tmp_splice, tmp_addr[index])
}
}
}
v.Ipv4 = tmp_splice
}
if key == "vrf" && value != nil {
for key, _ := range value.(map[string]interface{}) {
v.Vrf = key
}
}
}
// Include a GET for ip6 and populate .ipv6 attribute
ip6_url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id + "/" + "ip6_addresses"
res, body = get(c, ip6_url)
if res.StatusCode != http.StatusOK {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Retrieval Error"),
}
}
var ipv6_slice []string
for key, _ := range body {
if key != "" {
ipv6_slice = append(ipv6_slice, key)
}
}
ip6_addresses := make([]interface{}, len(ipv6_slice))
if len(ipv6_slice) > 0 {
for index := 0; index < len(ipv6_slice); index++ {
ip6_addresses[index] = ipv6_slice[index]
}
}
v.Ipv6 = ip6_addresses
v.materialized = true
return nil
}
// GetStatus returns True if VlanInterface exists on Client object or False if not.
func (i *VlanInterface) GetStatus() bool {
return i.materialized
}