-
Notifications
You must be signed in to change notification settings - Fork 62
/
importer.sh
executable file
·84 lines (70 loc) · 1.87 KB
/
importer.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
#!/usr/bin/env bash
. "$(dirname "$0")/config.sh"
declare -aa ORGMAP
for row in "${ORGS[@]}"; do
IFS=':' read -r -a values <<< "$row"
ORGMAP[${values[0]}]=${values[1]}
done
curl_wrap() {
FILE=$1
KEY=$2
URL=$3
HTTP_VERB=$4
[[ -z "$HTTP_VERB" ]] && HTTP_VERB=POST
curl --fail -k -X$HTTP_VERB \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $KEY" \
--data-binary @$FILE \
$URL
}
import_file() {
FILE="$1"
KEY="$2"
TYPE="$3"
if ! [ -f "$FILE" ]; then
echo "$FILE not found." >>/dev/stderr
return
fi
echo "Processing $FILE file..."
curl_wrap "$FILE" "$KEY" "${HOST}/api/$TYPE"
CURL_EXIT=$?
echo
if [[ ${CURL_EXIT} = 22 && $TYPE = "datasources" ]]; then
echo "409 conflict error is normal. Retrying as update."
id=$(basename $file .json)
curl_wrap "$FILE" "$KEY" "${HOST}/api/$TYPE/$id" PUT
elif [[ ${CURL_EXIT} = 22 && $TYPE = "alert-notifications" ]]; then
echo "500 server error is normal. Retrying as update."
id=$(basename $file .json)
curl_wrap "$FILE" "$KEY" "${HOST}/api/$TYPE/$id" PUT
fi
}
if [[ $# -eq 0 ]]; then
ARGS=(${FILE_DIR}/*/*/*.json)
else
ARGS=("$@")
fi
for FILE in "${ARGS[@]}"; do
IFS='/' read -r -a args <<< "$FILE"
if [ ${#args[@]} -ne 4 ]; then
echo "Wrong param \"${FILE}\". Must be data/{organization}/{type}/{file}"
fi
KEY=${ORGMAP[${args[1]}]}
TYPE=${args[2]}
# FILE=${args[3]}
case $TYPE in
alert-notifications)
import_file $FILE "$KEY" 'alert-notifications'
;;
dashboards)
import_file $FILE "$KEY" 'dashboards/db'
;;
datasources)
import_file $FILE "$KEY" 'datasources'
;;
*)
echo "Unknown type $TYPE"
;;
esac
done