-
-
Save jolbax/0df5bfc7eff09c437d50ecff7ab8eb40 to your computer and use it in GitHub Desktop.
CloudStack API client shell script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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