Files
wlan-cloud-owls/test_scripts/curl/cli
2023-04-14 08:58:28 -07:00

203 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
#
# License type: BSD 3-Clause License
# License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
#
# Created by Stephane Bourque on 2021-03-04.
# Arilia Wireless Inc.
#
if [[ "$(which jq)" == "" ]]
then
echo "You need the package jq installed to use this script."
exit 1
fi
if [[ "$(which curl)" == "" ]]
then
echo "You need the package curl installed to use this script."
exit 1
fi
if [[ -z "${OWSEC}" || -z "${OWSEC_USERNAME}" || -z "${OWSEC_PASSWORD}" ]]
then
echo "You must set the variables OWSEC, OWSEC_USERNAME, and OWSEC_PASSWORD in order to use this script. Something like"
echo "export OWSEC=security.isp.com:16001"
echo "export OWSEC_USERNAME=theusername@domain.com"
echo "export OWSEC_PASSWORD=mytoughpassword"
exit 1
fi
username=${OWSEC_USERNAME}
password=${OWSEC_PASSWORD}
if [[ "${FLAGS}" == "" ]]
then
FLAGS="-s"
fi
token=""
result_file=result.json
browser_list=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
browser=""
login() {
payload="{ \"userId\" : \"$username\" , \"password\" : \"$password\" }"
token=$(curl ${FLAGS} -X POST -H "Content-Type: application/json" -d "$payload" "https://${OWSEC}/api/v1/oauth2" | jq -r '.access_token')
if [[ "${token}" == "null" ]]
then
echo "Could not login. Please verify the host and username/password."
exit 13
fi
echo "${token}" > token.json
setowls
}
findbrowser() {
if [[ "${browser}" != "" ]]
then
echo
elif [[ "$(uname)" == "Darwin" ]]
then
browser=open
else
BROWSER_LIST=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
for br_name in "${browser_list[@]}"
do
if [[ $(which ${br_name}) != "" ]]
then
browser=${br_name}
break
fi
done
fi
}
setowls() {
if [ -z ${OWGW_OVERRIDE+x} ]; then
curl ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owls" ) | .uri')"
if [[ ! -z "${rawurl}" ]]; then
proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${rawurl/$proto/})"
user="$(echo $url | grep @ | cut -d@ -f1)"
hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
host="$(echo $hostport | sed -e 's,:.*,,g')"
port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
path="$(echo $url | grep / | cut -d/ -f2-)"
export OWLS=${url}
echo "Using ${OWLS}..."
else
echo "OWLS endpoint is not found:"
jq < ${result_file}
exit 1
fi
else
export OWLS=${OWLS_OVERRIDE}
fi
}
logout() {
curl ${FLAGS} -X DELETE -H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
"https://${OWSEC}/api/v1/oauth2/${token}"
rm -rf token.json
}
examplesim() {
cat > example-sim.json << EOF
{
"name":"loadsim",
"deviceType":"edgecore_ecw5410",
"devices":5,
"gateway":"https://ucentral.arilia.com:15002",
"macPrefix":"030000",
"simulationLength":600
}
EOF
}
createsim() {
if [[ -z "$1" ]]; then
echo "Simulation file is not passed as an argument. Please pass the simulation. To get example simulation definition, run 'examplesim' command and check 'example-sim.json' file"
exit 1
else
curl ${FLAGS} -X POST "https://${OWLS}/api/v1/simulation" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-H "accept: application/json" \
-d @$1 > ${result_file}
jq < ${result_file}
fi
}
listsims() {
curl ${FLAGS} -X GET "https://${OWLS}/api/v1/simulation/*" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deletesim() {
curl ${FLAGS} -X DELETE "https://${OWLS}/api/v1/simulation?id=$1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
startsim() {
curl ${FLAGS} -X POST "https://${OWLS}/api/v1/operation/0?simulationId=$1&operation=start" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
stopsim() {
curl ${FLAGS} -X POST "https://${OWLS}/api/v1/$1?operation=stop" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
cancelsim() {
curl ${FLAGS} -X POST "https://${OWLS}/api/v1/operation/$1?operation=cancel" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
getsimstats() {
curl ${FLAGS} -X GET "https://${OWLS}/api/v1/status/$1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
results() {
curl ${FLAGS} -X GET "https://${OWLS}/api/v1/results" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
shopt -s nocasematch
case "$1" in
"login") login; echo "You succesfully logged in..." ; logout ;;
"examplesim") examplesim;;
"createsim") login; createsim "$2"; logout;;
"listsims") login; listsims; logout;;
"deletesim") login; deletesim "$2"; logout;;
"startsim") login; startsim "$2"; logout;;
"stopsim") login; stopsim "$2" ; logout;;
"cancelsim") login; cancelsim "$2" ; logout;;
"getsimstats") login; getsimstats "$2"; logout;;
"results") login; results ; logout;;
*) help ;;
esac