diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt index 4ba16f48f..bd045f9b0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt @@ -8,7 +8,6 @@ package org.opensearch.alerting import org.opensearch.action.ActionRequest import org.opensearch.action.ActionResponse import org.opensearch.alerting.action.AcknowledgeAlertAction -import org.opensearch.alerting.action.DeleteMonitorAction import org.opensearch.alerting.action.ExecuteMonitorAction import org.opensearch.alerting.action.GetAlertsAction import org.opensearch.alerting.action.GetDestinationsAction @@ -176,7 +175,7 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R ActionPlugin.ActionHandler(GetMonitorAction.INSTANCE, TransportGetMonitorAction::class.java), ActionPlugin.ActionHandler(ExecuteMonitorAction.INSTANCE, TransportExecuteMonitorAction::class.java), ActionPlugin.ActionHandler(SearchMonitorAction.INSTANCE, TransportSearchMonitorAction::class.java), - ActionPlugin.ActionHandler(DeleteMonitorAction.INSTANCE, TransportDeleteMonitorAction::class.java), + ActionPlugin.ActionHandler(AlertingActions.DELETE_MONITOR_ACTION_TYPE, TransportDeleteMonitorAction::class.java), ActionPlugin.ActionHandler(AcknowledgeAlertAction.INSTANCE, TransportAcknowledgeAlertAction::class.java), ActionPlugin.ActionHandler(GetEmailAccountAction.INSTANCE, TransportGetEmailAccountAction::class.java), ActionPlugin.ActionHandler(SearchEmailAccountAction.INSTANCE, TransportSearchEmailAccountAction::class.java), diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorAction.kt deleted file mode 100644 index 402b95f41..000000000 --- a/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorAction.kt +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.action.ActionType -import org.opensearch.action.delete.DeleteResponse - -class DeleteMonitorAction private constructor() : ActionType(NAME, ::DeleteResponse) { - companion object { - val INSTANCE = DeleteMonitorAction() - const val NAME = "cluster:admin/opendistro/alerting/monitor/delete" - } -} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorRequest.kt b/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorRequest.kt deleted file mode 100644 index 6ea08bee2..000000000 --- a/alerting/src/main/kotlin/org/opensearch/alerting/action/DeleteMonitorRequest.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.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) : super() { - 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) - } -} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestDeleteMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestDeleteMonitorAction.kt index 70c81f121..97ea4b903 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestDeleteMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestDeleteMonitorAction.kt @@ -8,11 +8,11 @@ import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger import org.opensearch.action.support.WriteRequest.RefreshPolicy import org.opensearch.alerting.AlertingPlugin -import org.opensearch.alerting.action.DeleteMonitorAction -import org.opensearch.alerting.action.DeleteMonitorRequest import org.opensearch.alerting.model.Alert import org.opensearch.alerting.util.REFRESH import org.opensearch.client.node.NodeClient +import org.opensearch.commons.alerting.action.AlertingActions +import org.opensearch.commons.alerting.action.DeleteMonitorRequest import org.opensearch.rest.BaseRestHandler import org.opensearch.rest.BaseRestHandler.RestChannelConsumer import org.opensearch.rest.RestHandler.ReplacedRoute @@ -60,7 +60,7 @@ class RestDeleteMonitorAction : BaseRestHandler() { val deleteMonitorRequest = DeleteMonitorRequest(monitorId, refreshPolicy) return RestChannelConsumer { channel -> - client.execute(DeleteMonitorAction.INSTANCE, deleteMonitorRequest, RestToXContentListener(channel)) + client.execute(AlertingActions.DELETE_MONITOR_ACTION_TYPE, deleteMonitorRequest, RestToXContentListener(channel)) } } } diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteMonitorAction.kt index 7017e501f..755073b92 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteMonitorAction.kt @@ -8,14 +8,13 @@ package org.opensearch.alerting.transport import org.apache.logging.log4j.LogManager import org.opensearch.OpenSearchStatusException import org.opensearch.action.ActionListener +import org.opensearch.action.ActionRequest import org.opensearch.action.delete.DeleteRequest import org.opensearch.action.delete.DeleteResponse import org.opensearch.action.get.GetRequest import org.opensearch.action.get.GetResponse import org.opensearch.action.support.ActionFilters import org.opensearch.action.support.HandledTransportAction -import org.opensearch.alerting.action.DeleteMonitorAction -import org.opensearch.alerting.action.DeleteMonitorRequest import org.opensearch.alerting.settings.AlertingSettings import org.opensearch.alerting.util.AlertingException import org.opensearch.client.Client @@ -26,9 +25,13 @@ import org.opensearch.common.xcontent.LoggingDeprecationHandler import org.opensearch.common.xcontent.NamedXContentRegistry import org.opensearch.common.xcontent.XContentHelper import org.opensearch.common.xcontent.XContentType +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.model.Monitor import org.opensearch.commons.alerting.model.ScheduledJob import org.opensearch.commons.authuser.User +import org.opensearch.commons.utils.recreateObject import org.opensearch.index.query.QueryBuilders import org.opensearch.index.reindex.BulkByScrollResponse import org.opensearch.index.reindex.DeleteByQueryAction @@ -47,8 +50,8 @@ class TransportDeleteMonitorAction @Inject constructor( val clusterService: ClusterService, settings: Settings, val xContentRegistry: NamedXContentRegistry -) : HandledTransportAction( - DeleteMonitorAction.NAME, transportService, actionFilters, ::DeleteMonitorRequest +) : HandledTransportAction( + AlertingActions.DELETE_MONITOR_ACTION_NAME, transportService, actionFilters, ::DeleteMonitorRequest ), SecureTransportAction { @@ -58,22 +61,24 @@ class TransportDeleteMonitorAction @Inject constructor( listenFilterBySettingChange(clusterService) } - override fun doExecute(task: Task, request: DeleteMonitorRequest, actionListener: ActionListener) { + override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener) { + val transformedRequest = request as? DeleteMonitorRequest + ?: recreateObject(request) { DeleteMonitorRequest(it) } val user = readUserFromThreadContext(client) - val deleteRequest = DeleteRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, request.monitorId) - .setRefreshPolicy(request.refreshPolicy) + val deleteRequest = DeleteRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, transformedRequest.monitorId) + .setRefreshPolicy(transformedRequest.refreshPolicy) if (!validateUserBackendRoles(user, actionListener)) { return } client.threadPool().threadContext.stashContext().use { - DeleteMonitorHandler(client, actionListener, deleteRequest, user, request.monitorId).resolveUserAndStart() + DeleteMonitorHandler(client, actionListener, deleteRequest, user, transformedRequest.monitorId).resolveUserAndStart() } } inner class DeleteMonitorHandler( private val client: Client, - private val actionListener: ActionListener, + private val actionListener: ActionListener, private val deleteRequest: DeleteRequest, private val user: User?, private val monitorId: String @@ -142,7 +147,7 @@ class TransportDeleteMonitorAction @Inject constructor( } deleteMetadata() - actionListener.onResponse(response) + actionListener.onResponse(DeleteMonitorResponse(response.id, response.version)) } override fun onFailure(t: Exception) { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportIndexMonitorAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportIndexMonitorAction.kt index bfcad6d5a..6e8de68a2 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportIndexMonitorAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportIndexMonitorAction.kt @@ -109,20 +109,20 @@ class TransportIndexMonitorAction @Inject constructor( listenFilterBySettingChange(clusterService) } - override fun doExecute(task: Task, req: ActionRequest, actionListener: ActionListener) { - val request = req as? IndexMonitorRequest - ?: recreateObject(req) { IndexMonitorRequest(it) } + override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener) { + val transformedRequest = request as? IndexMonitorRequest + ?: recreateObject(request) { IndexMonitorRequest(it) } val user = readUserFromThreadContext(client) if (!validateUserBackendRoles(user, actionListener)) { return } - if (!isADMonitor(request.monitor)) { - checkIndicesAndExecute(client, actionListener, request, user) + if (!isADMonitor(transformedRequest.monitor)) { + checkIndicesAndExecute(client, actionListener, transformedRequest, user) } else { // check if user has access to any anomaly detector for AD monitor - checkAnomalyDetectorAndExecute(client, actionListener, request, user) + checkAnomalyDetectorAndExecute(client, actionListener, transformedRequest, user) } } diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorActionTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorActionTests.kt deleted file mode 100644 index 8db7761cd..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorActionTests.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.junit.Assert -import org.opensearch.test.OpenSearchTestCase - -class DeleteMonitorActionTests : OpenSearchTestCase() { - - fun `test delete monitor action name`() { - Assert.assertNotNull(DeleteMonitorAction.INSTANCE.name()) - Assert.assertEquals(DeleteMonitorAction.INSTANCE.name(), DeleteMonitorAction.NAME) - } -} diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorRequestTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorRequestTests.kt deleted file mode 100644 index e66940460..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/DeleteMonitorRequestTests.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.junit.Assert -import org.opensearch.action.support.WriteRequest -import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.test.OpenSearchTestCase - -class DeleteMonitorRequestTests : OpenSearchTestCase() { - - 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) - } -}