Allow multiple API keys management with profiles
- add --profile to override default configuration location - add --list-profile to display available profiles - rework messages to display command with the current profile if set Bugfix - ensure OVH App Key an App Secret are defined when creating consumer key Enhancements - when using --init, allow override default "all permissions grants" if --data is set - add -h/--helppull/4/head
parent
5719c46b2d
commit
1a0e3f220f
|
@ -1,5 +1,5 @@
|
||||||
.ovhApplication*
|
.ovhApplication*
|
||||||
.ovhConsumerKey*
|
.ovhConsumerKey*
|
||||||
libs/
|
libs/
|
||||||
|
profile/
|
||||||
|
|
||||||
|
|
14
README.md
14
README.md
|
@ -30,6 +30,11 @@ In order to create a new consumer key, run:
|
||||||
Options
|
Options
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
### Show 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)
|
||||||
|
@ -38,6 +43,8 @@ Possible arguments are:
|
||||||
--target <CA|EU> : the target API (default is EU)
|
--target <CA|EU> : the target API (default is EU)
|
||||||
--init : to initialize the consumer key
|
--init : to initialize the consumer key
|
||||||
--initApp : to initialize the API application
|
--initApp : to initialize the API application
|
||||||
|
--profile <dir> : load a configuration from profile/ directory, (override default location)
|
||||||
|
--list-profile : list available profiles in profile/ directory
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
|
@ -60,3 +67,10 @@ 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}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
create a Consumer key for different account or usage
|
||||||
|
```
|
||||||
|
mkdir profile/demo1
|
||||||
|
mkdir profile/demo2
|
||||||
|
./ovh-api-bash-client.sh --profile demo1 --init
|
||||||
|
./ovh-api-bash-client.sh --profile demo2 --init
|
||||||
|
```
|
||||||
|
|
|
@ -18,8 +18,13 @@ API_URLS[EU]="https://api.ovh.com/1.0"
|
||||||
declare -A API_CREATE_APP_URLS
|
declare -A API_CREATE_APP_URLS
|
||||||
API_CREATE_APP_URLS[CA]="https://ca.api.ovh.com/createApp/"
|
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/"
|
||||||
CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
||||||
|
|
||||||
|
BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
CURRENT_PATH="${BASE_PATH}"
|
||||||
|
#PROFILES_PATH
|
||||||
|
PROFILES_PATH="${BASE_PATH}/profile"
|
||||||
|
|
||||||
|
HELP_CMD="$0"
|
||||||
|
|
||||||
# THESE VARS WILL BE USED LATER
|
# THESE VARS WILL BE USED LATER
|
||||||
METHOD="GET"
|
METHOD="GET"
|
||||||
|
@ -53,6 +58,7 @@ isTargetValid()
|
||||||
|
|
||||||
createApp()
|
createApp()
|
||||||
{
|
{
|
||||||
|
|
||||||
echo "For which OVH API do you want to create a new API Application? ($( echo ${TARGETS[@]} | sed 's/\s/|/g' ))"
|
echo "For which OVH API do you want to create a new API Application? ($( echo ${TARGETS[@]} | sed 's/\s/|/g' ))"
|
||||||
while [ -z "$NEXT" ]
|
while [ -z "$NEXT" ]
|
||||||
do
|
do
|
||||||
|
@ -81,7 +87,7 @@ createApp()
|
||||||
initApplication
|
initApplication
|
||||||
createConsumerKey
|
createConsumerKey
|
||||||
else
|
else
|
||||||
echo -e "OK, no consumer key created for now.\nYou will be able to initiaze the consumer key later calling :\n$0 --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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,9 +95,16 @@ createConsumerKey()
|
||||||
{
|
{
|
||||||
METHOD="POST"
|
METHOD="POST"
|
||||||
URL="/auth/credential"
|
URL="/auth/credential"
|
||||||
POST_DATA='{ "accessRules": [ { "method": "GET", "path": "/*"}, { "method": "PUT", "path": "/*"}, { "method": "POST", "path": "/*"}, { "method": "DELETE", "path": "/*"} ] }'
|
|
||||||
|
|
||||||
|
# ensure an OVH App key is set
|
||||||
|
hasOvhAppKey || exit 1
|
||||||
|
|
||||||
|
# all grants if no post data defined
|
||||||
|
if [ -z "${POST_DATA}" ]; then
|
||||||
|
POST_DATA='{ "accessRules": [ { "method": "GET", "path": "/*"}, { "method": "PUT", "path": "/*"}, { "method": "POST", "path": "/*"}, { "method": "DELETE", "path": "/*"} ] }'
|
||||||
|
fi
|
||||||
ANSWER=$(requestNoAuth)
|
ANSWER=$(requestNoAuth)
|
||||||
|
|
||||||
getJSONFieldString "$ANSWER" 'consumerKey' > $CURRENT_PATH/${CONSUMER_KEY_FILE}_${TARGET}
|
getJSONFieldString "$ANSWER" 'consumerKey' > $CURRENT_PATH/${CONSUMER_KEY_FILE}_${TARGET}
|
||||||
echo -e "In order to validate the generated consumerKey, visit the validation url at:\n$(getJSONFieldString "$ANSWER" 'validationUrl')"
|
echo -e "In order to validate the generated consumerKey, visit the validation url at:\n$(getJSONFieldString "$ANSWER" 'validationUrl')"
|
||||||
}
|
}
|
||||||
|
@ -128,7 +141,7 @@ updateSignData()
|
||||||
|
|
||||||
help()
|
help()
|
||||||
{
|
{
|
||||||
echo
|
echo
|
||||||
echo "Help: possible arguments are:"
|
echo "Help: possible arguments are:"
|
||||||
echo " --url <url> : the API URL to call, for example /domains (default is /me)"
|
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 " --method <method> : the HTTP method to use, for example POST (default is GET)"
|
||||||
|
@ -136,6 +149,8 @@ help()
|
||||||
echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)"
|
echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)"
|
||||||
echo " --init : to initialize the consumer key"
|
echo " --init : to initialize the consumer key"
|
||||||
echo " --initApp : to initialize the API application"
|
echo " --initApp : to initialize the API application"
|
||||||
|
echo " --profile <dir> : load a configuration from profile/ directory, (override default location)"
|
||||||
|
echo " --list-profile : list available profiles in profile/ directory"
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,6 +185,19 @@ parseArguments()
|
||||||
TARGET=$1
|
TARGET=$1
|
||||||
isTargetValid
|
isTargetValid
|
||||||
;;
|
;;
|
||||||
|
--profile)
|
||||||
|
shift
|
||||||
|
initProfile "$1"
|
||||||
|
;;
|
||||||
|
--list-profile)
|
||||||
|
shift
|
||||||
|
listProfile
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
--help|-h)
|
||||||
|
help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unknow parameter $1"
|
echo "Unknow parameter $1"
|
||||||
help
|
help
|
||||||
|
@ -201,28 +229,70 @@ getJSONFieldString()
|
||||||
{
|
{
|
||||||
JSON="$1"
|
JSON="$1"
|
||||||
FIELD="$2"
|
FIELD="$2"
|
||||||
RESULT=$(echo $JSON | $CURRENT_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}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# override CURRENT_PATH with profile name
|
||||||
|
# usage : initProfile profile_name
|
||||||
|
initProfile()
|
||||||
|
{
|
||||||
|
local profile=$1
|
||||||
|
if [ -n "${profile}" ]; then
|
||||||
|
|
||||||
|
if [ ! -d "${PROFILES_PATH}/${profile}" ]
|
||||||
|
then
|
||||||
|
echo "${PROFILES_PATH}/${profile} should exists"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# override default configuration location
|
||||||
|
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
|
||||||
|
HELP_CMD="${HELP_CMD} --profile ${profile}"
|
||||||
|
else
|
||||||
|
echo "profile not set, choose in : "
|
||||||
|
listProfile
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
listProfile()
|
||||||
|
{
|
||||||
|
cd ${PROFILES_PATH} && ls -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# ensure OVH App Key an App Secret are defined
|
||||||
|
hasOvhAppKey()
|
||||||
|
{
|
||||||
|
if [ -z "$OVH_APP_KEY" ] && [ -z "$OVH_APP_SECRET" ]
|
||||||
|
then
|
||||||
|
echo -e "No application is defined for target $TARGET, please call to initialize it:\n${HELP_CMD} --initApp"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if [ ! -d "${PROFILES_PATH}" ]
|
||||||
|
then
|
||||||
|
mkdir "${PROFILES_PATH}"
|
||||||
|
fi
|
||||||
|
|
||||||
parseArguments "$@"
|
parseArguments "$@"
|
||||||
|
|
||||||
initApplication
|
initApplication
|
||||||
initConsumerKey
|
initConsumerKey
|
||||||
|
|
||||||
if [ -z $OVH_APP_KEY ] && [ -z $OVH_APP_SECRET ]
|
if hasOvhAppKey
|
||||||
then
|
then
|
||||||
echo -e "No application is defined for target $TARGET, please call to initialize it:\n$0 --initApp"
|
if [ -z "$OVH_CONSUMER_KEY" ]; then
|
||||||
elif [ -z $OVH_CONSUMER_KEY ]
|
echo -e "No consumer key for target $TARGET, please call to initialize it:\n${HELP_CMD} --init"
|
||||||
then
|
else
|
||||||
echo -e "No consumer key for target $TARGET, please call to initialize it:\n$0 --init"
|
|
||||||
else
|
|
||||||
request $METHOD $URL
|
request $METHOD $URL
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue