Skip to content

Commit

Permalink
expose delete monitor api from alerting (opensearch-project#568)
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <[email protected]>
  • Loading branch information
sbcd90 authored Sep 26, 2022
1 parent 7e812ab commit 61f93b4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -47,8 +50,8 @@ class TransportDeleteMonitorAction @Inject constructor(
val clusterService: ClusterService,
settings: Settings,
val xContentRegistry: NamedXContentRegistry
) : HandledTransportAction<DeleteMonitorRequest, DeleteResponse>(
DeleteMonitorAction.NAME, transportService, actionFilters, ::DeleteMonitorRequest
) : HandledTransportAction<ActionRequest, DeleteMonitorResponse>(
AlertingActions.DELETE_MONITOR_ACTION_NAME, transportService, actionFilters, ::DeleteMonitorRequest
),
SecureTransportAction {

Expand All @@ -58,22 +61,24 @@ class TransportDeleteMonitorAction @Inject constructor(
listenFilterBySettingChange(clusterService)
}

override fun doExecute(task: Task, request: DeleteMonitorRequest, actionListener: ActionListener<DeleteResponse>) {
override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener<DeleteMonitorResponse>) {
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<DeleteResponse>,
private val actionListener: ActionListener<DeleteMonitorResponse>,
private val deleteRequest: DeleteRequest,
private val user: User?,
private val monitorId: String
Expand Down Expand Up @@ -142,7 +147,7 @@ class TransportDeleteMonitorAction @Inject constructor(
}
deleteMetadata()

actionListener.onResponse(response)
actionListener.onResponse(DeleteMonitorResponse(response.id, response.version))
}

override fun onFailure(t: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ class TransportIndexMonitorAction @Inject constructor(
listenFilterBySettingChange(clusterService)
}

override fun doExecute(task: Task, req: ActionRequest, actionListener: ActionListener<IndexMonitorResponse>) {
val request = req as? IndexMonitorRequest
?: recreateObject(req) { IndexMonitorRequest(it) }
override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener<IndexMonitorResponse>) {
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)
}
}

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 61f93b4

Please sign in to comment.