84 lines
1.8 KiB
Bash
Executable File
84 lines
1.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
refresh_token=""
|
|
service_url=""
|
|
username=""
|
|
password=""
|
|
TOKENRC="${HOME}/.vratokenrc"
|
|
|
|
while getopts "h:u:p:a:" arg; do
|
|
case $arg in
|
|
h)
|
|
echo "usage"
|
|
;;
|
|
u)
|
|
username="${OPTARG}"
|
|
;;
|
|
p)
|
|
password="${OPTARG}"
|
|
;;
|
|
a)
|
|
service_url="${OPTARG}"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
login_uri="csp/gateway/am/api/login?access_token"
|
|
|
|
if [ -f ${TOKENRC} ]; then
|
|
source ${TOKENRC}
|
|
else
|
|
if [ -z "${service_url}" ]; then
|
|
echo "Missing VRA service url, use option -a"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z ${username} ]; then
|
|
echo -n "Username: "
|
|
read -r username
|
|
echo
|
|
fi
|
|
|
|
if [ -z ${password} ]; then
|
|
echo -n "Password: "
|
|
read -s password
|
|
echo
|
|
fi
|
|
|
|
token_request=$(curl -s --insecure -X POST \
|
|
"$service_url/${login_uri}" \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{
|
|
"username": "'"$username"'",
|
|
"password": "'"$password"'"
|
|
}'| jq )
|
|
|
|
refresh_token=$(echo ${token_request} | jq -r .refresh_token)
|
|
if [ ${refresh_token} = "null" ]; then
|
|
status=$(echo "${token_request}" | jq -r .status)
|
|
if [ "${status}" != "200" ];then
|
|
echo ${token_request} | jq -r .serverMessage
|
|
exit 4
|
|
fi
|
|
else
|
|
if [ ! -f "${TOKENRC}" ]; then
|
|
echo "service_url=${service_url}" >> "${TOKENRC}"
|
|
echo "refresh_token=${refresh_token}" >> "${TOKENRC}"
|
|
echo "username=${username}" >> "${TOKENRC}"
|
|
chmod 600 ${TOKENRC}
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
if [ ${?} -ne 0 ];then
|
|
echo "Error login to ${service_url} failed"
|
|
exit 3
|
|
fi
|
|
|
|
ARG1=${@:$OPTIND:1}
|
|
|
|
export TF_VAR_vra_url="${service_url}"
|
|
export TF_VAR_vra_username="${username}"
|
|
export TF_VAR_vra_refresh_token="${refresh_token}"
|
|
tofu ${ARG1} -no-color
|