|
| 1 | +package aoscxgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | +) |
| 14 | + |
| 15 | +type FullConfig struct { |
| 16 | + |
| 17 | + // Connection properties. |
| 18 | + FileName string `json:"filename"` |
| 19 | + //Hash hash.Hash `json:"hash"` |
| 20 | + Config string `json:"config"` |
| 21 | + uri string `json:"uri"` |
| 22 | +} |
| 23 | + |
| 24 | +// Create performs POST to create VLAN configuration on the given Client object. |
| 25 | +func (fc *FullConfig) Create(c *Client) (*http.Response, error) { |
| 26 | + if fc.FileName == "" { |
| 27 | + return nil, &RequestError{ |
| 28 | + StatusCode: "Missing FileName", |
| 29 | + Err: errors.New("Create Error"), |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + config_str, err := fc.ReadConfigFile(fc.FileName) |
| 34 | + |
| 35 | + if err != nil { |
| 36 | + return nil, &RequestError{ |
| 37 | + StatusCode: "Error in reading config file", |
| 38 | + Err: err, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + res, body := fc.ValidateConfig(c, config_str) |
| 43 | + |
| 44 | + if body == nil { |
| 45 | + return res, &RequestError{ |
| 46 | + StatusCode: res.Status, |
| 47 | + Err: errors.New("Validation Error"), |
| 48 | + } |
| 49 | + |
| 50 | + } else if body["state"] == "success" { |
| 51 | + res2, body2 := fc.ApplyConfig(c, config_str) |
| 52 | + if body2["state"] != "success" { |
| 53 | + errors_dict := body2["errors"].([]interface{}) |
| 54 | + error_str := convert_errors(errors_dict) |
| 55 | + |
| 56 | + return res2, &RequestError{ |
| 57 | + StatusCode: "Error in applying config error : \n" + error_str, |
| 58 | + Err: errors.New("Apply Error"), |
| 59 | + } |
| 60 | + } else { |
| 61 | + log.Println("New Config Applied Successfully") |
| 62 | + fc.Get(c) |
| 63 | + return res2, nil |
| 64 | + } |
| 65 | + } else if res != nil && body != nil { |
| 66 | + errors_dict := body["errors"].([]interface{}) |
| 67 | + errors_dict = errors_dict |
| 68 | + error_str := convert_errors(errors_dict) |
| 69 | + |
| 70 | + return res, &RequestError{ |
| 71 | + StatusCode: "Error in validating config error : \n" + error_str, |
| 72 | + Err: errors.New("Apply Error"), |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + return res, &RequestError{ |
| 78 | + StatusCode: res.Status, |
| 79 | + Err: errors.New("Validation Error"), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Get performs GET to retrieve Running configuration for the given Client object. |
| 84 | +func (fc *FullConfig) Get(c *Client) error { |
| 85 | + base_uri := "configs/running-config" |
| 86 | + url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri |
| 87 | + res, _ := get_accept_text(c, url) |
| 88 | + |
| 89 | + if res.Status != "200 OK" { |
| 90 | + return &RequestError{ |
| 91 | + StatusCode: res.Status + url, |
| 92 | + Err: errors.New("Retrieval Error"), |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Read the content |
| 97 | + var bodyBytes []byte |
| 98 | + if res.Body != nil { |
| 99 | + bodyBytes, _ = ioutil.ReadAll(res.Body) |
| 100 | + } |
| 101 | + // Restore the io.ReadCloser to its original state |
| 102 | + res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) |
| 103 | + // Use the content |
| 104 | + bodyString := string(bodyBytes) |
| 105 | + |
| 106 | + // config := bytes.NewBuffer(nil) |
| 107 | + // config, _ = ioutil.ReadAll(res.Body) |
| 108 | + fc.Config = bodyString |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func (fc *FullConfig) ReadConfigFile(filename string) (string, error) { |
| 114 | + config_contents, err := ioutil.ReadFile(filename) |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + err_str := "Unable to read file " + filename |
| 118 | + return "", &RequestError{ |
| 119 | + StatusCode: err_str, |
| 120 | + Err: err, |
| 121 | + } |
| 122 | + } |
| 123 | + config_str := string(config_contents) |
| 124 | + return config_str, nil |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +// Formats the errors provided by dryrun for user |
| 129 | +func convert_errors(errors_list []interface{}) string { |
| 130 | + errors_str := "" |
| 131 | + for _, error_dict := range errors_list { |
| 132 | + tmp_dict := error_dict.(map[string]interface{}) |
| 133 | + line_num := tmp_dict["line"] |
| 134 | + line_float := line_num.(float64) |
| 135 | + line_int := int(line_float) |
| 136 | + errors_str += "line " |
| 137 | + line_num_str := fmt.Sprintf("%d", line_int) |
| 138 | + errors_str += line_num_str |
| 139 | + errors_str += " | " |
| 140 | + errors_str += tmp_dict["message"].(string) |
| 141 | + errors_str += "\n" |
| 142 | + } |
| 143 | + return errors_str |
| 144 | +} |
| 145 | + |
| 146 | +// Sets Config Attribute |
| 147 | +func (fc *FullConfig) SetConfig(config string) error { |
| 148 | + fc.Config = config |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +// Compares supplied string Config to stored Object |
| 154 | +func (fc *FullConfig) CompareConfig(new_config string) string { |
| 155 | + |
| 156 | + return cmp.Diff(fc.Config, new_config) |
| 157 | +} |
| 158 | + |
| 159 | +// Compares supplied string Config to stored Object |
| 160 | +func (fc *FullConfig) DownloadConfig(c *Client, filename string) error { |
| 161 | + base_uri := "configs/running-config" |
| 162 | + url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri |
| 163 | + res, _ := get_accept_text(c, url) |
| 164 | + |
| 165 | + if res.Status != "200 OK" { |
| 166 | + return &RequestError{ |
| 167 | + StatusCode: res.Status + url, |
| 168 | + Err: errors.New("Retrieval Error"), |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Read the content |
| 173 | + var bodyBytes []byte |
| 174 | + if res.Body != nil { |
| 175 | + bodyBytes, _ = ioutil.ReadAll(res.Body) |
| 176 | + } |
| 177 | + // Restore the io.ReadCloser to its original state |
| 178 | + res.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) |
| 179 | + // Use the content |
| 180 | + bodyString := string(bodyBytes) |
| 181 | + |
| 182 | + err := ioutil.WriteFile(filename, []byte(bodyString), 0644) |
| 183 | + if err != nil { |
| 184 | + panic(err) |
| 185 | + } |
| 186 | + |
| 187 | + return err |
| 188 | +} |
| 189 | + |
| 190 | +// Validates supplied CLI configuration as string using dryrun |
| 191 | +func (fc *FullConfig) ValidateConfig(c *Client, config string) (*http.Response, map[string]interface{}) { |
| 192 | + base_uri := "configs/running-config" |
| 193 | + url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri |
| 194 | + dryrun_url := url + "?dryrun=validate" |
| 195 | + |
| 196 | + json_body := bytes.NewBufferString(config) |
| 197 | + |
| 198 | + res := post(c, dryrun_url, json_body) |
| 199 | + |
| 200 | + if res.Status != "200 OK" && res.Status != "202 Accepted" { |
| 201 | + return res, nil |
| 202 | + } |
| 203 | + |
| 204 | + dryrun_url = url + "?dryrun" |
| 205 | + |
| 206 | + res2, body := get(c, dryrun_url) |
| 207 | + |
| 208 | + iterations := 10 |
| 209 | + |
| 210 | + for iterations > 0 { |
| 211 | + iterations -= 1 |
| 212 | + if body["state"] == "success" || body["state"] == "error" { |
| 213 | + break |
| 214 | + } |
| 215 | + time.Sleep(2 * time.Second) |
| 216 | + res2, body = get(c, dryrun_url) |
| 217 | + } |
| 218 | + |
| 219 | + return res2, body |
| 220 | +} |
| 221 | + |
| 222 | +func (fc *FullConfig) ApplyConfig(c *Client, config string) (*http.Response, map[string]interface{}) { |
| 223 | + base_uri := "configs/running-config" |
| 224 | + url := "https://" + c.Hostname + "/rest/" + c.Version + "/" + base_uri |
| 225 | + dryrun_url := url + "?dryrun=apply" |
| 226 | + |
| 227 | + json_body := bytes.NewBufferString(config) |
| 228 | + |
| 229 | + res := post(c, dryrun_url, json_body) |
| 230 | + |
| 231 | + if res.Status != "200 OK" && res.Status != "202 Accepted" { |
| 232 | + return res, nil |
| 233 | + } |
| 234 | + |
| 235 | + dryrun_url = url + "?dryrun" |
| 236 | + |
| 237 | + res2, body := get(c, dryrun_url) |
| 238 | + |
| 239 | + iterations := 10 |
| 240 | + |
| 241 | + for iterations > 0 { |
| 242 | + iterations -= 1 |
| 243 | + if body["state"] == "success" || body["state"] == "error" { |
| 244 | + break |
| 245 | + } |
| 246 | + time.Sleep(2 * time.Second) |
| 247 | + res2, body = get(c, dryrun_url) |
| 248 | + } |
| 249 | + |
| 250 | + return res2, body |
| 251 | +} |
0 commit comments