/
fetch_tool.cue
61 lines (50 loc) · 1.17 KB
/
fetch_tool.cue
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
package main
import (
"list"
"tool/cli"
"tool/exec"
"text/template"
"tool/file"
"tool/http"
"encoding/json"
"encoding/yaml"
"github.com/networkop/cue-networking-II/inventory"
)
gqlQuery: file.Read & {
filename: "query.gql"
contents: string
}
command: fetch: {
for _, dev in inventory.#devices {
(dev.name): {
gqlRequest: http.Post & {
url: inventory.ipam.url + "/graphql/"
request: inventory.ipam.headers & {
body: json.Marshal({
query: template.Execute(gqlQuery.contents, {name: dev.name})
})
}
}
log: cli.Print & {
text: "gql response: \(gqlRequest.response.body)"
}
response: json.Unmarshal(gqlRequest.response.body)
if list.MinItems(response.data.devices, 1) {
let device = response.data.devices[0]
let dirName = "config/\(device.device_role.name)/\(device.name)"
dirs: file.MkdirAll & {
path: "\(dirName)"
}
input: file.Create & {
filename: "\(dirName)/\(device.name).yml"
contents: yaml.Marshal(device)
}
gen: exec.Run & {
cmd: "cue import -p main -f -l \"hostvars\" -l name \(device.name).yml"
dir: "\(dirName)"
$after: input
}
}
}
}
}