-
Notifications
You must be signed in to change notification settings - Fork 7
/
upsert-licenses-refdata
executable file
·60 lines (50 loc) · 2.07 KB
/
upsert-licenses-refdata
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
#!/bin/bash
# Insert and update /licenses/refdata entries, the entries are taken from
# a JSON file passed as parameter to this script.
#
# Intended usage: Deploy translated labels and additional entries commonly
# used in a library union like GBV.
#
# It uses desc and value for matching because id is different for each tenant.
set -e
# The file $1 must be similar to the response from /licenses/refdata,
# but doesn't need to have "id" properties; they are not used.
NEW=`cat "$1"`
set -x
OKAPI="${OKAPI:-http://localhost:8081}"
TENANT="${TENANT:-diku}"
OKAPIUSERNAME="${OKAPIUSERNAME:-diku_admin}"
OKAPIPASSWORD="${OKAPIPASSWORD:-admin}"
TOKEN=$( curl -s -S -D - -H "X-Okapi-Tenant: $TENANT" -H "Content-type: application/json" \
-d "{\"tenant\":\"$TENANT\",\"username\":\"$OKAPIUSERNAME\",\"password\":\"$OKAPIPASSWORD\"}" $OKAPI/authn/login \
| grep -i "^x-okapi-token: " | tr -d "\n\r" )
set +x
OLD=$( curl -w"\n" -sS -H "$TOKEN" -H "X-Okapi-Tenant: $TENANT" \
-H "Content-type: application/json" -H "Accept: application/json" "$OKAPI/licenses/refdata?perPage=100" )
LENGTH=$( echo "$OLD" | jq length )
if [[ $LENGTH -gt 99 ]]
then
echo "/licenses/refdata cannot process a perPage parameter value > 100"
echo "Therefore we can only handle up to 99 refdata entries, but found $LENGTH"
exit 1
fi
echo "$NEW" \
| jq -c --argjson old "$OLD" \
' INDEX($old[]; .desc) as $old
| .[] |= del(.id) | .[].values[] |= del(.id) | .[]
| [$old[.desc].values + .values | group_by(.value)? | .[] | add] as $values
| . + $old[.desc] + {"values": $values}' \
| while read JSON
do
ID=$( echo "$JSON" | jq -r .id )
if [[ $ID == "null" ]]
then
curl -w"\n" -sS -H "$TOKEN" -H "X-Okapi-Tenant: $TENANT" \
-H "Content-type: application/json" -H "Accept: application/json" \
-d "$JSON" -X POST "$OKAPI/licenses/refdata"
else
curl -w"\n" -sS -H "$TOKEN" -H "X-Okapi-Tenant: $TENANT" \
-H "Content-type: application/json" -H "Accept: application/json" \
-d "$JSON" -X PUT "$OKAPI/licenses/refdata/$ID"
fi
done