-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagreements-attachorgs
More file actions
executable file
·104 lines (79 loc) · 4.07 KB
/
agreements-attachorgs
File metadata and controls
executable file
·104 lines (79 loc) · 4.07 KB
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
#!/usr/bin/bash
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
infile="agreement-organization.tsv"
all_agreements=""
all_organizations=""
rm -f notfound
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?query=cql.allRecords=1&sort=name%3Basc&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
# 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}/organizations-storage/organizations?query=%28status%3D%3D%22Active%22%29+sortby+name%2Fsort.ascending&limit=100&offset=$offset" |jq .organizations)
recsretrieved=$(echo ${apicall} |jq '. |length')
totalrecs=$(($offset + $recsretrieved))
printf "%s Organizations retrieved\r" $totalrecs
all_organizations="${all_organizations}$(echo ${apicall} |jq .[])"
offset=$(($offset + 100))
done
echo "$totalrecs Organizations retrieved"
echo "${all_organizations}" > orgs
# load organizations into scalar array keyed by downcased name or code
eval "$(echo ${all_organizations} |jq -r '. | @sh "organization_dict[\(.code|tostring|ascii_downcase)]=\(.|tostring)"')"
#eval "$(echo ${all_organizations} |jq -r '. | @sh "organization_dict[\(.name|tostring|ascii_downcase)]=\(.|tostring)"')"
echo "Normalizing Organization keys"
# normalize keys
for key in "${!organization_dict[@]}";do
newkey=${key//[^a-z0-9]/}
organization_dict[${newkey}]=${organization_dict["${key}"]}
done
# get roles
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/refdata/SubscriptionAgreementOrg/role")
org_role_uuid=$(echo "${apicall}" |jq -r '.[] | select(.value=="content_provider") | .id')
# attach organization
while IFS=$'\t' read -r agreement organization;do
compareagreementkey=$(tr -dc "[0-9a-z]" <<< "${agreement,,}")
agreement_record="${agreement_dict[$compareagreementkey]}"
compareorganizationkey=$(tr -dc "[0-9a-z]" <<< "${organization,,}")
organization_record="${organization_dict[$compareorganizationkey]}"
if [[ ${agreement_record} =~ [a-z0-9A-Z] ]];then
agreement_uuid=$(echo ${agreement_record} |jq -r .id)
if [[ ${organization_record} =~ [a-z0-9A-Z] ]];then
organization_uuid=$(echo ${organization_record} |jq -r .id)
organization_name=$(echo ${organization_record} |jq -r .name)
# fix json and PUT
record=$(echo "${agreement_record}" | jq ".orgs += [ { \"_delete\": false, \"org\": {\"name\": \"${organization_name}\", \"orgsUuid\": \"${organization_uuid}\", \"primaryOrg\": false }, \"roles\": [ { \"_delete\": false, \"role\": { \"id\": \"${org_role_uuid}\" } }] }]")
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 "${organization} Organization not found"
fi
else
echo "${agreement} Agreement not found -- $compareagreementkey"
echo "${agreement}" >> notfound
fi
done < ${infile}