From 56237128c60d9a1356a7460e1dbb334566721e49 Mon Sep 17 00:00:00 2001 From: alex_jov Date: Mon, 24 Aug 2020 18:05:22 +0100 Subject: [PATCH] Merging action suppression from Develop (#34) * refactor app health rules (#27) * Feature/containerHealthRules (#28) * Feature/disable node hr (#29) * microservice health rules * Merge action-suppression and port-bugfix to develop from feature b; * Add action suppression upload from file; Typos; * Add suppress action config; * Add actions folder and example file; * Dash layout * Adding detele action suppression; * Controller port as parameter takes precedence; * Port controller; * Update create and delete action suppression; Update config; * Action suppression bugfix missing functions and folders; Save uploaded actions; * Action suppression adding proxy; Fix empty proxy; Backup only uploaded files; * Update action suppression help; * Update QAConfigMyApp.yml * Update QAConfigMyApp.yml Co-authored-by: Israel Ogbole --- .github/workflows/QAConfigMyApp.yml | 6 +-- .gitignore | 3 +- api_actions/actions/.gitkeep | 0 api_actions/actions/tiers_action.json | 17 ------- api_actions/application-action-suppression.sh | 40 ++++++++++++----- api_actions/delete-action-suppression.sh | 45 +++++++++++++------ .../upload-files-action-suppression.sh | 36 +++++++++++---- api_actions/uploaded/.gitkeep | 0 configMyApp.sh | 4 +- start.sh | 15 ++++--- 10 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 api_actions/actions/.gitkeep delete mode 100644 api_actions/actions/tiers_action.json create mode 100644 api_actions/uploaded/.gitkeep diff --git a/.github/workflows/QAConfigMyApp.yml b/.github/workflows/QAConfigMyApp.yml index 2e6e5b1..107e9bb 100644 --- a/.github/workflows/QAConfigMyApp.yml +++ b/.github/workflows/QAConfigMyApp.yml @@ -49,9 +49,9 @@ jobs: - name: TestCase4- Action suppression. Date and duration run: | echo Running Action Suppression, - ./start.sh -a Jenkins_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --suppress-action --suppress-start=$(date -u +%FT%T) --suppress-duration=120 + ./start.sh -a Jenkins_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --suppress-action --suppress-start=$(date -d " + 5 minutes" -u +%FT%T) --suppress-duration=120 - - name: TestCase4- SIM and DB + - name: TestCase5- SIM and DB run: | echo Running SIM and DB, - ./start.sh -a IoT_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --include-database --database-name 'ConfigMyApp' --include-sim + ./start.sh -a IoT_API -c configmyappdemo-2044no-uzyczrm0.appd-cx.com -p appd -u appd --include-database --database-name 'ConfigMyApp' --include-sim \ No newline at end of file diff --git a/.gitignore b/.gitignore index 840f692..6b9a660 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ Setup/Applications/* .vscode/* zz_demoflow* business_transactions/uploaded/* -api_actions/uploaded/*.json +api_actions/uploaded/*.json* +api_actions/actions/*.json* diff --git a/api_actions/actions/.gitkeep b/api_actions/actions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/api_actions/actions/tiers_action.json b/api_actions/actions/tiers_action.json deleted file mode 100644 index 691559f..0000000 --- a/api_actions/actions/tiers_action.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "CMA-suppression-tiers-nodes-test", - "disableAgentReporting": true, - "startTime": "2020-09-06T18:33:14+0000", - "endTime": "2020-09-06T19:33:14+0000", - "affects": { - "affectedInfoType": "TIERS_NODES", - "affectedEntities": { - "tierNodeType": "NODE", - "nodeType": "ALL_NODES", - "affectedNodes": { - "affectedNodeScope": "NODES_OF_SPECIFIC_TIERS", - "specificTiers": ["Web"] - } - } - } -} diff --git a/api_actions/application-action-suppression.sh b/api_actions/application-action-suppression.sh index 49c0d02..9346383 100755 --- a/api_actions/application-action-suppression.sh +++ b/api_actions/application-action-suppression.sh @@ -4,19 +4,24 @@ _controller_url=${1} # hostname + /controller _user_credentials=${2} # ${username}:${password} -_action_suppression_start=${3} -_action_suppression_duration=${4} +actions_proxy_details=${3} -_application_name=${5} +_action_suppression_start=${4} +_action_suppression_duration=${5} -_action_suppression_name=${6} +_application_name=${6} + +_action_suppression_name=${7} # 2. FUNCTIONS function func_check_http_response(){ local http_message_body="$1" local string_success_response_contains="$2" + local filePath="$3" + local fileName="$4" if [[ "$http_message_body" =~ "$string_success_response_contains" ]]; then # contains - echo "Success..." + cp -rf "$filePath" "./api_actions/uploaded/${fileName}.${dt}" + echo "Success..." else echo "${dt} ERROR "{$http_message_body}"" >> error.log echo "ERROR $http_message_body" @@ -24,10 +29,21 @@ function func_check_http_response(){ fi } +function func_check_http_status() { + local http_code=$1 + local message_on_failure=$2 + #echo "HTTP status code: $http_code" + if [[ $http_code -lt 200 ]] || [[ $http_code -gt 299 ]]; then + echo "${dt} ERROR "{$http_code: $message_on_failure}"" >> error.log + echo "ERROR $http_code: $message_on_failure" + exit 1 + fi +} + # 3. PREPARE REQUEST dt=$(date '+%Y-%m-%d_%H-%M-%S') -_payload_path="./api_actions/uploaded/action-suppression-payload-${dt}.json" +_payload_path="./api_actions/actions/action-suppression-payload-${dt}.json" _template_path="./api_actions/action-suppression-payload-template.json" _header="Content-Type: application/json; charset=utf8" @@ -49,7 +65,7 @@ fi sed -e "s/_action_suppression_name/${_action_suppression_name}/g" -e "s/_action_suppression_start/${_action_suppression_start}/g" -e "s/_action_suppression_end/${_action_suppression_end}/g" "${_template_path}" > "${_payload_path}" # application id -allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON) +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON ${_proxy_details}) applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) @@ -60,13 +76,15 @@ fi application_id=$(jq '.id' <<< $applicationObject) _resource_url="alerting/rest/v1/applications/${application_id}/action-suppressions" - # 4. SEND A CREATE REQUEST -echo "Uploading application supression action" -response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ) +echo "Uploading '"${_action_suppression_name}"' application supression action..." +response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ${_proxy_details} ) # 5. CHECK RESULT expected_response='"id":' # returns id on success -func_check_http_response "\{$response}" $expected_response +func_check_http_response "\{$response}" "${expected_response}" "${_payload_path}" "${_action_suppression_name}.json" + +#fileName="$(basename -- $_payload_path)" +#cp -rf "$_payload_path" "./api_actions/uploaded/${fileName}.${dt}" #echo "response is: $response" \ No newline at end of file diff --git a/api_actions/delete-action-suppression.sh b/api_actions/delete-action-suppression.sh index 6c95f07..aa15bb5 100755 --- a/api_actions/delete-action-suppression.sh +++ b/api_actions/delete-action-suppression.sh @@ -4,14 +4,29 @@ _controller_url=${1} # hostname + /controller _user_credentials=${2} # ${username}:${password} -_action_suppression_name=${3} +_proxy_details=${3} -_application_name=${4} +_action_suppression_name=${4} -# 2. PREPARE REQUEST +_application_name=${5} -# application id -allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON) +# 2. FUNCTIONS +dt=$(date '+%Y-%m-%d_%H-%M-%S') + +function func_check_http_status() { + local http_code=$1 + local message_on_failure=$2 + if [[ $http_code -lt 200 ]] || [[ $http_code -gt 299 ]]; then + echo "${dt} ERROR "{$http_code: $message_on_failure}"" >> error.log + echo "ERROR $http_code: $message_on_failure" + exit 1 + fi +} + +# 3. PREPARE REQUEST + +# 3.1 Get application id +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON ${_proxy_details}) applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) @@ -21,21 +36,23 @@ fi application_id=$(jq '.id' <<< $applicationObject) -# suppression id -actionSupressions=$(curl -s --user ${_user_credentials} ${_controller_url}/alerting/rest/v1/applications/${application_id}/action-suppressions) +# 3.2 Get suppression id +actionSupressions=$(curl -s --user ${_user_credentials} ${_controller_url}/alerting/rest/v1/applications/${application_id}/action-suppressions ${_proxy_details}) + +actionSupressionObject=$(jq --arg suppressionName "$_action_suppression_name" '.[] | select(.name == $suppressionName)' <<< $actionSupressions) -if [ "$actionSupressions" = "" ]; then +if [ "$actionSupressionObject" = "" ]; then func_check_http_status 404 "Action supression '"$_action_suppression_name"' not found. Aborting..." fi -actionSupressionObject=$(jq --arg suppressionName "$_action_suppression_name" '.[] | select(.name == $suppressionName)' <<< $actionSupressions) - action_supression_id=$(jq '.id' <<< $actionSupressionObject) -# request url +# 3.3 Set request url _resource_url="alerting/rest/v1/applications/${application_id}/action-suppressions/${action_supression_id}" -# 3. SEND A DELETE REQUEST -echo "Deleting application supression action" +# 4. SEND A DELETE REQUEST +echo "Deleting '"$_action_suppression_name"' application supression action..." # response is always empty -response=$(curl -s -X DELETE --user $_user_credentials $_controller_url/$_resource_url) +response=$(curl -s -X DELETE --user $_user_credentials $_controller_url/$_resource_url ${_proxy_details}) + +echo "Done." diff --git a/api_actions/upload-files-action-suppression.sh b/api_actions/upload-files-action-suppression.sh index 975ba96..54f1219 100755 --- a/api_actions/upload-files-action-suppression.sh +++ b/api_actions/upload-files-action-suppression.sh @@ -4,18 +4,33 @@ _controller_url=${1} # hostname + /controller _user_credentials=${2} # ${username}:${password} -_application_name=${3} +_proxy_details=${3} + +_application_name=${4} # 2. FUNCTIONS function func_check_http_response(){ local http_message_body="$1" local string_success_response_contains="$2" + local filePath="$3" + local fileName="$4" if [[ "$http_message_body" =~ "$string_success_response_contains" ]]; then # contains - echo "Success..." + cp -rf "$filePath" "./api_actions/uploaded/${fileName}.${dt}" + echo "Success..." else echo "${dt} ERROR "{$http_message_body}"" >> error.log echo "ERROR $http_message_body" - #exit 1 + # do not break on failure + fi +} + +function func_check_http_status() { + local http_code=$1 + local message_on_failure=$2 + if [[ $http_code -lt 200 ]] || [[ $http_code -gt 299 ]]; then + echo "${dt} ERROR "{$http_code: $message_on_failure}"" >> error.log + echo "ERROR $http_code: $message_on_failure" + exit 1 fi } @@ -25,7 +40,7 @@ dt=$(date '+%Y-%m-%d_%H-%M-%S') _header="Content-Type: application/json; charset=utf8" # application id -allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON) +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON ${_proxy_details}) applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) @@ -36,20 +51,25 @@ fi _application_id=$(jq '.id' <<< $applicationObject) _resource_url="alerting/rest/v1/applications/${_application_id}/action-suppressions" +# actions directory _action_suppression_files="./api_actions/actions/*.json" - for f in $_action_suppression_files; do - echo "Processing $f action suppression file" + + # Check if folder contains files + [ -f "$f" ] || func_check_http_status 404 "No files found that match pattern: '"$_action_suppression_files"'. Aborting..." + + echo "Processing '"$f"' action suppression file" _payload_path=$f # 4. SEND A CREATE REQUEST - response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ) + response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ${_proxy_details}) # 5. CHECK RESULT expected_response='"id":' # returns id on success - func_check_http_response "\{$response}" $expected_response + fileName="$(basename -- $f)" + func_check_http_response "\{$response}" "${expected_response}" ${_payload_path} "${fileName}" # echo "response is: $response" diff --git a/api_actions/uploaded/.gitkeep b/api_actions/uploaded/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/configMyApp.sh b/configMyApp.sh index 0309bb3..40035f5 100755 --- a/configMyApp.sh +++ b/configMyApp.sh @@ -128,8 +128,8 @@ function func_check_http_status() { local message_on_failure=$2 #echo "HTTP status code: $http_code" if [[ $http_code -lt 200 ]] || [[ $http_code -gt 299 ]]; then - - echo "$http_code : $message_on_failure" + echo "${dt} ERROR "{$http_code: $message_on_failure}"" >> error.log + echo "$http_code: $message_on_failure" func_cleanup exit 1 fi diff --git a/start.sh b/start.sh index 4b9b7eb..05bf9cf 100755 --- a/start.sh +++ b/start.sh @@ -114,9 +114,9 @@ print_help() printf '%s\n' "Action suppression options:" printf '\t%s\n' "--suppress-action, --no-suppress-action: use application action suppression (${_arg_suppress_action} by default)" - printf '\t%s\n' "--suppress-start: application suppression start date in \"yyyy-MM-ddThh:mm:ss+0000\" format (GMT), mandatory if suppress-action set to true (current datetime by default)" - printf '\t%s\n' "--suppress-duration: application suppression duration in minutes, mandatory if suppress-action is set to true (one hour by default)" - printf '\t%s\n' "--suppress-name: custom name of the supression action, if none specified name is auto-generated (no default)" + printf '\t%s\n' "--suppress-start: application suppression start date in \"yyyy-MM-ddThh:mm:ss+0000\" format (GMT) (current datetime by default)" + printf '\t%s\n' "--suppress-duration: application suppression duration in minutes (one hour by default)" + printf '\t%s\n' "--suppress-name: custom name of the supression action, if none specified name is auto-generated" printf '\t%s\n' "--suppress-upload-files, --no-suppress-upload-files: upload action suppression files from a folder (${_arg_suppress_upload_files} by default)" @@ -717,7 +717,7 @@ _arg_controller_url="$protocol://$_arg_controller_host:$_arg_controller_port/con # 3.4 Prepare proxy details if [ $_arg_use_proxy = true ]; then - _arg_proxy_details="-x $proxy_url:$proxy_port" + _arg_proxy_details="-x $_arg_proxy_url:$_arg_proxy_port" else _arg_proxy_details="" fi @@ -751,16 +751,17 @@ fi ### 4 ACTION SUPRESSION ### if [ $_arg_suppress_action = true ]; then - ./api_actions/application-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_suppress_start" "$_arg_suppress_duration" "$_arg_application_name" "$_arg_suppress_name" + ./api_actions/application-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_proxy_details" "$_arg_suppress_start" "$_arg_suppress_duration" "$_arg_application_name" "$_arg_suppress_name" + exit 0 # only do action suppression fi if [ $_arg_suppress_upload_files = true ]; then - ./api_actions/upload-files-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_application_name" + ./api_actions/upload-files-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_proxy_details" "$_arg_application_name" exit 0 # only upload files fi if [[ ! -z "${_arg_suppress_delete// }" ]]; then - ./api_actions/delete-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_suppress_delete" "$_arg_application_name" + ./api_actions/delete-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_proxy_details" "$_arg_suppress_delete" "$_arg_application_name" exit 0 # only delete action suppression fi