Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to enable/disable a monitor #770

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object AlertingActions {
const val INDEX_COMMENT_ACTION_NAME = "cluster:admin/opensearch/alerting/comments/write"
const val SEARCH_COMMENTS_ACTION_NAME = "cluster:admin/opensearch/alerting/comments/search"
const val DELETE_COMMENT_ACTION_NAME = "cluster:admin/opensearch/alerting/comments/delete"
const val TOGGLE_MONITOR_ACTION_NAME = "cluster:admin/opensearch/alerting/monitor/toggle"

@JvmField
val INDEX_MONITOR_ACTION_TYPE =
Expand Down Expand Up @@ -88,4 +89,8 @@ object AlertingActions {
@JvmField
val DELETE_COMMENT_ACTION_TYPE =
ActionType(DELETE_COMMENT_ACTION_NAME, ::DeleteCommentResponse)

@JvmField
val TOGGLE_MONITOR_ACTION_TYPE =
ActionType(TOGGLE_MONITOR_ACTION_NAME, ::ToggleMonitorResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.rest.RestRequest
import java.io.IOException

class ToggleMonitorRequest : ActionRequest {
val monitorId: String
val enabled: Boolean
val seqNo: Long
val primaryTerm: Long
val method: RestRequest.Method

constructor(
monitorId: String,
enabled: Boolean,
seqNo: Long,
primaryTerm: Long,
method: RestRequest.Method
) : super() {
this.monitorId = monitorId
this.enabled = enabled
this.seqNo = seqNo
this.primaryTerm = primaryTerm
this.method = method
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this (
monitorId = sin.readString(),
enabled = sin.readBoolean(),
seqNo = sin.readLong(),
primaryTerm = sin.readLong(),
method = sin.readEnum(RestRequest.Method::class.java)
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(monitorId)
out.writeBoolean(enabled)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
out.writeEnum(method)
}

override fun validate(): ActionRequestValidationException? {
return null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this function about?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an abstract method defined in the ActionRequest class, so need to provide an implementation here. Based on it's usage in other classes, it is used to validate the data members, which is not required in this case.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.opensearch.commons.alerting.action

import org.opensearch.commons.alerting.model.Monitor
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.Params
import org.opensearch.core.xcontent.XContentBuilder
import java.io.IOException

class ToggleMonitorResponse : BaseResponse {
var id: String
var version: Long
var seqNo: Long
var primaryTerm: Long
var monitor: Monitor

constructor(
id: String,
version: Long,
seqNo: Long,
primaryTerm: Long,
monitor: Monitor
) : super() {
this.id = id
this.version = version
this.seqNo = seqNo
this.primaryTerm = primaryTerm
this.monitor = monitor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider excluding the monitor from the response. Security analytics creates per document monitors that can have thousands of queries (a.k.a., rules) in them. That can make the response size pretty big, which may be extraneous for this kind of API. Perhaps we can instead just return the updated enabled value, instead of the entire monitor? We can discuss with the team.

}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
sin.readString(), // id
sin.readLong(), // version
sin.readLong(), // seqNo
sin.readLong(), // primaryTerm
Monitor.readFrom(sin) as Monitor // monitor
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeLong(version)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
monitor.writeTo(out)
}

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: Params): XContentBuilder {
return builder.startObject()
.field(_ID, id)
.field(_VERSION, version)
.field(_SEQ_NO, seqNo)
.field(_PRIMARY_TERM, primaryTerm)
.field("Monitor", monitor)
.endObject()
}
}
Loading