Skip to content

Commit

Permalink
Enabled parsing of bucket level monitors when executing the transport…
Browse files Browse the repository at this point in the history
… action on the same node (opensearch-project#631) (opensearch-project#639)

Signed-off-by: Stevan Buzejic <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] authored Nov 4, 2022
1 parent 6064dad commit eb693ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.opensearch.alerting.util.isADMonitor
import org.opensearch.client.Client
import org.opensearch.cluster.service.ClusterService
import org.opensearch.common.inject.Inject
import org.opensearch.common.io.stream.NamedWriteableRegistry
import org.opensearch.common.settings.Settings
import org.opensearch.common.unit.TimeValue
import org.opensearch.common.xcontent.LoggingDeprecationHandler
Expand Down Expand Up @@ -87,7 +88,8 @@ class TransportIndexMonitorAction @Inject constructor(
val docLevelMonitorQueries: DocLevelMonitorQueries,
val clusterService: ClusterService,
val settings: Settings,
val xContentRegistry: NamedXContentRegistry
val xContentRegistry: NamedXContentRegistry,
val namedWriteableRegistry: NamedWriteableRegistry,
) : HandledTransportAction<ActionRequest, IndexMonitorResponse>(
AlertingActions.INDEX_MONITOR_ACTION_NAME, transportService, actionFilters, ::IndexMonitorRequest
),
Expand All @@ -111,7 +113,10 @@ class TransportIndexMonitorAction @Inject constructor(

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

val user = readUserFromThreadContext(client)

if (!validateUserBackendRoles(user, actionListener)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.opensearch.alerting.randomAction
import org.opensearch.alerting.randomAlert
import org.opensearch.alerting.randomAnomalyDetector
import org.opensearch.alerting.randomAnomalyDetectorWithUser
import org.opensearch.alerting.randomBucketLevelMonitor
import org.opensearch.alerting.randomBucketLevelTrigger
import org.opensearch.alerting.randomDocumentLevelMonitor
import org.opensearch.alerting.randomDocumentLevelTrigger
Expand Down Expand Up @@ -107,6 +108,21 @@ class MonitorRestApiIT : AlertingRestTestCase() {
assertEquals("Incorrect Location header", "$ALERTING_BASE_URI/$createdId", createResponse.getHeader("Location"))
}

@Throws(Exception::class)
fun `test creating a bucket monitor`() {
val monitor = randomBucketLevelMonitor()

val createResponse = client().makeRequest("POST", ALERTING_BASE_URI, emptyMap(), monitor.toHttpEntity())

assertEquals("Create monitor failed", RestStatus.CREATED, createResponse.restStatus())
val responseBody = createResponse.asMap()
val createdId = responseBody["_id"] as String
val createdVersion = responseBody["_version"] as Int
assertNotEquals("response is missing Id", Monitor.NO_ID, createdId)
assertTrue("incorrect version", createdVersion > 0)
assertEquals("Incorrect Location header", "$ALERTING_BASE_URI/$createdId", createResponse.getHeader("Location"))
}

fun `test creating a monitor with legacy ODFE`() {
val monitor = randomQueryLevelMonitor()
val createResponse = client().makeRequest("POST", LEGACY_OPENDISTRO_ALERTING_BASE_URI, emptyMap(), monitor.toHttpEntity())
Expand Down

0 comments on commit eb693ad

Please sign in to comment.