From 6ac125cabbbed5cfb3b37c6f5dc328058c3ee0f2 Mon Sep 17 00:00:00 2001 From: Chase Engelbrecht Date: Tue, 19 Dec 2023 22:41:36 -0800 Subject: [PATCH] Don't attempt to parse workflow if it doesn't exist Signed-off-by: Chase Engelbrecht --- .../TransportDeleteWorkflowAction.kt | 32 ++++++++++++++----- 1 file changed, 24 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..3aee9b2c0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt @@ -60,6 +60,7 @@ import org.opensearch.index.reindex.DeleteByQueryRequestBuilder import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.tasks.Task import org.opensearch.transport.TransportService +import java.util.Optional private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) /** @@ -119,7 +120,12 @@ class TransportDeleteWorkflowAction @Inject constructor( ) { suspend fun resolveUserAndStart() { try { - val workflow = getWorkflow() + val optionalWorkflow = getWorkflow() + if (optionalWorkflow.isEmpty) { + return + } + + val workflow = optionalWorkflow.get() val canDelete = user == null || !doFilterForUser(user) || @@ -296,17 +302,27 @@ class TransportDeleteWorkflowAction @Inject constructor( return deletableMonitors } - private suspend fun getWorkflow(): Workflow { + private suspend fun getWorkflow(): Optional { 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 Optional.empty() } + + return Optional.of(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