From cb88de02ffd3627db0c01ffebc7caa83ca288769 Mon Sep 17 00:00:00 2001 From: Surya Sashank Nistala Date: Fri, 14 Oct 2022 16:28:05 -0700 Subject: [PATCH] use acknowledge alert request,response, actions from common-utils dependencies (#606) Signed-off-by: Surya Sashank Nistala (cherry picked from commit e94ea321d7f7ca8a09c97e5379627c7096f9ea74) --- .../org/opensearch/alerting/AlertingPlugin.kt | 3 +- .../alerting/action/AcknowledgeAlertAction.kt | 15 ---- .../action/AcknowledgeAlertRequest.kt | 48 ----------- .../action/AcknowledgeAlertResponse.kt | 80 ------------------- .../resthandler/RestAcknowledgeAlertAction.kt | 6 +- .../TransportAcknowledgeAlertAction.kt | 17 ++-- .../action/AcknowledgeAlertActionTests.kt | 17 ---- .../action/AcknowledgeAlertRequestTests.kt | 28 ------- .../action/AcknowledgeAlertResponseTests.kt | 51 ------------ 9 files changed, 16 insertions(+), 249 deletions(-) delete mode 100644 alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertAction.kt delete mode 100644 alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequest.kt delete mode 100644 alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponse.kt delete mode 100644 alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertActionTests.kt delete mode 100644 alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequestTests.kt delete mode 100644 alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponseTests.kt diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt index d7ce0d32a..66ebd5eb3 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt @@ -7,7 +7,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.ExecuteMonitorAction import org.opensearch.alerting.action.GetDestinationsAction import org.opensearch.alerting.action.GetEmailAccountAction @@ -174,7 +173,7 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R ActionPlugin.ActionHandler(ExecuteMonitorAction.INSTANCE, TransportExecuteMonitorAction::class.java), ActionPlugin.ActionHandler(SearchMonitorAction.INSTANCE, TransportSearchMonitorAction::class.java), ActionPlugin.ActionHandler(AlertingActions.DELETE_MONITOR_ACTION_TYPE, TransportDeleteMonitorAction::class.java), - ActionPlugin.ActionHandler(AcknowledgeAlertAction.INSTANCE, TransportAcknowledgeAlertAction::class.java), + ActionPlugin.ActionHandler(AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_TYPE, TransportAcknowledgeAlertAction::class.java), ActionPlugin.ActionHandler(GetEmailAccountAction.INSTANCE, TransportGetEmailAccountAction::class.java), ActionPlugin.ActionHandler(SearchEmailAccountAction.INSTANCE, TransportSearchEmailAccountAction::class.java), ActionPlugin.ActionHandler(GetEmailGroupAction.INSTANCE, TransportGetEmailGroupAction::class.java), diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertAction.kt deleted file mode 100644 index d1d968f71..000000000 --- a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertAction.kt +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.action.ActionType - -class AcknowledgeAlertAction private constructor() : ActionType(NAME, ::AcknowledgeAlertResponse) { - companion object { - val INSTANCE = AcknowledgeAlertAction() - const val NAME = "cluster:admin/opendistro/alerting/alerts/ack" - } -} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequest.kt b/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequest.kt deleted file mode 100644 index 59b1cd027..000000000 --- a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequest.kt +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.action.ActionRequest -import org.opensearch.action.ActionRequestValidationException -import org.opensearch.action.support.WriteRequest -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.io.stream.StreamOutput -import java.io.IOException -import java.util.Collections - -class AcknowledgeAlertRequest : ActionRequest { - val monitorId: String - val alertIds: List - val refreshPolicy: WriteRequest.RefreshPolicy - - constructor( - monitorId: String, - alertIds: List, - refreshPolicy: WriteRequest.RefreshPolicy - ) : super() { - this.monitorId = monitorId - this.alertIds = alertIds - this.refreshPolicy = refreshPolicy - } - - @Throws(IOException::class) - constructor(sin: StreamInput) : this( - sin.readString(), // monitorId - Collections.unmodifiableList(sin.readStringList()), // alertIds - WriteRequest.RefreshPolicy.readFrom(sin) // refreshPolicy - ) - - override fun validate(): ActionRequestValidationException? { - return null - } - - @Throws(IOException::class) - override fun writeTo(out: StreamOutput) { - out.writeString(monitorId) - out.writeStringCollection(alertIds) - refreshPolicy.writeTo(out) - } -} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponse.kt b/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponse.kt deleted file mode 100644 index ad1a990d9..000000000 --- a/alerting/src/main/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponse.kt +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.action.ActionResponse -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.io.stream.StreamOutput -import org.opensearch.common.xcontent.ToXContent -import org.opensearch.common.xcontent.ToXContentObject -import org.opensearch.common.xcontent.XContentBuilder -import org.opensearch.commons.alerting.model.Alert -import java.io.IOException -import java.util.Collections - -class AcknowledgeAlertResponse : ActionResponse, ToXContentObject { - - val acknowledged: List - val failed: List - val missing: List - - constructor( - acknowledged: List, - failed: List, - missing: List - ) : super() { - this.acknowledged = acknowledged - this.failed = failed - this.missing = missing - } - - @Throws(IOException::class) - constructor(sin: StreamInput) : this( - Collections.unmodifiableList(sin.readList(::Alert)), // acknowledged - Collections.unmodifiableList(sin.readList(::Alert)), // failed - Collections.unmodifiableList(sin.readStringList()) // missing - ) - - @Throws(IOException::class) - override fun writeTo(out: StreamOutput) { - out.writeCollection(acknowledged) - out.writeCollection(failed) - out.writeStringCollection(missing) - } - - @Throws(IOException::class) - override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { - - builder.startObject().startArray("success") - acknowledged.forEach { builder.value(it.id) } - builder.endArray().startArray("failed") - failed.forEach { buildFailedAlertAcknowledgeObject(builder, it) } - missing.forEach { buildMissingAlertAcknowledgeObject(builder, it) } - return builder.endArray().endObject() - } - - private fun buildFailedAlertAcknowledgeObject(builder: XContentBuilder, failedAlert: Alert) { - builder.startObject() - .startObject(failedAlert.id) - val reason = when (failedAlert.state) { - Alert.State.ERROR -> "Alert is in an error state and can not be acknowledged." - Alert.State.COMPLETED -> "Alert has already completed and can not be acknowledged." - Alert.State.ACKNOWLEDGED -> "Alert has already been acknowledged." - else -> "Alert state unknown and can not be acknowledged" - } - builder.field("failed_reason", reason) - .endObject() - .endObject() - } - - private fun buildMissingAlertAcknowledgeObject(builder: XContentBuilder, alertID: String) { - builder.startObject() - .startObject(alertID) - .field("failed_reason", "Alert: $alertID does not exist (it may have already completed).") - .endObject() - .endObject() - } -} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestAcknowledgeAlertAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestAcknowledgeAlertAction.kt index 5cdde7424..bca3ef6f8 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestAcknowledgeAlertAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/resthandler/RestAcknowledgeAlertAction.kt @@ -9,12 +9,12 @@ 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.AcknowledgeAlertAction -import org.opensearch.alerting.action.AcknowledgeAlertRequest import org.opensearch.alerting.util.REFRESH import org.opensearch.client.node.NodeClient import org.opensearch.common.xcontent.XContentParser import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken +import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest +import org.opensearch.commons.alerting.action.AlertingActions import org.opensearch.rest.BaseRestHandler import org.opensearch.rest.BaseRestHandler.RestChannelConsumer import org.opensearch.rest.RestHandler.ReplacedRoute @@ -65,7 +65,7 @@ class RestAcknowledgeAlertAction : BaseRestHandler() { val acknowledgeAlertRequest = AcknowledgeAlertRequest(monitorId, alertIds, refreshPolicy) return RestChannelConsumer { channel -> - client.execute(AcknowledgeAlertAction.INSTANCE, acknowledgeAlertRequest, RestToXContentListener(channel)) + client.execute(AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_TYPE, acknowledgeAlertRequest, RestToXContentListener(channel)) } } diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt index a0e91cbc2..eaa48f21e 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt @@ -19,9 +19,6 @@ import org.opensearch.action.search.SearchResponse import org.opensearch.action.support.ActionFilters import org.opensearch.action.support.HandledTransportAction import org.opensearch.action.update.UpdateRequest -import org.opensearch.alerting.action.AcknowledgeAlertAction -import org.opensearch.alerting.action.AcknowledgeAlertRequest -import org.opensearch.alerting.action.AcknowledgeAlertResponse import org.opensearch.alerting.alerts.AlertIndices import org.opensearch.alerting.opensearchapi.suspendUntil import org.opensearch.alerting.settings.AlertingSettings @@ -37,8 +34,12 @@ import org.opensearch.common.xcontent.XContentHelper import org.opensearch.common.xcontent.XContentParser import org.opensearch.common.xcontent.XContentParserUtils import org.opensearch.common.xcontent.XContentType +import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest +import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse +import org.opensearch.commons.alerting.action.AlertingActions import org.opensearch.commons.alerting.model.Alert import org.opensearch.commons.alerting.util.optionalTimeField +import org.opensearch.commons.utils.recreateObject import org.opensearch.index.query.QueryBuilders import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.tasks.Task @@ -56,7 +57,7 @@ class TransportAcknowledgeAlertAction @Inject constructor( val settings: Settings, val xContentRegistry: NamedXContentRegistry ) : HandledTransportAction( - AcknowledgeAlertAction.NAME, transportService, actionFilters, ::AcknowledgeAlertRequest + AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_NAME, transportService, actionFilters, ::AcknowledgeAlertRequest ) { @Volatile private var isAlertHistoryEnabled = AlertingSettings.ALERT_HISTORY_ENABLED.get(settings) @@ -65,7 +66,13 @@ class TransportAcknowledgeAlertAction @Inject constructor( clusterService.clusterSettings.addSettingsUpdateConsumer(AlertingSettings.ALERT_HISTORY_ENABLED) { isAlertHistoryEnabled = it } } - override fun doExecute(task: Task, request: AcknowledgeAlertRequest, actionListener: ActionListener) { + override fun doExecute( + task: Task, + acknowledgeAlertRequest: AcknowledgeAlertRequest, + actionListener: ActionListener + ) { + val request = acknowledgeAlertRequest as? AcknowledgeAlertRequest + ?: recreateObject(acknowledgeAlertRequest) { AcknowledgeAlertRequest(it) } client.threadPool().threadContext.stashContext().use { scope.launch { AcknowledgeHandler(client, actionListener, request).start() diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertActionTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertActionTests.kt deleted file mode 100644 index 15c0f1cd3..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertActionTests.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.junit.Assert -import org.opensearch.test.OpenSearchTestCase - -class AcknowledgeAlertActionTests : OpenSearchTestCase() { - - fun `test ack alert action name`() { - Assert.assertNotNull(AcknowledgeAlertAction.INSTANCE.name()) - Assert.assertEquals(AcknowledgeAlertAction.INSTANCE.name(), AcknowledgeAlertAction.NAME) - } -} diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequestTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequestTests.kt deleted file mode 100644 index 0e699146f..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertRequestTests.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.junit.Assert -import org.opensearch.action.support.WriteRequest -import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.test.OpenSearchTestCase - -class AcknowledgeAlertRequestTests : OpenSearchTestCase() { - - fun `test acknowledge alert request`() { - val req = AcknowledgeAlertRequest("1234", mutableListOf("1", "2", "3", "4"), WriteRequest.RefreshPolicy.IMMEDIATE) - Assert.assertNotNull(req) - - val out = BytesStreamOutput() - req.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newReq = AcknowledgeAlertRequest(sin) - Assert.assertEquals("1234", newReq.monitorId) - Assert.assertEquals(4, newReq.alertIds.size) - Assert.assertEquals(WriteRequest.RefreshPolicy.IMMEDIATE, newReq.refreshPolicy) - } -} diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponseTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponseTests.kt deleted file mode 100644 index 7928c2ee0..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/AcknowledgeAlertResponseTests.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.junit.Assert -import org.opensearch.alerting.randomUser -import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.commons.alerting.alerts.AlertError -import org.opensearch.commons.alerting.model.ActionExecutionResult -import org.opensearch.commons.alerting.model.Alert -import org.opensearch.test.OpenSearchTestCase -import java.time.Instant - -class AcknowledgeAlertResponseTests : OpenSearchTestCase() { - - fun `test acknowledge alert response`() { - - val acknowledged = mutableListOf( - Alert( - "1234", 0L, 1, "monitor-1234", "test-monitor", 0L, randomUser(), - "trigger-14", "test-trigger", ArrayList(), ArrayList(), Alert.State.ACKNOWLEDGED, - Instant.now(), Instant.now(), Instant.now(), Instant.now(), null, ArrayList(), - "sev-2", ArrayList(), null - ) - ) - val failed = mutableListOf( - Alert( - "1234", 0L, 1, "monitor-1234", "test-monitor", 0L, randomUser(), - "trigger-14", "test-trigger", ArrayList(), ArrayList(), Alert.State.ERROR, Instant.now(), Instant.now(), - Instant.now(), Instant.now(), null, mutableListOf(AlertError(Instant.now(), "Error msg")), - "sev-2", mutableListOf(ActionExecutionResult("7890", null, 0)), null - ) - ) - val missing = mutableListOf("1", "2", "3", "4") - - val req = AcknowledgeAlertResponse(acknowledged, failed, missing) - Assert.assertNotNull(req) - - val out = BytesStreamOutput() - req.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newReq = AcknowledgeAlertResponse(sin) - Assert.assertEquals(1, newReq.acknowledged.size) - Assert.assertEquals(1, newReq.failed.size) - Assert.assertEquals(4, newReq.missing.size) - } -}