From 49465bd6d431ffb06393ae8077d91e1f22159862 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 3 Jan 2024 22:13:09 +0000 Subject: [PATCH] Don't attempt to parse workflow if it doesn't exist (#1346) Signed-off-by: Chase Engelbrecht (cherry picked from commit 733fd4e8b307251d4cd1d7c1a8509de346fd3190) Signed-off-by: github-actions[bot] --- .../TransportDeleteWorkflowAction.kt | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt index 9b076a600..5a9938f56 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt @@ -119,7 +119,7 @@ class TransportDeleteWorkflowAction @Inject constructor( ) { suspend fun resolveUserAndStart() { try { - val workflow = getWorkflow() + val workflow: Workflow = getWorkflow() ?: return val canDelete = user == null || !doFilterForUser(user) || @@ -296,17 +296,27 @@ class TransportDeleteWorkflowAction @Inject constructor( return deletableMonitors } - private suspend fun getWorkflow(): Workflow { + private suspend fun getWorkflow(): Workflow? { val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, workflowId) val getResponse: GetResponse = client.suspendUntil { get(getRequest, it) } - if (getResponse.isExists == false) { - actionListener.onFailure( - AlertingException.wrap( - OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND) - ) - ) + if (!getResponse.isExists) { + handleWorkflowMissing() + return null } + + return parseWorkflow(getResponse) + } + + private fun handleWorkflowMissing() { + actionListener.onFailure( + AlertingException.wrap( + OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND) + ) + ) + } + + private fun parseWorkflow(getResponse: GetResponse): Workflow { val xcp = XContentHelper.createParser( xContentRegistry, LoggingDeprecationHandler.INSTANCE, getResponse.sourceAsBytesRef, XContentType.JSON