CallHome Tests First draft.
[integration/test.git] / csit / variables / netconf / callhome / credentials_set.sh
1 #!/bin/bash
2
3 # This script is called within the docker-compose to provision the controller with either
4 # the global or per-device credentials.
5
6 stderr() { echo "$@" 1>&2; }
7
8 [ $# -eq 0 ] && { stderr "Usage: $0 ( -global | -per-device id ) username password(s)"; exit 1; }
9
10 : ${1?"Usage: $0 -global | -per-device"}
11
12 option=${1};
13
14 if [[ "${option}" == "-per-device" ]]; then
15   : ${2?"Usage: $0 Device Id"}
16   : ${3?"Usage: $0 Device Username"}
17   : ${4?"Usage: $0 Device Password"}
18   devid=${2}
19   user=${3}
20   shift; shift; shift
21   pwds="$@"
22 elif [[ "${option}" == "-global" ]]; then
23   : ${2?"Usage: $0 Global Username"}
24   : ${3?"Usage: $0 Global Password"}
25   devid=""
26   user=${2}
27   shift; shift
28   pwds="$@"
29 else
30   stderr "$0: must supply -global or -per-device command line argument for global password changes, not '${option}''"
31   exit 1
32 fi
33
34 pwdsjson=""
35
36 for pwd in $pwds; do
37   if [[ ! -z "$pwdsjson" ]]; then
38     pwdsjson+=","
39   fi
40   pwdsjson+="'$pwd'"
41 done
42
43 set -e
44 controller=ODL_SYSTEM_IP
45 port=8181
46 basicauth="YWRtaW46YWRtaW4="
47
48 baseurl="http://${controller}:${port}/restconf/config/odl-netconf-callhome-server:netconf-callhome-server"
49
50 if [[ "${option}" == "-global" ]]; then
51   url="${baseurl}/global/credentials"
52 else
53   url="${baseurl}/allowed-devices/device/${devid}/credentials"
54 fi
55
56 set +e
57 read -r -d '' payload << EOM
58 {
59     "credentials": {
60         "username": "${user}",
61           "passwords": [${pwdsjson}]
62     }
63 }
64 EOM
65 set -e
66
67 payload=$(echo "${payload}" | tr '\n' ' ' | tr -s " ")
68
69 echo "PUT of user (${user}) and pwd (${pwd})"
70 res=$(curl -s -X PUT \
71       -H "Authorization: Basic ${basicauth}" \
72       -H "Content-Type: application/json" \
73       -H "Cache-Control: no-cache" \
74       --data "${payload}" \
75       ${url})
76
77 if [[ $res == *"error-message"* ]]; then
78   stderr "$0: ${res}"
79   exit 1
80 fi
81
82 echo "Getting user/pwd ..."
83
84 res=$(curl -s -X GET \
85       -H "Authorization: Basic ${basicauth}" \
86       -H "Cache-Control: no-cache" \
87       ${url})
88 echo ${res}