-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryPanelMigration.sh
53 lines (38 loc) · 1.61 KB
/
LibraryPanelMigration.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
#!/bin/bash
format=$(getopt -n "$0" -l "sourceUrl:,sourceToken:,targetUrl:,targetToken:" -- -- -- -- "$@")
if [ $# -lt 4 ]; then
echo "Wrong number of arguments are passed."
exit
fi
eval set -- "$format"
while [ $# -gt 0 ]
do
case "$1" in
--sourceUrl) Grafana_Source_Url="$2"; shift;;
--sourceToken) Grafana_Source_Token="$2"; shift;;
--targetUrl) Grafana_Target_Url="$2"; shift;;
--targetToken) Grafana_Target_Token="$2"; shift;;
--) shift;;
esac
shift;
done
GetHeaders=("Authorization: Bearer $Grafana_Source_Token")
ContentType="Content-Type: application/json"
WriteHostBreak=$'\r\n------------------------------------------'
LibraryPanelResult=$(curl -X GET "$Grafana_Source_Url/api/library-elements" -H "${GetHeaders[@]}" -H "$ContentType")
LibraryPanels=$(echo "$LibraryPanelResult" | jq '.')
echo "$(echo "$LibraryPanels" | jq '.result.elements | length') Library Panels discovered." "$WriteHostBreak"
elements="$(echo "$LibraryPanels" | jq '.result.elements | length')"
elements="$(($elements-1))"
for i in $( eval echo {0..$elements} )
do
PostBody="$(echo "$LibraryPanels" | jq ".result.elements["$i"]")"
PostBody=$(echo "$PostBody" | jq '.folderUid = "" | .meta.folderUid = "" | .folderId = 0')
PostHeaders=("Authorization: Bearer $Grafana_Target_Token")
response=$(curl -X POST "$Grafana_Target_Url/api/library-elements" -d "$PostBody" -H "${PostHeaders[@]}" -H "$ContentType")
if [ $? -eq 0 ]; then
echo "Element created on target Grafana Instance: $Grafana_Target_Url"
else
echo "Error Message: $response" >&2
fi
done