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 b43ec3c3f..593582d9c 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