From a7cd781242f4e41cb1542345138664eb0f4324a6 Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Tue, 16 Jan 2024 14:08:42 -0800 Subject: [PATCH 1/6] rough draft --- .../deployment/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index fbc84a6..00a68d7 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -246,6 +246,22 @@ function s3ActivityMessageText(message) { return `${userName} generated S3 event ${eventName} from region ${awsRegion} for bucket ${bucketName} in Dockstore ${dockstoreEnvironment}`; } +function ecsActivityMessageText(message) { + if (message["detail-type"] === "ECS Task State Change") { + return ecsTaskStateChangeMessageText(message); + } else { + return ecsAutoScalingMessageText(message); + } +} + +function ecsTaskStateChangeMessageText(message) { + const taskArn = message.resources[0]; + const clusterArn = message.detail.clusterArn; + const lastStatus = message.detail.lastStatus; + console.log(`Task ${taskArn} in cluster ${clusterArn} is now ${lastStatus}`); + return `Task is now ${lastStatus}`; +} + function ecsAutoScalingMessageText(message) { const serviceName = message.detail.requestParameters.service; const newDesiredCount = message.detail.requestParameters.desiredCount; @@ -268,7 +284,7 @@ function messageTextFromMessageObject(message) { } else if (message.source === "dockstore.deployer") { return dockstoreDeployerMessageText(message); } else if (message.source === "aws.ecs") { - return ecsAutoScalingMessageText(message); + return ecsActivityMessageText(message); } else if (message.source === "aws.cloudwatch") { return cloudWatchEventBridgeAlarmMessageText(message); } else { From 2f19931178477a7bf178a2d96a1815dce7490a35 Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Wed, 17 Jan 2024 13:06:22 -0800 Subject: [PATCH 2/6] update task stopped message --- cloud-watch-to-slack-testing/deployment/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 00a68d7..0614e1d 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -255,11 +255,18 @@ function ecsActivityMessageText(message) { } function ecsTaskStateChangeMessageText(message) { - const taskArn = message.resources[0]; - const clusterArn = message.detail.clusterArn; + const taskArn = message.detail.taskArn; const lastStatus = message.detail.lastStatus; - console.log(`Task ${taskArn} in cluster ${clusterArn} is now ${lastStatus}`); - return `Task is now ${lastStatus}`; + let messageText = `Task ${taskArn} is now ${lastStatus}`; + ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach( + field => { + const value = message.detail[field]; + if (value != undefined) { + messageText += `\n${field}: ${value}`; + } + } + ); + return messageText; } function ecsAutoScalingMessageText(message) { From e712e205adea37dff244279e424fb2c286e5e885 Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Wed, 17 Jan 2024 13:38:41 -0800 Subject: [PATCH 3/6] make more robust to bad input --- .../deployment/index.js | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 0614e1d..2d47841 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -255,17 +255,19 @@ function ecsActivityMessageText(message) { } function ecsTaskStateChangeMessageText(message) { - const taskArn = message.detail.taskArn; - const lastStatus = message.detail.lastStatus; + const taskArn = message?.detail?.taskArn; + const lastStatus = message?.detail?.lastStatus; let messageText = `Task ${taskArn} is now ${lastStatus}`; - ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach( - field => { - const value = message.detail[field]; - if (value != undefined) { - messageText += `\n${field}: ${value}`; + if (message?.detail != undefined) { + ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach( + (name) => { + const value = message?.detail[name]; + if (value != undefined) { + messageText += `\n${name}: ${value}`; + } } - } - ); + ); + } return messageText; } From 47998374e7679526ae4f8181916ff41e9ef7729d Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Wed, 17 Jan 2024 13:46:53 -0800 Subject: [PATCH 4/6] fix eslint --- cloud-watch-to-slack-testing/deployment/index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 2d47841..7ca21a4 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -259,14 +259,12 @@ function ecsTaskStateChangeMessageText(message) { const lastStatus = message?.detail?.lastStatus; let messageText = `Task ${taskArn} is now ${lastStatus}`; if (message?.detail != undefined) { - ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach( - (name) => { - const value = message?.detail[name]; - if (value != undefined) { - messageText += `\n${name}: ${value}`; - } + ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach((name) => { + const value = message?.detail[name]; + if (value != undefined) { + messageText += `\n${name}: ${value}`; } - ); + }); } return messageText; } From 3f992c8466cc00e0829af5617ec435eb765e971a Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Wed, 17 Jan 2024 16:04:14 -0800 Subject: [PATCH 5/6] remove startedBy --- cloud-watch-to-slack-testing/deployment/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 7ca21a4..53d5fd2 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -259,7 +259,7 @@ function ecsTaskStateChangeMessageText(message) { const lastStatus = message?.detail?.lastStatus; let messageText = `Task ${taskArn} is now ${lastStatus}`; if (message?.detail != undefined) { - ["startedAt", "startedBy", "stoppedAt", "stoppedReason"].forEach((name) => { + ["startedAt", "stoppedAt", "stoppedReason"].forEach((name) => { const value = message?.detail[name]; if (value != undefined) { messageText += `\n${name}: ${value}`; From 5d549633ec9817d183fd23f60e89b8158fcae3e9 Mon Sep 17 00:00:00 2001 From: Stephen Von Worley Date: Thu, 18 Jan 2024 10:02:33 -0800 Subject: [PATCH 6/6] eliminate an if --- cloud-watch-to-slack-testing/deployment/index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cloud-watch-to-slack-testing/deployment/index.js b/cloud-watch-to-slack-testing/deployment/index.js index 53d5fd2..3b2c448 100644 --- a/cloud-watch-to-slack-testing/deployment/index.js +++ b/cloud-watch-to-slack-testing/deployment/index.js @@ -258,14 +258,12 @@ function ecsTaskStateChangeMessageText(message) { const taskArn = message?.detail?.taskArn; const lastStatus = message?.detail?.lastStatus; let messageText = `Task ${taskArn} is now ${lastStatus}`; - if (message?.detail != undefined) { - ["startedAt", "stoppedAt", "stoppedReason"].forEach((name) => { - const value = message?.detail[name]; - if (value != undefined) { - messageText += `\n${name}: ${value}`; - } - }); - } + ["startedAt", "stoppedAt", "stoppedReason"].forEach((name) => { + const value = message?.detail?.[name]; + if (value != undefined) { + messageText += `\n${name}: ${value}`; + } + }); return messageText; }