Skip to content

Commit

Permalink
Rewrite get monitor and search monitor transport action requests
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Ohlsen <[email protected]>
  • Loading branch information
ohltyler committed Nov 27, 2023
1 parent 9d05072 commit 15464b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.launch
import org.apache.logging.log4j.LogManager
import org.apache.lucene.search.join.ScoreMode
import org.opensearch.OpenSearchStatusException
import org.opensearch.action.ActionRequest
import org.opensearch.action.get.GetRequest
import org.opensearch.action.get.GetResponse
import org.opensearch.action.search.SearchRequest
Expand All @@ -37,6 +38,7 @@ import org.opensearch.commons.alerting.action.GetMonitorResponse.AssociatedWorkf
import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.model.ScheduledJob
import org.opensearch.commons.alerting.model.Workflow
import org.opensearch.commons.utils.recreateObject
import org.opensearch.core.action.ActionListener
import org.opensearch.core.rest.RestStatus
import org.opensearch.core.xcontent.NamedXContentRegistry
Expand All @@ -55,7 +57,7 @@ class TransportGetMonitorAction @Inject constructor(
val xContentRegistry: NamedXContentRegistry,
val clusterService: ClusterService,
settings: Settings,
) : HandledTransportAction<GetMonitorRequest, GetMonitorResponse>(
) : HandledTransportAction<ActionRequest, GetMonitorResponse>(
AlertingActions.GET_MONITOR_ACTION_NAME,
transportService,
actionFilters,
Expand All @@ -70,12 +72,17 @@ class TransportGetMonitorAction @Inject constructor(
listenFilterBySettingChange(clusterService)
}

override fun doExecute(task: Task, getMonitorRequest: GetMonitorRequest, actionListener: ActionListener<GetMonitorResponse>) {
override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener<GetMonitorResponse>) {
val transformedRequest = request as? GetMonitorRequest
?: recreateObject(request) {
GetMonitorRequest(it)
}

val user = readUserFromThreadContext(client)

val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, getMonitorRequest.monitorId)
.version(getMonitorRequest.version)
.fetchSourceContext(getMonitorRequest.srcContext)
val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, transformedRequest.monitorId)
.version(transformedRequest.version)
.fetchSourceContext(transformedRequest.srcContext)

if (!validateUserBackendRoles(user, actionListener)) {
return
Expand Down Expand Up @@ -115,7 +122,7 @@ class TransportGetMonitorAction @Inject constructor(
monitor?.user,
actionListener,
"monitor",
getMonitorRequest.monitorId
transformedRequest.monitorId
)
) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.alerting.transport

import org.apache.logging.log4j.LogManager
import org.opensearch.action.ActionRequest
import org.opensearch.action.search.SearchRequest
import org.opensearch.action.search.SearchResponse
import org.opensearch.action.support.ActionFilters
Expand All @@ -24,6 +25,7 @@ import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.model.ScheduledJob
import org.opensearch.commons.alerting.model.Workflow
import org.opensearch.commons.authuser.User
import org.opensearch.commons.utils.recreateObject
import org.opensearch.core.action.ActionListener
import org.opensearch.index.query.BoolQueryBuilder
import org.opensearch.index.query.ExistsQueryBuilder
Expand All @@ -40,7 +42,7 @@ class TransportSearchMonitorAction @Inject constructor(
val client: Client,
clusterService: ClusterService,
actionFilters: ActionFilters
) : HandledTransportAction<SearchMonitorRequest, SearchResponse>(
) : HandledTransportAction<ActionRequest, SearchResponse>(
AlertingActions.SEARCH_MONITORS_ACTION_NAME, transportService, actionFilters, ::SearchMonitorRequest
),
SecureTransportAction {
Expand All @@ -50,8 +52,13 @@ class TransportSearchMonitorAction @Inject constructor(
listenFilterBySettingChange(clusterService)
}

override fun doExecute(task: Task, searchMonitorRequest: SearchMonitorRequest, actionListener: ActionListener<SearchResponse>) {
val searchSourceBuilder = searchMonitorRequest.searchRequest.source()
override fun doExecute(task: Task, request: ActionRequest, actionListener: ActionListener<SearchResponse>) {
val transformedRequest = request as? SearchMonitorRequest
?: recreateObject(request) {
SearchMonitorRequest(it)
}

val searchSourceBuilder = transformedRequest.searchRequest.source()
.seqNoAndPrimaryTerm(true)
.version(true)
val queryBuilder = if (searchSourceBuilder.query() == null) BoolQueryBuilder()
Expand All @@ -60,7 +67,7 @@ class TransportSearchMonitorAction @Inject constructor(
// The SearchMonitor API supports one 'index' parameter of either the SCHEDULED_JOBS_INDEX or ALL_ALERT_INDEX_PATTERN.
// When querying the ALL_ALERT_INDEX_PATTERN, we don't want to check whether the MONITOR_TYPE field exists
// because we're querying alert indexes.
if (searchMonitorRequest.searchRequest.indices().contains(ScheduledJob.SCHEDULED_JOBS_INDEX)) {
if (transformedRequest.searchRequest.indices().contains(ScheduledJob.SCHEDULED_JOBS_INDEX)) {
val monitorWorkflowType = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery(Monitor.MONITOR_TYPE))
.should(QueryBuilders.existsQuery(Workflow.WORKFLOW_TYPE))
queryBuilder.must(monitorWorkflowType)
Expand All @@ -69,10 +76,10 @@ class TransportSearchMonitorAction @Inject constructor(
searchSourceBuilder.query(queryBuilder)
.seqNoAndPrimaryTerm(true)
.version(true)
addOwnerFieldIfNotExists(searchMonitorRequest.searchRequest)
addOwnerFieldIfNotExists(transformedRequest.searchRequest)
val user = readUserFromThreadContext(client)
client.threadPool().threadContext.stashContext().use {
resolve(searchMonitorRequest, actionListener, user)
resolve(transformedRequest, actionListener, user)
}
}

Expand Down

0 comments on commit 15464b2

Please sign in to comment.