Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose delete monitor api from alerting #251

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ 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.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.notifications.action.BaseResponse
Expand Down Expand Up @@ -43,6 +45,24 @@ object AlertingPluginInterface {
)
}

fun deleteMonitor(
client: NodeClient,
request: DeleteMonitorRequest,
listener: ActionListener<DeleteMonitorResponse>
) {
client.execute(
AlertingActions.DELETE_MONITOR_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
DeleteMonitorResponse(
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 @@ -11,4 +11,9 @@ object AlertingActions {

val INDEX_MONITOR_ACTION_TYPE =
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)

const val DELETE_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/delete"

val DELETE_MONITOR_ACTION_TYPE =
ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 java.io.IOException

class DeleteMonitorRequest : ActionRequest {

val monitorId: String
val refreshPolicy: WriteRequest.RefreshPolicy

constructor(monitorId: String, refreshPolicy: WriteRequest.RefreshPolicy) : super() {
this.monitorId = monitorId
this.refreshPolicy = refreshPolicy
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
monitorId = sin.readString(),
refreshPolicy = WriteRequest.RefreshPolicy.readFrom(sin)
)

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

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(monitorId)
refreshPolicy.writeTo(out)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.util.IndexUtils
import org.opensearch.commons.notifications.action.BaseResponse

class DeleteMonitorResponse : BaseResponse {
var id: String
var version: Long

constructor(
id: String,
version: Long
) : super() {
this.id = id
this.version = version
}

constructor(sin: StreamInput) : this(
sin.readString(), // id
sin.readLong() // version
)

override fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeLong(version)
}

override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
return builder.startObject()
.field(IndexUtils._ID, id)
.field(IndexUtils._VERSION, version)
.endObject()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.opensearch.action.ActionListener
import org.opensearch.action.ActionType
import org.opensearch.client.node.NodeClient
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.model.Monitor
Expand Down Expand Up @@ -41,4 +43,20 @@ internal class AlertingPluginInterfaceTests {
AlertingPluginInterface.indexMonitor(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}

@Test
fun deleteMonitor() {
val request = mock(DeleteMonitorRequest::class.java)
val response = DeleteMonitorResponse(Monitor.NO_ID, Monitor.NO_VERSION)
val listener: ActionListener<DeleteMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<DeleteMonitorResponse>

Mockito.doAnswer {
(it.getArgument(2) as ActionListener<DeleteMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())

AlertingPluginInterface.deleteMonitor(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.opensearch.commons.alerting.action

import org.junit.Assert
import org.junit.Test
import org.opensearch.action.support.WriteRequest
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.common.io.stream.StreamInput

class DeleteMonitorRequestTests {

@Test
fun `test delete monitor request`() {

val req = DeleteMonitorRequest("1234", WriteRequest.RefreshPolicy.IMMEDIATE)
Assert.assertNotNull(req)
Assert.assertEquals("1234", req.monitorId)
Assert.assertEquals("true", req.refreshPolicy.value)

val out = BytesStreamOutput()
req.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = DeleteMonitorRequest(sin)
Assert.assertEquals("1234", newReq.monitorId)
Assert.assertEquals("true", newReq.refreshPolicy.value)
}
}