From a19fccf0db917b408bc6dcb8ec57dcc100614baf Mon Sep 17 00:00:00 2001 From: Severin Neumann Date: Tue, 14 Nov 2017 08:51:56 +0100 Subject: [PATCH] Added federation setup --- adc.sh | 82 +++++++++++++++++++++++++++++--- commands/controller/call.sh | 4 +- commands/federation/createkey.sh | 10 ++++ commands/federation/establish.sh | 19 ++++++++ commands/federation/setup.sh | 56 ++++++++++++++++++++++ helpers/apiCall.sh | 6 +-- helpers/httpClient.sh | 2 +- main.sh | 5 ++ test.sh | 36 +++++++++++--- 9 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 commands/federation/createkey.sh create mode 100644 commands/federation/establish.sh create mode 100644 commands/federation/setup.sh diff --git a/adc.sh b/adc.sh index 70b9054..1aba249 100755 --- a/adc.sh +++ b/adc.sh @@ -1,6 +1,6 @@ #!/bin/bash ADC_VERSION="v0.2.0" -ADC_LAST_COMMIT="1b5e629b1d5f9ec47d11e15e71e62df6a301988c" +ADC_LAST_COMMIT="b0df122812228c8a0c27fa03a8d54873dc904e99" USER_CONFIG="$HOME/.appdynamics/adc/config.sh" GLOBAL_CONFIG="/etc/appdynamics/adc/config.sh" CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt" @@ -62,7 +62,7 @@ function output { fi } function httpClient { - debug "$*" + # debug "$*" local TIMEOUT=10 if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then TIMEOUT=$CONFIG_HTTP_TIMEOUT @@ -157,7 +157,7 @@ function apiCall { # PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG} # ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG} while [[ $PAYLOAD =~ \${$opt\??} ]] ; do - PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG} + PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$OPTARG} done; while [[ $ENDPOINT =~ \${$opt\??} ]] ; do ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG} @@ -189,9 +189,6 @@ function apiCall { controller_call -X $METHOD $ENDPOINT fi } -# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce -# echo "########" -# __call GET "/controller/rest/applications/\${a}/nodes/\${n}" -n Web2 -a ECommerce function _config { local FORCE=0 local GLOBAL=0 @@ -368,8 +365,9 @@ function controller_call { ENDPOINT=$* controller_login # Debug the COMMAND_RESULT from controller_login - debug $COMMAND_RESULT + debug "Login result: $COMMAND_RESULT" if [ $CONTROLLER_LOGIN_STATUS -eq 1 ]; then + debug "Endpoint: $ENDPOINT" COMMAND_RESULT=$(httpClient -s -b $CONFIG_CONTROLLER_COOKIE_LOCATION \ -X $METHOD\ -H "X-CSRF-TOKEN: $XCSRFTOKEN" \ @@ -378,6 +376,7 @@ function controller_call { "`[ -n "$PAYLOAD" ] && echo -d ${PAYLOAD}`" \ "`[ -n "$FORM" ] && echo -F ${FORM}`" \ $CONFIG_CONTROLLER_HOST$ENDPOINT) + debug "Command result: $COMMAND_RESULT" else COMMAND_RESULT="Controller Login Error! Please check hostname and credentials" fi @@ -866,6 +865,75 @@ register dashboard_delete Delete a specific dashboard describe dashboard_delete << EOF Delete a specific dashboard EOF +function federation_createkey { + apiCall -X POST -d '{"apiKeyName": "${n}"}' "/controller/rest/federation/apikeyforfederation" "$@" +} +register federation_createkey Create API Key for Federation +describe federation_createkey << EOF +Create API Key for Federation. +EOF +function federation_establish { + local ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@} + ACCOUNT=${ACCOUNT%%:*} + info "Establishing friendship..." + apiCall -X POST -d "{ \ + \"accountName\": \"${ACCOUNT}\", \ + \"controllerUrl\": \"${CONFIG_CONTROLLER_HOST}\", \ + \"friendAccountName\": \"\${a}\", \ + \"friendAccountApiKey\": \"\${k}\", \ + \"friendAccountControllerUrl\": \"\${c}\" \ + }" "/controller/rest/federation/establishmutualfriendship" "$@" +} +register federation_establish Establish Mutual Friendship +describe federation_establish << EOF +Establish Mutual Friendship +EOF +function federation_setup { + local FRIEND_CONTROLLER_CREDENTIALS="" + local FRIEND_CONTROLLER_HOST="" + local KEY_NAME="" + local MY_ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@} + MY_ACCOUNT=${MY_ACCOUNT%%:*} + while getopts "c:h:k:" opt "$@"; + do + case "${opt}" in + c) + FRIEND_CONTROLLER_CREDENTIALS=${OPTARG} + ;; + h) + FRIEND_CONTROLLER_HOST=${OPTARG} + ;; + k) + KEY_NAME=${OPTARG} + ;; + esac + done; + shiftOptInd + shift $SHIFTS + if [ -z "$KEY_NAME" ] ; then + local FRIEND_ACCOUNT=${FRIEND_CONTROLLER_CREDENTIALS##*@} + FRIEND_ACCOUNT=${FRIEND_ACCOUNT%%:*} + KEY_NAME=${FRIEND_ACCOUNT}_${FRIEND_CONTROLLER_HOST//[:\/]/_}_$RANDOM + fi; + federation_createkey -n $KEY_NAME + debug "Key creation result: $COMMAND_RESULT" + KEY=${COMMAND_RESULT##*\"key\": \"} + KEY=${KEY%%\",\"*} + debug "Identified key: $KEY" + debug "Establishing mutual friendship: $0 -J /tmp/appdynamics-federation-cookie.txt -H $FRIEND_CONTROLLER_HOST -C $FRIEND_CONTROLLER_CREDENTIALS federation establish -a $MY_ACCOUNT -k $KEY -c $CONFIG_CONTROLLER_HOST" + FRIEND_RESULT=`$0 -J /tmp/appdynamics-federation-cookie.txt -H "$FRIEND_CONTROLLER_HOST" -C "$FRIEND_CONTROLLER_CREDENTIALS" federation establish -a "$MY_ACCOUNT" -k "$KEY" -c "$CONFIG_CONTROLLER_HOST"` + if [ -z "$FRIEND_RESULT" ] ; then + COMMAND_RESULT="Federation between $CONFIG_CONTROLLER_HOST and $FRIEND_CONTROLLER_HOST successfully established." + else + COMMAND_RESULT="" + error "Federation setup failed. Error from $FRIEND_CONTROLLER_HOST: ${FRIEND_RESULT}" + fi + rm /tmp/appdynamics-federation-cookie.txt +} +register federation_setup Setup a controller federation: Generates a key and establishes the mutal friendship. +describe federation_setup << EOF +Setup a controller federation: Generates a key and establishes the mutal friendship. +EOF if [ -f "${GLOBAL_CONFIG}" ]; then debug "Sourcing global config from ${GLOBAL_CONFIG} " . ${GLOBAL_CONFIG} diff --git a/commands/controller/call.sh b/commands/controller/call.sh index f3a0f18..74d640b 100644 --- a/commands/controller/call.sh +++ b/commands/controller/call.sh @@ -26,8 +26,9 @@ function controller_call { controller_login # Debug the COMMAND_RESULT from controller_login - debug $COMMAND_RESULT + debug "Login result: $COMMAND_RESULT" if [ $CONTROLLER_LOGIN_STATUS -eq 1 ]; then + debug "Endpoint: $ENDPOINT" COMMAND_RESULT=$(httpClient -s -b $CONFIG_CONTROLLER_COOKIE_LOCATION \ -X $METHOD\ -H "X-CSRF-TOKEN: $XCSRFTOKEN" \ @@ -36,6 +37,7 @@ function controller_call { "`[ -n "$PAYLOAD" ] && echo -d ${PAYLOAD}`" \ "`[ -n "$FORM" ] && echo -F ${FORM}`" \ $CONFIG_CONTROLLER_HOST$ENDPOINT) + debug "Command result: $COMMAND_RESULT" else COMMAND_RESULT="Controller Login Error! Please check hostname and credentials" fi diff --git a/commands/federation/createkey.sh b/commands/federation/createkey.sh new file mode 100644 index 0000000..8c631f3 --- /dev/null +++ b/commands/federation/createkey.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +function federation_createkey { + apiCall -X POST -d '{"apiKeyName": "${n}"}' "/controller/rest/federation/apikeyforfederation" "$@" +} + +register federation_createkey Create API Key for Federation +describe federation_createkey << EOF +Create API Key for Federation. +EOF diff --git a/commands/federation/establish.sh b/commands/federation/establish.sh new file mode 100644 index 0000000..72b8f00 --- /dev/null +++ b/commands/federation/establish.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +function federation_establish { + local ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@} + ACCOUNT=${ACCOUNT%%:*} + info "Establishing friendship..." + apiCall -X POST -d "{ \ + \"accountName\": \"${ACCOUNT}\", \ + \"controllerUrl\": \"${CONFIG_CONTROLLER_HOST}\", \ + \"friendAccountName\": \"\${a}\", \ + \"friendAccountApiKey\": \"\${k}\", \ + \"friendAccountControllerUrl\": \"\${c}\" \ + }" "/controller/rest/federation/establishmutualfriendship" "$@" +} + +register federation_establish Establish Mutual Friendship +describe federation_establish << EOF +Establish Mutual Friendship +EOF diff --git a/commands/federation/setup.sh b/commands/federation/setup.sh new file mode 100644 index 0000000..475d8b9 --- /dev/null +++ b/commands/federation/setup.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +function federation_setup { + + local FRIEND_CONTROLLER_CREDENTIALS="" + local FRIEND_CONTROLLER_HOST="" + local KEY_NAME="" + + local MY_ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@} + MY_ACCOUNT=${MY_ACCOUNT%%:*} + + while getopts "c:h:k:" opt "$@"; + do + case "${opt}" in + c) + FRIEND_CONTROLLER_CREDENTIALS=${OPTARG} + ;; + h) + FRIEND_CONTROLLER_HOST=${OPTARG} + ;; + k) + KEY_NAME=${OPTARG} + ;; + esac + done; + shiftOptInd + shift $SHIFTS + if [ -z "$KEY_NAME" ] ; then + local FRIEND_ACCOUNT=${FRIEND_CONTROLLER_CREDENTIALS##*@} + FRIEND_ACCOUNT=${FRIEND_ACCOUNT%%:*} + KEY_NAME=${FRIEND_ACCOUNT}_${FRIEND_CONTROLLER_HOST//[:\/]/_}_$RANDOM + fi; + federation_createkey -n $KEY_NAME + debug "Key creation result: $COMMAND_RESULT" + KEY=${COMMAND_RESULT##*\"key\": \"} + KEY=${KEY%%\",\"*} + debug "Identified key: $KEY" + + debug "Establishing mutual friendship: $0 -J /tmp/appdynamics-federation-cookie.txt -H $FRIEND_CONTROLLER_HOST -C $FRIEND_CONTROLLER_CREDENTIALS federation establish -a $MY_ACCOUNT -k $KEY -c $CONFIG_CONTROLLER_HOST" + FRIEND_RESULT=`$0 -J /tmp/appdynamics-federation-cookie.txt -H "$FRIEND_CONTROLLER_HOST" -C "$FRIEND_CONTROLLER_CREDENTIALS" federation establish -a "$MY_ACCOUNT" -k "$KEY" -c "$CONFIG_CONTROLLER_HOST"` + + if [ -z "$FRIEND_RESULT" ] ; then + COMMAND_RESULT="Federation between $CONFIG_CONTROLLER_HOST and $FRIEND_CONTROLLER_HOST successfully established." + else + COMMAND_RESULT="" + error "Federation setup failed. Error from $FRIEND_CONTROLLER_HOST: ${FRIEND_RESULT}" + fi + + + rm /tmp/appdynamics-federation-cookie.txt +} + +register federation_setup Setup a controller federation: Generates a key and establishes the mutal friendship. +describe federation_setup << EOF +Setup a controller federation: Generates a key and establishes the mutal friendship. +EOF diff --git a/helpers/apiCall.sh b/helpers/apiCall.sh index 5be08b5..7d6ca49 100755 --- a/helpers/apiCall.sh +++ b/helpers/apiCall.sh @@ -58,7 +58,7 @@ function apiCall { # PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG} # ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG} while [[ $PAYLOAD =~ \${$opt\??} ]] ; do - PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG} + PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$OPTARG} done; while [[ $ENDPOINT =~ \${$opt\??} ]] ; do ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG} @@ -93,7 +93,3 @@ function apiCall { controller_call -X $METHOD $ENDPOINT fi } - -# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce -# echo "########" -# __call GET "/controller/rest/applications/\${a}/nodes/\${n}" -n Web2 -a ECommerce diff --git a/helpers/httpClient.sh b/helpers/httpClient.sh index 74d7f9d..2726d2d 100644 --- a/helpers/httpClient.sh +++ b/helpers/httpClient.sh @@ -1,7 +1,7 @@ #!/bin/bash function httpClient { - debug "$*" + # debug "$*" local TIMEOUT=10 if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then TIMEOUT=$CONFIG_HTTP_TIMEOUT diff --git a/main.sh b/main.sh index 76389d8..d1e0fb5 100755 --- a/main.sh +++ b/main.sh @@ -96,6 +96,11 @@ source ./commands/dashboard/export.sh source ./commands/dashboard/import.sh source ./commands/dashboard/delete.sh +source ./commands/federation/createkey.sh +source ./commands/federation/establish.sh +source ./commands/federation/setup.sh + + if [ -f "${GLOBAL_CONFIG}" ]; then debug "Sourcing global config from ${GLOBAL_CONFIG} " . ${GLOBAL_CONFIG} diff --git a/test.sh b/test.sh index d60599c..a36e939 100755 --- a/test.sh +++ b/test.sh @@ -4,17 +4,28 @@ COOKIE_PATH="/tmp/adc-test-cookie" SUCCESS_COUNTER=0 TEST_COUNTER=0 SKIP_COUNTER=0 +LAST_TEST_STATUS=0 declare -i SUCCESS_COUNTER declare -i SKIP_COUNTER declare -i TEST_COUNTER +FRIEND_CONTROLLER_HOST="http://controller.local:8090" +FRIEND_CONTROLLER_CREDENTIALS="admin@customer1:admin" +FRIEND_COOKIE_PATH="/tmp/adc-test-friend-cookie" + +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" + function assert_equals { TEST_COUNTER=$TEST_COUNTER+1 if [[ "$2" = "$1" ]]; then SUCCESS_COUNTER=$SUCCESS_COUNTER+1 + LAST_TEST_STATUS=0 echo -en "\033[0;32m.\033[0m" else + LAST_TEST_STATUS=1 echo -e "\n\033[0;31mTest \033[0;34m$3\033[0;31m failed: \033[0;33m$1\033[0;31m doesn't equal \033[0;35m$2\033[0m" fi } @@ -24,7 +35,9 @@ function assert_empty { if [[ -z "$1" ]]; then SUCCESS_COUNTER=$SUCCESS_COUNTER+1 echo -en "\033[0;32m.\033[0m" + LAST_TEST_STATUS=0 else + LAST_TEST_STATUS=1 echo -e "\n\033[0;31mTest \033[0;34m$3\033[0;31m failed: \033[0;33m$1\033[0;31m is not an empty string." fi } @@ -34,7 +47,9 @@ function assert_contains_substring { if [[ $2 == *$1* ]]; then SUCCESS_COUNTER=$SUCCESS_COUNTER+1 echo -en "\033[0;32m.\033[0m" + LAST_TEST_STATUS=0 else + LAST_TEST_STATUS=1 echo -e "\n\033[0;31mTest \033[0;34m$3\033[0;31m failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m" fi } @@ -44,16 +59,15 @@ function assert_regex { if [[ $2 =~ $1 ]]; then SUCCESS_COUNTER=$SUCCESS_COUNTER+1 echo -en "\033[0;32m.\033[0m" + LAST_TEST_STATUS=0 else + LAST_TEST_STATUS=1 echo -e "\n\033[0;31mTest \033[0;34m$3\033[0;31m failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m" fi } -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 -N -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH" +ADC_FRIEND="./adc.sh -N -H $FRIEND_CONTROLLER_HOST -C $FRIEND_CONTROLLER_CREDENTIALS -J $FRIEND_COOKIE_PATH" #### BEGIN TESTS #### ##### Test controller functionality ##### @@ -95,14 +109,22 @@ if [[ $CREATE_APPLICATION =~ \"id\"\ \:\ ([0-9]+) ]] ; then # 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`" - + ##### Federation ##### + FRIEND_LOGIN="`${ADC_FRIEND} controller login`" + assert_contains_substring "Login Successful" "$FRIEND_LOGIN" "Federation Friend login successful" + if [ $LAST_TEST_STATUS -eq 0 ]; then + assert_contains_substring "Federation Key for account {customer1}" "`${ADC} federation createkey -n key_${RANDOM}`" "Create federation key" + assert_contains_substring "successfully established" "`${ADC_FRIEND} federation setup -h "${CONFIG_CONTROLLER_HOST}" -c "${CONFIG_CONTROLLER_CREDENTIALS}"`" "Federation Setup" + else + SKIP_COUNTER=$SKIP_COUNTER+2 + echo -en "\033[0;33m!!\033[0m" + fi ##### 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 - echo $APPLICATION_ID - # assert_empty "`${ADC} application delete $APPLICATION_ID`" + assert_empty "`${ADC} application delete $APPLICATION_ID`" fi #### END TESTS ####