Bash coding style review
- functions: use lower case local variables - set some configuration variables as constant - review some variables naming - test command: use built-in [[ instead of [ - always keep if+then in the same line - change shebang for portability Enhancements - API headers: use OVH API's timestamp instead of user's one to bypass misconfigured host. - simplify valid target check - use API_URL when target is set - help output rework - refresh READMEpull/16/head
parent
ec0e2e8a0b
commit
920bc41873
32
README.md
32
README.md
|
@ -9,42 +9,46 @@ Initialize
|
||||||
### Retrieve dependency
|
### Retrieve dependency
|
||||||
|
|
||||||
First in order to retrieve needed dependency, run:
|
First in order to retrieve needed dependency, run:
|
||||||
|
|
||||||
```
|
```
|
||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create an OVH API Application
|
### Create an OVH API Application
|
||||||
|
|
||||||
In order to create a new OVH API application, run:
|
In order to create a new OVH API application, run:
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --initApp
|
./ovh-api-bash-client.sh --initApp
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create a Consumer Key
|
### Create a Consumer Key
|
||||||
|
|
||||||
In order to create a new consumer key, run:
|
In order to create a new consumer key, run:
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --init
|
./ovh-api-bash-client.sh --init
|
||||||
```
|
```
|
||||||
|
|
||||||
Options
|
Options
|
||||||
-------
|
-------
|
||||||
|
|
||||||
### Show help
|
### Show help
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --help
|
./ovh-api-bash-client.sh --help
|
||||||
```
|
```
|
||||||
|
|
||||||
Possible arguments are:
|
Possible arguments are:
|
||||||
|
|
||||||
```
|
```
|
||||||
--url <url> : the API URL to call, for example /domains (default is /me)
|
--url <url> : the API URL to call, for example /domains (default is /me)
|
||||||
--method <method> : the HTTP method to use, for example POST (default is GET)
|
--method <method> : the HTTP method to use, for example POST (default is GET)
|
||||||
--data <JSON data> : the data body to send with the request
|
--data <JSON data> : the data body to send with the request
|
||||||
--target <CA|EU> : the target API (default is EU)
|
--target <target> : the target API [CA|EU|US] (default is EU)
|
||||||
--init : to initialize the consumer key, and manage custom access rules file
|
--init : to initialize the consumer key, and manage custom access rules file
|
||||||
--initApp : to initialize the API application
|
--initApp : to initialize the API application
|
||||||
--list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory
|
--list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory
|
||||||
--profile <value>
|
--profile <profile>
|
||||||
* default : from ~/.ovh-api-bash-client/profile directory
|
* default : from ~/.ovh-api-bash-client/profile directory
|
||||||
* <dir> : from ~/.ovh-api-bash-client/profile/<dir> directory
|
* <dir> : from ~/.ovh-api-bash-client/profile/<dir> directory
|
||||||
```
|
```
|
||||||
|
@ -54,25 +58,29 @@ Usage
|
||||||
|
|
||||||
### Just some examples:
|
### Just some examples:
|
||||||
|
|
||||||
To make a basic call on GET /me just run:
|
To make a basic call on GET `/me` just run:
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh
|
./ovh-api-bash-client.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
To retrieve your domain list, run:
|
To retrieve your domain list, run:
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --url "/domain"
|
./ovh-api-bash-client.sh --url "/domain"
|
||||||
```
|
```
|
||||||
|
|
||||||
To activate the monitoring on your dedicated server, run:
|
To activate the monitoring on your dedicated server, run:
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --method PUT --url "/dedicated/server/ns00000.ovh.net" --data '{"monitoring": true}'
|
./ovh-api-bash-client.sh --method PUT --url "/dedicated/server/ns00000.ovh.net" --data '{"monitoring": true}'
|
||||||
```
|
```
|
||||||
|
|
||||||
To create a Consumer key for different account or usage (profile is created if missing)
|
To create a Consumer key for different account or usage (profile is created if missing)
|
||||||
|
|
||||||
```
|
```
|
||||||
./ovh-api-bash-client.sh --profile demo1 --init
|
./ovh-api-bash-client.sh --profile demo1 --init
|
||||||
./ovh-api-bash-client.sh --profile demo2 --init
|
./ovh-api-bash-client.sh --profile demo2 --init
|
||||||
```
|
```
|
||||||
|
|
||||||
Embedded lib for external scripts
|
Embedded lib for external scripts
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# DEFAULT CONFIG
|
# DEFAULT CONFIG
|
||||||
OVH_CONSUMER_KEY=""
|
OVH_CONSUMER_KEY=""
|
||||||
OVH_APP_KEY=""
|
OVH_APP_KEY=""
|
||||||
OVH_APP_SECRET=""
|
OVH_APP_SECRET=""
|
||||||
|
|
||||||
CONSUMER_KEY_FILE=".ovhConsumerKey"
|
readonly CONSUMER_KEY_FILE=".ovhConsumerKey"
|
||||||
OVH_APPLICATION_FILE=".ovhApplication"
|
readonly OVH_APPLICATION_FILE=".ovhApplication"
|
||||||
LIBS="libs"
|
readonly LIBS="libs"
|
||||||
|
|
||||||
TARGETS=(CA EU US)
|
readonly TARGETS=(CA EU US)
|
||||||
|
|
||||||
declare -A API_URLS
|
declare -A API_URLS
|
||||||
API_URLS[CA]="https://ca.api.ovh.com/1.0"
|
API_URLS[CA]="https://ca.api.ovh.com/1.0"
|
||||||
|
@ -21,10 +21,13 @@ API_CREATE_APP_URLS[CA]="https://ca.api.ovh.com/createApp/"
|
||||||
API_CREATE_APP_URLS[EU]="https://api.ovh.com/createApp/"
|
API_CREATE_APP_URLS[EU]="https://api.ovh.com/createApp/"
|
||||||
API_CREATE_APP_URLS[US]="https://api.ovhcloud.com/createApp/"
|
API_CREATE_APP_URLS[US]="https://api.ovhcloud.com/createApp/"
|
||||||
|
|
||||||
|
readonly API_URLS
|
||||||
|
readonly API_CREATE_APP_URLS
|
||||||
|
|
||||||
## https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
|
## https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
|
||||||
# resolve $SOURCE until the file is no longer a symlink
|
# resolve $SOURCE until the file is no longer a symlink
|
||||||
SOURCE="${BASH_SOURCE[0]}"
|
SOURCE="${BASH_SOURCE[0]}"
|
||||||
while [ -h "${SOURCE}" ]
|
while [[ -h "${SOURCE}" ]]
|
||||||
do
|
do
|
||||||
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
|
||||||
SOURCE="$(readlink "${SOURCE}")"
|
SOURCE="$(readlink "${SOURCE}")"
|
||||||
|
@ -34,20 +37,11 @@ do
|
||||||
done
|
done
|
||||||
BASE_PATH=$( cd -P "$( dirname "${SOURCE}" )" && pwd )
|
BASE_PATH=$( cd -P "$( dirname "${SOURCE}" )" && pwd )
|
||||||
|
|
||||||
LEGACY_PROFILES_PATH="${BASE_PATH}/profile"
|
readonly LEGACY_PROFILES_PATH="${BASE_PATH}/profile"
|
||||||
PROFILES_PATH="${HOME}/.ovh-api-bash-client/profile"
|
readonly PROFILES_PATH="${HOME}/.ovh-api-bash-client/profile"
|
||||||
|
|
||||||
HELP_CMD="$0"
|
HELP_CMD="$0"
|
||||||
|
|
||||||
# THESE VARS WILL BE USED LATER
|
|
||||||
METHOD="GET"
|
|
||||||
URL="/me"
|
|
||||||
TARGET="EU"
|
|
||||||
TIME=""
|
|
||||||
SIGDATA=""
|
|
||||||
POST_DATA=""
|
|
||||||
PROFILE=""
|
|
||||||
|
|
||||||
_echoWarning()
|
_echoWarning()
|
||||||
{
|
{
|
||||||
echo >&2 "[WARNING] $*"
|
echo >&2 "[WARNING] $*"
|
||||||
|
@ -74,38 +68,38 @@ _StringToUpper()
|
||||||
echo "$1" | tr '[:lower:]' '[:upper:]'
|
echo "$1" | tr '[:lower:]' '[:upper:]'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# verify if an array contains an item
|
||||||
|
# _in_array "wanted" "${array[@]}"
|
||||||
|
# _in_array "wanted_key" "${!array[@]}"
|
||||||
|
_in_array()
|
||||||
|
{
|
||||||
|
local item wanted
|
||||||
|
wanted="$1"
|
||||||
|
shift
|
||||||
|
for item; do
|
||||||
|
[[ "${item}" == "${wanted}" ]] && return 0
|
||||||
|
done
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
isTargetValid()
|
isTargetValid()
|
||||||
{
|
{
|
||||||
local VALID
|
if ! _in_array "${TARGET}" "${TARGETS[@]}"; then
|
||||||
VALID=0
|
help "'${TARGET}' is not a valid target, accepted values are: ${TARGETS[*]}"
|
||||||
for i in ${TARGETS[*]}
|
|
||||||
do
|
|
||||||
if [ "$i" == "${TARGET}" ]
|
|
||||||
then
|
|
||||||
VALID=1
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ ${VALID} -eq 0 ]
|
|
||||||
then
|
|
||||||
echo "Error: ${TARGET} is not a valid target, accepted values are: ${TARGETS[*]}"
|
|
||||||
echo
|
|
||||||
help
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
createApp()
|
createApp()
|
||||||
{
|
{
|
||||||
local NEXT
|
local answer
|
||||||
|
|
||||||
echo "For which OVH API do you want to create a new API Application? ($( _arrayJoin "|" "${TARGETS[@]}"))"
|
echo "For which OVH API do you want to create a new API Application? ($( _arrayJoin "|" "${TARGETS[@]}"))"
|
||||||
while [ -z "${NEXT}" ]
|
while [[ -z "${answer}" ]]
|
||||||
do
|
do
|
||||||
read -r NEXT
|
read -r answer
|
||||||
done
|
done
|
||||||
TARGET=$( _StringToUpper "${NEXT}" )
|
TARGET=$( _StringToUpper "${answer}" )
|
||||||
isTargetValid
|
isTargetValid
|
||||||
|
|
||||||
echo
|
echo
|
||||||
|
@ -122,50 +116,44 @@ createApp()
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Do you also need to create a consumer key? (y/n)"
|
echo "Do you also need to create a consumer key? (y/n)"
|
||||||
read -r NEXT
|
read -r answer
|
||||||
if [ -n "${NEXT}" ] && [ "$( _StringToLower "${NEXT}")" == "y" ]
|
if [[ -n "${answer}" ]] && [[ "$( _StringToLower "${answer}")" == "y" ]]; then
|
||||||
then
|
|
||||||
createConsumerKey
|
createConsumerKey
|
||||||
else
|
else
|
||||||
echo -e "OK, no consumer key created for now.\\nYou will be able to initalize the consumer key later calling :\\n${HELP_CMD} --init"
|
echo -e "OK, no consumer key created for now.\\nYou will be able to initalize the consumer key later calling:\\n${HELP_CMD} --init"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
createConsumerKey()
|
createConsumerKey()
|
||||||
{
|
{
|
||||||
local ANSWER
|
local answer
|
||||||
|
|
||||||
METHOD="POST"
|
|
||||||
URL="/auth/credential"
|
|
||||||
|
|
||||||
# ensure an OVH App key is set
|
# ensure an OVH App key is set
|
||||||
initApplication
|
initApplication
|
||||||
hasOvhAppKey || exit 1
|
hasOvhAppKey || exit 1
|
||||||
|
|
||||||
# condition keeped for retro-compatibility, to always allow post accessRules from --data
|
# condition keeped for retro-compatibility, to always allow post accessRules from --data
|
||||||
if [ -z "${POST_DATA}" ]; then
|
if [[ -z "${POST_DATA}" ]]; then
|
||||||
buildAccessRules
|
buildAccessRules
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ANSWER=$(requestNoAuth)
|
answer=$(requestNoAuth "POST" "/auth/credential")
|
||||||
|
|
||||||
getJSONFieldString "${ANSWER}" 'consumerKey' > "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}"
|
getJSONFieldString "${answer}" 'consumerKey' > "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}"
|
||||||
echo "In order to validate the generated consumerKey, visit the validation url at:"
|
echo "In order to validate the generated consumerKey, visit the validation url at:"
|
||||||
getJSONFieldString "${ANSWER}" 'validationUrl'
|
getJSONFieldString "${answer}" 'validationUrl'
|
||||||
}
|
}
|
||||||
|
|
||||||
initConsumerKey()
|
initConsumerKey()
|
||||||
{
|
{
|
||||||
if cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}" &> /dev/null;
|
if cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}" &> /dev/null; then
|
||||||
then
|
|
||||||
OVH_CONSUMER_KEY="$(cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}")"
|
OVH_CONSUMER_KEY="$(cat "${CURRENT_PATH}/${CONSUMER_KEY_FILE}_${TARGET}")"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
initApplication()
|
initApplication()
|
||||||
{
|
{
|
||||||
if cat "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}" &> /dev/null;
|
if cat "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}" &> /dev/null; then
|
||||||
then
|
|
||||||
OVH_APP_KEY=$(sed -n 1p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
OVH_APP_KEY=$(sed -n 1p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
||||||
OVH_APP_SECRET=$(sed -n 2p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
OVH_APP_SECRET=$(sed -n 2p "${CURRENT_PATH}/${OVH_APPLICATION_FILE}_${TARGET}")
|
||||||
fi
|
fi
|
||||||
|
@ -173,31 +161,44 @@ initApplication()
|
||||||
|
|
||||||
updateTime()
|
updateTime()
|
||||||
{
|
{
|
||||||
TIME=$(date '+%s')
|
# use OVH API's timestamp instead of user's one to bypass misconfigured host.
|
||||||
|
curl -s "${API_URL}${url}/auth/time"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# usage:
|
||||||
|
# updateSignData "method" "url" "post_data" "timestamp"
|
||||||
|
# return: print signature
|
||||||
updateSignData()
|
updateSignData()
|
||||||
{
|
{
|
||||||
local SIGDATA
|
local sig_data
|
||||||
SIGDATA="${OVH_APP_SECRET}+${OVH_CONSUMER_KEY}+$1+${API_URLS[${TARGET}]}$2+$3+${TIME}"
|
local method=$1
|
||||||
SIG="\$1\$"$(echo -n "${SIGDATA}" | sha1sum - | cut -d' ' -f1)
|
local url=$2
|
||||||
|
local post_data=$3
|
||||||
|
local timestamp=$4
|
||||||
|
|
||||||
|
sig_data="${OVH_APP_SECRET}+${OVH_CONSUMER_KEY}+${method}+${API_URL}${url}+${post_data}+${timestamp}"
|
||||||
|
echo "\$1\$$(echo -n "${sig_data}" | sha1sum - | cut -d' ' -f1)"
|
||||||
}
|
}
|
||||||
|
|
||||||
help()
|
help()
|
||||||
{
|
{
|
||||||
echo
|
# print error message if set
|
||||||
echo "Help: possible arguments are:"
|
[[ -n "$1" ]] && echo -e "Error: $1\\n"
|
||||||
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)"
|
cat <<EOF
|
||||||
echo " --data <JSON data> : the data body to send with the request"
|
Help: possible arguments are:
|
||||||
echo " --target <$( _arrayJoin "|" "${TARGETS[@]}")> : the target API (default is EU)"
|
--url <url> : the API URL to call, for example /domains (default is /me)
|
||||||
echo " --init : to initialize the consumer key, and manage custom access rules file"
|
--method <method> : the HTTP method to use, for example POST (default is GET)
|
||||||
echo " --initApp : to initialize the API application"
|
--data <JSON data> : the data body to send with the request
|
||||||
echo " --list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory"
|
--target <target> : the target API [$( _arrayJoin "|" "${TARGETS[@]}")] (default is EU)
|
||||||
echo " --profile <value>"
|
--init : to initialize the consumer key, and manage custom access rules file
|
||||||
echo " * default : from ~/.ovh-api-bash-client/profile directory"
|
--initApp : to initialize the API application
|
||||||
echo " * <dir> : from ~/.ovh-api-bash-client/profile/<dir> directory"
|
--list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory
|
||||||
echo
|
--profile <profile>
|
||||||
|
* default : from ~/.ovh-api-bash-client/profile directory
|
||||||
|
* <dir> : from ~/.ovh-api-bash-client/profile/<dir> directory
|
||||||
|
|
||||||
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
buildAccessRules()
|
buildAccessRules()
|
||||||
|
@ -207,7 +208,7 @@ buildAccessRules()
|
||||||
local json_rules
|
local json_rules
|
||||||
local answer
|
local answer
|
||||||
|
|
||||||
if [ ! -f "${access_rules_file}" ]; then
|
if [[ ! -f "${access_rules_file}" ]]; then
|
||||||
echo "${access_rules_file} missing, created full access rules"
|
echo "${access_rules_file} missing, created full access rules"
|
||||||
echo -e "GET /*\\nPUT /*\\nPOST /*\\nDELETE /*" > "${CURRENT_PATH}/access.rules"
|
echo -e "GET /*\\nPUT /*\\nPOST /*\\nDELETE /*" > "${CURRENT_PATH}/access.rules"
|
||||||
fi
|
fi
|
||||||
|
@ -226,12 +227,12 @@ buildAccessRules()
|
||||||
|
|
||||||
while read -r method path;
|
while read -r method path;
|
||||||
do
|
do
|
||||||
if [ -n "${method}" ] && [ -n "${path}" ]; then
|
if [[ -n "${method}" ]] && [[ -n "${path}" ]]; then
|
||||||
json_rules+='{ "method": "'${method}'", "path": "'${path}'"},'
|
json_rules+='{ "method": "'${method}'", "path": "'${path}'"},'
|
||||||
fi
|
fi
|
||||||
done < "${access_rules_file}"
|
done < "${access_rules_file}"
|
||||||
json_rules=${json_rules::-1}
|
json_rules=${json_rules::-1}
|
||||||
if [ -z "${json_rules}" ]; then
|
if [[ -z "${json_rules}" ]]; then
|
||||||
echoWarning "no rule defined, please verify your file '${access_rules_file}'"
|
echoWarning "no rule defined, please verify your file '${access_rules_file}'"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
@ -244,7 +245,7 @@ parseArguments()
|
||||||
# an action launched out of this function
|
# an action launched out of this function
|
||||||
INIT_KEY_ACTION=
|
INIT_KEY_ACTION=
|
||||||
|
|
||||||
while [ $# -gt 0 ]
|
while [[ $# -gt 0 ]]
|
||||||
do
|
do
|
||||||
case $1 in
|
case $1 in
|
||||||
--data)
|
--data)
|
||||||
|
@ -283,8 +284,7 @@ parseArguments()
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unknow parameter $1"
|
help "Unknow parameter $1"
|
||||||
help
|
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
@ -293,82 +293,88 @@ parseArguments()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# usage:
|
||||||
|
# requestNoAuth "method" "url"
|
||||||
requestNoAuth()
|
requestNoAuth()
|
||||||
{
|
{
|
||||||
updateTime
|
local method=$1
|
||||||
curl -s -X "${METHOD}" \
|
local url=$2
|
||||||
|
|
||||||
|
local timestamp
|
||||||
|
timestamp=$(updateTime)
|
||||||
|
|
||||||
|
curl -s -X "${method}" \
|
||||||
--header 'Content-Type:application/json;charset=utf-8' \
|
--header 'Content-Type:application/json;charset=utf-8' \
|
||||||
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
||||||
--header "X-Ovh-Timestamp:${TIME}" \
|
--header "X-Ovh-Timestamp:${timestamp}" \
|
||||||
--data "${POST_DATA}" \
|
--data "${POST_DATA}" \
|
||||||
"${API_URLS[${TARGET}]}${URL}"
|
"${API_URL}${url}"
|
||||||
}
|
}
|
||||||
|
|
||||||
request()
|
request()
|
||||||
{
|
{
|
||||||
local RESPONSE RESPONSE_STATUS RESPONSE_CONTENT
|
local response response_status response_content sig timestamp
|
||||||
|
|
||||||
updateTime
|
timestamp=$(updateTime)
|
||||||
updateSignData "${METHOD}" "${URL}" "${POST_DATA}"
|
sig=$(updateSignData "${METHOD}" "${URL}" "${POST_DATA}" "${timestamp}")
|
||||||
|
|
||||||
RESPONSE=$(curl -s -w "\\n%{http_code}\\n" -X "${METHOD}" \
|
response=$(curl -s -w '\n%{http_code}\n' -X "${METHOD}" \
|
||||||
--header 'Content-Type:application/json;charset=utf-8' \
|
--header 'Content-Type:application/json;charset=utf-8' \
|
||||||
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
--header "X-Ovh-Application:${OVH_APP_KEY}" \
|
||||||
--header "X-Ovh-Timestamp:${TIME}" \
|
--header "X-Ovh-Timestamp:${timestamp}" \
|
||||||
--header "X-Ovh-Signature:${SIG}" \
|
--header "X-Ovh-Signature:${sig}" \
|
||||||
--header "X-Ovh-Consumer:${OVH_CONSUMER_KEY}" \
|
--header "X-Ovh-Consumer:${OVH_CONSUMER_KEY}" \
|
||||||
--data "${POST_DATA}" \
|
--data "${POST_DATA}" \
|
||||||
"${API_URLS[${TARGET}]}${URL}")
|
"${API_URL}${URL}")
|
||||||
|
|
||||||
RESPONSE_STATUS=$(echo "${RESPONSE}" | sed -n '$p')
|
response_status=$(echo "${response}" | sed -n '$p')
|
||||||
RESPONSE_CONTENT=$(echo "${RESPONSE}" | sed '$d')
|
response_content=$(echo "${response}" | sed '$d')
|
||||||
echo "${RESPONSE_STATUS} ${RESPONSE_CONTENT}"
|
echo "${response_status} ${response_content}"
|
||||||
}
|
}
|
||||||
|
|
||||||
getJSONFieldString()
|
getJSONFieldString()
|
||||||
{
|
{
|
||||||
local JSON FIELD RESULT
|
local json field result
|
||||||
|
|
||||||
JSON="$1"
|
json="$1"
|
||||||
FIELD="$2"
|
field="$2"
|
||||||
# shellcheck disable=SC1117
|
# shellcheck disable=SC1117
|
||||||
RESULT=$(echo "${JSON}" | "${BASE_PATH}/${LIBS}/JSON.sh" | grep "\[\"${FIELD}\"\]" | sed -r "s/\[\"${FIELD}\"\]\s+(.*)/\1/")
|
result=$(echo "${json}" | "${BASE_PATH}/${LIBS}/JSON.sh" | grep "\[\"${field}\"\]" | sed -r "s/\[\"${field}\"\]\s+(.*)/\1/")
|
||||||
echo "${RESULT:1:${#RESULT}-2}"
|
echo "${result:1:${#result}-2}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# set CURRENT_PATH with profile name
|
# set CURRENT_PATH with profile name
|
||||||
# usage : initProfile |set|get] profile_name
|
# usage: initProfile |set|get] profile_name
|
||||||
# set : create the profile if missing
|
# set: create the profile if missing
|
||||||
# get : raise an error if no profile with that name
|
# get: raise an error if no profile with that name
|
||||||
initProfile()
|
initProfile()
|
||||||
{
|
{
|
||||||
local createProfile=$1
|
local create_profile=$1
|
||||||
local profile=$2
|
local profile=$2
|
||||||
|
|
||||||
if [ ! -d "${PROFILES_PATH}" ]
|
if [[ ! -d "${PROFILES_PATH}" ]]; then
|
||||||
then
|
|
||||||
mkdir -pv "${PROFILES_PATH}" || exit 1
|
mkdir -pv "${PROFILES_PATH}" || exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# checking if some profiles remains in legacy profile path
|
# checking if some profiles remains in legacy profile path
|
||||||
local legacy_profiles=
|
local legacy_profiles=
|
||||||
local legacy_default_profile=
|
local legacy_default_profile=
|
||||||
if [ -d "${LEGACY_PROFILES_PATH}" ]; then
|
if [[ -d "${LEGACY_PROFILES_PATH}" ]]; then
|
||||||
# is there any profile in legacy path ?
|
# is there any profile in legacy path ?
|
||||||
legacy_profiles=$(ls -A "${LEGACY_PROFILES_PATH}" 2>/dev/null)
|
legacy_profiles=$(ls -A "${LEGACY_PROFILES_PATH}" 2>/dev/null)
|
||||||
legacy_default_profile=$(cd "${BASE_PATH}" && ls .ovh* access.rules 2>/dev/null)
|
legacy_default_profile=$(cd "${BASE_PATH}" && ls .ovh* access.rules 2>/dev/null)
|
||||||
|
|
||||||
if [ -n "${legacy_profiles}" ] || [ -n "${legacy_default_profile}" ]; then
|
if [[ -n "${legacy_profiles}" ]] || [[ -n "${legacy_default_profile}" ]]; then
|
||||||
# notify about migration to new location:
|
# notify about migration to new location:
|
||||||
_echoWarning "Your profiles were in the legacy path, migrating to ${PROFILES_PATH} :"
|
_echoWarning "Your profiles were in the legacy path, migrating to ${PROFILES_PATH}:"
|
||||||
|
|
||||||
if [ -n "${legacy_default_profile}" ]; then
|
if [[ -n "${legacy_default_profile}" ]]; then
|
||||||
_echoWarning "> migrating default profile:"
|
_echoWarning "> migrating default profile:"
|
||||||
echo "${legacy_default_profile}"
|
echo "${legacy_default_profile}"
|
||||||
mv "${BASE_PATH}"/{.ovh*,access.rules} "${PROFILES_PATH}"
|
mv "${BASE_PATH}"/{.ovh*,access.rules} "${PROFILES_PATH}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "${legacy_profiles}" ]; then
|
if [[ -n "${legacy_profiles}" ]]; then
|
||||||
_echoWarning "> migrating custom profiles:"
|
_echoWarning "> migrating custom profiles:"
|
||||||
echo "${legacy_profiles}"
|
echo "${legacy_profiles}"
|
||||||
mv "${LEGACY_PROFILES_PATH}"/* "${PROFILES_PATH}"
|
mv "${LEGACY_PROFILES_PATH}"/* "${PROFILES_PATH}"
|
||||||
|
@ -378,15 +384,13 @@ initProfile()
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# if profile is not set, or with value 'default'
|
# if profile is not set, or with value 'default'
|
||||||
if [[ -z "${profile}" ]] || [[ "${profile}" == "default" ]]
|
if [[ -z "${profile}" ]] || [[ "${profile}" == "default" ]]; then
|
||||||
then
|
|
||||||
# configuration stored in the profile main path
|
# configuration stored in the profile main path
|
||||||
CURRENT_PATH="${PROFILES_PATH}"
|
CURRENT_PATH="${PROFILES_PATH}"
|
||||||
else
|
else
|
||||||
# ensure profile directory exists
|
# ensure profile directory exists
|
||||||
if [ ! -d "${PROFILES_PATH}/${profile}" ]
|
if [[ ! -d "${PROFILES_PATH}/${profile}" ]]; then
|
||||||
then
|
case ${create_profile} in
|
||||||
case ${createProfile} in
|
|
||||||
get)
|
get)
|
||||||
echo "${PROFILES_PATH}/${profile} should exists"
|
echo "${PROFILES_PATH}/${profile} should exists"
|
||||||
listProfile
|
listProfile
|
||||||
|
@ -401,8 +405,7 @@ initProfile()
|
||||||
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
|
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "${profile}" ]
|
if [[ -n "${profile}" ]]; then
|
||||||
then
|
|
||||||
HELP_CMD="${HELP_CMD} --profile ${profile}"
|
HELP_CMD="${HELP_CMD} --profile ${profile}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -411,11 +414,10 @@ initProfile()
|
||||||
listProfile()
|
listProfile()
|
||||||
{
|
{
|
||||||
local dir=
|
local dir=
|
||||||
echo "Available profiles : "
|
echo "Available profiles: "
|
||||||
echo "- default"
|
echo "- default"
|
||||||
|
|
||||||
if [ -d "${PROFILES_PATH}" ]
|
if [[ -d "${PROFILES_PATH}" ]]; then
|
||||||
then
|
|
||||||
# only list directory
|
# only list directory
|
||||||
for dir in $(cd "${PROFILES_PATH}" && ls -d -- */ 2>/dev/null)
|
for dir in $(cd "${PROFILES_PATH}" && ls -d -- */ 2>/dev/null)
|
||||||
do
|
do
|
||||||
|
@ -428,8 +430,7 @@ listProfile()
|
||||||
# ensure OVH App Key an App Secret are defined
|
# ensure OVH App Key an App Secret are defined
|
||||||
hasOvhAppKey()
|
hasOvhAppKey()
|
||||||
{
|
{
|
||||||
if [ -z "${OVH_APP_KEY}" ] && [ -z "${OVH_APP_SECRET}" ]
|
if [[ -z "${OVH_APP_KEY}" ]] && [[ -z "${OVH_APP_SECRET}" ]]; then
|
||||||
then
|
|
||||||
echo -e "No application is defined for target ${TARGET}, please call to initialize it:\\n${HELP_CMD} --initApp"
|
echo -e "No application is defined for target ${TARGET}, please call to initialize it:\\n${HELP_CMD} --initApp"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
@ -438,12 +439,20 @@ hasOvhAppKey()
|
||||||
|
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
|
|
||||||
parseArguments "$@"
|
parseArguments "$@"
|
||||||
|
|
||||||
|
# set to default value if empty
|
||||||
|
TARGET=${TARGET:-"EU"}
|
||||||
|
METHOD=${METHOD:-"GET"}
|
||||||
|
URL=${URL:-"/me"}
|
||||||
|
PROFILE=${PROFILE:-"default"}
|
||||||
|
POST_DATA=${POST_DATA:-}
|
||||||
|
|
||||||
|
readonly API_URL="${API_URLS[${TARGET}]}"
|
||||||
|
|
||||||
local profileAction="get"
|
local profileAction="get"
|
||||||
|
|
||||||
if [ -n "${INIT_KEY_ACTION}" ]; then
|
if [[ -n "${INIT_KEY_ACTION}" ]]; then
|
||||||
profileAction="set"
|
profileAction="set"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -455,14 +464,13 @@ main()
|
||||||
ConsumerKey) createConsumerKey;;
|
ConsumerKey) createConsumerKey;;
|
||||||
esac
|
esac
|
||||||
## exit after initializing any API Keys
|
## exit after initializing any API Keys
|
||||||
[ -n "${INIT_KEY_ACTION}" ] && exit 0
|
[[ -n "${INIT_KEY_ACTION}" ]] && exit 0
|
||||||
|
|
||||||
initApplication
|
initApplication
|
||||||
initConsumerKey
|
initConsumerKey
|
||||||
|
|
||||||
if hasOvhAppKey
|
if hasOvhAppKey; then
|
||||||
then
|
if [[ -z "${OVH_CONSUMER_KEY}" ]]; then
|
||||||
if [ -z "${OVH_CONSUMER_KEY}" ]; then
|
|
||||||
echo "No consumer key for target ${TARGET}, please call to initialize it:"
|
echo "No consumer key for target ${TARGET}, please call to initialize it:"
|
||||||
echo "${HELP_CMD} --init"
|
echo "${HELP_CMD} --init"
|
||||||
else
|
else
|
||||||
|
@ -471,5 +479,4 @@ main()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|
Loading…
Reference in New Issue