-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterface.go
More file actions
213 lines (160 loc) · 4.78 KB
/
interface.go
File metadata and controls
213 lines (160 loc) · 4.78 KB
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
package aoscxgo
import (
"bytes"
"encoding/json"
"errors"
"net/url"
"regexp"
)
type Interface struct {
// Connection properties.
Name string `json:"name"`
Description string `json:"description"`
AdminState string `json:"admin"`
InterfaceDetails map[string]interface{} `json:"details"`
materialized bool `json:"materialized"`
uri string `json:"uri"`
}
// checkName validates if interface Name is valid or not
func checkName(name string) bool {
re := "\\d+/\\d+/\\d+"
found, err := regexp.MatchString(re, name)
if found && err == nil {
return true
}
return false
}
// checkValues validates if interface Name and AdminState are valid or not
func (i *Interface) checkValues() error {
if !checkName(i.Name) {
return &RequestError{
StatusCode: "Invalid Required Value: Name",
Err: errors.New("Create Error"),
}
}
status_str := "Invalid Required Value: AdminState - valid options are 'up' or 'down' received: " + i.AdminState
if i.AdminState != "down" && i.AdminState != "up" {
return &RequestError{
StatusCode: status_str,
Err: errors.New("Create Error"),
}
}
return nil
}
// Create performs POST to create Interface configuration on the given Client object.
func (i *Interface) Create(c *Client) error {
base_uri := "system/interfaces"
url_str := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
int_str := url.PathEscape(i.Name)
i.uri = "/rest/" + c.Version + "/" + base_uri + "/" + int_str
err := i.checkValues()
if err != nil {
return err
}
postMap := map[string]interface{}{
"name": i.Name,
"description": i.Description,
"admin": i.AdminState,
}
if i.AdminState == "down" {
postMap["user_config"] = map[string]interface{}{
"admin": "down"}
} else if i.AdminState == "up" {
postMap["user_config"] = map[string]interface{}{
"admin": "up"}
}
postBody, _ := json.Marshal(postMap)
json_body := bytes.NewBuffer(postBody)
res := post(c, url_str, json_body)
if res.Status != "201 Created" {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Create Error"),
}
}
i.materialized = true
return nil
}
// Update performs PATCH to update Interface configuration on the given Client object.
func (i *Interface) Update(c *Client) error {
base_uri := "system/interfaces"
err := i.checkValues()
if err != nil {
return err
}
int_str := url.PathEscape(i.Name)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + int_str
patchMap := map[string]interface{}{
"description": i.Description,
"admin": i.AdminState,
}
if i.AdminState == "down" {
patchMap["user_config"] = map[string]interface{}{
"admin": "down"}
}
if i.AdminState == "up" {
patchMap["user_config"] = map[string]interface{}{
"admin": "up"}
}
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 PUT to remove/default Interface configuration from the given Client object.
func (i *Interface) Delete(c *Client) error {
base_uri := "system/interfaces"
int_str := url.PathEscape(i.Name)
putMap := map[string]interface{}{}
putBody, _ := json.Marshal(putMap)
json_body := bytes.NewBuffer(putBody)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + int_str
//res := delete(c, url)
//need logic for handling interfaces between platforms
res := put(c, url, json_body)
if res.Status != "204 No Content" && res.Status != "200 OK" {
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Delete Error"),
}
}
return nil
}
// Get performs GET to retrieve Interface configuration from the given Client object.
func (i *Interface) Get(c *Client) error {
base_uri := "system/interfaces"
int_str := url.PathEscape(i.Name)
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri + "/" + int_str + ""
res, body := get(c, url)
if res.Status != "200 OK" {
i.materialized = false
return &RequestError{
StatusCode: res.Status,
Err: errors.New("Retrieval Error"),
}
}
if i.InterfaceDetails == nil {
i.InterfaceDetails = map[string]interface{}{}
}
for key, value := range body {
i.InterfaceDetails[key] = value
if key == "description" && value != nil {
i.Description = value.(string)
}
if key == "admin" && value != nil {
i.AdminState = value.(string)
}
}
i.materialized = true
return nil
}
// GetStatus returns True if Interface exists on Client object or False if not.
func (i *Interface) GetStatus() bool {
return i.materialized
}