-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Alerting data models over to common-utils (#227)
Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
- Loading branch information
Showing
40 changed files
with
5,496 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/org/opensearch/commons/alerting/AlertingPluginInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.Writeable | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.IndexMonitorRequest | ||
import org.opensearch.commons.alerting.action.IndexMonitorResponse | ||
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 listener The listener for getting response | ||
*/ | ||
fun indexMonitor( | ||
client: NodeClient, | ||
request: IndexMonitorRequest, | ||
listener: ActionListener<IndexMonitorResponse> | ||
) { | ||
client.execute( | ||
AlertingActions.INDEX_MONITOR_ACTION_TYPE, | ||
request, | ||
wrapActionListener(listener) { response -> | ||
recreateObject(response) { | ||
IndexMonitorResponse( | ||
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> | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.action.ActionType | ||
|
||
object AlertingActions { | ||
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write" | ||
|
||
val INDEX_MONITOR_ACTION_TYPE = | ||
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse) | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.commons.alerting.model.Monitor | ||
import org.opensearch.rest.RestRequest | ||
import java.io.IOException | ||
|
||
class IndexMonitorRequest : ActionRequest { | ||
val monitorId: String | ||
val seqNo: Long | ||
val primaryTerm: Long | ||
val refreshPolicy: WriteRequest.RefreshPolicy | ||
val method: RestRequest.Method | ||
var monitor: Monitor | ||
|
||
constructor( | ||
monitorId: String, | ||
seqNo: Long, | ||
primaryTerm: Long, | ||
refreshPolicy: WriteRequest.RefreshPolicy, | ||
method: RestRequest.Method, | ||
monitor: Monitor | ||
) : super() { | ||
this.monitorId = monitorId | ||
this.seqNo = seqNo | ||
this.primaryTerm = primaryTerm | ||
this.refreshPolicy = refreshPolicy | ||
this.method = method | ||
this.monitor = monitor | ||
} | ||
|
||
@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), | ||
monitor = Monitor.readFrom(sin) as Monitor | ||
) | ||
|
||
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) | ||
monitor.writeTo(out) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/org/opensearch/commons/alerting/action/IndexMonitorResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.opensearch.commons.alerting.action | ||
|
||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
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 java.io.IOException | ||
|
||
class IndexMonitorResponse : 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 | ||
} | ||
|
||
@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: ToXContent.Params): XContentBuilder { | ||
return builder.startObject() | ||
.field(_ID, id) | ||
.field(_VERSION, version) | ||
.field(_SEQ_NO, seqNo) | ||
.field(_PRIMARY_TERM, primaryTerm) | ||
.field("monitor", monitor) | ||
.endObject() | ||
} | ||
} |
Oops, something went wrong.