From f059464f30f7dc922381fed0b1b256a91b417eed Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Mon, 8 Sep 2025 09:14:16 -0700 Subject: [PATCH 1/2] PPLMonitor CRUD Models Signed-off-by: Dennis Toepker --- .../alerting/action/AlertingActions.kt | 28 +++++++ .../alerting/action/DeleteMonitorV2Request.kt | 34 +++++++++ .../action/DeleteMonitorV2Response.kt | 38 ++++++++++ .../action/ExecuteMonitorV2Request.kt | 71 ++++++++++++++++++ .../action/ExecuteMonitorV2Response.kt | 33 ++++++++ .../alerting/action/GetMonitorV2Request.kt | 47 ++++++++++++ .../alerting/action/GetMonitorV2Response.kt | 75 +++++++++++++++++++ .../alerting/action/IndexMonitorV2Request.kt | 64 ++++++++++++++++ .../alerting/action/IndexMonitorV2Response.kt | 68 +++++++++++++++++ .../alerting/action/SearchMonitorV2Request.kt | 32 ++++++++ 10 files changed, 490 insertions(+) create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Request.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Response.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Response.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Request.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Response.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Request.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Response.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorV2Request.kt diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt index fcf98261..4e7d7621 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt @@ -8,6 +8,7 @@ import org.opensearch.action.ActionType import org.opensearch.action.search.SearchResponse object AlertingActions { + // Alerting V1 const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write" const val INDEX_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/write" const val GET_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/get" @@ -25,6 +26,13 @@ object AlertingActions { const val SEARCH_COMMENTS_ACTION_NAME = "cluster:admin/opensearch/alerting/comments/search" const val DELETE_COMMENT_ACTION_NAME = "cluster:admin/opensearch/alerting/comments/delete" + // Alerting V2 + const val INDEX_MONITOR_V2_ACTION_NAME = "cluster:admin/opensearch/alerting/v2/monitor/write" + const val GET_MONITOR_V2_ACTION_NAME = "cluster:admin/opensearch/alerting/v2/monitor/get" + const val SEARCH_MONITORS_V2_ACTION_NAME = "cluster:admin/opensearch/alerting/v2/monitor/search" + const val DELETE_MONITOR_V2_ACTION_NAME = "cluster:admin/opensearch/alerting/v2/monitor/delete" + const val EXECUTE_MONITOR_V2_ACTION_NAME = "cluster:admin/opensearch/alerting/v2/monitor/execute" + @JvmField val INDEX_MONITOR_ACTION_TYPE = ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse) @@ -88,4 +96,24 @@ object AlertingActions { @JvmField val DELETE_COMMENT_ACTION_TYPE = ActionType(DELETE_COMMENT_ACTION_NAME, ::DeleteCommentResponse) + + @JvmField + val INDEX_MONITOR_V2_ACTION_TYPE = + ActionType(INDEX_MONITOR_V2_ACTION_NAME, ::IndexMonitorV2Response) + + @JvmField + val GET_MONITOR_V2_ACTION_TYPE = + ActionType(GET_MONITOR_V2_ACTION_NAME, ::GetMonitorV2Response) + + @JvmField + val SEARCH_MONITORS_V2_ACTION_TYPE = + ActionType(SEARCH_MONITORS_V2_ACTION_NAME, ::SearchResponse) + + @JvmField + val DELETE_MONITOR_V2_ACTION_TYPE = + ActionType(DELETE_MONITOR_V2_ACTION_NAME, ::DeleteMonitorV2Response) + + @JvmField + val EXECUTE_MONITOR_V2_ACTION_TYPE = + ActionType(EXECUTE_MONITOR_V2_ACTION_NAME, ::ExecuteMonitorV2Response) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Request.kt new file mode 100644 index 00000000..410ae250 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Request.kt @@ -0,0 +1,34 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.action.support.WriteRequest +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import java.io.IOException + +class DeleteMonitorV2Request : ActionRequest { + val monitorV2Id: String + val refreshPolicy: WriteRequest.RefreshPolicy + + constructor(monitorV2Id: String, refreshPolicy: WriteRequest.RefreshPolicy) : super() { + this.monitorV2Id = monitorV2Id + this.refreshPolicy = refreshPolicy + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + monitorV2Id = sin.readString(), + refreshPolicy = WriteRequest.RefreshPolicy.readFrom(sin) + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(monitorV2Id) + refreshPolicy.writeTo(out) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Response.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Response.kt new file mode 100644 index 00000000..6d7ffef0 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorV2Response.kt @@ -0,0 +1,38 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.util.IndexUtils +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.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder + +class DeleteMonitorV2Response : BaseResponse { + var id: String + var version: Long + + constructor( + id: String, + version: Long + ) : super() { + this.id = id + this.version = version + } + + constructor(sin: StreamInput) : this( + sin.readString(), // id + sin.readLong() // version + ) + + override fun writeTo(out: StreamOutput) { + out.writeString(id) + out.writeLong(version) + } + + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return builder.startObject() + .field(IndexUtils._ID, id) + .field(IndexUtils._VERSION, version) + .endObject() + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt new file mode 100644 index 00000000..7a825d15 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt @@ -0,0 +1,71 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.action.ValidateActions +import org.opensearch.common.unit.TimeValue +import org.opensearch.commons.alerting.model.MonitorV2 +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import java.io.IOException + +class ExecuteMonitorV2Request : ActionRequest { + val dryrun: Boolean + val monitorId: String? // exactly one of monitorId or monitor must be non-null + val monitorV2: MonitorV2? + val requestStart: TimeValue? + val requestEnd: TimeValue + + constructor( + dryrun: Boolean, + monitorId: String?, + monitorV2: MonitorV2?, + requestStart: TimeValue? = null, + requestEnd: TimeValue + ) : super() { + this.dryrun = dryrun + this.monitorId = monitorId + this.monitorV2 = monitorV2 + this.requestStart = requestStart + this.requestEnd = requestEnd + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + sin.readBoolean(), // dryrun + sin.readOptionalString(), // monitorId + if (sin.readBoolean()) { + MonitorV2.readFrom(sin) // monitor + } else { + null + }, + sin.readOptionalTimeValue(), + sin.readTimeValue() // requestEnd + ) + + override fun validate(): ActionRequestValidationException? { + // ensure exactly one of monitor ID or monitorV2 is supplied + var exception: ActionRequestValidationException? = null + if (monitorV2 == null && monitorId == null) { + exception = ValidateActions.addValidationError( + "Neither a monitor ID or monitor object was supplied", + exception + ) + } + return exception + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeBoolean(dryrun) + out.writeOptionalString(monitorId) + if (monitorV2 != null) { + out.writeBoolean(true) + monitorV2.writeTo(out) + } else { + out.writeBoolean(false) + } + out.writeOptionalTimeValue(requestStart) + out.writeTimeValue(requestEnd) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Response.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Response.kt new file mode 100644 index 00000000..4437c47e --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Response.kt @@ -0,0 +1,33 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.model.MonitorV2RunResult +import org.opensearch.core.action.ActionResponse +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.core.xcontent.ToXContent +import org.opensearch.core.xcontent.ToXContentObject +import org.opensearch.core.xcontent.XContentBuilder +import java.io.IOException + +class ExecuteMonitorV2Response : ActionResponse, ToXContentObject { + val monitorV2RunResult: MonitorV2RunResult<*> + + constructor(monitorV2RunResult: MonitorV2RunResult<*>) : super() { + this.monitorV2RunResult = monitorV2RunResult + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + MonitorV2RunResult.readFrom(sin) // monitorRunResult + ) + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + monitorV2RunResult.writeTo(out) + } + + @Throws(IOException::class) + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return monitorV2RunResult.toXContent(builder, ToXContent.EMPTY_PARAMS) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Request.kt new file mode 100644 index 00000000..bd944a50 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Request.kt @@ -0,0 +1,47 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.search.fetch.subphase.FetchSourceContext +import java.io.IOException + +class GetMonitorV2Request : ActionRequest { + val monitorV2Id: String + val version: Long + val srcContext: FetchSourceContext? + + constructor( + monitorV2Id: String, + version: Long, + srcContext: FetchSourceContext? + ) : super() { + this.monitorV2Id = monitorV2Id + this.version = version + this.srcContext = srcContext + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + sin.readString(), // monitorV2Id + sin.readLong(), // version + if (sin.readBoolean()) { + FetchSourceContext(sin) // srcContext + } else { + null + } + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(monitorV2Id) + out.writeLong(version) + out.writeBoolean(srcContext != null) + srcContext?.writeTo(out) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Response.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Response.kt new file mode 100644 index 00000000..8232fda0 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorV2Response.kt @@ -0,0 +1,75 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.model.MonitorV2 +import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID +import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM +import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO +import org.opensearch.commons.alerting.util.IndexUtils.Companion._VERSION +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.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder +import java.io.IOException + +class GetMonitorV2Response : BaseResponse { + var id: String + var version: Long + var seqNo: Long + var primaryTerm: Long + var monitorV2: MonitorV2? + + constructor( + id: String, + version: Long, + seqNo: Long, + primaryTerm: Long, + monitorV2: MonitorV2? + ) : super() { + this.id = id + this.version = version + this.seqNo = seqNo + this.primaryTerm = primaryTerm + this.monitorV2 = monitorV2 + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + id = sin.readString(), // id + version = sin.readLong(), // version + seqNo = sin.readLong(), // seqNo + primaryTerm = sin.readLong(), // primaryTerm + monitorV2 = if (sin.readBoolean()) { + MonitorV2.readFrom(sin) // monitorV2 + } else { + null + } + ) + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(id) + out.writeLong(version) + out.writeLong(seqNo) + out.writeLong(primaryTerm) + if (monitorV2 != null) { + out.writeBoolean(true) + monitorV2?.writeTo(out) + } else { + out.writeBoolean(false) + } + } + + @Throws(IOException::class) + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + builder.startObject() + .field(_ID, id) + .field(_VERSION, version) + .field(_SEQ_NO, seqNo) + .field(_PRIMARY_TERM, primaryTerm) + if (monitorV2 != null) { + builder.field("monitorV2", monitorV2) + } + return builder.endObject() + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Request.kt new file mode 100644 index 00000000..298372b7 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Request.kt @@ -0,0 +1,64 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.action.support.WriteRequest +import org.opensearch.commons.alerting.model.MonitorV2 +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.rest.RestRequest +import java.io.IOException + +class IndexMonitorV2Request : ActionRequest { + val monitorId: String + val seqNo: Long + val primaryTerm: Long + val refreshPolicy: WriteRequest.RefreshPolicy + val method: RestRequest.Method + var monitorV2: MonitorV2 +// val rbacRoles: List? + + constructor( + monitorId: String, + seqNo: Long, + primaryTerm: Long, + refreshPolicy: WriteRequest.RefreshPolicy, + method: RestRequest.Method, + monitorV2: MonitorV2 +// rbacRoles: List? = null + ) : super() { + this.monitorId = monitorId + this.seqNo = seqNo + this.primaryTerm = primaryTerm + this.refreshPolicy = refreshPolicy + this.method = method + this.monitorV2 = monitorV2 +// this.rbacRoles = rbacRoles + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + monitorId = sin.readString(), + seqNo = sin.readLong(), + primaryTerm = sin.readLong(), + refreshPolicy = WriteRequest.RefreshPolicy.readFrom(sin), + method = sin.readEnum(RestRequest.Method::class.java), + monitorV2 = MonitorV2.readFrom(sin) +// rbacRoles = sin.readOptionalStringList() + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(monitorId) + out.writeLong(seqNo) + out.writeLong(primaryTerm) + refreshPolicy.writeTo(out) + out.writeEnum(method) + MonitorV2.writeTo(out, monitorV2) +// out.writeOptionalStringCollection(rbacRoles) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Response.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Response.kt new file mode 100644 index 00000000..c47f12ab --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorV2Response.kt @@ -0,0 +1,68 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.model.MonitorV2 +import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID +import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM +import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO +import org.opensearch.commons.alerting.util.IndexUtils.Companion._VERSION +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.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder +import java.io.IOException + +class IndexMonitorV2Response : BaseResponse { + var id: String + var version: Long + var seqNo: Long + var primaryTerm: Long + var monitorV2: MonitorV2 + + constructor( + id: String, + version: Long, + seqNo: Long, + primaryTerm: Long, + monitorV2: MonitorV2 + ) : super() { + this.id = id + this.version = version + this.seqNo = seqNo + this.primaryTerm = primaryTerm + this.monitorV2 = monitorV2 + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + sin.readString(), // id + sin.readLong(), // version + sin.readLong(), // seqNo + sin.readLong(), // primaryTerm + MonitorV2.readFrom(sin) // monitorV2 + ) + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(id) + out.writeLong(version) + out.writeLong(seqNo) + out.writeLong(primaryTerm) + MonitorV2.writeTo(out, monitorV2) + } + + @Throws(IOException::class) + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return builder.startObject() + .field(_ID, id) + .field(_VERSION, version) + .field(_SEQ_NO, seqNo) + .field(_PRIMARY_TERM, primaryTerm) + .field(MONITOR_V2_FIELD, monitorV2) + .endObject() + } + + companion object { + const val MONITOR_V2_FIELD = "monitor_v2" + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorV2Request.kt new file mode 100644 index 00000000..12a2129f --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorV2Request.kt @@ -0,0 +1,32 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.action.search.SearchRequest +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import java.io.IOException + +class SearchMonitorV2Request : ActionRequest { + val searchRequest: SearchRequest + + constructor( + searchRequest: SearchRequest + ) : super() { + this.searchRequest = searchRequest + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + searchRequest = SearchRequest(sin) + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + searchRequest.writeTo(out) + } +} From 5238307334bd5e3a73666125cbb3b040982375af Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Mon, 8 Sep 2025 09:29:56 -0700 Subject: [PATCH 2/2] minor updates Signed-off-by: Dennis Toepker --- .../alerting/action/ExecuteMonitorV2Request.kt | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt index 7a825d15..15fc3b9f 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/ExecuteMonitorV2Request.kt @@ -43,17 +43,12 @@ class ExecuteMonitorV2Request : ActionRequest { sin.readTimeValue() // requestEnd ) - override fun validate(): ActionRequestValidationException? { - // ensure exactly one of monitor ID or monitorV2 is supplied - var exception: ActionRequestValidationException? = null + override fun validate(): ActionRequestValidationException? = if (monitorV2 == null && monitorId == null) { - exception = ValidateActions.addValidationError( - "Neither a monitor ID or monitor object was supplied", - exception - ) + ValidateActions.addValidationError("Neither a monitor ID nor monitor object was supplied", null) + } else { + null } - return exception - } @Throws(IOException::class) override fun writeTo(out: StreamOutput) {