-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_tool.cue
More file actions
148 lines (128 loc) · 3.52 KB
/
Copy pathmain_tool.cue
File metadata and controls
148 lines (128 loc) · 3.52 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
package main
import (
"tool/cli"
"tool/exec"
"tool/http"
"strings"
"encoding/json"
"encoding/yaml"
"encoding/base64"
"github.com/networkop/cue-networking-II/inventory"
)
command: try: {
for _, dev in inventory.#devices {
(dev.name): {
let vars = hostvars[(dev.name)]
hvars: cli.Print & {
text: "-== hostvars[\(dev.name)] ==-\n" + yaml.Marshal(vars)
}
}
}
}
command: show: {
for _, dev in inventory.#devices {
(dev.name): {
let conf = config[(dev.name)]
if dev.vendor == "Arista1" {
commands: exec.Run & {
cmd: ["j2", "--tests", "jinja/tests/defined.py", "--filters", "jinja/filters/combined.py", "-f", "json", "schemas/arista/eos.conf_original.j2", "-"]
stdin: json.Marshal(conf)
stdout: string
}
split: strings.Split(commands.stdout, "\n")
split_commands: [ for x in split if x != "" {x}]
print: cli.Print & {
text: "-== configs[\(dev.name)] ==-\n" + strings.Join(split_commands, "\n")
}
}
if dev.vendor == "NVIDIA1" {
cfgs: cli.Print & {
text: "-== configs[\(dev.name)] ==-\n" + yaml.Marshal(conf)
}
}
}
}
}
jsonrpc_body: {
jsonrpc: "2.0"
method: "runCmds"
params: {
version: 1
cmds: [...string]
}
}
eapi_wrapper: {
input: [...string]
output: ["enable", "configure"] + input + ["write"]
}
saveNVUE: {
state: "apply"
"auto-prompt": {
ays: "ays_yes"
"ignore_fail": "ignore_fail_yes"
}
}
command: push: {
for _, dev in inventory.#devices {
let auth = base64.Encode(null, "\(dev.user):\(dev.password)")
let conf = config[(dev.name)]
(dev.name): {
if dev.vendor == "Arista1" {
commands: exec.Run & {
cmd: ["j2", "--tests", "jinja/tests/defined.py", "--filters", "jinja/filters/combined.py", "-f", "json", "schemas/arista/eos.conf_original.j2", "-"]
stdin: json.Marshal(conf)
stdout: string
}
split: strings.Split(commands.stdout, "\n")
split_commands: [ for x in split if x != "" {x}]
wrapped_commands: eapi_wrapper & {input: split_commands}
create: http.Post & {
url: "https://\(dev.name):443/command-api"
tls: verify: false
request: {
header: "Authorization": "Basic \(auth)"
body: json.Marshal(jsonrpc_body & {
params: cmds: wrapped_commands.output
id: dev.name
})
}
}
response: cli.Print & {
text: "CREATE RESPONSE \(create.response.body)"
}
}
if dev.vendor == "NVIDIA1" {
createRevision: http.Post & {
url: "https://\(dev.name):8765/nvue_v1/revision"
tls: verify: false
request: header: "Authorization": "Basic \(auth)"
}
revisionID: [ for k, v in json.Unmarshal(createRevision.response.body) {k}]
escapedID: strings.Replace(revisionID[0], "/", "%2F", -1)
patchRevision: http.Do & {
method: "PATCH"
url: "https://\(dev.name):8765/nvue_v1/?rev=\(escapedID)"
tls: verify: false
request: header: "Authorization": "Basic \(auth)"
request: header: "Content-Type": "application/json"
request: body: json.Marshal(conf)
}
checkPatch: cli.Print & {
text: "PATCH RESPONSE: \(patchRevision.response.body)"
}
applyRevision: http.Do & {
$after: patchRevision
method: "PATCH"
url: "https://\(dev.name):8765/nvue_v1/revision/\(escapedID)"
tls: verify: false
request: header: "Authorization": "Basic \(auth)"
request: header: "Content-Type": "application/json"
request: body: json.Marshal(saveNVUE)
}
checkApply: cli.Print & {
text: "APPLY RESPONSE \(applyRevision.response.body)"
}
}
}
}
}