diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertMover.kt b/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertMover.kt index c7742d1c7..dfe3be6f0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertMover.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/alerts/AlertMover.kt @@ -28,6 +28,7 @@ import org.opensearch.common.xcontent.XContentParserUtils import org.opensearch.common.xcontent.XContentType import org.opensearch.commons.alerting.model.Alert import org.opensearch.commons.alerting.model.CompositeInput +import org.opensearch.commons.alerting.model.DataSources import org.opensearch.commons.alerting.model.Monitor import org.opensearch.commons.alerting.model.ScheduledJob import org.opensearch.commons.alerting.model.Workflow @@ -166,9 +167,7 @@ class AlertMover { monitorCtx.xContentRegistry!!, response = getResponse ) - /** check if alert index is initialized **/ - if (monitorCtx.alertIndices!!.isAlertInitialized(monitor.dataSources) == false) - return + alertIndex = monitor.dataSources.alertsIndex alertHistoryIndex = if (monitor.dataSources.alertsHistoryIndex == null) alertHistoryIndex @@ -181,8 +180,12 @@ class AlertMover { } } } + val dataSources = DataSources().copy(alertsHistoryIndex = alertHistoryIndex, alertsIndex = alertIndex) + /** check if alert index is initialized **/ + if (monitorCtx.alertIndices!!.isAlertInitialized(dataSources) == false) + return val boolQuery = QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Alert.WORKFLOW_ID_FIELD, workflowId)) + .must(QueryBuilders.termQuery(Alert.WORKFLOW_ID_FIELD, workflowId)) if (workflow != null) { boolQuery.mustNot(QueryBuilders.termsQuery(Alert.TRIGGER_ID_FIELD, workflow.triggers.map { it.id })) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/AlertingRestTestCase.kt b/alerting/src/test/kotlin/org/opensearch/alerting/AlertingRestTestCase.kt index a79d424b7..bdc568d24 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/AlertingRestTestCase.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/AlertingRestTestCase.kt @@ -41,7 +41,6 @@ import org.opensearch.common.xcontent.XContentFactory import org.opensearch.common.xcontent.XContentFactory.jsonBuilder import org.opensearch.common.xcontent.XContentParserUtils import org.opensearch.common.xcontent.XContentType -import org.opensearch.common.xcontent.json.JsonXContent import org.opensearch.common.xcontent.json.JsonXContent.jsonXContent import org.opensearch.commons.alerting.action.GetFindingsResponse import org.opensearch.commons.alerting.model.Alert @@ -662,9 +661,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() { val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", StringEntity(request, APPLICATION_JSON)) assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus()) - val searchResponse = SearchResponse.fromXContent(createParser(JsonXContent.jsonXContent, httpResponse.entity.content)) + val searchResponse = SearchResponse.fromXContent(createParser(jsonXContent, httpResponse.entity.content)) return searchResponse.hits.hits.map { - val xcp = createParser(JsonXContent.jsonXContent, it.sourceRef).also { it.nextToken() } + val xcp = createParser(jsonXContent, it.sourceRef).also { it.nextToken() } Alert.parse(xcp, it.id, it.version) }.filter { alert -> alert.monitorId == monitor.id } } @@ -707,9 +706,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() { val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", StringEntity(request, APPLICATION_JSON)) assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus()) - val searchResponse = SearchResponse.fromXContent(createParser(JsonXContent.jsonXContent, httpResponse.entity.content)) + val searchResponse = SearchResponse.fromXContent(createParser(jsonXContent, httpResponse.entity.content)) return searchResponse.hits.hits.map { - val xcp = createParser(JsonXContent.jsonXContent, it.sourceRef).also { it.nextToken() } + val xcp = createParser(jsonXContent, it.sourceRef).also { it.nextToken() } Finding.parse(xcp) }.filter { finding -> finding.monitorId == monitor.id } } @@ -732,9 +731,9 @@ abstract class AlertingRestTestCase : ODFERestTestCase() { val httpResponse = adminClient().makeRequest("GET", "/$indices/_search", searchParams, StringEntity(request, APPLICATION_JSON)) assertEquals("Search failed", RestStatus.OK, httpResponse.restStatus()) - val searchResponse = SearchResponse.fromXContent(createParser(JsonXContent.jsonXContent, httpResponse.entity.content)) + val searchResponse = SearchResponse.fromXContent(createParser(jsonXContent, httpResponse.entity.content)) return searchResponse.hits.hits.map { - val xcp = createParser(JsonXContent.jsonXContent, it.sourceRef).also { it.nextToken() } + val xcp = createParser(jsonXContent, it.sourceRef).also { it.nextToken() } Alert.parse(xcp, it.id, it.version) } } @@ -1618,7 +1617,7 @@ abstract class AlertingRestTestCase : ODFERestTestCase() { ) assertEquals("Unable to create a new monitor", RestStatus.CREATED, response.restStatus()) - val workflowJson = JsonXContent.jsonXContent.createParser( + val workflowJson = jsonXContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.entity.content ).map() diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt index ff3180cd4..c19e8b556 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt @@ -5499,7 +5499,6 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() { } fun `test postIndex on workflow update with trigger deletion`() { - val monitorRunnerService = getInstanceFromNode(MonitorRunnerService.javaClass) val docQuery1 = DocLevelQuery(query = "test_field_1:\"us-west-2\"", name = "3") val docLevelInput1 = DocLevelMonitorInput("description", listOf(index), listOf(docQuery1)) val trigger1 = randomDocumentLevelTrigger(condition = ALWAYS_RUN) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt index e7cc5eddb..ec128109f 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt @@ -1069,7 +1069,7 @@ class WorkflowRestApiIT : AlertingRestTestCase() { owner = "alerting", triggers = listOf(andTrigger) ) - val workflowById = createWorkflow(workflow)!! + val workflowById = createWorkflow(workflow) assertNotNull(workflowById) val workflowId = workflowById.id