Skip to content

Commit b555e24

Browse files
authored
apis for get workflow alerts and acknowledge chained alerts (#472)
Signed-off-by: Surya Sashank Nistala <[email protected]>
1 parent 37e36b7 commit b555e24

17 files changed

+766
-129
lines changed

src/main/kotlin/org/opensearch/commons/alerting/AlertingPluginInterface.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.opensearch.common.io.stream.NamedWriteableRegistry
1111
import org.opensearch.common.io.stream.Writeable
1212
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
1313
import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse
14+
import org.opensearch.commons.alerting.action.AcknowledgeChainedAlertRequest
1415
import org.opensearch.commons.alerting.action.AlertingActions
1516
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
1617
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
@@ -20,6 +21,8 @@ import org.opensearch.commons.alerting.action.GetAlertsRequest
2021
import org.opensearch.commons.alerting.action.GetAlertsResponse
2122
import org.opensearch.commons.alerting.action.GetFindingsRequest
2223
import org.opensearch.commons.alerting.action.GetFindingsResponse
24+
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
25+
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
2326
import org.opensearch.commons.alerting.action.GetWorkflowRequest
2427
import org.opensearch.commons.alerting.action.GetWorkflowResponse
2528
import org.opensearch.commons.alerting.action.IndexMonitorRequest
@@ -147,6 +150,30 @@ object AlertingPluginInterface {
147150
)
148151
}
149152

153+
/**
154+
* Get Workflow Alerts interface.
155+
* @param client Node client for making transport action
156+
* @param request The request object
157+
* @param listener The listener for getting response
158+
*/
159+
fun getWorkflowAlerts(
160+
client: NodeClient,
161+
request: GetWorkflowAlertsRequest,
162+
listener: ActionListener<GetWorkflowAlertsResponse>
163+
) {
164+
client.execute(
165+
AlertingActions.GET_WORKFLOW_ALERTS_ACTION_TYPE,
166+
request,
167+
wrapActionListener(listener) { response ->
168+
recreateObject(response) {
169+
GetWorkflowAlertsResponse(
170+
it
171+
)
172+
}
173+
}
174+
)
175+
}
176+
150177
/**
151178
* Get Workflow interface.
152179
* @param client Node client for making transport action
@@ -237,6 +264,30 @@ object AlertingPluginInterface {
237264
)
238265
}
239266

267+
/**
268+
* Acknowledge Chained Alerts interface.
269+
* @param client Node client for making transport action
270+
* @param request The request object
271+
* @param listener The listener for getting response
272+
*/
273+
fun acknowledgeChainedAlerts(
274+
client: NodeClient,
275+
request: AcknowledgeChainedAlertRequest,
276+
listener: ActionListener<AcknowledgeAlertResponse>
277+
) {
278+
client.execute(
279+
AlertingActions.ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE,
280+
request,
281+
wrapActionListener(listener) { response ->
282+
recreateObject(response) {
283+
AcknowledgeAlertResponse(
284+
it
285+
)
286+
}
287+
}
288+
)
289+
}
290+
240291
@Suppress("UNCHECKED_CAST")
241292
private fun <Response : BaseResponse> wrapActionListener(
242293
listener: ActionListener<Response>,

src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,54 @@ object AlertingActions {
1010
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write"
1111
const val INDEX_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/write"
1212
const val GET_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/get"
13+
const val GET_WORKFLOW_ALERTS_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow_alerts/get"
1314
const val GET_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/get"
1415
const val DELETE_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/delete"
1516
const val DELETE_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/delete"
1617
const val GET_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/get"
1718
const val ACKNOWLEDGE_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/ack"
19+
const val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/chained_alerts/ack"
1820
const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe"
1921

2022
@JvmField
2123
val INDEX_MONITOR_ACTION_TYPE =
2224
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)
25+
2326
@JvmField
2427
val INDEX_WORKFLOW_ACTION_TYPE =
2528
ActionType(INDEX_WORKFLOW_ACTION_NAME, ::IndexWorkflowResponse)
2629
@JvmField
2730
val GET_ALERTS_ACTION_TYPE =
2831
ActionType(GET_ALERTS_ACTION_NAME, ::GetAlertsResponse)
32+
33+
@JvmField
34+
val GET_WORKFLOW_ALERTS_ACTION_TYPE =
35+
ActionType(GET_WORKFLOW_ALERTS_ACTION_NAME, ::GetWorkflowAlertsResponse)
36+
2937
@JvmField
3038
val GET_WORKFLOW_ACTION_TYPE =
3139
ActionType(GET_WORKFLOW_ACTION_NAME, ::GetWorkflowResponse)
3240

3341
@JvmField
3442
val DELETE_MONITOR_ACTION_TYPE =
3543
ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse)
44+
3645
@JvmField
3746
val DELETE_WORKFLOW_ACTION_TYPE =
3847
ActionType(DELETE_WORKFLOW_ACTION_NAME, ::DeleteWorkflowResponse)
3948
@JvmField
4049
val GET_FINDINGS_ACTION_TYPE =
4150
ActionType(GET_FINDINGS_ACTION_NAME, ::GetFindingsResponse)
51+
4252
@JvmField
4353
val ACKNOWLEDGE_ALERTS_ACTION_TYPE =
4454
ActionType(ACKNOWLEDGE_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)
55+
4556
@JvmField
4657
val SUBSCRIBE_FINDINGS_ACTION_TYPE =
4758
ActionType(SUBSCRIBE_FINDINGS_ACTION_NAME, ::SubscribeFindingsResponse)
59+
60+
@JvmField
61+
val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE =
62+
ActionType(ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)
4863
}

src/main/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class GetAlertsRequest : ActionRequest {
1414
val monitorId: String?
1515
val alertIndex: String?
1616
val monitorIds: List<String>?
17+
val workflowIds: List<String>?
1718
val alertIds: List<String>?
1819

1920
constructor(
@@ -23,6 +24,7 @@ class GetAlertsRequest : ActionRequest {
2324
monitorId: String?,
2425
alertIndex: String?,
2526
monitorIds: List<String>? = null,
27+
workflowIds: List<String>? = null,
2628
alertIds: List<String>? = null
2729
) : super() {
2830
this.table = table
@@ -31,6 +33,7 @@ class GetAlertsRequest : ActionRequest {
3133
this.monitorId = monitorId
3234
this.alertIndex = alertIndex
3335
this.monitorIds = monitorIds
36+
this.workflowIds = workflowIds
3437
this.alertIds = alertIds
3538
}
3639

@@ -42,6 +45,7 @@ class GetAlertsRequest : ActionRequest {
4245
monitorId = sin.readOptionalString(),
4346
alertIndex = sin.readOptionalString(),
4447
monitorIds = sin.readOptionalStringList(),
48+
workflowIds = sin.readOptionalStringList(),
4549
alertIds = sin.readOptionalStringList()
4650
)
4751

@@ -57,6 +61,7 @@ class GetAlertsRequest : ActionRequest {
5761
out.writeOptionalString(monitorId)
5862
out.writeOptionalString(alertIndex)
5963
out.writeOptionalStringCollection(monitorIds)
64+
out.writeOptionalStringCollection(workflowIds)
6065
out.writeOptionalStringCollection(alertIds)
6166
}
6267
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.opensearch.commons.alerting.action
2+
3+
import org.opensearch.action.ActionRequest
4+
import org.opensearch.action.ActionRequestValidationException
5+
import org.opensearch.common.io.stream.StreamInput
6+
import org.opensearch.common.io.stream.StreamOutput
7+
import org.opensearch.commons.alerting.model.Table
8+
import java.io.IOException
9+
10+
class GetWorkflowAlertsRequest : ActionRequest {
11+
val table: Table
12+
val severityLevel: String
13+
val alertState: String
14+
val alertIndex: String?
15+
val monitorIds: List<String>?
16+
val workflowIds: List<String>?
17+
val alertIds: List<String>?
18+
val getAssociatedAlerts: Boolean
19+
20+
constructor(
21+
table: Table,
22+
severityLevel: String,
23+
alertState: String,
24+
alertIndex: String?,
25+
monitorIds: List<String>? = null,
26+
workflowIds: List<String>? = null,
27+
alertIds: List<String>? = null,
28+
getAssociatedAlerts: Boolean
29+
) : super() {
30+
this.table = table
31+
this.severityLevel = severityLevel
32+
this.alertState = alertState
33+
this.alertIndex = alertIndex
34+
this.monitorIds = monitorIds
35+
this.workflowIds = workflowIds
36+
this.alertIds = alertIds
37+
this.getAssociatedAlerts = getAssociatedAlerts
38+
}
39+
40+
@Throws(IOException::class)
41+
constructor(sin: StreamInput) : this(
42+
table = Table.readFrom(sin),
43+
severityLevel = sin.readString(),
44+
alertState = sin.readString(),
45+
alertIndex = sin.readOptionalString(),
46+
monitorIds = sin.readOptionalStringList(),
47+
workflowIds = sin.readOptionalStringList(),
48+
alertIds = sin.readOptionalStringList(),
49+
getAssociatedAlerts = sin.readBoolean()
50+
)
51+
52+
override fun validate(): ActionRequestValidationException? {
53+
return null
54+
}
55+
56+
@Throws(IOException::class)
57+
override fun writeTo(out: StreamOutput) {
58+
table.writeTo(out)
59+
out.writeString(severityLevel)
60+
out.writeString(alertState)
61+
out.writeOptionalString(alertIndex)
62+
out.writeOptionalStringCollection(monitorIds)
63+
out.writeOptionalStringCollection(workflowIds)
64+
out.writeOptionalStringCollection(alertIds)
65+
out.writeBoolean(getAssociatedAlerts)
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.opensearch.commons.alerting.action
2+
3+
import org.opensearch.common.io.stream.StreamInput
4+
import org.opensearch.common.io.stream.StreamOutput
5+
import org.opensearch.commons.alerting.model.Alert
6+
import org.opensearch.commons.notifications.action.BaseResponse
7+
import org.opensearch.core.xcontent.ToXContent
8+
import org.opensearch.core.xcontent.XContentBuilder
9+
import java.io.IOException
10+
import java.util.Collections
11+
12+
class GetWorkflowAlertsResponse : BaseResponse {
13+
val alerts: List<Alert>
14+
val associatedAlerts: List<Alert>
15+
// totalAlerts is not the same as the size of alerts because there can be 30 alerts from the request, but
16+
// the request only asked for 5 alerts, so totalAlerts will be 30, but alerts will only contain 5 alerts
17+
val totalAlerts: Int?
18+
19+
constructor(
20+
alerts: List<Alert>,
21+
associatedAlerts: List<Alert>,
22+
totalAlerts: Int?
23+
) : super() {
24+
this.alerts = alerts
25+
this.associatedAlerts = associatedAlerts
26+
this.totalAlerts = totalAlerts
27+
}
28+
29+
@Throws(IOException::class)
30+
constructor(sin: StreamInput) : this(
31+
alerts = Collections.unmodifiableList(sin.readList(::Alert)),
32+
associatedAlerts = Collections.unmodifiableList(sin.readList(::Alert)),
33+
totalAlerts = sin.readOptionalInt()
34+
)
35+
36+
@Throws(IOException::class)
37+
override fun writeTo(out: StreamOutput) {
38+
out.writeCollection(alerts)
39+
out.writeCollection(associatedAlerts)
40+
out.writeOptionalInt(totalAlerts)
41+
}
42+
43+
@Throws(IOException::class)
44+
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
45+
builder.startObject()
46+
.field("alerts", alerts)
47+
.field("associatedAlerts", associatedAlerts)
48+
.field("totalAlerts", totalAlerts)
49+
return builder.endObject()
50+
}
51+
}

0 commit comments

Comments
 (0)