-
Notifications
You must be signed in to change notification settings - Fork 1
/
license-update
executable file
·69 lines (50 loc) · 2.09 KB
/
license-update
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
auth 2>/dev/null || authn 2>/dev/null
# Script expects a delimited file with name of license in first column and name of organization in second colum
tenant=$(cat tenant)
okapi_url=$(cat okapi.url)
okapi_token=$(cat okapi.token)
declare -A license_dict
declare -A organization_dict
infile="infile"
all_licenses=""
all_organizations=""
offset=0
recsretrieved=100
# retrieve all licenses and add to array
while [[ $recsretrieved -eq 100 ]] ;do
apicall=$(curl -s -w '\n' -X GET -H "Accept: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" "${okapi_url}/licenses/licenses?sort=name%3Bdesc&perPage=100&offset=$offset")
recsretrieved=$(echo ${apicall} |jq '. |length')
totalrecs=$(($offset + $recsretrieved))
printf "%s Licenses retrieved\r" $totalrecs
all_licenses="${all_licenses}$(echo ${apicall} |jq .[])"
offset=$(($offset + 100))
done
echo "$totalrecs Licenses retrieved"
# load licenses into scalar array keyed by downcased name
eval "$(echo ${all_licenses} |jq -r '. | @sh "license_dict[\(.name|tostring|ascii_downcase)]=\(.|tostring)"')"
echo "Normalizing License keys"
# normalize keys
counter=0
# normalize keys
for key in "${!license_dict[@]}";do
newkey=${key//[^a-z0-9]/}
license_dict[${newkey}]=${license_dict["${key}"]}
if (( $counter % 10 == 0 ));then
printf "%s keys normalized\r" $counter
fi
counter=$(($counter + 1))
done
echo "$counter keys normalized"
# attach altnames
while IFS=$'\t' read -r license;do
comparelicensekey=$(tr -dc "[0-9a-z]" <<< "${license,,}")
license_record="${license_dict[$comparelicensekey]}"
if [[ ${license_record} =~ [a-z0-9A-Z] ]];then
license_uuid=$(echo ${license_record} |jq -r .id)
# fix json and PUT
record=$(echo "${license_record}" | jq '(.description = (.description | gsub("\\\\n"; "\n")))')
curl --http1.1 -s -w '\n' -X PUT -H "Content-type: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" -d "${record}" "${okapi_url}/licenses/licenses/${license_uuid}"
else
echo "${license} License not found"
fi
done < ${infile}