diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt index a759b40e3..b90c8b1e3 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt @@ -51,7 +51,7 @@ import org.opensearch.alerting.resthandler.RestSearchAlertingCommentAction import org.opensearch.alerting.resthandler.RestSearchEmailAccountAction import org.opensearch.alerting.resthandler.RestSearchEmailGroupAction import org.opensearch.alerting.resthandler.RestSearchMonitorAction -import org.opensearch.alerting.resthandler.RestUpdateMonitorStateAction +import org.opensearch.alerting.resthandler.RestToggleMonitorAction import org.opensearch.alerting.script.TriggerScript import org.opensearch.alerting.service.DeleteMonitorService import org.opensearch.alerting.settings.AlertingSettings @@ -84,7 +84,7 @@ import org.opensearch.alerting.transport.TransportSearchAlertingCommentAction import org.opensearch.alerting.transport.TransportSearchEmailAccountAction import org.opensearch.alerting.transport.TransportSearchEmailGroupAction import org.opensearch.alerting.transport.TransportSearchMonitorAction -import org.opensearch.alerting.transport.TransportUpdateMonitorStateAction +import org.opensearch.alerting.transport.TransportToggleMonitorAction import org.opensearch.alerting.util.DocLevelMonitorQueries import org.opensearch.alerting.util.destinationmigration.DestinationMigrationCoordinator import org.opensearch.client.Client @@ -219,7 +219,7 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R RestIndexAlertingCommentAction(), RestSearchAlertingCommentAction(), RestDeleteAlertingCommentAction(), - RestUpdateMonitorStateAction(), + RestToggleMonitorAction(), ) } @@ -252,7 +252,7 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R ActionPlugin.ActionHandler(ExecuteWorkflowAction.INSTANCE, TransportExecuteWorkflowAction::class.java), ActionPlugin.ActionHandler(GetRemoteIndexesAction.INSTANCE, TransportGetRemoteIndexesAction::class.java), ActionPlugin.ActionHandler(DocLevelMonitorFanOutAction.INSTANCE, TransportDocLevelMonitorFanOutAction::class.java), - ActionPlugin.ActionHandler(AlertingActions.UPDATE_MONITOR_STATE_ACTION_TYPE, TransportUpdateMonitorStateAction::class.java) + ActionPlugin.ActionHandler(AlertingActions.TOGGLE_MONITOR_ACTION_TYPE, TransportToggleMonitorAction::class.java) ) } diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestUpdateMonitorStateAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestToggleMonitorAction.kt similarity index 77% rename from alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestUpdateMonitorStateAction.kt rename to alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestToggleMonitorAction.kt index 4e8f188fc..318f1ae3d 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestUpdateMonitorStateAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestToggleMonitorAction.kt @@ -9,9 +9,9 @@ import org.apache.logging.log4j.LogManager import org.opensearch.alerting.AlertingPlugin import org.opensearch.client.node.NodeClient import org.opensearch.common.xcontent.XContentType -import org.opensearch.commons.alerting.action.AlertingActions.UPDATE_MONITOR_STATE_ACTION_TYPE -import org.opensearch.commons.alerting.action.UpdateMonitorStateRequest -import org.opensearch.commons.alerting.action.UpdateMonitorStateResponse +import org.opensearch.commons.alerting.action.AlertingActions.TOGGLE_MONITOR_ACTION_TYPE +import org.opensearch.commons.alerting.action.ToggleMonitorRequest +import org.opensearch.commons.alerting.action.ToggleMonitorResponse import org.opensearch.commons.alerting.model.Monitor import org.opensearch.commons.alerting.util.AlertingException import org.opensearch.core.action.ActionListener @@ -27,12 +27,11 @@ import org.opensearch.rest.RestRequest import org.opensearch.rest.RestRequest.Method.PUT import java.io.IOException -private val log = LogManager.getLogger(RestUpdateMonitorStateAction::class.java) +private val log = LogManager.getLogger(RestToggleMonitorAction::class.java) -// Rest handler to enable and disable monitors. -class RestUpdateMonitorStateAction : BaseRestHandler() { +class RestToggleMonitorAction : BaseRestHandler() { override fun getName(): String { - return "update_monitor_state_action" + return "toggle_monitor_action" } override fun routes(): List { @@ -69,19 +68,19 @@ class RestUpdateMonitorStateAction : BaseRestHandler() { log.debug("{} {}/{}/{}", request.method(), AlertingPlugin.MONITOR_BASE_URI, monitorId, if (enabled) "enable" else "disable") return RestChannelConsumer { channel -> - val updateMonitorStateRequest = UpdateMonitorStateRequest( + val toggleMonitorRequest = ToggleMonitorRequest( monitorId = monitorId, enabled = enabled, - seqNo = -1, // This will be handled in transport layer - primaryTerm = -1, // This will be handled in transport layer + seqNo = -1, // Updated in the transport layer + primaryTerm = -1, // Updated in the transport layer method = request.method() ) client.execute( - UPDATE_MONITOR_STATE_ACTION_TYPE, - updateMonitorStateRequest, - object : ActionListener { - override fun onResponse(response: UpdateMonitorStateResponse) { + TOGGLE_MONITOR_ACTION_TYPE, + toggleMonitorRequest, + object : ActionListener { + override fun onResponse(response: ToggleMonitorResponse) { channel.sendResponse( BytesRestResponse( RestStatus.OK, diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportUpdateMonitorStateAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportToggleMonitorAction.kt similarity index 85% rename from alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportUpdateMonitorStateAction.kt rename to alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportToggleMonitorAction.kt index ea7da5117..652943df5 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportUpdateMonitorStateAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportToggleMonitorAction.kt @@ -5,7 +5,6 @@ package org.opensearch.alerting.transport -import org.apache.logging.log4j.LogManager import org.opensearch.OpenSearchStatusException import org.opensearch.action.support.ActionFilters import org.opensearch.action.support.HandledTransportAction @@ -19,8 +18,8 @@ import org.opensearch.commons.alerting.action.GetMonitorRequest import org.opensearch.commons.alerting.action.GetMonitorResponse import org.opensearch.commons.alerting.action.IndexMonitorRequest import org.opensearch.commons.alerting.action.IndexMonitorResponse -import org.opensearch.commons.alerting.action.UpdateMonitorStateRequest -import org.opensearch.commons.alerting.action.UpdateMonitorStateResponse +import org.opensearch.commons.alerting.action.ToggleMonitorRequest +import org.opensearch.commons.alerting.action.ToggleMonitorResponse import org.opensearch.core.action.ActionListener import org.opensearch.core.common.io.stream.NamedWriteableRegistry import org.opensearch.core.rest.RestStatus @@ -28,23 +27,22 @@ import org.opensearch.tasks.Task import org.opensearch.transport.TransportService import java.time.Instant -class TransportUpdateMonitorStateAction @Inject constructor( +class TransportToggleMonitorAction @Inject constructor( transportService: TransportService, val client: NodeClient, val namedWriteableRegistry: NamedWriteableRegistry, actionFilters: ActionFilters -) : HandledTransportAction( - AlertingActions.UPDATE_MONITOR_STATE_ACTION_NAME, +) : HandledTransportAction( + AlertingActions.TOGGLE_MONITOR_ACTION_NAME, transportService, actionFilters, - ::UpdateMonitorStateRequest + ::ToggleMonitorRequest ) { - private val log = LogManager.getLogger(javaClass) override fun doExecute( task: Task, - request: UpdateMonitorStateRequest, - actionListener: ActionListener + request: ToggleMonitorRequest, + actionListener: ActionListener ) { val monitorId = request.monitorId val enabled = request.enabled @@ -79,13 +77,13 @@ class TransportUpdateMonitorStateAction @Inject constructor( return } - // Create updated monitor with new enabled state + // Create a copy of the monitor with the updated state val updatedMonitor = getMonitorResponse.monitor!!.copy( enabled = enabled, enabledTime = if (enabled) Instant.now() else null ) - // Create index monitor request + // Call indexMonitor API to update the monitor val indexMonitorRequest = IndexMonitorRequest( monitorId = monitorId, seqNo = getMonitorResponse.seqNo, @@ -95,14 +93,13 @@ class TransportUpdateMonitorStateAction @Inject constructor( monitor = updatedMonitor, ) - // Execute index monitor request client.execute( INDEX_MONITOR_ACTION_TYPE, indexMonitorRequest, object : ActionListener { override fun onResponse(indexMonitorResponse: IndexMonitorResponse) { actionListener.onResponse( - UpdateMonitorStateResponse( + ToggleMonitorResponse( id = monitorId, version = indexMonitorResponse.version, seqNo = indexMonitorResponse.seqNo,