-
Notifications
You must be signed in to change notification settings - Fork 1
/
agreements-attachlicenses
executable file
·124 lines (93 loc) · 4.17 KB
/
agreements-attachlicenses
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
auth 2>/dev/null || authn 2>/dev/null
# Script expects a delimited file with name of agreement in first column and name of license in second colum
tenant=$(cat tenant)
okapi_url=$(cat okapi.url)
okapi_token=$(cat okapi.token)
declare -A agreement_dict
declare -A license_dict
automatic_match="no"
infile="agreement-license.tsv"
all_agreements=""
all_licenses=""
offset=0
recsretrieved=100
if [[ ${automatic_match} == "yes" ]];then
echo "Automatch enabled. Ignoring input files"
fi
# retrieve all agreements 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}/erm/sas?perPage=100&offset=$offset")
recsretrieved=$(echo ${apicall} |jq '. |length')
totalrecs=$(($offset + $recsretrieved))
printf "%s Agreements retrieved\r" $totalrecs
all_agreements="${all_agreements}$(echo ${apicall} |jq .[])"
offset=$(($offset + 100))
done
echo "$totalrecs Agreements retrieved"
# load agreements into scalar array keyed by downcased name
eval "$(echo ${all_agreements} |jq -r '. | @sh "agreement_dict[\(.name|tostring|ascii_downcase)]=\(.|tostring)"')"
echo "Normalizing Agreement keys"
# normalize keys
regex="[\'\"\\/\\\| ]"
for key in "${!agreement_dict[@]}";do
newkey=${key//${regex}/}
agreement_dict[${newkey}]=${agreement_dict["${key}"]}
done
offset=0
recsretrieved=100
# retrieve all records
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
for key in "${!license_dict[@]}";do
newkey=${key//${regex}/}
license_dict[${newkey}]=${license_dict["${key}"]}
if [[ ${automatic_match} == "yes" ]];then
agreement_record="${agreement_dict[${newkey}]}"
# check to see if license exists with same name. If so, link the Agreement
if [[ ${agreement_record} =~ [a-z0-9A-Z] ]];then
agreement_uuid=$(echo ${agreement_record} |jq -r .id)
license_uuid=$(echo ${license_dict["${key}"]} |jq -r .id)
record=$(echo "${agreement_record}" | jq ".linkedLicenses += [{\"_delete\": false, \"amendments\": [], \"remoteId\": \"${license_uuid}\", \"status\": \"controlling\" }]")
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}/erm/sas/${agreement_uuid}"
fi
fi
done
if [[ ${automatic_match} == "yes" ]];then
echo "Exiting without processing file due to automatch"
exit
fi
# attach license
while IFS=$'\t' read -r agreement license status;do
echo "Processing ${license}"
status=${status,,}
compareagreementkey=${agreement//${regex}/}
compareagreementkey="${compareagreementkey,,}"
agreement_record="${agreement_dict[$compareagreementkey]}"
comparelicensekey=${license//${regex}/}
comparelicensekey=${license,,}
license_record="${license_dict[$comparelicensekey]}"
if [[ ${agreement_record} =~ [a-z0-9A-Z] ]];then
agreement_uuid=$(echo ${agreement_record} |jq -r .id)
if [[ ${license_record} =~ [a-z0-9A-Z] ]];then
license_uuid=$(echo ${license_record} |jq -r .id)
# fix json and PUT
record=$(echo "${agreement_record}" | jq ".linkedLicenses += [{\"_delete\": false, \"amendments\": [], \"remoteId\": \"${license_uuid}\", \"status\": \"${status}\" }]")
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}/erm/sas/${agreement_uuid}"
else
echo "${license} License not found"
fi
else
echo "${agreement} Agreement not found"
fi
done < ${infile}