Skip to content

Instantly share code, notes, and snippets.

@k14i
Last active January 11, 2020 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save k14i/6201354 to your computer and use it in GitHub Desktop.
Save k14i/6201354 to your computer and use it in GitHub Desktop.
CloudStack API client shell script.
#!/usr/bin/env bash
ADDRESS="https://"
API_KEY=""
SECRET_KEY=""
USE_XMLLINT=0 # true => 1, false => 0
if [ x$ADDRESS == x ] || [ x$API_KEY == x ] || [ x$SECRET_KEY == x ] || [ x$USE_XMLLINT == x ]; then
echo 'ERROR: Set all required valiables.'
exit 1
fi
api_path="/client/api?"
if [ $# -lt 1 ]; then
echo "usage: $0 command=... paramter=... parameter=..."; exit;
elif [[ $1 != "command="* ]]; then
echo "usage: $0 command=... paramter=... parameter=..."; exit;
elif [ $1 == "command=" ]; then
echo "usage: $0 command=... paramter=... parameter=..."; exit;
fi
data_array=("$@" "apikey=${API_KEY}")
temp1=$(echo -n ${data_array[@]} | \
tr " " "\n" | \
sort -fd -t'=' | \
perl -pe's/([^-_.~A-Za-z0-9=\s])/sprintf("%%%02X", ord($1))/seg'| \
tr "A-Z" "a-z" | \
tr "\n" "&" )
signature=$(echo -n ${temp1[@]})
signature=${signature%&}
signature=$(echo -n $signature | \
openssl sha1 -binary -hmac $SECRET_KEY | \
openssl base64 )
signature=$(echo -n $signature | \
perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg')
url=${ADDRESS}${api_path}$(echo -n $@ | tr " " "&")"&"apikey=$API_KEY"&"signature=$signature
if [ $USE_XMLLINT -eq 1 ] && [ `type xmllint > /dev/null; echo $?` -eq 0 ]; then
curl -k ${url} | xmllint --format -
else
curl -k ${url}
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment