-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfull_config.go
251 lines (201 loc) · 5.9 KB
/
full_config.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
package aoscxgo
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/google/go-cmp/cmp"
)
type FullConfig struct {
// Connection properties.
FileName string `json:"filename"`
//Hash hash.Hash `json:"hash"`
Config string `json:"config"`
uri string `json:"uri"`
}
// Create performs POST to create VLAN configuration on the given Client object.
func (fc *FullConfig) Create(c *Client) (*http.Response, error) {
if fc.FileName == "" {
return nil, &RequestError{
StatusCode: "Missing FileName",
Err: errors.New("Create Error"),
}
}
config_str, err := fc.ReadConfigFile(fc.FileName)
if err != nil {
return nil, &RequestError{
StatusCode: "Error in reading config file",
Err: err,
}
}
res, body := fc.ValidateConfig(c, config_str)
if body == nil {
return res, &RequestError{
StatusCode: res.Status,
Err: errors.New("Validation Error"),
}
} else if body["state"] == "success" {
res2, body2 := fc.ApplyConfig(c, config_str)
if body2["state"] != "success" {
errors_dict := body2["errors"].([]interface{})
error_str := convert_errors(errors_dict)
return res2, &RequestError{
StatusCode: "Error in applying config error : \n" + error_str,
Err: errors.New("Apply Error"),
}
} else {
log.Println("New Config Applied Successfully")
fc.Get(c)
return res2, nil
}
} else if res != nil && body != nil {
errors_dict := body["errors"].([]interface{})
errors_dict = errors_dict
error_str := convert_errors(errors_dict)
return res, &RequestError{
StatusCode: "Error in validating config error : \n" + error_str,
Err: errors.New("Apply Error"),
}
}
return res, &RequestError{
StatusCode: res.Status,
Err: errors.New("Validation Error"),
}
}
// Get performs GET to retrieve Running configuration for the given Client object.
func (fc *FullConfig) Get(c *Client) error {
base_uri := "configs/running-config"
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
res, _ := get_accept_text(c, url)
if res.Status != "200 OK" {
return &RequestError{
StatusCode: res.Status + url,
Err: errors.New("Retrieval Error"),
}
}
// Read the content
var bodyBytes []byte
if res.Body != nil {
bodyBytes, _ = ioutil.ReadAll(res.Body)
}
// Restore the io.ReadCloser to its original state
res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// Use the content
bodyString := string(bodyBytes)
// config := bytes.NewBuffer(nil)
// config, _ = ioutil.ReadAll(res.Body)
fc.Config = bodyString
return nil
}
func (fc *FullConfig) ReadConfigFile(filename string) (string, error) {
config_contents, err := ioutil.ReadFile(filename)
if err != nil {
err_str := "Unable to read file " + filename
return "", &RequestError{
StatusCode: err_str,
Err: err,
}
}
config_str := string(config_contents)
return config_str, nil
}
// Formats the errors provided by dryrun for user
func convert_errors(errors_list []interface{}) string {
errors_str := ""
for _, error_dict := range errors_list {
tmp_dict := error_dict.(map[string]interface{})
line_num := tmp_dict["line"]
line_float := line_num.(float64)
line_int := int(line_float)
errors_str += "line "
line_num_str := fmt.Sprintf("%d", line_int)
errors_str += line_num_str
errors_str += " | "
errors_str += tmp_dict["message"].(string)
errors_str += "\n"
}
return errors_str
}
// Sets Config Attribute
func (fc *FullConfig) SetConfig(config string) error {
fc.Config = config
return nil
}
// Compares supplied string Config to stored Object
func (fc *FullConfig) CompareConfig(new_config string) string {
return cmp.Diff(fc.Config, new_config)
}
// Compares supplied string Config to stored Object
func (fc *FullConfig) DownloadConfig(c *Client, filename string) error {
base_uri := "configs/running-config"
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
res, _ := get_accept_text(c, url)
if res.Status != "200 OK" {
return &RequestError{
StatusCode: res.Status + url,
Err: errors.New("Retrieval Error"),
}
}
// Read the content
var bodyBytes []byte
if res.Body != nil {
bodyBytes, _ = ioutil.ReadAll(res.Body)
}
// Restore the io.ReadCloser to its original state
res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// Use the content
bodyString := string(bodyBytes)
err := ioutil.WriteFile(filename, []byte(bodyString), 0644)
if err != nil {
panic(err)
}
return err
}
// Validates supplied CLI configuration as string using dryrun
func (fc *FullConfig) ValidateConfig(c *Client, config string) (*http.Response, map[string]interface{}) {
base_uri := "configs/running-config"
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
dryrun_url := url + "?dryrun=validate"
json_body := bytes.NewBufferString(config)
res := post(c, dryrun_url, json_body)
if res.Status != "200 OK" && res.Status != "202 Accepted" {
return res, nil
}
dryrun_url = url + "?dryrun"
res2, body := get(c, dryrun_url)
iterations := 10
for iterations > 0 {
iterations -= 1
if body["state"] == "success" || body["state"] == "error" {
break
}
time.Sleep(2 * time.Second)
res2, body = get(c, dryrun_url)
}
return res2, body
}
func (fc *FullConfig) ApplyConfig(c *Client, config string) (*http.Response, map[string]interface{}) {
base_uri := "configs/running-config"
url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri
dryrun_url := url + "?dryrun=apply"
json_body := bytes.NewBufferString(config)
res := post(c, dryrun_url, json_body)
if res.Status != "200 OK" && res.Status != "202 Accepted" {
return res, nil
}
dryrun_url = url + "?dryrun"
res2, body := get(c, dryrun_url)
iterations := 10
for iterations > 0 {
iterations -= 1
if body["state"] == "success" || body["state"] == "error" {
break
}
time.Sleep(2 * time.Second)
res2, body = get(c, dryrun_url)
}
return res2, body
}