From b0df122812228c8a0c27fa03a8d54873dc904e99 Mon Sep 17 00:00:00 2001 From: Severin Neumann Date: Thu, 9 Nov 2017 18:06:44 +0100 Subject: [PATCH] Added optional arguments for apiCall and added event list --- adc.sh | 109 ++++++++++++++++++--------------------- commands/event/create.sh | 41 +-------------- commands/event/list.sh | 10 ++++ helpers/apiCall.sh | 40 +++++++++----- helpers/output.sh | 6 --- main.sh | 17 +++++- test.sh | 15 ++++-- 7 files changed, 119 insertions(+), 119 deletions(-) create mode 100644 commands/event/list.sh diff --git a/adc.sh b/adc.sh index 7e1f505..70b9054 100755 --- a/adc.sh +++ b/adc.sh @@ -1,6 +1,6 @@ #!/bin/bash ADC_VERSION="v0.2.0" -ADC_LAST_COMMIT="423890fe40e000d1f75fa6fc96065877ecdb9e2c" +ADC_LAST_COMMIT="1b5e629b1d5f9ec47d11e15e71e62df6a301988c" USER_CONFIG="$HOME/.appdynamics/adc/config.sh" GLOBAL_CONFIG="/etc/appdynamics/adc/config.sh" CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt" @@ -13,6 +13,12 @@ CONFIG_PORTAL_COOKIE_LOCATION="/tmp/appdynamics-portal-cookie.txt" # - output (msg to stdout) # An empty string silents all output CONFIG_OUTPUT_VERBOSITY="error,output" +# Default Colors +COLOR_WARNING="\033[0;33m" +COLOR_INFO="\033[0;32m" +COLOR_ERROR="\033[0;31m" +COLOR_DEBUG="\033[0;35m" +COLOR_RESET="\033[0m" GLOBAL_COMMANDS="" GLOBAL_HELP="" GLOBAL_LONG_HELP_COUNTER=0 @@ -30,11 +36,6 @@ function describe { GLOBAL_LONG_HELP_COMMANDS[${#GLOBAL_LONG_HELP_COMMANDS[@]}]="$1" GLOBAL_LONG_HELP_STRINGS[${#GLOBAL_LONG_HELP_STRINGS[@]}]=$(cat) } -COLOR_WARNING="\033[0;33m" -COLOR_INFO="\033[0;32m" -COLOR_ERROR="\033[0;31m" -COLOR_DEBUG="\033[0;35m" -COLOR_RESET="\033[0m" function debug { if [ "${CONFIG_OUTPUT_VERBOSITY/debug}" != "$CONFIG_OUTPUT_VERBOSITY" ]; then echo -e "${COLOR_DEBUG}DEBUG: $*${COLOR_RESET}" @@ -106,6 +107,7 @@ function recursiveSource { } function apiCall { local OPTS + local OPTIONAL_OPTIONS="" local METHOD="GET" while getopts "X:d:" opt "$@"; do @@ -127,39 +129,53 @@ function apiCall { OLDIFS=$IFS IFS="\$" for MATCH in $PAYLOAD ; do - if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then - MATCH=${MATCH:1} - OPT=${MATCH%%\}*}: + if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then + OPT=${BASH_REMATCH[1]}: + if [ "${BASH_REMATCH[2]}" = "?" ] ; then + OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT} + fi OPTS="${OPTS}${OPT}" fi done; for MATCH in $ENDPOINT ; do - if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then - MATCH=${MATCH:1} - OPT=${MATCH%%\}*}: + if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then + OPT=${BASH_REMATCH[1]}: + if [ "${BASH_REMATCH[2]}" = "?" ] ; then + OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT} + fi OPTS="${OPTS}${OPT}" fi done; IFS=$OLDIFS + debug "Identified Options: ${OPTS}" + debug "Optional Options: $OPTIONAL_OPTIONS" if [ -n "$OPTS" ] ; then while getopts ${OPTS} opt; do - PAYLOAD=${PAYLOAD//\$\{$opt\}/$OPTARG} - ENDPOINT=${ENDPOINT//\$\{$opt\}/$OPTARG} + local ARG=`urlencode "$OPTARG"` + debug "Applying $opt with $ARG" + # PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG} + # ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG} + while [[ $PAYLOAD =~ \${$opt\??} ]] ; do + PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG} + done; + while [[ $ENDPOINT =~ \${$opt\??} ]] ; do + ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG} + done; done shiftOptInd shift $SHIFTS fi - while [[ $PAYLOAD =~ \${[^}]*} ]] ; do - if [ -z "$1" ] ; then + while [[ $PAYLOAD =~ \${([a-zA-Z])(\??)} ]] ; do + if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then error "Please provide an argument for paramater -${BASH_REMATCH:2:1}" return; fi PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$1} shift done - while [[ $ENDPOINT =~ \${[^}]*} ]] ; do - if [ -z "$1" ] ; then + while [[ $ENDPOINT =~ \${([a-zA-Z])(\??)} ]] ; do + if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then error "Please provide an argument for paramater -${BASH_REMATCH:2:1}" return; fi @@ -742,48 +758,18 @@ describe dbmon_delete << EOF Delete a database collector. Provide the collector id as parameter. EOF function event_create { - local APPLICATION=${CONFIG_CONTROLLER_DEFAULT_APPLICATION} - local NODE - local TIER - local SEVERITY - local EVENTTYPE - local BT - local COMMENT - while getopts "n:e:s:t:c:a:" opt "$@"; - do - case "${opt}" in - n) - NODE=${OPTARG} - ;; - t) - TIER=${OPTARG} - ;; - s) - SEVERITY=${OPTARG} - ;; - e) - EVENTTYPE=${OPTARG} - ;; - b) - BT=${OPTARG} - ;; - c) - COMMENT=`urlencode "$OPTARG"` - ;; - a) - APPLICATION=${OPTARG} - ;; - esac - done; - shiftOptInd - shift $SHIFTS - SUMMARY=`urlencode "$*"` - debug -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}" - controller_call -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}" + apiCall -X POST "/controller/rest/applications/\${a}/events?summary=\${s}&comment=\${c?}&eventtype=\${e}&severity=\${l}&bt=&\${b?}node=\${n?}&tier=\${t?}" "$@" } register event_create Create a custom event for a given application describe event_create << EOF -Create a custom event for a given application +Create a custom event for a given application. Application, summary, event type and severity are required parameters. +EOF +function event_list { + apiCall '/controller/rest/applications/${a}/events?time-range-type=${t}&duration-in-mins=${d?}&start-time=${b?}&end-time=${f?}&event-types=${e}&severities=${s}' "$@" +} +register event_list List all events for a given time range. +describe event_list << EOF +List all events for a given time range. EOF function timerange_create { local START_TIME=-1 @@ -893,7 +879,7 @@ else warning "File ${USER_CONFIG} not found!" fi # Parse global options -while getopts "H:C:J:D:P:S:F:v" opt; +while getopts "H:C:J:D:P:S:F:Nv" opt; do case "${opt}" in H) @@ -924,6 +910,13 @@ do CONFIG_PORTAL_CREDENTIALS=${OPTARG} debug "Set CONFIG_PORTAL_CREDENTIALS=${CONFIG_PORTAL_CREDENTIALS}" ;; + N) + COLOR_WARNING="" + COLOR_INFO="" + COLOR_ERROR="" + COLOR_DEBUG="" + COLOR_RESET="" + ;; F) CONTROLLER_INFO_XML=${OPTARG} debug "Reading CONFIG_CONTROLLER_HOST from $CONTROLLER_INFO_XML" diff --git a/commands/event/create.sh b/commands/event/create.sh index f403a70..a375ca3 100644 --- a/commands/event/create.sh +++ b/commands/event/create.sh @@ -1,47 +1,10 @@ #!/bin/bash function event_create { - local APPLICATION=${CONFIG_CONTROLLER_DEFAULT_APPLICATION} - local NODE - local TIER - local SEVERITY - local EVENTTYPE - local BT - local COMMENT - while getopts "n:e:s:t:c:a:" opt "$@"; - do - case "${opt}" in - n) - NODE=${OPTARG} - ;; - t) - TIER=${OPTARG} - ;; - s) - SEVERITY=${OPTARG} - ;; - e) - EVENTTYPE=${OPTARG} - ;; - b) - BT=${OPTARG} - ;; - c) - COMMENT=`urlencode "$OPTARG"` - ;; - a) - APPLICATION=${OPTARG} - ;; - esac - done; - shiftOptInd - shift $SHIFTS - SUMMARY=`urlencode "$*"` - debug -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}" - controller_call -X POST "/controller/rest/applications/${APPLICATION}/events?summary=${SUMMARY}&comment=${COMMENT}&eventtype=${EVENTTYPE}&severity=${SEVERITY}&bt=${BT}&node=${NODE}&tier=${TIER}" + apiCall -X POST "/controller/rest/applications/\${a}/events?summary=\${s}&comment=\${c?}&eventtype=\${e}&severity=\${l}&bt=&\${b?}node=\${n?}&tier=\${t?}" "$@" } register event_create Create a custom event for a given application describe event_create << EOF -Create a custom event for a given application +Create a custom event for a given application. Application, summary, event type and severity are required parameters. EOF diff --git a/commands/event/list.sh b/commands/event/list.sh new file mode 100644 index 0000000..1d1d749 --- /dev/null +++ b/commands/event/list.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +function event_list { + apiCall '/controller/rest/applications/${a}/events?time-range-type=${t}&duration-in-mins=${d?}&start-time=${b?}&end-time=${f?}&event-types=${e}&severities=${s}' "$@" +} + +register event_list List all events for a given time range. +describe event_list << EOF +List all events for a given time range. +EOF diff --git a/helpers/apiCall.sh b/helpers/apiCall.sh index a6aec92..5be08b5 100755 --- a/helpers/apiCall.sh +++ b/helpers/apiCall.sh @@ -2,6 +2,7 @@ function apiCall { local OPTS + local OPTIONAL_OPTIONS="" local METHOD="GET" while getopts "X:d:" opt "$@"; @@ -26,34 +27,49 @@ function apiCall { OLDIFS=$IFS IFS="\$" for MATCH in $PAYLOAD ; do - if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then - MATCH=${MATCH:1} - OPT=${MATCH%%\}*}: + if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then + OPT=${BASH_REMATCH[1]}: + if [ "${BASH_REMATCH[2]}" = "?" ] ; then + OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT} + fi OPTS="${OPTS}${OPT}" fi done; for MATCH in $ENDPOINT ; do - if [ "${MATCH::1}" = "{" ] && [ "${MATCH:2:1}" = "}" ] ; then - MATCH=${MATCH:1} - OPT=${MATCH%%\}*}: + if [[ $MATCH =~ \{([a-zA-Z])(\??)\} ]]; then + OPT=${BASH_REMATCH[1]}: + if [ "${BASH_REMATCH[2]}" = "?" ] ; then + OPTIONAL_OPTIONS=${OPTIONAL_OPTIONS}${OPT} + fi OPTS="${OPTS}${OPT}" fi done; IFS=$OLDIFS + debug "Identified Options: ${OPTS}" + debug "Optional Options: $OPTIONAL_OPTIONS" + if [ -n "$OPTS" ] ; then while getopts ${OPTS} opt; do - PAYLOAD=${PAYLOAD//\$\{$opt\}/$OPTARG} - ENDPOINT=${ENDPOINT//\$\{$opt\}/$OPTARG} + local ARG=`urlencode "$OPTARG"` + debug "Applying $opt with $ARG" + # PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG} + # ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG} + while [[ $PAYLOAD =~ \${$opt\??} ]] ; do + PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG} + done; + while [[ $ENDPOINT =~ \${$opt\??} ]] ; do + ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG} + done; done shiftOptInd shift $SHIFTS fi - while [[ $PAYLOAD =~ \${[^}]*} ]] ; do - if [ -z "$1" ] ; then + while [[ $PAYLOAD =~ \${([a-zA-Z])(\??)} ]] ; do + if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then error "Please provide an argument for paramater -${BASH_REMATCH:2:1}" return; fi @@ -61,8 +77,8 @@ function apiCall { shift done - while [[ $ENDPOINT =~ \${[^}]*} ]] ; do - if [ -z "$1" ] ; then + while [[ $ENDPOINT =~ \${([a-zA-Z])(\??)} ]] ; do + if [ -z "$1" ] && [[ "${OPTIONAL_OPTIONS}" != *"${BASH_REMATCH[1]}"* ]] ; then error "Please provide an argument for paramater -${BASH_REMATCH:2:1}" return; fi diff --git a/helpers/output.sh b/helpers/output.sh index 72a7378..8697721 100755 --- a/helpers/output.sh +++ b/helpers/output.sh @@ -1,11 +1,5 @@ #!/bin/bash -COLOR_WARNING="\033[0;33m" -COLOR_INFO="\033[0;32m" -COLOR_ERROR="\033[0;31m" -COLOR_DEBUG="\033[0;35m" -COLOR_RESET="\033[0m" - function debug { if [ "${CONFIG_OUTPUT_VERBOSITY/debug}" != "$CONFIG_OUTPUT_VERBOSITY" ]; then echo -e "${COLOR_DEBUG}DEBUG: $*${COLOR_RESET}" diff --git a/main.sh b/main.sh index a933c93..76389d8 100755 --- a/main.sh +++ b/main.sh @@ -16,6 +16,13 @@ CONFIG_PORTAL_COOKIE_LOCATION="/tmp/appdynamics-portal-cookie.txt" # An empty string silents all output CONFIG_OUTPUT_VERBOSITY="error,output" +# Default Colors +COLOR_WARNING="\033[0;33m" +COLOR_INFO="\033[0;32m" +COLOR_ERROR="\033[0;31m" +COLOR_DEBUG="\033[0;35m" +COLOR_RESET="\033[0m" + GLOBAL_COMMANDS="" GLOBAL_HELP="" GLOBAL_LONG_HELP_COUNTER=0 @@ -78,6 +85,7 @@ source ./commands/dbmon/list.sh source ./commands/dbmon/delete.sh source ./commands/event/create.sh +source ./commands/event/list.sh source ./commands/timerange/create.sh source ./commands/timerange/list.sh @@ -104,7 +112,7 @@ else fi # Parse global options -while getopts "H:C:J:D:P:S:F:v" opt; +while getopts "H:C:J:D:P:S:F:Nv" opt; do case "${opt}" in H) @@ -135,6 +143,13 @@ do CONFIG_PORTAL_CREDENTIALS=${OPTARG} debug "Set CONFIG_PORTAL_CREDENTIALS=${CONFIG_PORTAL_CREDENTIALS}" ;; + N) + COLOR_WARNING="" + COLOR_INFO="" + COLOR_ERROR="" + COLOR_DEBUG="" + COLOR_RESET="" + ;; F) CONTROLLER_INFO_XML=${OPTARG} debug "Reading CONFIG_CONTROLLER_HOST from $CONTROLLER_INFO_XML" diff --git a/test.sh b/test.sh index 951301d..d60599c 100755 --- a/test.sh +++ b/test.sh @@ -53,7 +53,7 @@ echo "Sourcing user config for controller host and credentials..." source "$HOME/.appdynamics/adc/config.sh" echo "Will use the following controller for testing: $CONFIG_CONTROLLER_HOST" -ADC="./adc.sh -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH" +ADC="./adc.sh -N -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH" #### BEGIN TESTS #### ##### Test controller functionality ##### @@ -69,7 +69,6 @@ assert_contains_substring "\"name\" : \"${APPNAME}\"," "$CREATE_APPLICATION" if [[ $CREATE_APPLICATION =~ \"id\"\ \:\ ([0-9]+) ]] ; then APPLICATION_ID=${BASH_REMATCH[1]} - ##### List different entities ##### assert_contains_substring "" "`${ADC} application list`" assert_contains_substring "" "`${ADC} tier list -a $APPLICATION_ID`" @@ -89,11 +88,21 @@ if [[ $CREATE_APPLICATION =~ \"id\"\ \:\ ([0-9]+) ]] ; then echo -en "\033[0;33m!!\033[0m" fi + ##### Events ##### + assert_contains_substring "Successfully created the event id:" "`${ADC} event create -a ${APPLICATION_ID} -s "Test" -l INFO -e CUSTOM`" "Create custom event" + assert_contains_substring "Successfully created the event id:" "`${ADC} event create -a ${APPLICATION_ID} -s "Test" -l INFO -e APPLICATION_DEPLOYMENT`" "Create application deployment" + assert_contains_substring "Successfully created the event id:" "`${ADC} event create -a ${APPLICATION_ID} -s "Urlencoding Test" -c "With Comment" -l INFO -e APPLICATION_DEPLOYMENT`" "Create application deployment" + # It takes the controller several seconds to update the list of events, so we (currently) skip checking the existence of the ids above + assert_contains_substring "" "`${ADC} event list -a ${APPLICATION_ID} -t BEFORE_NOW -d 60 -e APPLICATION_DEPLOYMENT -s INFO`" + + ##### Error handling ##### assert_equals "Error" "`env CONFIG_HTTP_TIMEOUT=1 ./adc.sh -H 127.0.0.2:8009 controller ping`" + assert_equals "ERROR: Please provide an argument for paramater -a" "`${ADC} event create`" "Missing required argument" ##### Delete the test application - assert_empty "`${ADC} application delete $APPLICATION_ID`" + echo $APPLICATION_ID + # assert_empty "`${ADC} application delete $APPLICATION_ID`" fi #### END TESTS ####