[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / resources / ci / scripts / addUsersFromList_new.sh
1 #!/bin/bash
2
3 function help_usage ()
4 {
5         echo "$0 -ip <ip> -f <userToAdd_file>"
6         echo "for example: -ip 127.0.0.1 -f /var/tmp/userToAdd.txt"
7         exit
8 }
9
10 function check_file_existance ()
11 {
12 echo "check_file_existance $1"
13 if [ $1 == "" ]; then
14         echo "Please provide full path to user file"
15         exit;
16 elif [ -f $1 ]; then
17         source $1
18         USERS=("${USER_LIST[@]}")
19         echo "file exist" $1
20 else
21         echo "Provided user file does not exist"
22         exit
23 fi
24 }
25
26 function check_ip_existance ()
27 {
28 if [ $1 == "" ]; then
29         echo "Please provide ip address"
30         exit;
31 fi
32 }
33
34 function addUser ()
35 {
36         #for user in "${USER_LIST[@]}"; do
37         for user in "${USERS[@]}"; do
38                 PING=`ping -c 1 $IP  > /var/tmp/ping.log`
39                 pattern1='100% packet loss'
40                 pattern2='Host Unreachable'
41                 COUNT=`egrep -c "$pattern1|$pattern2" /var/tmp/ping.log`
42                 if [ $COUNT -eq 0 ]; then
43                 #       curl -i -X post -d '{ "userId" : "kk1123", "role" : "ADMIN" }'  -H "Content-Type: application/json" -H "USER_ID: jh0003" http://192.168.111.9:8080/sdc2/rest/v1/user
44                         userId=`echo $user|awk '{print $1}'`
45                         role=`echo $user|awk '{print $2}'`
46                         curl -i -X post -d '{ "userId" : "'${userId}'", "role" : "'${role}'" }'  -H "Content-Type: application/json" -H "USER_ID: jh0003" http://${IP}:8080/sdc2/rest/v1/user
47                 else
48                         echo "Host" $IP "Is Unreachable"
49                 fi
50         done
51 }
52
53 #main
54 [ $# -eq 0 ] && help_usage
55 while [ $# -ne 0 ]; do
56         case $1 in
57                 "-f")
58                         USER_FILE=$2
59                         shift 1
60                         shift 1
61                 ;;      
62                 -ip)
63                         IP=$2
64                         shift 1
65                         shift 1
66                 ;;
67                 *)
68 #                       help_usage
69                 ;;
70         esac
71 done
72
73 check_file_existance $USER_FILE
74 check_ip_existance $IP
75 addUser