-
Notifications
You must be signed in to change notification settings - Fork 1
/
agreements-attachtags
executable file
·76 lines (56 loc) · 2.16 KB
/
agreements-attachtags
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
auth 2>/dev/null || authn 2>/dev/null
# Script expects a delimited file with name of agreement 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 agreement_dict
declare -A organization_dict
declare -A map_dict
map_dict['a']="jan_renewal"
map_dict['b']="feb_renewal"
map_dict['c']="mar_renewal"
map_dict['d']="apr_renewal"
map_dict['e']="may_renewal"
map_dict['f']="jun_renewal"
map_dict['g']="jul_renewal"
map_dict['h']="aug_renewal"
map_dict['i']="sep_renewal"
map_dict['j']="oct_renewal"
map_dict['k']="nov_renewal"
map_dict['l']="dec_renewal"
infile="agreement-tag.tsv"
all_agreements=""
all_organizations=""
offset=0
recsretrieved=100
# 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
for key in "${!agreement_dict[@]}";do
newkey=${key//[^a-z0-9]/}
agreement_dict[${newkey}]=${agreement_dict["${key}"]}
done
offset=0
recsretrieved=100
tag='{"tags":[{"value":"sep_renewal"}]}'
while IFS=$'\t' read -r agreement_key tag;do
agreement_key=${agreement_key,,}
agreement_key=${agreement_key//[^a-z0-9]/}
agreement_uuid=$(jq -r .id <<< ${agreement_dict[${agreement_key}]})
tag=${map_dict[$tag]}
if [[ ${tag} =~ [a-z] ]];then
tag="{\"tags\":[{\"value\":\"${tag}\"}]}"
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 "${tag}" "${okapi_url}/erm/sas/${agreement_uuid}"
fi
done < ${infile}