-
Notifications
You must be signed in to change notification settings - Fork 2
/
vlan.go
187 lines (146 loc) · 4.39 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
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
package aoscxgo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
)
type Vlan struct {
// Connection properties.
VlanId int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
AdminState string `json:"admin_state"`
VlanDetails map[string]interface{} `json:"details"`
materialized bool `json:"materialized"`
uri string `json:"uri"`
}
// Create performs POST to create VLAN configuration on the given Client object.
func (v *Vlan) Create(c *Client) error {
base_uri := "system/vlans"
vlan_str := strconv.Itoa(v.VlanId)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
v.uri = "/rest/" + c.Version + "/" + base_uri + "/" + vlan_str
if v.VlanId == 0 || v.Name == "" {
return &RequestError{
StatusCode: "Missing Required Values VlanId & Name",
Err: errors.New("Create Error"),
}
}
postMap := map[string]interface{}{
"id": v.VlanId,
"name": v.Name,
"type": "static", //default value
}
if v.Description != "" {
postMap["description"] = v.Description
}
if v.AdminState != "" {
postMap["admin"] = v.AdminState
}
postBody, _ := json.Marshal(postMap)
json_body := bytes.NewBuffer(postBody)
res := post(c, url, json_body)
if res.Status != "201 Created" {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Create Error"),
}
}
v.materialized = true
return nil
}
// Update performs PATCH to update VLAN configuration on the given Client object.
func (v *Vlan) Update(c *Client) error {
base_uri := "system/vlans"
vlan_str := strconv.Itoa(v.VlanId)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_str
if v.VlanId == 0 || v.Name == "" {
return &RequestError{
StatusCode: "Missing Required Values VlanId & Name",
Err: errors.New("Update Error"),
}
}
patchMap := map[string]interface{}{
"name": v.Name,
"description": v.Description,
"admin": v.AdminState,
"type": "static", //default value
}
patchBody, _ := json.Marshal(patchMap)
json_body := bytes.NewBuffer(patchBody)
res := patch(c, url, json_body)
if res.Status != "204 No Content" {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Update Error"),
}
}
return nil
}
// Delete performs DELETE to remove VLAN configuration from the given Client object.
func (v *Vlan) Delete(c *Client) error {
base_uri := "system/vlans"
// Check if Vlan Interface exists, if so then fail
vlan_interface_id := fmt.Sprintf("vlan%d", v.VlanId)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_interface_id
res, _ := get(c, url)
if res.Status == "200 OK" {
err_str := "VlanInterface " + vlan_interface_id + " exists - delete VlanInterface before deleting Vlan " + vlan_interface_id
return &RequestError{
StatusCode: err_str,
Err: errors.New("Delete Error"),
}
}
vlan_str := strconv.Itoa(v.VlanId)
url = "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_str
res = delete(c, url)
if res.Status != "204 No Content" {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Delete Error"),
}
}
return nil
}
// Get performs GET to retrieve VLAN configuration for the given Client object.
func (v *Vlan) Get(c *Client) error {
base_uri := "system/vlans"
vlan_str := strconv.Itoa(v.VlanId)
v.uri = "/rest/" + c.Version + "/" + base_uri + "/" + vlan_str
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + vlan_str
res, body := get(c, url)
if res.Status != "200 OK" {
v.materialized = false
return &RequestError{
StatusCode: res.Status + url,
Err: errors.New("Retrieval Error"),
}
}
if v.VlanDetails == nil {
v.VlanDetails = map[string]interface{}{}
}
for key, value := range body {
v.VlanDetails[key] = value
if key == "name" && value != nil {
v.Name = value.(string)
}
if key == "description" && value != nil {
v.Description = value.(string)
}
if key == "admin" {
v.AdminState = value.(string)
}
}
v.materialized = true
return nil
}
// GetStatus returns True if VLAN exists on Client object or False if not.
func (v *Vlan) GetStatus() bool {
return v.materialized
}
// GetURI returns URI of VLAN.
func (v *Vlan) GetURI() string {
return v.uri
}