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.
Created inital version of Rest layer for workflows
Signed-off-by: Stevan Buzejic <[email protected]>
- Loading branch information
Showing
11 changed files
with
567 additions
and
58 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
3 changes: 0 additions & 3 deletions
3
alerting/src/main/kotlin/org/opensearch/alerting/model/workflow/WorkflowRunResult.kt
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestDeleteWorkflowAction.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,59 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.resthandler | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.action.support.WriteRequest | ||
import org.opensearch.alerting.AlertingPlugin | ||
import org.opensearch.alerting.util.REFRESH | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.DeleteWorkflowRequest | ||
import org.opensearch.rest.BaseRestHandler | ||
import org.opensearch.rest.RestHandler | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.rest.action.RestToXContentListener | ||
import java.io.IOException | ||
|
||
/** | ||
* This class consists of the REST handler to delete workflows. | ||
*/ | ||
class RestDeleteWorkflowAction : BaseRestHandler() { | ||
|
||
private val log = LogManager.getLogger(javaClass) | ||
|
||
override fun getName(): String { | ||
return "delete_workflow_action" | ||
} | ||
|
||
override fun routes(): List<RestHandler.Route> { | ||
return listOf( | ||
RestHandler.Route( | ||
RestRequest.Method.DELETE, | ||
"${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}" | ||
) | ||
) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
log.debug("${request.method()} ${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}") | ||
|
||
val workflowId = request.param("workflowID") | ||
log.debug("${request.method()} ${AlertingPlugin.WORKFLOW_BASE_URI}/$workflowId") | ||
|
||
val refreshPolicy = | ||
WriteRequest.RefreshPolicy.parse(request.param(REFRESH, WriteRequest.RefreshPolicy.IMMEDIATE.value)) | ||
val deleteWorkflowRequest = DeleteWorkflowRequest(workflowId, refreshPolicy) | ||
|
||
return RestChannelConsumer { channel -> | ||
client.execute( | ||
AlertingActions.DELETE_WORKFLOW_ACTION_TYPE, deleteWorkflowRequest, | ||
RestToXContentListener(channel) | ||
) | ||
} | ||
} | ||
} |
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
98 changes: 98 additions & 0 deletions
98
alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestIndexWorkflowAction.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,98 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.alerting.resthandler | ||
|
||
import org.opensearch.action.support.WriteRequest | ||
import org.opensearch.alerting.AlertingPlugin | ||
import org.opensearch.alerting.util.IF_PRIMARY_TERM | ||
import org.opensearch.alerting.util.IF_SEQ_NO | ||
import org.opensearch.alerting.util.REFRESH | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.XContentParser | ||
import org.opensearch.common.xcontent.XContentParserUtils | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.IndexWorkflowRequest | ||
import org.opensearch.commons.alerting.action.IndexWorkflowResponse | ||
import org.opensearch.commons.alerting.model.Workflow | ||
import org.opensearch.index.seqno.SequenceNumbers | ||
import org.opensearch.rest.BaseRestHandler | ||
import org.opensearch.rest.BaseRestHandler.RestChannelConsumer | ||
import org.opensearch.rest.BytesRestResponse | ||
import org.opensearch.rest.RestChannel | ||
import org.opensearch.rest.RestHandler | ||
import org.opensearch.rest.RestRequest | ||
import org.opensearch.rest.RestResponse | ||
import org.opensearch.rest.RestStatus | ||
import org.opensearch.rest.action.RestResponseListener | ||
import java.io.IOException | ||
import java.time.Instant | ||
|
||
/** | ||
* Rest handlers to create and update workflows. | ||
*/ | ||
class RestIndexWorkflowAction : BaseRestHandler() { | ||
|
||
override fun getName(): String { | ||
return "index_workflow_action" | ||
} | ||
|
||
override fun routes(): List<RestHandler.Route> { | ||
return listOf( | ||
RestHandler.Route(RestRequest.Method.POST, AlertingPlugin.WORKFLOW_BASE_URI), | ||
RestHandler.Route( | ||
RestRequest.Method.PUT, | ||
"${AlertingPlugin.WORKFLOW_BASE_URI}/{workflowID}" | ||
) | ||
) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { | ||
val id = request.param("workflowID", Workflow.NO_ID) | ||
if (request.method() == RestRequest.Method.PUT && Workflow.NO_ID == id) { | ||
throw IllegalArgumentException("Missing workflow ID") | ||
} | ||
|
||
// Validate request by parsing JSON to Monitor | ||
val xcp = request.contentParser() | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp) | ||
val workflow = Workflow.parse(xcp, id).copy(lastUpdateTime = Instant.now()) | ||
val rbacRoles = request.contentParser().map()["rbac_roles"] as List<String>? | ||
|
||
val seqNo = request.paramAsLong(IF_SEQ_NO, SequenceNumbers.UNASSIGNED_SEQ_NO) | ||
val primaryTerm = request.paramAsLong(IF_PRIMARY_TERM, SequenceNumbers.UNASSIGNED_PRIMARY_TERM) | ||
val refreshPolicy = if (request.hasParam(REFRESH)) { | ||
WriteRequest.RefreshPolicy.parse(request.param(REFRESH)) | ||
} else { | ||
WriteRequest.RefreshPolicy.IMMEDIATE | ||
} | ||
val workflowRequest = | ||
IndexWorkflowRequest(id, seqNo, primaryTerm, refreshPolicy, request.method(), workflow, rbacRoles) | ||
|
||
return RestChannelConsumer { channel -> | ||
client.execute(AlertingActions.INDEX_WORKFLOW_ACTION_TYPE, workflowRequest, indexMonitorResponse(channel, request.method())) | ||
} | ||
} | ||
|
||
private fun indexMonitorResponse(channel: RestChannel, restMethod: RestRequest.Method): RestResponseListener<IndexWorkflowResponse> { | ||
return object : RestResponseListener<IndexWorkflowResponse>(channel) { | ||
@Throws(Exception::class) | ||
override fun buildResponse(response: IndexWorkflowResponse): RestResponse { | ||
var returnStatus = RestStatus.CREATED | ||
if (restMethod == RestRequest.Method.PUT) | ||
returnStatus = RestStatus.OK | ||
|
||
val restResponse = | ||
BytesRestResponse(returnStatus, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)) | ||
if (returnStatus == RestStatus.CREATED) { | ||
val location = "${AlertingPlugin.WORKFLOW_BASE_URI}/${response.id}" | ||
restResponse.addHeader("Location", location) | ||
} | ||
return restResponse | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.