-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathAlertingPluginInterface.kt
182 lines (173 loc) · 5.91 KB
/
AlertingPluginInterface.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.alerting
import org.opensearch.action.ActionListener
import org.opensearch.action.ActionResponse
import org.opensearch.client.node.NodeClient
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.AlertingActions
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
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.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.action.PublishFindingsRequest
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.commons.utils.recreateObject
/**
* All the transport action plugin interfaces for the Alerting plugin
*/
object AlertingPluginInterface {
/**
* Index monitor interface.
* @param client Node client for making transport action
* @param request The request object
* @param namedWriteableRegistry Registry for building aggregations
* @param listener The listener for getting response
*/
fun indexMonitor(
client: NodeClient,
request: IndexMonitorRequest,
namedWriteableRegistry: NamedWriteableRegistry,
listener: ActionListener<IndexMonitorResponse>
) {
client.execute(
AlertingActions.INDEX_MONITOR_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response, namedWriteableRegistry) {
IndexMonitorResponse(
it
)
}
}
)
}
fun deleteMonitor(
client: NodeClient,
request: DeleteMonitorRequest,
listener: ActionListener<DeleteMonitorResponse>
) {
client.execute(
AlertingActions.DELETE_MONITOR_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
DeleteMonitorResponse(
it
)
}
}
)
}
/**
* Get Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun getAlerts(
client: NodeClient,
request: GetAlertsRequest,
listener: ActionListener<GetAlertsResponse>
) {
client.execute(
AlertingActions.GET_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
GetAlertsResponse(
it
)
}
}
)
}
/**
* Get Findings interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun getFindings(
client: NodeClient,
request: GetFindingsRequest,
listener: ActionListener<GetFindingsResponse>
) {
client.execute(
AlertingActions.GET_FINDINGS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
GetFindingsResponse(
it
)
}
}
)
}
/**
* Acknowledge Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun acknowledgeAlerts(
client: NodeClient,
request: AcknowledgeAlertRequest,
listener: ActionListener<AcknowledgeAlertResponse>
) {
client.execute(
AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
AcknowledgeAlertResponse(
it
)
}
}
)
}
fun publishFinding(
client: NodeClient,
request: PublishFindingsRequest,
listener: ActionListener<SubscribeFindingsResponse>
) {
client.execute(
AlertingActions.SUBSCRIBE_FINDINGS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
SubscribeFindingsResponse(
it
)
}
}
)
}
@Suppress("UNCHECKED_CAST")
private fun <Response : BaseResponse> wrapActionListener(
listener: ActionListener<Response>,
recreate: (Writeable) -> Response
): ActionListener<Response> {
return object : ActionListener<ActionResponse> {
override fun onResponse(response: ActionResponse) {
val recreated = response as? Response ?: recreate(response)
listener.onResponse(recreated)
}
override fun onFailure(exception: java.lang.Exception) {
listener.onFailure(exception)
}
} as ActionListener<Response>
}
}