Skip to content

Commit

Permalink
Added federation setup
Browse files Browse the repository at this point in the history
  • Loading branch information
svrnm committed Nov 14, 2017
1 parent b0df122 commit a19fccf
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 21 deletions.
82 changes: 75 additions & 7 deletions adc.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -62,7 +62,7 @@ function output {
fi
}
function httpClient {
debug "$*"
# debug "$*"
local TIMEOUT=10
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
TIMEOUT=$CONFIG_HTTP_TIMEOUT
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" \
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 3 additions & 1 deletion commands/controller/call.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions commands/federation/createkey.sh
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions commands/federation/establish.sh
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions commands/federation/setup.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions helpers/apiCall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion helpers/httpClient.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

function httpClient {
debug "$*"
# debug "$*"
local TIMEOUT=10
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
TIMEOUT=$CONFIG_HTTP_TIMEOUT
Expand Down
5 changes: 5 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
36 changes: 29 additions & 7 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 #####
Expand Down Expand Up @@ -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 "<events></events>" "`${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 ####

Expand Down

0 comments on commit a19fccf

Please sign in to comment.