-
Notifications
You must be signed in to change notification settings - Fork 1
/
output-list.sh
executable file
·126 lines (116 loc) · 3.39 KB
/
output-list.sh
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
#!/usr/bin/env bash
set -euo pipefail
edge_url="${EDGE_URL?:missing environment variable: EDGE_URL}"
cookie_jar="$("$(dirname -- "${BASH_SOURCE[0]}")/login.sh" "$edge_url")"
output_format=short
while [[ $# -gt 0 ]]; do
case $1 in
-o|--output)
case "$2" in
short|wide)
output_format="$2"
;;
*)
echo >&2 "$2 is not a valid output method"
exit 1
esac
shift 2
;;
esac
done
parse_admin_status() {
if [ "$1" == 1 ]; then
echo -n on
elif [ "$1" == 0 ]; then
echo -n off
else
echo -n unknown
fi
}
parse_redundancy() {
if [ "$1" == 0 ]; then
echo -n none
elif [ "$1" == 1 ]; then
echo -n failover
elif [ "$1" == 2 ]; then
echo -n active
else
echo -n unknown
fi
}
parse_delay_mode() {
if [ "$1" == 1 ]; then
echo -n On Arrival
elif [ "$1" == 2 ]; then
echo -n On Origin
else
echo -n unknown
fi
}
declare -A groups
# Only the wide format needs the group name
if [ "$output_format" == "wide" ]; then
while read -r group_id group_name ; do
groups["$group_id"]="$group_name"
done < <("$(dirname -- "${BASH_SOURCE[0]}")/group-list.sh")
fi
output_tsv() {
if [ "$output_format" == "short" ]; then
printf "%s\t%s\t%s\n" "ID" "Name" "Health"
elif [ "$output_format" == "wide" ]; then
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" \
"ID" "Name" "Group" "Enabled" "Input" "Redudancy" "Delay" "Delay mode" "Appliances" "Health"
fi
while IFS=$'\t' read -r \
id \
name \
admin_status \
redundancy_mode \
group \
input \
delay \
delay_mode \
health_state \
health_title \
appliances
do
if [ "$health_state" == "allOk" ]; then
health="\e[32m✓\e[0m"
else
health="\e[31m✗\e[0m $health_title"
fi
if [ "$output_format" == "short" ]; then
printf "%s\t%s\t$health\n" "$id" "$name"
elif [ "$output_format" == "wide" ]; then
# We don't resolve input for performance reasons
printf "%s\t%s\t%s\t%s\t%s\t%s\t%sms\t%s\t%s\t$health\n" \
"$id" \
"$name" \
"${groups[$group]}" \
"$(parse_admin_status "$admin_status")" \
"$input" \
"$(parse_redundancy "$redundancy_mode")" \
"$delay" \
"$(parse_delay_mode "$delay_mode")" \
"$appliances"
fi
done < <(curl "$edge_url/api/output/" \
--silent \
--get \
--cookie "$cookie_jar" \
--data 'q={"limit":5000}' \
| jq --raw-output '.items | map([
.id,
.name,
.adminStatus,
.redundancyMode,
.group,
( if .input then .input else "-" end),
.delay,
.delayMode,
.health.state,
( if .health.title == "" then "-" else .health.title end), # read with empty fields and IFS=\t is not supported
(.appliances | map(.name) | join(", "))
])[] | @tsv')
}
output_tsv | column -t -s $'\t'