Skip to content

Commit

Permalink
Move get monitor and search monitor action / request / responses to c…
Browse files Browse the repository at this point in the history
…ommon-utils (#566)

* Add get monitor request/response

Signed-off-by: Tyler Ohlsen <[email protected]>

* Remove status from response; add to interface

Signed-off-by: Tyler Ohlsen <[email protected]>

* Add UT

Signed-off-by: Tyler Ohlsen <[email protected]>

* Repeat for search monitor action

Signed-off-by: Tyler Ohlsen <[email protected]>

---------

Signed-off-by: Tyler Ohlsen <[email protected]>
(cherry picked from commit 2ff995b)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Nov 22, 2023
1 parent a56599c commit 6a0231a
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.opensearch.commons.alerting

import org.opensearch.action.search.SearchResponse
import org.opensearch.client.node.NodeClient
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse
Expand All @@ -17,6 +18,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.GetMonitorRequest
import org.opensearch.commons.alerting.action.GetMonitorResponse
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
import org.opensearch.commons.alerting.action.GetWorkflowRequest
Expand All @@ -26,6 +29,7 @@ import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.action.IndexWorkflowRequest
import org.opensearch.commons.alerting.action.IndexWorkflowResponse
import org.opensearch.commons.alerting.action.PublishFindingsRequest
import org.opensearch.commons.alerting.action.SearchMonitorRequest
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.commons.utils.recreateObject
Expand Down Expand Up @@ -288,6 +292,51 @@ object AlertingPluginInterface {
)
}

/**
* Get Monitor interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun getMonitor(
client: NodeClient,
request: GetMonitorRequest,
listener: ActionListener<GetMonitorResponse>
) {
client.execute(
AlertingActions.GET_MONITOR_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
GetMonitorResponse(
it

Check warning on line 312 in src/main/kotlin/org/opensearch/commons/alerting/AlertingPluginInterface.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/AlertingPluginInterface.kt#L310-L312

Added lines #L310 - L312 were not covered by tests
)
}
}
)
}

/**
* Search Monitors interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun searchMonitors(
client: NodeClient,
request: SearchMonitorRequest,
listener: ActionListener<SearchResponse>
) {
client.execute(
AlertingActions.SEARCH_MONITORS_ACTION_TYPE,
request,
// we do not use the wrapActionListener in this case since there is no need
// to recreate any object or specially handle onResponse / onFailure. It is
// simply returning a SearchResponse.
listener
)
}

@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 @@ -5,6 +5,7 @@
package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionType
import org.opensearch.action.search.SearchResponse

object AlertingActions {
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write"
Expand All @@ -18,6 +19,8 @@ object AlertingActions {
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"
const val GET_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/get"
const val SEARCH_MONITORS_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/search"

@JvmField
val INDEX_MONITOR_ACTION_TYPE =
Expand Down Expand Up @@ -60,4 +63,12 @@ object AlertingActions {
@JvmField
val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)

@JvmField
val GET_MONITOR_ACTION_TYPE =
ActionType(GET_MONITOR_ACTION_NAME, ::GetMonitorResponse)

@JvmField
val SEARCH_MONITORS_ACTION_TYPE =
ActionType(SEARCH_MONITORS_ACTION_NAME, ::SearchResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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 org.opensearch.search.fetch.subphase.FetchSourceContext
import java.io.IOException

class GetMonitorRequest : ActionRequest {
val monitorId: String
val version: Long
val method: RestRequest.Method
val srcContext: FetchSourceContext?

Check warning on line 20 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L17-L20

Added lines #L17 - L20 were not covered by tests

constructor(
monitorId: String,
version: Long,
method: RestRequest.Method,
srcContext: FetchSourceContext?
) : super() {
this.monitorId = monitorId
this.version = version
this.method = method
this.srcContext = srcContext

Check warning on line 31 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L27-L31

Added lines #L27 - L31 were not covered by tests
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
sin.readString(), // monitorId
sin.readLong(), // version
sin.readEnum(RestRequest.Method::class.java), // method

Check warning on line 38 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L35-L38

Added lines #L35 - L38 were not covered by tests
if (sin.readBoolean()) {
FetchSourceContext(sin) // srcContext
} else null
)

Check warning on line 42 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L40-L42

Added lines #L40 - L42 were not covered by tests

override fun validate(): ActionRequestValidationException? {
return null

Check warning on line 45 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L45

Added line #L45 was not covered by tests
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(monitorId)
out.writeLong(version)
out.writeEnum(method)

Check warning on line 52 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorRequest.kt#L50-L52

Added lines #L50 - L52 were not covered by tests
out.writeBoolean(srcContext != null)
srcContext?.writeTo(out)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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
import org.opensearch.core.xcontent.ToXContentFragment
import org.opensearch.core.xcontent.XContentBuilder
import java.io.IOException

class GetMonitorResponse : BaseResponse {
var id: String
var version: Long
var seqNo: Long
var primaryTerm: Long
var monitor: Monitor?
var associatedWorkflows: List<AssociatedWorkflow>?

Check warning on line 27 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L22-L27

Added lines #L22 - L27 were not covered by tests

constructor(
id: String,
version: Long,
seqNo: Long,
primaryTerm: Long,
monitor: Monitor?,
associatedCompositeMonitors: List<AssociatedWorkflow>?,
) : super() {
this.id = id
this.version = version
this.seqNo = seqNo
this.primaryTerm = primaryTerm
this.monitor = monitor
this.associatedWorkflows = associatedCompositeMonitors ?: emptyList()
}

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

Check warning on line 50 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L46-L50

Added lines #L46 - L50 were not covered by tests
monitor = if (sin.readBoolean()) {
Monitor.readFrom(sin) // monitor
} else null,
associatedCompositeMonitors = sin.readList((AssociatedWorkflow)::readFrom),
)

Check warning on line 55 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L52-L55

Added lines #L52 - L55 were not covered by tests

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

Check warning on line 62 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L59-L62

Added lines #L59 - L62 were not covered by tests
if (monitor != null) {
out.writeBoolean(true)

Check warning on line 64 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L64

Added line #L64 was not covered by tests
monitor?.writeTo(out)
} else {
out.writeBoolean(false)

Check warning on line 67 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L67

Added line #L67 was not covered by tests
}
associatedWorkflows?.forEach {
it.writeTo(out)

Check warning on line 70 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L70

Added line #L70 was not covered by tests
}
}

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
builder.startObject()
.field(_ID, id)
.field(_VERSION, version)
.field(_SEQ_NO, seqNo)
.field(_PRIMARY_TERM, primaryTerm)

Check warning on line 80 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L76-L80

Added lines #L76 - L80 were not covered by tests
if (monitor != null) {
builder.field("monitor", monitor)

Check warning on line 82 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L82

Added line #L82 was not covered by tests
}
if (associatedWorkflows != null) {
builder.field("associated_workflows", associatedWorkflows!!.toTypedArray())

Check warning on line 85 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L85

Added line #L85 was not covered by tests
}
return builder.endObject()

Check warning on line 87 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L87

Added line #L87 was not covered by tests
}

class AssociatedWorkflow : ToXContentFragment {
val id: String
val name: String

Check warning on line 92 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L91-L92

Added lines #L91 - L92 were not covered by tests

constructor(id: String, name: String) {
this.id = id
this.name = name

Check warning on line 96 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L94-L96

Added lines #L94 - L96 were not covered by tests
}

override fun toXContent(builder: XContentBuilder, params: ToXContent.Params?): XContentBuilder {
builder.startObject()
.field("id", id)
.field("name", name)
.endObject()
return builder

Check warning on line 104 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L100-L104

Added lines #L100 - L104 were not covered by tests
}

fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeString(name)

Check warning on line 109 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L108-L109

Added lines #L108 - L109 were not covered by tests
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
sin.readString(),
sin.readString()
)

Check warning on line 116 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L113-L116

Added lines #L113 - L116 were not covered by tests

companion object {
@JvmStatic
@Throws(IOException::class)
fun readFrom(sin: StreamInput): AssociatedWorkflow {
return AssociatedWorkflow(sin)

Check warning on line 122 in src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/GetMonitorResponse.kt#L122

Added line #L122 was not covered by tests
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.action.search.SearchRequest
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.core.common.io.stream.StreamOutput
import java.io.IOException

class SearchMonitorRequest : ActionRequest {

val searchRequest: SearchRequest

Check warning on line 17 in src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt#L17

Added line #L17 was not covered by tests

constructor(
searchRequest: SearchRequest
) : super() {
this.searchRequest = searchRequest

Check warning on line 22 in src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt#L21-L22

Added lines #L21 - L22 were not covered by tests
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
searchRequest = SearchRequest(sin)
)

Check warning on line 28 in src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt#L26-L28

Added lines #L26 - L28 were not covered by tests

override fun validate(): ActionRequestValidationException? {
return null

Check warning on line 31 in src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt#L31

Added line #L31 was not covered by tests
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
searchRequest.writeTo(out)

Check warning on line 36 in src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/action/SearchMonitorRequest.kt#L36

Added line #L36 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.junit.jupiter.MockitoExtension
import org.opensearch.action.ActionType
import org.opensearch.action.search.SearchResponse
import org.opensearch.client.node.NodeClient
import org.opensearch.common.settings.Settings
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
Expand All @@ -23,13 +24,16 @@ 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.GetMonitorRequest
import org.opensearch.commons.alerting.action.GetMonitorResponse
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.action.IndexWorkflowRequest
import org.opensearch.commons.alerting.action.IndexWorkflowResponse
import org.opensearch.commons.alerting.action.PublishFindingsRequest
import org.opensearch.commons.alerting.action.SearchMonitorRequest
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
import org.opensearch.commons.alerting.model.FindingDocument
import org.opensearch.commons.alerting.model.FindingWithDocs
Expand Down Expand Up @@ -250,4 +254,32 @@ internal class AlertingPluginInterfaceTests {
AlertingPluginInterface.acknowledgeChainedAlerts(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}

@Test
fun getMonitor() {
val request = mock(GetMonitorRequest::class.java)
val response = GetMonitorResponse("test-id", 1, 1, 1, null, null)
val listener: ActionListener<GetMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<GetMonitorResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getMonitor(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}

@Test
fun searchMonitors() {
val request = mock(SearchMonitorRequest::class.java)
val response = mock(SearchResponse::class.java)
val listener: ActionListener<SearchResponse> =
mock(ActionListener::class.java) as ActionListener<SearchResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<SearchResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.searchMonitors(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
}
Loading

0 comments on commit 6a0231a

Please sign in to comment.