-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmxadmin.sh
executable file
·88 lines (77 loc) · 2.9 KB
/
bmxadmin.sh
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
#!/bin/bash
# author: Leigh Williamson, [email protected]
# requires the Bluemix CLI which can be downloaded from http://clis.ng.bluemix.net/ui/home.html
# requires use of an APIKey to login to Bluemix: https://console.bluemix.net/iam/?env_id=ibm:yp:us-south#/apikeys
# set default values to be used if no alternative is passed in on the command line
endpoint="https://api.ng.bluemix.net"
org="myOrg"
loginspace="dev"
apiKeyFile="myApiKey.json"
log_file="bmxadmin.log"
inputfile="example.csv"
help=""
# parse any arguments passed in on the command line
while getopts he:l:a:o:s:f: option
do
case "${option}"
in
h) help="true";;
e) endpoint=${OPTARG};;
l) log_file=${OPTARG};;
a) apiKeyFile=${OPTARG};;
o) org=${OPTARG};;
s) loginspace=${OPTARG};;
f) inputfile=$OPTARG;;
esac
done
if [ $help = "true" ]
then
echo "usage: bmxadmin.sh [-h ] [-f <input csv file>] [-e <endpoint>] [-l <log file>] [-a <apiKey file>] [-o <organization>] [-s <login space>]"
exit
fi
# initialize & clear the log file
echo " " > $log_file
echo "Starting script" 2>&1 | tee -a $log_file
echo " " 2>&1 | tee -a $log_file
# log the environment used for this execution of the script
echo "Values used in this run of the script:" 2>&1 | tee -a $log_file
echo "Endpoint: $endpoint" 2>&1 | tee -a $log_file
echo "Log file: $log_file" 2>&1 | tee -a $log_file
echo "ApiKey file: $apiKeyFile" 2>&1 | tee -a $log_file
echo "Space used to login: $loginspace" 2>&1 | tee -a $log_file
echo "Organization: $org" 2>&1 | tee -a $log_file
echo "Input csv file: $inputfile" 2>&1 | tee -a $log_file
echo " " 2>&1 | tee -a $log_file
echo "Setting CLI endpoint..." $endpoint
bx api $endpoint >> $log_file
echo " " 2>&1 | tee -a $log_file
echo "Logging in using: " $apiKeyFile 2>&1 | tee -a $log_file
bx login --apikey @$apiKeyFile -s $loginspace >> $log_file
echo " " 2>&1 | tee -a $log_file
echo "List existing users for organization: " $org >> $log_file
bx iam org-users $org >> $log_file
echo " " >> $log_file
echo "Reading input file " $inputfile "..." 2>&1 | tee -a $log_file
echo " " 2>&1 | tee -a $log_file
IFS=","
while read name space role
do
if [ "$name" != "username" ] # skip the csv file header line
then
echo "Processing username: $name; space: $space; role: " ${role%?} 2>&1 | tee -a $log_file
echo "executing: bx iam org-user-add $name $org" >> $log_file
bx iam org-user-add $name $org >> $log_file
echo "executing: bx iam space-role-set $name $org $space " ${role%?} >> $log_file
bx iam space-role-set $name $org $space ${role%?} >> $log_file
else
echo " " >> $log_file
echo "Skipping cvs file header line" >> $log_file
echo " " >> $log_file
fi
done < $inputfile
echo " " 2>&1 | tee -a $log_file
echo "Resulting list of users for organization: " $org >> $log_file
bx iam org-users $org >> $log_file
echo " " >> $log_file
echo "Script complete!" 2>&1 | tee -a $log_file
echo "Look in $log_file file for details of script execution."