-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlan.go
150 lines (140 loc) · 3.38 KB
/
vlan.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
package ciscoutils
import (
"encoding/csv"
"os"
"strconv"
)
// Vlan is a struct that you can use to keep some information about a VLAN.
type Vlan struct {
ID int // VLAN ID
Name string // name of VLAN
Tenant string // tenant name
VRF string // name of VRF
Domain string // physical domain
App string // Application associated with Vlan
EPG string // EPG associated with Vlan
}
// NewVlan return a new Vlan with optional default values
func NewVlan() *Vlan {
return new(Vlan)
}
// FindVlanDomain Look through an array of Vlans to find a VLAN inside a domain
func FindVlanDomain(ID int, AllVlans *[]Vlan, domain string) *Vlan {
if AllVlans == nil {
return nil
}
for _, each := range *AllVlans {
if each.ID == ID && each.Domain == domain {
return &each
}
}
return nil
}
// FindVlan Look through an array of VLANs and find first VLAN with an ID, return nil if not found
func FindVlan(ID int, AllVlans []Vlan) *Vlan {
if AllVlans == nil {
return nil
}
for _, each := range AllVlans {
if each.ID == ID {
return &each
}
}
return nil
}
// MakeTestVlans this func just returns a list of VLANs that is used for testing code
func MakeTestVlans() []Vlan {
vl2 := Vlan{2, "VLAN-2-SOMETHING", "TEST-TENANT", "VRF1", "", "", ""}
vl444 := Vlan{4, "STRANGE-VLAN", "TEST-TENANT", "VRF1", "", "", ""}
var AllVlans []Vlan
AllVlans = append(AllVlans, vl2)
AllVlans = append(AllVlans, vl444)
return AllVlans
}
// ReadVlanCsv This func reads a CSV file in the format "vlan,name,tenant,vrf,application,EPG"
func ReadVlanCsv(inputFile string, separator rune, domain string) (vlans []Vlan, err error) {
var AllVlans []Vlan
csvFile, err := os.Open(inputFile)
if err != nil {
return nil, err
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
reader.Comma = separator
reader.FieldsPerRecord = -1
csvData, err := reader.ReadAll()
if err != nil {
return nil, err
}
var oneRecord Vlan
oneRecord.Domain = domain
for _, each := range csvData {
oneRecord.ID, _ = strconv.Atoi(each[0])
oneRecord.Name = each[1]
// check tenant
if len(each) > 2 {
oneRecord.Tenant = each[2]
} else {
oneRecord.Tenant = ""
}
// check for VRF
if len(each) > 3 {
if each[3] == "" {
oneRecord.VRF = "VRF1"
} else {
oneRecord.VRF = each[3]
}
} else {
oneRecord.VRF = ""
}
// check for application
if len(each) > 4 {
oneRecord.App = each[4]
} else {
oneRecord.App = ""
}
// check for EPG
if len(each) > 5 {
oneRecord.EPG = each[5]
} else {
oneRecord.EPG = ""
}
// sanity check
if oneRecord.ID > 0 && oneRecord.ID < 4095 {
AllVlans = append(AllVlans, oneRecord)
}
}
return AllVlans, nil
}
// GetAppName Return the name of the application
func GetAppName(vlan *Vlan, defaultname string) string {
// return the default name if no vlan or no App name
if vlan == nil || vlan.App == "" {
return defaultname
}
return vlan.App
}
// GetEPGName Return the name of the EPG
func GetEPGName(vlan *Vlan) string {
// return no name if no VLAN
if vlan == nil {
return ""
}
// return default name if no name
if vlan.EPG == "" {
return "VL" + strconv.Itoa(vlan.ID) + "-L2"
}
return vlan.EPG
}
// GetVRFName Return the name of the VRF
func GetVRFName(vlan *Vlan) string {
// return no name if no VLAN
if vlan == nil {
return ""
}
// return default name if none specified
if vlan.VRF == "" {
return "VRF1"
}
return vlan.VRF
}