forked from opensearch-project/alerting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chase Engelbrecht <engechas@amazon.com>
- Loading branch information
Showing
10 changed files
with
247 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestRunWorkflowAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.opensearch.alerting.resthandler | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.alerting.AlertingPlugin | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.RunWorkflowRequest | ||
import org.opensearch.core.xcontent.XContentParser | ||
import org.opensearch.core.xcontent.XContentParserUtils | ||
import org.opensearch.rest.BaseRestHandler | ||
import org.opensearch.rest.RestHandler | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.rest.action.RestToXContentListener | ||
|
||
class RestRunWorkflowAction : BaseRestHandler() { | ||
private val log = LogManager.getLogger(javaClass) | ||
|
||
override fun getName(): String { | ||
return "run_workflow_action" | ||
} | ||
|
||
override fun routes(): List<RestHandler.Route> { | ||
return listOf( | ||
RestHandler.Route( | ||
RestRequest.Method.POST, | ||
"${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}/run" | ||
) | ||
) | ||
} | ||
|
||
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
log.debug("${request.method()} ${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}/run") | ||
|
||
val xcp = request.contentParser() | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp) | ||
val runWorkflowRequest = RunWorkflowRequest.parse(xcp) | ||
|
||
return RestChannelConsumer { | ||
channel -> | ||
client.execute( | ||
AlertingActions.RUN_WORKFLOW_ACTION_TYPE, | ||
runWorkflowRequest, | ||
RestToXContentListener(channel) | ||
) | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportRunWorkflowAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.alerting.transport | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.ResourceNotFoundException | ||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.support.ActionFilters | ||
import org.opensearch.action.support.HandledTransportAction | ||
import org.opensearch.alerting.MonitorRunnerService | ||
import org.opensearch.alerting.opensearchapi.suspendUntil | ||
import org.opensearch.alerting.util.AlertingException | ||
import org.opensearch.alerting.workflow.CompositeWorkflowRunner | ||
import org.opensearch.client.Client | ||
import org.opensearch.common.inject.Inject | ||
import org.opensearch.common.io.stream.BytesStreamOutput | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.GetWorkflowRequest | ||
import org.opensearch.commons.alerting.action.GetWorkflowResponse | ||
import org.opensearch.commons.alerting.action.RunWorkflowRequest | ||
import org.opensearch.commons.alerting.action.RunWorkflowResponse | ||
import org.opensearch.core.action.ActionListener | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.tasks.Task | ||
import org.opensearch.transport.TransportService | ||
import java.time.Instant | ||
|
||
private val log = LogManager.getLogger(TransportRunWorkflowAction::class.java) | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) | ||
|
||
class TransportRunWorkflowAction @Inject constructor( | ||
transportService: TransportService, | ||
val client: Client, | ||
actionFilters: ActionFilters, | ||
val transportGetWorkflowAction: TransportGetWorkflowAction | ||
) : HandledTransportAction<ActionRequest, RunWorkflowResponse>( | ||
AlertingActions.RUN_WORKFLOW_ACTION_NAME, transportService, actionFilters, | ||
::RunWorkflowRequest | ||
) { | ||
|
||
override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener<RunWorkflowResponse>) { | ||
// TODO ser/de here is a hack to avoid a ClassCastException. Security Analytics and Alerting both use the RunWorkflowRequest object | ||
// but have different ClassLoaders - https://stackoverflow.com/a/826345 | ||
val outputStream = BytesStreamOutput() | ||
request.writeTo(outputStream) | ||
val actualRequest = RunWorkflowRequest(outputStream.copyBytes().streamInput()) | ||
|
||
scope.launch { | ||
val getWorkflowResponse: GetWorkflowResponse = | ||
transportGetWorkflowAction.client.suspendUntil { | ||
val getWorkflowRequest = GetWorkflowRequest(actualRequest.workflowId, RestRequest.Method.GET) | ||
execute(AlertingActions.GET_WORKFLOW_ACTION_TYPE, getWorkflowRequest, it) | ||
} | ||
|
||
if (getWorkflowResponse.workflow != null) { | ||
try { | ||
// TODO - is using monitorCtx like this safe? | ||
// TODO - is Instant.now() fine? | ||
val workflowRunResult = CompositeWorkflowRunner.runWorkflow( | ||
getWorkflowResponse.workflow!!, | ||
MonitorRunnerService.monitorCtx, | ||
Instant.now(), | ||
Instant.now(), | ||
false, | ||
actualRequest.documents | ||
) | ||
|
||
if (workflowRunResult.error != null) { | ||
actionListener.onFailure(workflowRunResult.error) | ||
} else { | ||
actionListener.onResponse(RunWorkflowResponse(RestStatus.OK)) | ||
} | ||
} catch (e: Exception) { | ||
actionListener.onFailure( | ||
AlertingException.wrap(e) | ||
) | ||
} | ||
} else { | ||
actionListener.onFailure( | ||
AlertingException.wrap( | ||
ResourceNotFoundException("Workflow with id ${actualRequest.workflowId} not found", RestStatus.NOT_FOUND) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.