Skip to content

Commit

Permalink
apis for get workflow alerts and acknowledge chained alerts (#472)
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <[email protected]>
  • Loading branch information
eirsep authored Jul 6, 2023
1 parent 37e36b7 commit b555e24
Show file tree
Hide file tree
Showing 17 changed files with 766 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.opensearch.common.io.stream.NamedWriteableRegistry
import org.opensearch.common.io.stream.Writeable
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse
import org.opensearch.commons.alerting.action.AcknowledgeChainedAlertRequest
import org.opensearch.commons.alerting.action.AlertingActions
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
Expand All @@ -20,6 +21,8 @@ import org.opensearch.commons.alerting.action.GetAlertsRequest
import org.opensearch.commons.alerting.action.GetAlertsResponse
import org.opensearch.commons.alerting.action.GetFindingsRequest
import org.opensearch.commons.alerting.action.GetFindingsResponse
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
import org.opensearch.commons.alerting.action.GetWorkflowRequest
import org.opensearch.commons.alerting.action.GetWorkflowResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
Expand Down Expand Up @@ -147,6 +150,30 @@ object AlertingPluginInterface {
)
}

/**
* Get Workflow Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun getWorkflowAlerts(
client: NodeClient,
request: GetWorkflowAlertsRequest,
listener: ActionListener<GetWorkflowAlertsResponse>
) {
client.execute(
AlertingActions.GET_WORKFLOW_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
GetWorkflowAlertsResponse(
it
)
}
}
)
}

/**
* Get Workflow interface.
* @param client Node client for making transport action
Expand Down Expand Up @@ -237,6 +264,30 @@ object AlertingPluginInterface {
)
}

/**
* Acknowledge Chained Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun acknowledgeChainedAlerts(
client: NodeClient,
request: AcknowledgeChainedAlertRequest,
listener: ActionListener<AcknowledgeAlertResponse>
) {
client.execute(
AlertingActions.ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
AcknowledgeAlertResponse(
it
)
}
}
)
}

@Suppress("UNCHECKED_CAST")
private fun <Response : BaseResponse> wrapActionListener(
listener: ActionListener<Response>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,54 @@ object AlertingActions {
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"
const val GET_WORKFLOW_ALERTS_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow_alerts/get"
const val GET_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/get"
const val DELETE_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/delete"
const val DELETE_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/delete"
const val GET_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/get"
const val ACKNOWLEDGE_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/ack"
const val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/chained_alerts/ack"
const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe"

@JvmField
val INDEX_MONITOR_ACTION_TYPE =
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)

@JvmField
val INDEX_WORKFLOW_ACTION_TYPE =
ActionType(INDEX_WORKFLOW_ACTION_NAME, ::IndexWorkflowResponse)
@JvmField
val GET_ALERTS_ACTION_TYPE =
ActionType(GET_ALERTS_ACTION_NAME, ::GetAlertsResponse)

@JvmField
val GET_WORKFLOW_ALERTS_ACTION_TYPE =
ActionType(GET_WORKFLOW_ALERTS_ACTION_NAME, ::GetWorkflowAlertsResponse)

@JvmField
val GET_WORKFLOW_ACTION_TYPE =
ActionType(GET_WORKFLOW_ACTION_NAME, ::GetWorkflowResponse)

@JvmField
val DELETE_MONITOR_ACTION_TYPE =
ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse)

@JvmField
val DELETE_WORKFLOW_ACTION_TYPE =
ActionType(DELETE_WORKFLOW_ACTION_NAME, ::DeleteWorkflowResponse)
@JvmField
val GET_FINDINGS_ACTION_TYPE =
ActionType(GET_FINDINGS_ACTION_NAME, ::GetFindingsResponse)

@JvmField
val ACKNOWLEDGE_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)

@JvmField
val SUBSCRIBE_FINDINGS_ACTION_TYPE =
ActionType(SUBSCRIBE_FINDINGS_ACTION_NAME, ::SubscribeFindingsResponse)

@JvmField
val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GetAlertsRequest : ActionRequest {
val monitorId: String?
val alertIndex: String?
val monitorIds: List<String>?
val workflowIds: List<String>?
val alertIds: List<String>?

constructor(
Expand All @@ -23,6 +24,7 @@ class GetAlertsRequest : ActionRequest {
monitorId: String?,
alertIndex: String?,
monitorIds: List<String>? = null,
workflowIds: List<String>? = null,
alertIds: List<String>? = null
) : super() {
this.table = table
Expand All @@ -31,6 +33,7 @@ class GetAlertsRequest : ActionRequest {
this.monitorId = monitorId
this.alertIndex = alertIndex
this.monitorIds = monitorIds
this.workflowIds = workflowIds
this.alertIds = alertIds
}

Expand All @@ -42,6 +45,7 @@ class GetAlertsRequest : ActionRequest {
monitorId = sin.readOptionalString(),
alertIndex = sin.readOptionalString(),
monitorIds = sin.readOptionalStringList(),
workflowIds = sin.readOptionalStringList(),
alertIds = sin.readOptionalStringList()
)

Expand All @@ -57,6 +61,7 @@ class GetAlertsRequest : ActionRequest {
out.writeOptionalString(monitorId)
out.writeOptionalString(alertIndex)
out.writeOptionalStringCollection(monitorIds)
out.writeOptionalStringCollection(workflowIds)
out.writeOptionalStringCollection(alertIds)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.commons.alerting.model.Table
import java.io.IOException

class GetWorkflowAlertsRequest : ActionRequest {
val table: Table
val severityLevel: String
val alertState: String
val alertIndex: String?
val monitorIds: List<String>?
val workflowIds: List<String>?
val alertIds: List<String>?
val getAssociatedAlerts: Boolean

constructor(
table: Table,
severityLevel: String,
alertState: String,
alertIndex: String?,
monitorIds: List<String>? = null,
workflowIds: List<String>? = null,
alertIds: List<String>? = null,
getAssociatedAlerts: Boolean
) : super() {
this.table = table
this.severityLevel = severityLevel
this.alertState = alertState
this.alertIndex = alertIndex
this.monitorIds = monitorIds
this.workflowIds = workflowIds
this.alertIds = alertIds
this.getAssociatedAlerts = getAssociatedAlerts
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
table = Table.readFrom(sin),
severityLevel = sin.readString(),
alertState = sin.readString(),
alertIndex = sin.readOptionalString(),
monitorIds = sin.readOptionalStringList(),
workflowIds = sin.readOptionalStringList(),
alertIds = sin.readOptionalStringList(),
getAssociatedAlerts = sin.readBoolean()
)

override fun validate(): ActionRequestValidationException? {
return null
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
table.writeTo(out)
out.writeString(severityLevel)
out.writeString(alertState)
out.writeOptionalString(alertIndex)
out.writeOptionalStringCollection(monitorIds)
out.writeOptionalStringCollection(workflowIds)
out.writeOptionalStringCollection(alertIds)
out.writeBoolean(getAssociatedAlerts)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.opensearch.commons.alerting.action

import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.commons.alerting.model.Alert
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.core.xcontent.XContentBuilder
import java.io.IOException
import java.util.Collections

class GetWorkflowAlertsResponse : BaseResponse {
val alerts: List<Alert>
val associatedAlerts: List<Alert>
// totalAlerts is not the same as the size of alerts because there can be 30 alerts from the request, but
// the request only asked for 5 alerts, so totalAlerts will be 30, but alerts will only contain 5 alerts
val totalAlerts: Int?

constructor(
alerts: List<Alert>,
associatedAlerts: List<Alert>,
totalAlerts: Int?
) : super() {
this.alerts = alerts
this.associatedAlerts = associatedAlerts
this.totalAlerts = totalAlerts
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
alerts = Collections.unmodifiableList(sin.readList(::Alert)),
associatedAlerts = Collections.unmodifiableList(sin.readList(::Alert)),
totalAlerts = sin.readOptionalInt()
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeCollection(alerts)
out.writeCollection(associatedAlerts)
out.writeOptionalInt(totalAlerts)
}

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
builder.startObject()
.field("alerts", alerts)
.field("associatedAlerts", associatedAlerts)
.field("totalAlerts", totalAlerts)
return builder.endObject()
}
}
Loading

0 comments on commit b555e24

Please sign in to comment.