Skip to content

Commit a19fccf

Browse files
committed
Added federation setup
1 parent b0df122 commit a19fccf

File tree

9 files changed

+199
-21
lines changed

9 files changed

+199
-21
lines changed

adc.sh

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
ADC_VERSION="v0.2.0"
3-
ADC_LAST_COMMIT="1b5e629b1d5f9ec47d11e15e71e62df6a301988c"
3+
ADC_LAST_COMMIT="b0df122812228c8a0c27fa03a8d54873dc904e99"
44
USER_CONFIG="$HOME/.appdynamics/adc/config.sh"
55
GLOBAL_CONFIG="/etc/appdynamics/adc/config.sh"
66
CONFIG_CONTROLLER_COOKIE_LOCATION="/tmp/appdynamics-controller-cookie.txt"
@@ -62,7 +62,7 @@ function output {
6262
fi
6363
}
6464
function httpClient {
65-
debug "$*"
65+
# debug "$*"
6666
local TIMEOUT=10
6767
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
6868
TIMEOUT=$CONFIG_HTTP_TIMEOUT
@@ -157,7 +157,7 @@ function apiCall {
157157
# PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG}
158158
# ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG}
159159
while [[ $PAYLOAD =~ \${$opt\??} ]] ; do
160-
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG}
160+
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$OPTARG}
161161
done;
162162
while [[ $ENDPOINT =~ \${$opt\??} ]] ; do
163163
ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG}
@@ -189,9 +189,6 @@ function apiCall {
189189
controller_call -X $METHOD $ENDPOINT
190190
fi
191191
}
192-
# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce
193-
# echo "########"
194-
# __call GET "/controller/rest/applications/\${a}/nodes/\${n}" -n Web2 -a ECommerce
195192
function _config {
196193
local FORCE=0
197194
local GLOBAL=0
@@ -368,8 +365,9 @@ function controller_call {
368365
ENDPOINT=$*
369366
controller_login
370367
# Debug the COMMAND_RESULT from controller_login
371-
debug $COMMAND_RESULT
368+
debug "Login result: $COMMAND_RESULT"
372369
if [ $CONTROLLER_LOGIN_STATUS -eq 1 ]; then
370+
debug "Endpoint: $ENDPOINT"
373371
COMMAND_RESULT=$(httpClient -s -b $CONFIG_CONTROLLER_COOKIE_LOCATION \
374372
-X $METHOD\
375373
-H "X-CSRF-TOKEN: $XCSRFTOKEN" \
@@ -378,6 +376,7 @@ function controller_call {
378376
"`[ -n "$PAYLOAD" ] && echo -d ${PAYLOAD}`" \
379377
"`[ -n "$FORM" ] && echo -F ${FORM}`" \
380378
$CONFIG_CONTROLLER_HOST$ENDPOINT)
379+
debug "Command result: $COMMAND_RESULT"
381380
else
382381
COMMAND_RESULT="Controller Login Error! Please check hostname and credentials"
383382
fi
@@ -866,6 +865,75 @@ register dashboard_delete Delete a specific dashboard
866865
describe dashboard_delete << EOF
867866
Delete a specific dashboard
868867
EOF
868+
function federation_createkey {
869+
apiCall -X POST -d '{"apiKeyName": "${n}"}' "/controller/rest/federation/apikeyforfederation" "$@"
870+
}
871+
register federation_createkey Create API Key for Federation
872+
describe federation_createkey << EOF
873+
Create API Key for Federation.
874+
EOF
875+
function federation_establish {
876+
local ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@}
877+
ACCOUNT=${ACCOUNT%%:*}
878+
info "Establishing friendship..."
879+
apiCall -X POST -d "{ \
880+
\"accountName\": \"${ACCOUNT}\", \
881+
\"controllerUrl\": \"${CONFIG_CONTROLLER_HOST}\", \
882+
\"friendAccountName\": \"\${a}\", \
883+
\"friendAccountApiKey\": \"\${k}\", \
884+
\"friendAccountControllerUrl\": \"\${c}\" \
885+
}" "/controller/rest/federation/establishmutualfriendship" "$@"
886+
}
887+
register federation_establish Establish Mutual Friendship
888+
describe federation_establish << EOF
889+
Establish Mutual Friendship
890+
EOF
891+
function federation_setup {
892+
local FRIEND_CONTROLLER_CREDENTIALS=""
893+
local FRIEND_CONTROLLER_HOST=""
894+
local KEY_NAME=""
895+
local MY_ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@}
896+
MY_ACCOUNT=${MY_ACCOUNT%%:*}
897+
while getopts "c:h:k:" opt "$@";
898+
do
899+
case "${opt}" in
900+
c)
901+
FRIEND_CONTROLLER_CREDENTIALS=${OPTARG}
902+
;;
903+
h)
904+
FRIEND_CONTROLLER_HOST=${OPTARG}
905+
;;
906+
k)
907+
KEY_NAME=${OPTARG}
908+
;;
909+
esac
910+
done;
911+
shiftOptInd
912+
shift $SHIFTS
913+
if [ -z "$KEY_NAME" ] ; then
914+
local FRIEND_ACCOUNT=${FRIEND_CONTROLLER_CREDENTIALS##*@}
915+
FRIEND_ACCOUNT=${FRIEND_ACCOUNT%%:*}
916+
KEY_NAME=${FRIEND_ACCOUNT}_${FRIEND_CONTROLLER_HOST//[:\/]/_}_$RANDOM
917+
fi;
918+
federation_createkey -n $KEY_NAME
919+
debug "Key creation result: $COMMAND_RESULT"
920+
KEY=${COMMAND_RESULT##*\"key\": \"}
921+
KEY=${KEY%%\",\"*}
922+
debug "Identified key: $KEY"
923+
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"
924+
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"`
925+
if [ -z "$FRIEND_RESULT" ] ; then
926+
COMMAND_RESULT="Federation between $CONFIG_CONTROLLER_HOST and $FRIEND_CONTROLLER_HOST successfully established."
927+
else
928+
COMMAND_RESULT=""
929+
error "Federation setup failed. Error from $FRIEND_CONTROLLER_HOST: ${FRIEND_RESULT}"
930+
fi
931+
rm /tmp/appdynamics-federation-cookie.txt
932+
}
933+
register federation_setup Setup a controller federation: Generates a key and establishes the mutal friendship.
934+
describe federation_setup << EOF
935+
Setup a controller federation: Generates a key and establishes the mutal friendship.
936+
EOF
869937
if [ -f "${GLOBAL_CONFIG}" ]; then
870938
debug "Sourcing global config from ${GLOBAL_CONFIG} "
871939
. ${GLOBAL_CONFIG}

commands/controller/call.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ function controller_call {
2626

2727
controller_login
2828
# Debug the COMMAND_RESULT from controller_login
29-
debug $COMMAND_RESULT
29+
debug "Login result: $COMMAND_RESULT"
3030
if [ $CONTROLLER_LOGIN_STATUS -eq 1 ]; then
31+
debug "Endpoint: $ENDPOINT"
3132
COMMAND_RESULT=$(httpClient -s -b $CONFIG_CONTROLLER_COOKIE_LOCATION \
3233
-X $METHOD\
3334
-H "X-CSRF-TOKEN: $XCSRFTOKEN" \
@@ -36,6 +37,7 @@ function controller_call {
3637
"`[ -n "$PAYLOAD" ] && echo -d ${PAYLOAD}`" \
3738
"`[ -n "$FORM" ] && echo -F ${FORM}`" \
3839
$CONFIG_CONTROLLER_HOST$ENDPOINT)
40+
debug "Command result: $COMMAND_RESULT"
3941
else
4042
COMMAND_RESULT="Controller Login Error! Please check hostname and credentials"
4143
fi

commands/federation/createkey.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
function federation_createkey {
4+
apiCall -X POST -d '{"apiKeyName": "${n}"}' "/controller/rest/federation/apikeyforfederation" "$@"
5+
}
6+
7+
register federation_createkey Create API Key for Federation
8+
describe federation_createkey << EOF
9+
Create API Key for Federation.
10+
EOF

commands/federation/establish.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
function federation_establish {
4+
local ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@}
5+
ACCOUNT=${ACCOUNT%%:*}
6+
info "Establishing friendship..."
7+
apiCall -X POST -d "{ \
8+
\"accountName\": \"${ACCOUNT}\", \
9+
\"controllerUrl\": \"${CONFIG_CONTROLLER_HOST}\", \
10+
\"friendAccountName\": \"\${a}\", \
11+
\"friendAccountApiKey\": \"\${k}\", \
12+
\"friendAccountControllerUrl\": \"\${c}\" \
13+
}" "/controller/rest/federation/establishmutualfriendship" "$@"
14+
}
15+
16+
register federation_establish Establish Mutual Friendship
17+
describe federation_establish << EOF
18+
Establish Mutual Friendship
19+
EOF

commands/federation/setup.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
function federation_setup {
4+
5+
local FRIEND_CONTROLLER_CREDENTIALS=""
6+
local FRIEND_CONTROLLER_HOST=""
7+
local KEY_NAME=""
8+
9+
local MY_ACCOUNT=${CONFIG_CONTROLLER_CREDENTIALS##*@}
10+
MY_ACCOUNT=${MY_ACCOUNT%%:*}
11+
12+
while getopts "c:h:k:" opt "$@";
13+
do
14+
case "${opt}" in
15+
c)
16+
FRIEND_CONTROLLER_CREDENTIALS=${OPTARG}
17+
;;
18+
h)
19+
FRIEND_CONTROLLER_HOST=${OPTARG}
20+
;;
21+
k)
22+
KEY_NAME=${OPTARG}
23+
;;
24+
esac
25+
done;
26+
shiftOptInd
27+
shift $SHIFTS
28+
if [ -z "$KEY_NAME" ] ; then
29+
local FRIEND_ACCOUNT=${FRIEND_CONTROLLER_CREDENTIALS##*@}
30+
FRIEND_ACCOUNT=${FRIEND_ACCOUNT%%:*}
31+
KEY_NAME=${FRIEND_ACCOUNT}_${FRIEND_CONTROLLER_HOST//[:\/]/_}_$RANDOM
32+
fi;
33+
federation_createkey -n $KEY_NAME
34+
debug "Key creation result: $COMMAND_RESULT"
35+
KEY=${COMMAND_RESULT##*\"key\": \"}
36+
KEY=${KEY%%\",\"*}
37+
debug "Identified key: $KEY"
38+
39+
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"
40+
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"`
41+
42+
if [ -z "$FRIEND_RESULT" ] ; then
43+
COMMAND_RESULT="Federation between $CONFIG_CONTROLLER_HOST and $FRIEND_CONTROLLER_HOST successfully established."
44+
else
45+
COMMAND_RESULT=""
46+
error "Federation setup failed. Error from $FRIEND_CONTROLLER_HOST: ${FRIEND_RESULT}"
47+
fi
48+
49+
50+
rm /tmp/appdynamics-federation-cookie.txt
51+
}
52+
53+
register federation_setup Setup a controller federation: Generates a key and establishes the mutal friendship.
54+
describe federation_setup << EOF
55+
Setup a controller federation: Generates a key and establishes the mutal friendship.
56+
EOF

helpers/apiCall.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function apiCall {
5858
# PAYLOAD=${PAYLOAD//\$\{${opt}\}/$OPTARG}
5959
# ENDPOINT=${ENDPOINT//\$\{${opt}\}/$OPTARG}
6060
while [[ $PAYLOAD =~ \${$opt\??} ]] ; do
61-
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$ARG}
61+
PAYLOAD=${PAYLOAD//${BASH_REMATCH[0]}/$OPTARG}
6262
done;
6363
while [[ $ENDPOINT =~ \${$opt\??} ]] ; do
6464
ENDPOINT=${ENDPOINT//${BASH_REMATCH[0]}/$ARG}
@@ -93,7 +93,3 @@ function apiCall {
9393
controller_call -X $METHOD $ENDPOINT
9494
fi
9595
}
96-
97-
# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce
98-
# echo "########"
99-
# __call GET "/controller/rest/applications/\${a}/nodes/\${n}" -n Web2 -a ECommerce

helpers/httpClient.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
function httpClient {
4-
debug "$*"
4+
# debug "$*"
55
local TIMEOUT=10
66
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
77
TIMEOUT=$CONFIG_HTTP_TIMEOUT

main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ source ./commands/dashboard/export.sh
9696
source ./commands/dashboard/import.sh
9797
source ./commands/dashboard/delete.sh
9898

99+
source ./commands/federation/createkey.sh
100+
source ./commands/federation/establish.sh
101+
source ./commands/federation/setup.sh
102+
103+
99104
if [ -f "${GLOBAL_CONFIG}" ]; then
100105
debug "Sourcing global config from ${GLOBAL_CONFIG} "
101106
. ${GLOBAL_CONFIG}

test.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ COOKIE_PATH="/tmp/adc-test-cookie"
44
SUCCESS_COUNTER=0
55
TEST_COUNTER=0
66
SKIP_COUNTER=0
7+
LAST_TEST_STATUS=0
78

89
declare -i SUCCESS_COUNTER
910
declare -i SKIP_COUNTER
1011
declare -i TEST_COUNTER
1112

13+
FRIEND_CONTROLLER_HOST="http://controller.local:8090"
14+
FRIEND_CONTROLLER_CREDENTIALS="admin@customer1:admin"
15+
FRIEND_COOKIE_PATH="/tmp/adc-test-friend-cookie"
16+
17+
echo "Sourcing user config for controller host and credentials..."
18+
source "$HOME/.appdynamics/adc/config.sh"
19+
echo "Will use the following controller for testing: $CONFIG_CONTROLLER_HOST"
20+
1221
function assert_equals {
1322
TEST_COUNTER=$TEST_COUNTER+1
1423
if [[ "$2" = "$1" ]]; then
1524
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
25+
LAST_TEST_STATUS=0
1626
echo -en "\033[0;32m.\033[0m"
1727
else
28+
LAST_TEST_STATUS=1
1829
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"
1930
fi
2031
}
@@ -24,7 +35,9 @@ function assert_empty {
2435
if [[ -z "$1" ]]; then
2536
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
2637
echo -en "\033[0;32m.\033[0m"
38+
LAST_TEST_STATUS=0
2739
else
40+
LAST_TEST_STATUS=1
2841
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."
2942
fi
3043
}
@@ -34,7 +47,9 @@ function assert_contains_substring {
3447
if [[ $2 == *$1* ]]; then
3548
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
3649
echo -en "\033[0;32m.\033[0m"
50+
LAST_TEST_STATUS=0
3751
else
52+
LAST_TEST_STATUS=1
3853
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"
3954
fi
4055
}
@@ -44,16 +59,15 @@ function assert_regex {
4459
if [[ $2 =~ $1 ]]; then
4560
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
4661
echo -en "\033[0;32m.\033[0m"
62+
LAST_TEST_STATUS=0
4763
else
64+
LAST_TEST_STATUS=1
4865
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"
4966
fi
5067
}
5168

52-
echo "Sourcing user config for controller host and credentials..."
53-
source "$HOME/.appdynamics/adc/config.sh"
54-
echo "Will use the following controller for testing: $CONFIG_CONTROLLER_HOST"
55-
5669
ADC="./adc.sh -N -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH"
70+
ADC_FRIEND="./adc.sh -N -H $FRIEND_CONTROLLER_HOST -C $FRIEND_CONTROLLER_CREDENTIALS -J $FRIEND_COOKIE_PATH"
5771
#### BEGIN TESTS ####
5872

5973
##### Test controller functionality #####
@@ -95,14 +109,22 @@ if [[ $CREATE_APPLICATION =~ \"id\"\ \:\ ([0-9]+) ]] ; then
95109
# It takes the controller several seconds to update the list of events, so we (currently) skip checking the existence of the ids above
96110
assert_contains_substring "<events></events>" "`${ADC} event list -a ${APPLICATION_ID} -t BEFORE_NOW -d 60 -e APPLICATION_DEPLOYMENT -s INFO`"
97111

98-
112+
##### Federation #####
113+
FRIEND_LOGIN="`${ADC_FRIEND} controller login`"
114+
assert_contains_substring "Login Successful" "$FRIEND_LOGIN" "Federation Friend login successful"
115+
if [ $LAST_TEST_STATUS -eq 0 ]; then
116+
assert_contains_substring "Federation Key for account {customer1}" "`${ADC} federation createkey -n key_${RANDOM}`" "Create federation key"
117+
assert_contains_substring "successfully established" "`${ADC_FRIEND} federation setup -h "${CONFIG_CONTROLLER_HOST}" -c "${CONFIG_CONTROLLER_CREDENTIALS}"`" "Federation Setup"
118+
else
119+
SKIP_COUNTER=$SKIP_COUNTER+2
120+
echo -en "\033[0;33m!!\033[0m"
121+
fi
99122
##### Error handling #####
100123
assert_equals "Error" "`env CONFIG_HTTP_TIMEOUT=1 ./adc.sh -H 127.0.0.2:8009 controller ping`"
101124
assert_equals "ERROR: Please provide an argument for paramater -a" "`${ADC} event create`" "Missing required argument"
102125

103126
##### Delete the test application
104-
echo $APPLICATION_ID
105-
# assert_empty "`${ADC} application delete $APPLICATION_ID`"
127+
assert_empty "`${ADC} application delete $APPLICATION_ID`"
106128
fi
107129
#### END TESTS ####
108130

0 commit comments

Comments
 (0)