2014-04-17 19:53:28 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# DEFAULT CONFIG
|
|
|
|
OVH_CONSUMER_KEY=""
|
|
|
|
OVH_APP_KEY=""
|
|
|
|
OVH_APP_SECRET=""
|
|
|
|
|
|
|
|
CONSUMER_KEY_FILE=".ovhConsumerKey"
|
|
|
|
OVH_APPLICATION_FILE=".ovhApplication"
|
|
|
|
LIBS="libs"
|
|
|
|
|
2014-04-28 23:14:33 +02:00
|
|
|
TARGETS=(CA EU)
|
|
|
|
|
|
|
|
declare -A API_URLS
|
|
|
|
API_URLS[CA]="https://ca.api.ovh.com/1.0"
|
|
|
|
API_URLS[EU]="https://api.ovh.com/1.0"
|
|
|
|
|
|
|
|
declare -A API_CREATE_APP_URLS
|
|
|
|
API_CREATE_APP_URLS[CA]="https://ca.api.ovh.com/createApp/"
|
|
|
|
API_CREATE_APP_URLS[EU]="https://api.ovh.com/createApp/"
|
2014-04-17 19:53:28 +02:00
|
|
|
|
2018-02-20 12:15:36 +01:00
|
|
|
## https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
|
|
|
|
# resolve $SOURCE until the file is no longer a symlink
|
|
|
|
SOURCE="${BASH_SOURCE[0]}"
|
|
|
|
while [ -h "${SOURCE}" ]
|
|
|
|
do
|
|
|
|
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
|
|
|
SOURCE="$(readlink "${SOURCE}")"
|
|
|
|
# if $SOURCE was a relative symlink,
|
|
|
|
# we need to resolve it relative to the path where the symlink file was located
|
|
|
|
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}"
|
|
|
|
done
|
|
|
|
BASE_PATH=$( cd -P "$( dirname "${SOURCE}" )" && pwd )
|
|
|
|
|
2018-02-20 17:12:34 +01:00
|
|
|
LEGACY_PROFILES_PATH="${BASE_PATH}/profile"
|
|
|
|
PROFILES_PATH="${HOME}/.ovh-api-bash-client/profile"
|
2017-06-13 18:06:41 +02:00
|
|
|
|
|
|
|
HELP_CMD="$0"
|
2014-04-17 19:53:28 +02:00
|
|
|
|
|
|
|
# THESE VARS WILL BE USED LATER
|
|
|
|
METHOD="GET"
|
|
|
|
URL="/me"
|
2014-04-28 23:14:33 +02:00
|
|
|
TARGET="EU"
|
2014-04-17 19:53:28 +02:00
|
|
|
TIME=""
|
|
|
|
SIGDATA=""
|
|
|
|
POST_DATA=""
|
2017-06-21 00:21:47 +02:00
|
|
|
PROFILE=""
|
2014-04-17 19:53:28 +02:00
|
|
|
|
2018-02-20 17:12:34 +01:00
|
|
|
_echoWarning()
|
|
|
|
{
|
|
|
|
echo >&2 "[WARNING] $*"
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
# join alements of an array with a separator (single char)
|
|
|
|
# usage:
|
|
|
|
# _arrayJoin "|" "${my_array[@]}"
|
|
|
|
#
|
|
|
|
_arrayJoin()
|
|
|
|
{
|
|
|
|
local IFS="$1"
|
|
|
|
shift
|
|
|
|
echo "$*"
|
|
|
|
}
|
|
|
|
|
|
|
|
_StringToLower()
|
|
|
|
{
|
|
|
|
echo "$1" | tr '[:upper:]' '[:lower:]'
|
|
|
|
}
|
|
|
|
|
|
|
|
_StringToUpper()
|
|
|
|
{
|
|
|
|
echo "$1" | tr '[:lower:]' '[:upper:]'
|
|
|
|
}
|
|
|
|
|
2014-04-28 23:14:33 +02:00
|
|
|
isTargetValid()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local VALID
|
2014-04-28 23:14:33 +02:00
|
|
|
VALID=0
|
2018-06-21 15:04:32 +02:00
|
|
|
for i in ${TARGETS[*]}
|
2014-04-28 23:14:33 +02:00
|
|
|
do
|
2018-06-21 15:04:32 +02:00
|
|
|
if [ "$i" == "${TARGET}" ]
|
2014-04-28 23:14:33 +02:00
|
|
|
then
|
|
|
|
VALID=1
|
2014-04-29 00:25:57 +02:00
|
|
|
break
|
2014-04-28 23:14:33 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
if [ ${VALID} -eq 0 ]
|
2014-04-28 23:14:33 +02:00
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
echo "Error: ${TARGET} is not a valid target, accepted values are: ${TARGETS[*]}"
|
2014-04-28 23:14:33 +02:00
|
|
|
echo
|
|
|
|
help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2014-04-17 19:53:28 +02:00
|
|
|
|
|
|
|
createApp()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local NEXT
|
2017-06-13 18:06:41 +02:00
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
echo "For which OVH API do you want to create a new API Application? ($( _arrayJoin "|" "${TARGETS[@]}"))"
|
|
|
|
while [ -z "${NEXT}" ]
|
2014-04-28 23:14:33 +02:00
|
|
|
do
|
2018-06-21 15:04:32 +02:00
|
|
|
read -r NEXT
|
2014-04-28 23:14:33 +02:00
|
|
|
done
|
2018-06-21 15:04:32 +02:00
|
|
|
TARGET=$( _StringToUpper "${NEXT}" )
|
2014-04-28 23:14:33 +02:00
|
|
|
isTargetValid
|
|
|
|
|
|
|
|
echo
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "In order to create an API Application, please visit the link below:\\n${API_CREATE_APP_URLS[${TARGET}]}"
|
2014-04-17 19:53:28 +02:00
|
|
|
echo
|
|
|
|
echo "Once your application is created, we will configure this script for this application"
|
|
|
|
echo -n "Enter the Application Key: "
|
2018-06-21 15:04:32 +02:00
|
|
|
read -r OVH_APP_KEY
|
2014-04-17 19:53:28 +02:00
|
|
|
echo -n "Enter the Application Secret: "
|
2018-06-21 15:04:32 +02:00
|
|
|
read -r OVH_APP_SECRET
|
2014-04-17 19:53:28 +02:00
|
|
|
echo "OK!"
|
2018-06-21 15:04:32 +02:00
|
|
|
echo "These informations will be stored in the following file: ${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}"
|
|
|
|
echo -e "${OVH_APP_KEY}\\n${OVH_APP_SECRET}" > "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}"
|
2014-04-17 19:53:28 +02:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Do you also need to create a consumer key? (y/n)"
|
2018-06-21 15:04:32 +02:00
|
|
|
read -r NEXT
|
|
|
|
if [ -n "${NEXT}" ] && [ "$( _StringToLower "${NEXT}")" == "y" ]
|
2014-04-17 19:53:28 +02:00
|
|
|
then
|
|
|
|
createConsumerKey
|
2014-04-28 12:19:11 +02:00
|
|
|
else
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "OK, no consumer key created for now.\\nYou will be able to initalize the consumer key later calling :\\n${HELP_CMD} --init"
|
2014-04-17 19:53:28 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
createConsumerKey()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local ANSWER
|
2017-06-21 00:21:47 +02:00
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
METHOD="POST"
|
|
|
|
URL="/auth/credential"
|
|
|
|
|
2017-06-13 18:06:41 +02:00
|
|
|
# ensure an OVH App key is set
|
2017-06-21 00:21:47 +02:00
|
|
|
initApplication
|
2017-06-13 18:06:41 +02:00
|
|
|
hasOvhAppKey || exit 1
|
|
|
|
|
2018-02-12 18:07:45 +01:00
|
|
|
# condition keeped for retro-compatibility, to always allow post accessRules from --data
|
2017-06-13 18:06:41 +02:00
|
|
|
if [ -z "${POST_DATA}" ]; then
|
2018-02-12 18:07:45 +01:00
|
|
|
buildAccessRules
|
2017-06-13 18:06:41 +02:00
|
|
|
fi
|
2018-02-12 18:07:45 +01:00
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
ANSWER=$(requestNoAuth)
|
2017-06-13 18:06:41 +02:00
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
getJSONFieldString "${ANSWER}" 'consumerKey' > "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}"
|
|
|
|
echo "In order to validate the generated consumerKey, visit the validation url at:"
|
|
|
|
getJSONFieldString "${ANSWER}" 'validationUrl'
|
2014-04-17 19:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initConsumerKey()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
if cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}" &> /dev/null;
|
2014-04-17 19:53:28 +02:00
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
OVH_CONSUMER_KEY="$(cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}")"
|
2014-04-17 19:53:28 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
initApplication()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
if cat "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}" &> /dev/null;
|
2014-04-17 19:53:28 +02:00
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
OVH_APP_KEY=$(sed -n 1p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
|
|
|
OVH_APP_SECRET=$(sed -n 2p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
2014-04-17 19:53:28 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTime()
|
|
|
|
{
|
|
|
|
TIME=$(date '+%s')
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSignData()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local SIGDATA
|
|
|
|
SIGDATA="${OVH_APP_SECRET}+${OVH_CONSUMER_KEY}+$1+${API_URLS[${TARGET}]}$2+$3+${TIME}"
|
|
|
|
SIG="\$1\$"$(echo -n "${SIGDATA}" | sha1sum - | cut -d' ' -f1)
|
2014-04-17 19:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
2017-06-13 18:06:41 +02:00
|
|
|
echo
|
2014-04-17 19:53:28 +02:00
|
|
|
echo "Help: possible arguments are:"
|
2017-06-19 18:36:33 +02:00
|
|
|
echo " --url <url> : the API URL to call, for example /domains (default is /me)"
|
|
|
|
echo " --method <method> : the HTTP method to use, for example POST (default is GET)"
|
|
|
|
echo " --data <JSON data> : the data body to send with the request"
|
2018-06-21 15:04:32 +02:00
|
|
|
echo " --target <$( _arrayJoin "|" "${TARGETS[@]}")> : the target API (default is EU)"
|
2018-02-12 18:07:45 +01:00
|
|
|
echo " --init : to initialize the consumer key, and manage custom access rules file"
|
2017-06-19 18:36:33 +02:00
|
|
|
echo " --initApp : to initialize the API application"
|
2018-02-20 17:12:34 +01:00
|
|
|
echo " --list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory"
|
2017-06-19 18:36:33 +02:00
|
|
|
echo " --profile <value>"
|
2018-02-20 17:12:34 +01:00
|
|
|
echo " * default : from ~/.ovh-api-bash-client/profile directory"
|
|
|
|
echo " * <dir> : from ~/.ovh-api-bash-client/profile/<dir> directory"
|
2014-04-17 19:53:28 +02:00
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:07:45 +01:00
|
|
|
buildAccessRules()
|
|
|
|
{
|
|
|
|
local access_rules_file="${CURRENT_PATH}/access.rules"
|
|
|
|
local method path
|
|
|
|
local json_rules
|
|
|
|
local answer
|
|
|
|
|
|
|
|
if [ ! -f "${access_rules_file}" ]; then
|
|
|
|
echo "${access_rules_file} missing, created full access rules"
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "GET /*\\nPUT /*\\nPOST /*\\nDELETE /*" > "${CURRENT_PATH}/access.rules"
|
2018-02-12 18:07:45 +01:00
|
|
|
fi
|
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "Current rules for that profile\\n"
|
2018-02-12 18:07:45 +01:00
|
|
|
cat "${access_rules_file}"
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "\\nDo you need to customize this rules ?"
|
|
|
|
read -n1 -r -p "(y/n)> " answer
|
|
|
|
echo -e "\\n"
|
2018-02-12 18:07:45 +01:00
|
|
|
|
|
|
|
case ${answer} in
|
|
|
|
[Yy]) echo "Operation canceled, please edit ${access_rules_file}"; exit;;
|
|
|
|
[Nn]) echo "Now generating POST JSON Data for accessRules";;
|
|
|
|
*) echo "bad choice"; exit 1;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
while read -r method path;
|
|
|
|
do
|
|
|
|
if [ -n "${method}" ] && [ -n "${path}" ]; then
|
|
|
|
json_rules+='{ "method": "'${method}'", "path": "'${path}'"},'
|
|
|
|
fi
|
|
|
|
done < "${access_rules_file}"
|
|
|
|
json_rules=${json_rules::-1}
|
|
|
|
if [ -z "${json_rules}" ]; then
|
2018-02-20 17:12:34 +01:00
|
|
|
echoWarning "no rule defined, please verify your file '${access_rules_file}'"
|
2018-02-12 18:07:45 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
POST_DATA='{ "accessRules": [ '${json_rules}' ] }'
|
|
|
|
|
|
|
|
}
|
2014-04-17 19:53:28 +02:00
|
|
|
parseArguments()
|
|
|
|
{
|
2017-06-21 00:21:47 +02:00
|
|
|
# an action launched out of this function
|
|
|
|
INIT_KEY_ACTION=
|
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
while [ $# -gt 0 ]
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
--data)
|
|
|
|
shift
|
|
|
|
POST_DATA=$1
|
|
|
|
;;
|
|
|
|
--init)
|
2017-06-21 00:21:47 +02:00
|
|
|
INIT_KEY_ACTION="ConsumerKey"
|
2014-04-17 19:53:28 +02:00
|
|
|
;;
|
|
|
|
--initApp)
|
2017-06-21 00:21:47 +02:00
|
|
|
INIT_KEY_ACTION="AppKey"
|
2014-04-17 19:53:28 +02:00
|
|
|
;;
|
|
|
|
--method)
|
|
|
|
shift
|
|
|
|
METHOD=$1
|
|
|
|
;;
|
|
|
|
--url)
|
|
|
|
shift
|
|
|
|
URL=$1
|
|
|
|
;;
|
2014-04-28 23:14:33 +02:00
|
|
|
--target)
|
|
|
|
shift
|
|
|
|
TARGET=$1
|
|
|
|
isTargetValid
|
|
|
|
;;
|
2017-06-13 18:06:41 +02:00
|
|
|
--profile)
|
|
|
|
shift
|
2017-06-19 18:36:33 +02:00
|
|
|
PROFILE=$1
|
2017-06-13 18:06:41 +02:00
|
|
|
;;
|
|
|
|
--list-profile)
|
|
|
|
listProfile
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--help|-h)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
2014-04-17 19:53:28 +02:00
|
|
|
*)
|
|
|
|
echo "Unknow parameter $1"
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
requestNoAuth()
|
|
|
|
{
|
|
|
|
updateTime
|
2018-06-21 15:04:32 +02:00
|
|
|
curl -s -X "${METHOD}" \
|
|
|
|
--header 'Content-Type:application/json;charset=utf-8' \
|
|
|
|
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
|
|
|
--header "X-Ovh-Timestamp:${TIME}" \
|
|
|
|
--data "${POST_DATA}" \
|
|
|
|
"${API_URLS[${TARGET}]}${URL}"
|
2014-04-17 19:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
request()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local RESPONSE RESPONSE_STATUS RESPONSE_CONTENT
|
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
updateTime
|
2018-06-21 15:04:32 +02:00
|
|
|
updateSignData "${METHOD}" "${URL}" "${POST_DATA}"
|
|
|
|
|
|
|
|
RESPONSE=$(curl -s -w "\\n%{http_code}\\n" -X "${METHOD}" \
|
|
|
|
--header 'Content-Type:application/json;charset=utf-8' \
|
|
|
|
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
|
|
|
--header "X-Ovh-Timestamp:${TIME}" \
|
|
|
|
--header "X-Ovh-Signature:${SIG}" \
|
|
|
|
--header "X-Ovh-Consumer:${OVH_CONSUMER_KEY}" \
|
|
|
|
--data "${POST_DATA}" \
|
|
|
|
"${API_URLS[${TARGET}]}${URL}")
|
|
|
|
|
|
|
|
RESPONSE_STATUS=$(echo "${RESPONSE}" | sed -n '$p')
|
|
|
|
RESPONSE_CONTENT=$(echo "${RESPONSE}" | sed '$d')
|
|
|
|
echo "${RESPONSE_STATUS} ${RESPONSE_CONTENT}"
|
2014-04-17 19:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getJSONFieldString()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
local JSON FIELD RESULT
|
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
JSON="$1"
|
|
|
|
FIELD="$2"
|
2018-06-21 15:04:32 +02:00
|
|
|
# shellcheck disable=SC1117
|
|
|
|
RESULT=$(echo "${JSON}" | "${BASE_PATH}/${LIBS}/JSON.sh" | grep "\[\"${FIELD}\"\]" | sed -r "s/\[\"${FIELD}\"\]\s+(.*)/\1/")
|
|
|
|
echo "${RESULT:1:${#RESULT}-2}"
|
2014-04-17 19:53:28 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 18:36:33 +02:00
|
|
|
# set CURRENT_PATH with profile name
|
2017-06-21 14:31:13 +02:00
|
|
|
# usage : initProfile |set|get] profile_name
|
|
|
|
# set : create the profile if missing
|
|
|
|
# get : raise an error if no profile with that name
|
2017-06-13 18:06:41 +02:00
|
|
|
initProfile()
|
|
|
|
{
|
2017-06-21 14:31:13 +02:00
|
|
|
local createProfile=$1
|
|
|
|
local profile=$2
|
2017-06-13 18:06:41 +02:00
|
|
|
|
2017-06-19 18:36:33 +02:00
|
|
|
if [ ! -d "${PROFILES_PATH}" ]
|
|
|
|
then
|
2018-02-20 17:12:34 +01:00
|
|
|
mkdir -pv "${PROFILES_PATH}" || exit 1
|
2017-06-19 18:36:33 +02:00
|
|
|
fi
|
|
|
|
|
2018-02-20 17:12:34 +01:00
|
|
|
# checking if some profiles remains in legacy profile path
|
|
|
|
local legacy_profiles=
|
|
|
|
local legacy_default_profile=
|
|
|
|
if [ -d "${LEGACY_PROFILES_PATH}" ]; then
|
|
|
|
# is there any profile in legacy path ?
|
|
|
|
legacy_profiles=$(ls -A "${LEGACY_PROFILES_PATH}" 2>/dev/null)
|
2018-02-26 14:18:18 +01:00
|
|
|
legacy_default_profile=$(cd "${BASE_PATH}" && ls .ovh* access.rules 2>/dev/null)
|
|
|
|
|
2018-02-20 17:12:34 +01:00
|
|
|
if [ -n "${legacy_profiles}" ] || [ -n "${legacy_default_profile}" ]; then
|
2018-02-26 14:18:18 +01:00
|
|
|
# notify about migration to new location:
|
|
|
|
_echoWarning "Your profiles were in the legacy path, migrating to ${PROFILES_PATH} :"
|
|
|
|
|
|
|
|
if [ -n "${legacy_default_profile}" ]; then
|
|
|
|
_echoWarning "> migrating default profile:"
|
|
|
|
echo "${legacy_default_profile}"
|
2018-06-21 15:04:32 +02:00
|
|
|
mv "${BASE_PATH}"/{.ovh*,access.rules} "${PROFILES_PATH}"
|
2018-02-26 14:18:18 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${legacy_profiles}" ]; then
|
|
|
|
_echoWarning "> migrating custom profiles:"
|
|
|
|
echo "${legacy_profiles}"
|
2018-06-21 15:04:32 +02:00
|
|
|
mv "${LEGACY_PROFILES_PATH}"/* "${PROFILES_PATH}"
|
2018-02-26 14:18:18 +01:00
|
|
|
fi
|
|
|
|
|
2018-02-20 17:12:34 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-06-19 18:36:33 +02:00
|
|
|
# if profile is not set, or with value 'default'
|
|
|
|
if [[ -z "${profile}" ]] || [[ "${profile}" == "default" ]]
|
|
|
|
then
|
2018-02-20 17:12:34 +01:00
|
|
|
# configuration stored in the profile main path
|
|
|
|
CURRENT_PATH="${PROFILES_PATH}"
|
2017-06-19 18:36:33 +02:00
|
|
|
else
|
|
|
|
# ensure profile directory exists
|
2017-06-13 18:06:41 +02:00
|
|
|
if [ ! -d "${PROFILES_PATH}/${profile}" ]
|
|
|
|
then
|
2017-06-21 14:31:13 +02:00
|
|
|
case ${createProfile} in
|
|
|
|
get)
|
|
|
|
echo "${PROFILES_PATH}/${profile} should exists"
|
|
|
|
listProfile
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
set)
|
|
|
|
mkdir "${PROFILES_PATH}/${profile}" || exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2017-06-13 18:06:41 +02:00
|
|
|
fi
|
|
|
|
# override default configuration location
|
|
|
|
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
|
2017-06-21 14:31:13 +02:00
|
|
|
fi
|
|
|
|
|
2018-02-12 18:07:45 +01:00
|
|
|
if [ -n "${profile}" ]
|
2017-06-21 14:31:13 +02:00
|
|
|
then
|
2017-06-13 18:06:41 +02:00
|
|
|
HELP_CMD="${HELP_CMD} --profile ${profile}"
|
|
|
|
fi
|
2017-06-19 18:36:33 +02:00
|
|
|
|
2017-06-13 18:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
listProfile()
|
|
|
|
{
|
2017-06-21 00:21:47 +02:00
|
|
|
local dir=
|
2017-06-19 18:36:33 +02:00
|
|
|
echo "Available profiles : "
|
2017-06-21 00:21:47 +02:00
|
|
|
echo "- default"
|
2017-06-21 14:31:13 +02:00
|
|
|
|
2017-06-21 00:21:47 +02:00
|
|
|
if [ -d "${PROFILES_PATH}" ]
|
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
# only list directory
|
|
|
|
for dir in $(cd "${PROFILES_PATH}" && ls -d -- */ 2>/dev/null)
|
2017-06-21 00:21:47 +02:00
|
|
|
do
|
|
|
|
# display directory name without slash
|
|
|
|
echo "- ${dir%%/}"
|
|
|
|
done
|
|
|
|
fi
|
2017-06-13 18:06:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# ensure OVH App Key an App Secret are defined
|
|
|
|
hasOvhAppKey()
|
|
|
|
{
|
2018-06-21 15:04:32 +02:00
|
|
|
if [ -z "${OVH_APP_KEY}" ] && [ -z "${OVH_APP_SECRET}" ]
|
2017-06-13 18:06:41 +02:00
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
echo -e "No application is defined for target ${TARGET}, please call to initialize it:\\n${HELP_CMD} --initApp"
|
2017-06-13 18:06:41 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
main()
|
|
|
|
{
|
2017-06-13 18:06:41 +02:00
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
parseArguments "$@"
|
2017-06-13 18:06:41 +02:00
|
|
|
|
2017-06-21 14:31:13 +02:00
|
|
|
local profileAction="get"
|
|
|
|
|
|
|
|
if [ -n "${INIT_KEY_ACTION}" ]; then
|
|
|
|
profileAction="set"
|
|
|
|
fi
|
|
|
|
|
2018-06-21 15:04:32 +02:00
|
|
|
initProfile "${profileAction}" "${PROFILE}"
|
2017-06-19 18:36:33 +02:00
|
|
|
|
2017-06-21 00:21:47 +02:00
|
|
|
# user want to add An API Key
|
|
|
|
case ${INIT_KEY_ACTION} in
|
|
|
|
AppKey) createApp;;
|
|
|
|
ConsumerKey) createConsumerKey;;
|
|
|
|
esac
|
|
|
|
## exit after initializing any API Keys
|
|
|
|
[ -n "${INIT_KEY_ACTION}" ] && exit 0
|
|
|
|
|
2014-04-17 19:53:28 +02:00
|
|
|
initApplication
|
|
|
|
initConsumerKey
|
2017-06-13 18:06:41 +02:00
|
|
|
|
|
|
|
if hasOvhAppKey
|
2014-04-17 19:53:28 +02:00
|
|
|
then
|
2018-06-21 15:04:32 +02:00
|
|
|
if [ -z "${OVH_CONSUMER_KEY}" ]; then
|
|
|
|
echo "No consumer key for target ${TARGET}, please call to initialize it:"
|
|
|
|
echo "${HELP_CMD} --init"
|
2017-06-13 18:06:41 +02:00
|
|
|
else
|
2018-06-21 15:04:32 +02:00
|
|
|
request "${METHOD}" "${URL}"
|
2017-06-13 18:06:41 +02:00
|
|
|
fi
|
2014-04-17 19:53:28 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
main "$@"
|