forked from opensearch-project/common-utils
-
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.
Add execute streaming workflow changes
Signed-off-by: Chase Engelbrecht <[email protected]>
- Loading branch information
Showing
14 changed files
with
498 additions
and
9 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
83 changes: 83 additions & 0 deletions
83
src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteStreamingWorkflowRequest.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,83 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.ActionRequestValidationException | ||
import org.opensearch.commons.alerting.model.StreamingIndex | ||
import org.opensearch.core.common.io.stream.StreamInput | ||
import org.opensearch.core.common.io.stream.StreamOutput | ||
import org.opensearch.core.xcontent.XContentParser | ||
import org.opensearch.core.xcontent.XContentParserUtils | ||
import java.io.IOException | ||
|
||
class ExecuteStreamingWorkflowRequest : ActionRequest { | ||
var workflowId: String | ||
var indices: List<StreamingIndex> | ||
|
||
constructor(workflowId: String, indices: List<StreamingIndex>) : super() { | ||
this.workflowId = workflowId | ||
this.indices = indices | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
workflowId = sin.readString(), | ||
indices = sin.readList(::StreamingIndex) | ||
) | ||
|
||
override fun validate(): ActionRequestValidationException? { | ||
return null | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeString(workflowId) | ||
out.writeCollection(indices) | ||
} | ||
|
||
companion object { | ||
const val WORKFLOW_ID_FIELD = "workflowId" | ||
const val INDICES_FIELD = "indices" | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser): ExecuteStreamingWorkflowRequest { | ||
var workflowId: String? = null | ||
var indices: MutableList<StreamingIndex> = mutableListOf() | ||
|
||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
WORKFLOW_ID_FIELD -> workflowId = xcp.text() | ||
INDICES_FIELD -> { | ||
XContentParserUtils.ensureExpectedToken( | ||
XContentParser.Token.START_ARRAY, | ||
xcp.currentToken(), | ||
xcp | ||
) | ||
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { | ||
val index = StreamingIndex.parse(xcp) | ||
indices.add(index) | ||
} | ||
} | ||
|
||
else -> { | ||
xcp.skipChildren() | ||
} | ||
} | ||
} | ||
|
||
return ExecuteStreamingWorkflowRequest( | ||
requireNotNull(workflowId) { "workflowId is null" }, | ||
indices | ||
) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteStreamingWorkflowResponse.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,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.commons.notifications.action.BaseResponse | ||
import org.opensearch.core.common.io.stream.StreamInput | ||
import org.opensearch.core.common.io.stream.StreamOutput | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.core.xcontent.ToXContent | ||
import org.opensearch.core.xcontent.XContentBuilder | ||
import java.io.IOException | ||
|
||
// TODO - return more info so that it can be passed back to the client if there are failures? | ||
class ExecuteStreamingWorkflowResponse : BaseResponse { | ||
private var status: RestStatus | ||
|
||
constructor(status: RestStatus) : super() { | ||
this.status = status | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) { | ||
this.status = sin.readEnum(RestStatus::class.java) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeEnum(status) | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field("status", status.status) | ||
return builder.endObject() | ||
} | ||
|
||
override fun getStatus(): RestStatus { | ||
return this.status | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/org/opensearch/commons/alerting/model/IdDocPair.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,87 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.commons.alerting.model | ||
|
||
import org.opensearch.core.common.bytes.BytesReference | ||
import org.opensearch.core.common.io.stream.StreamInput | ||
import org.opensearch.core.common.io.stream.StreamOutput | ||
import org.opensearch.core.common.io.stream.Writeable | ||
import org.opensearch.core.xcontent.ToXContent | ||
import org.opensearch.core.xcontent.ToXContentObject | ||
import org.opensearch.core.xcontent.XContentBuilder | ||
import org.opensearch.core.xcontent.XContentParser | ||
import org.opensearch.core.xcontent.XContentParserUtils | ||
import java.io.IOException | ||
|
||
class IdDocPair : ToXContentObject, Writeable { | ||
var docId: String | ||
var document: BytesReference | ||
|
||
constructor(docId: String, document: BytesReference) : super() { | ||
this.docId = docId | ||
this.document = document | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
docId = sin.readString(), | ||
document = sin.readBytesReference() | ||
) | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeString(docId) | ||
out.writeBytesReference(document) | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field("docId", docId) | ||
.field("document", document) | ||
return builder.endObject() | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun readFrom(sin: StreamInput): IdDocPair { | ||
return IdDocPair(sin) | ||
} | ||
|
||
companion object { | ||
const val DOC_ID_FIELD = "docId" | ||
const val DOCUMENT_FIELD = "document" | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser): IdDocPair { | ||
var docId: String? = null | ||
var document: BytesReference? = null | ||
|
||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
DOC_ID_FIELD -> docId = xcp.text() | ||
DOCUMENT_FIELD -> { | ||
val xContentBuilder = XContentBuilder.builder(xcp.contentType().xContent()) | ||
xContentBuilder.copyCurrentStructure(xcp) | ||
document = BytesReference.bytes(xContentBuilder) | ||
} | ||
|
||
else -> { | ||
xcp.skipChildren() | ||
} | ||
} | ||
} | ||
|
||
return IdDocPair( | ||
requireNotNull(docId) { "docId is null" }, | ||
requireNotNull(document) { "document is null" } | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.