From 0fd6ac59b13eeec953799d51d6ba03f3f8879a90 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Sun, 26 May 2024 17:09:38 -0700 Subject: [PATCH 01/10] initial commit, functional but needs refactoring Signed-off-by: Dennis Toepker --- .../alerting/action/AlertingActions.kt | 15 ++ .../alerting/action/DeleteNoteRequest.kt | 29 ++++ .../alerting/action/DeleteNoteResponse.kt | 32 ++++ .../alerting/action/IndexNoteRequest.kt | 57 +++++++ .../alerting/action/IndexNoteResponse.kt | 58 +++++++ .../alerting/action/SearchNoteRequest.kt | 33 ++++ .../commons/alerting/model/DataSources.kt | 10 ++ .../opensearch/commons/alerting/model/Note.kt | 146 ++++++++++++++++++ .../commons/alerting/util/IndexUtils.kt | 7 + 9 files changed, 387 insertions(+) create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt create mode 100644 src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt index f2ada6a5..8b4f9d38 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt @@ -21,6 +21,9 @@ object AlertingActions { const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe" const val GET_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/get" const val SEARCH_MONITORS_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/search" + const val INDEX_NOTE_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/write" + const val SEARCH_NOTES_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/search" + const val DELETE_NOTES_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/delete" @JvmField val INDEX_MONITOR_ACTION_TYPE = @@ -73,4 +76,16 @@ object AlertingActions { @JvmField val SEARCH_MONITORS_ACTION_TYPE = ActionType(SEARCH_MONITORS_ACTION_NAME, ::SearchResponse) + + @JvmField + val INDEX_NOTE_ACTION_TYPE = + ActionType(INDEX_NOTE_ACTION_NAME, ::IndexNoteResponse) + + @JvmField + val SEARCH_NOTES_ACTION_TYPE = + ActionType(SEARCH_NOTES_ACTION_NAME, ::SearchResponse) + + @JvmField + val DELETE_NOTES_ACTION_TYPE = + ActionType(DELETE_NOTES_ACTION_NAME, ::DeleteNoteResponse) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt new file mode 100644 index 00000000..4e91f6be --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt @@ -0,0 +1,29 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import java.io.IOException + +class DeleteNoteRequest : ActionRequest { + val noteId: String + + constructor(noteId: String) : super() { + this.noteId = noteId + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + noteId = sin.readString() + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(noteId) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt new file mode 100644 index 00000000..db6b7d0a --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt @@ -0,0 +1,32 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.util.IndexUtils +import org.opensearch.commons.notifications.action.BaseResponse +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.core.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder + +class DeleteNoteResponse : BaseResponse { + var id: String + + constructor( + id: String + ) : super() { + this.id = id + } + + constructor(sin: StreamInput) : this( + sin.readString() // id + ) + + override fun writeTo(out: StreamOutput) { + out.writeString(id) + } + + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return builder.startObject() + .field(IndexUtils._ID, id) + .endObject() + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt new file mode 100644 index 00000000..40d4c7e5 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt @@ -0,0 +1,57 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.rest.RestRequest +import java.io.IOException + +class IndexNoteRequest : ActionRequest { + val alertId: String + val noteId: String + val seqNo: Long + val primaryTerm: Long + val method: RestRequest.Method + var content: String + + constructor( + alertId: String, + noteId: String, + seqNo: Long, + primaryTerm: Long, + method: RestRequest.Method, + content: String + ) : super() { + this.alertId = alertId + this.noteId = noteId + this.seqNo = seqNo + this.primaryTerm = primaryTerm + this.method = method + this.content = content + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + alertId = sin.readString(), + noteId = sin.readString(), + seqNo = sin.readLong(), + primaryTerm = sin.readLong(), + method = sin.readEnum(RestRequest.Method::class.java), + content = sin.readString() + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(alertId) + out.writeString(noteId) + out.writeLong(seqNo) + out.writeLong(primaryTerm) + out.writeEnum(method) + out.writeString(content) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt new file mode 100644 index 00000000..55bb7be8 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt @@ -0,0 +1,58 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.commons.alerting.model.Note +import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID +import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM +import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO +import org.opensearch.commons.notifications.action.BaseResponse +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.core.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder +import java.io.IOException + +class IndexNoteResponse : BaseResponse { + // TODO: do we really need sequence num and primary term? probs delete em + var id: String + var seqNo: Long + var primaryTerm: Long + var note: Note + + constructor( + id: String, + seqNo: Long, + primaryTerm: Long, + note: Note + ) : super() { + this.id = id + this.seqNo = seqNo + this.primaryTerm = primaryTerm + this.note = note + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + sin.readString(), // id + sin.readLong(), // seqNo + sin.readLong(), // primaryTerm + Note.readFrom(sin) // note + ) + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(id) + out.writeLong(seqNo) + out.writeLong(primaryTerm) + note.writeTo(out) + } + + @Throws(IOException::class) + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return builder.startObject() + .field(_ID, id) + .field(_SEQ_NO, seqNo) + .field(_PRIMARY_TERM, primaryTerm) + .field("note", note) + .endObject() + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt new file mode 100644 index 00000000..4b7d671a --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt @@ -0,0 +1,33 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.ActionRequest +import org.opensearch.action.ActionRequestValidationException +import org.opensearch.action.search.SearchRequest +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import java.io.IOException + +class SearchNoteRequest : ActionRequest { + + val searchRequest: SearchRequest + + constructor( + searchRequest: SearchRequest + ) : super() { + this.searchRequest = searchRequest + } + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + searchRequest = SearchRequest(sin) + ) + + override fun validate(): ActionRequestValidationException? { + return null + } + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + searchRequest.writeTo(out) + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt index b922a706..4695c241 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt @@ -31,6 +31,16 @@ data class DataSources( /** Configures a custom index pattern for alertHistoryIndex alias.*/ val alertsHistoryIndexPattern: String? = "<.opendistro-alerting-alert-history-{now/d}-1>", // AlertIndices.ALERT_HISTORY_INDEX_PATTERN + /** Configures a custom index to store notes associated with an alert. Creates a new index if index with given name not present. + * If index is pre-existing, mapping is updated. */ + val notesIndex: String = ".opensearch-alerting-notes", // NotesIndices.NOTES_INDEX + + /** Configures a custom index alias to store historical notes associated with historical alerts.*/ + val notesHistoryIndex: String? = ".opensearch-alerting-notes-history-write", // AlertIndices.NOTES_HISTORY_WRITE_INDEX + + /** Configures a custom index pattern for notesHistoryIndex alias.*/ + val notesHistoryIndexPattern: String? = "<.opensearch-alerting-notes-history-{now/d}-1>", // AlertIndices.NOTES_HISTORY_INDEX_PATTERN + /** Configures custom mappings by field type for query index. * Custom query index mappings are configurable, only if a custom query index is configured too. */ val queryIndexMappingsByType: Map> = mapOf(), diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt new file mode 100644 index 00000000..6ee0aef1 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt @@ -0,0 +1,146 @@ +package org.opensearch.commons.alerting.model + +import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID +import org.opensearch.commons.alerting.util.instant +import org.opensearch.commons.alerting.util.optionalTimeField +import org.opensearch.commons.alerting.util.optionalUsernameField +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.core.common.io.stream.StreamOutput +import org.opensearch.core.common.io.stream.Writeable +import org.opensearch.core.xcontent.ToXContent +import org.opensearch.core.xcontent.XContentBuilder +import org.opensearch.core.xcontent.XContentParser +import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken +import java.io.IOException +import java.time.Instant +import java.util.Collections +import org.apache.logging.log4j.LogManager +import org.opensearch.commons.alerting.util.optionalUserField +import org.opensearch.commons.authuser.User + +private val log = LogManager.getLogger(Note::class.java) + +data class Note( + val id: String = NO_ID, + val alertId: String = Alert.NO_ID, + val content: String, + val time: Instant, + val user: User? +) : Writeable, ToXContent { + + @Throws(IOException::class) + constructor(sin: StreamInput) : this( + id = sin.readString(), + alertId = sin.readString(), + content = sin.readString(), + time = sin.readInstant(), + user = if (sin.readBoolean()) { + User(sin) + } else { + null + } + ) + + @Throws(IOException::class) + override fun writeTo(out: StreamOutput) { + out.writeString(id) + out.writeString(alertId) + out.writeString(content) + out.writeInstant(time) + out.writeBoolean(user != null) + user?.writeTo(out) + } + + fun asTemplateArg(): Map { + val templateArgMap = mutableMapOf( + _ID to id, + ALERT_ID_FIELD to alertId, + NOTE_CONTENT_FIELD to content, + NOTE_TIME_FIELD to time + ) + if (user != null) { + templateArgMap[NOTE_USER_FIELD] = user.name + } + return Collections.unmodifiableMap(templateArgMap) + } + + // used to create the Note JSON object for an API response (displayed to user) + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { + return createXContentBuilder(builder, false) + } + + // used to create the Note JSON object for indexing a doc into an index (not displayed to user) + fun toXContentWithUser(builder: XContentBuilder): XContentBuilder { + return createXContentBuilder(builder, true) + } + + private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder { + log.info("include full user: $includeFullUser") + builder.startObject() + .field(ALERT_ID_FIELD, alertId) + .field(NOTE_CONTENT_FIELD, content) + .optionalTimeField(NOTE_TIME_FIELD, time) + + if (includeFullUser) { + // if we're storing a Note into an internal index, include full User + builder.optionalUserField(NOTE_USER_FIELD, user) + } else { + // if we're displaying the Note as part of an API call response, only include username + builder.optionalUsernameField(NOTE_USER_FIELD, user) + } + + builder.endObject() + return builder + } + + companion object { + const val ALERT_ID_FIELD = "alert_id" + const val NOTE_CONTENT_FIELD = "content" + const val NOTE_TIME_FIELD = "time" + const val NOTE_USER_FIELD = "user" + const val NO_ID = "" + + @JvmStatic + @JvmOverloads + @Throws(IOException::class) + fun parse(xcp: XContentParser, id: String = NO_ID): Note { + lateinit var alertId: String + var content = "" + lateinit var time: Instant + var user: User? = null + + log.info("note parse curr token: ${xcp.currentToken()}") + ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) + while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { + val fieldName = xcp.currentName() + xcp.nextToken() + + when (fieldName) { + ALERT_ID_FIELD -> alertId = xcp.text() + NOTE_CONTENT_FIELD -> content = xcp.text() + NOTE_TIME_FIELD -> time = requireNotNull(xcp.instant()) + NOTE_USER_FIELD -> + user = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { + null + } else { + User.parse(xcp) + } + } + } + + return Note( + id = id, + alertId = requireNotNull(alertId) { "Alert ID is null" }, + content = content, + time = requireNotNull(time), + user = user + ) + } + + @JvmStatic + @Throws(IOException::class) + fun readFrom(sin: StreamInput): Note { + return Note(sin) + } + } +} diff --git a/src/main/kotlin/org/opensearch/commons/alerting/util/IndexUtils.kt b/src/main/kotlin/org/opensearch/commons/alerting/util/IndexUtils.kt index 18454465..aa45d99e 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/util/IndexUtils.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/util/IndexUtils.kt @@ -55,6 +55,13 @@ fun XContentBuilder.optionalUserField(name: String, user: User?): XContentBuilde return this.field(name, user) } +fun XContentBuilder.optionalUsernameField(name: String, user: User?): XContentBuilder { + if (user == null) { + return nullField(name) + } + return this.field(name, user.name) +} + fun XContentBuilder.optionalTimeField(name: String, instant: Instant?): XContentBuilder { if (instant == null) { return nullField(name) From c12827e6dd4e69fb39bb9a3497fadc4d0d748e87 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Mon, 27 May 2024 14:11:21 -0700 Subject: [PATCH 02/10] added lastUpdatedTime to Note object Signed-off-by: Dennis Toepker --- .../opensearch/commons/alerting/model/Note.kt | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt index 6ee0aef1..797e861b 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt @@ -13,18 +13,16 @@ import org.opensearch.core.xcontent.XContentParser import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken import java.io.IOException import java.time.Instant -import java.util.Collections import org.apache.logging.log4j.LogManager import org.opensearch.commons.alerting.util.optionalUserField import org.opensearch.commons.authuser.User -private val log = LogManager.getLogger(Note::class.java) - data class Note( val id: String = NO_ID, - val alertId: String = Alert.NO_ID, + val alertId: String = NO_ID, val content: String, - val time: Instant, + val createdTime: Instant, + val lastUpdatedTime: Instant?, val user: User? ) : Writeable, ToXContent { @@ -33,7 +31,8 @@ data class Note( id = sin.readString(), alertId = sin.readString(), content = sin.readString(), - time = sin.readInstant(), + createdTime = sin.readInstant(), + lastUpdatedTime = sin.readOptionalInstant(), user = if (sin.readBoolean()) { User(sin) } else { @@ -41,27 +40,39 @@ data class Note( } ) + constructor( + alertId: String, + content: String, + createdTime: Instant, + user: User? + ) : this ( + alertId = alertId, + content = content, + createdTime = createdTime, + lastUpdatedTime = null, + user = user + ) + @Throws(IOException::class) override fun writeTo(out: StreamOutput) { out.writeString(id) out.writeString(alertId) out.writeString(content) - out.writeInstant(time) + out.writeInstant(createdTime) + out.writeOptionalInstant(lastUpdatedTime) out.writeBoolean(user != null) user?.writeTo(out) } fun asTemplateArg(): Map { - val templateArgMap = mutableMapOf( + return mapOf( _ID to id, ALERT_ID_FIELD to alertId, NOTE_CONTENT_FIELD to content, - NOTE_TIME_FIELD to time + NOTE_CREATED_TIME_FIELD to createdTime, + NOTE_LAST_UPDATED_TIME_FIELD to lastUpdatedTime, + NOTE_USER_FIELD to user?.name ) - if (user != null) { - templateArgMap[NOTE_USER_FIELD] = user.name - } - return Collections.unmodifiableMap(templateArgMap) } // used to create the Note JSON object for an API response (displayed to user) @@ -75,11 +86,11 @@ data class Note( } private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder { - log.info("include full user: $includeFullUser") builder.startObject() .field(ALERT_ID_FIELD, alertId) .field(NOTE_CONTENT_FIELD, content) - .optionalTimeField(NOTE_TIME_FIELD, time) + .optionalTimeField(NOTE_CREATED_TIME_FIELD, createdTime) + .optionalTimeField(NOTE_LAST_UPDATED_TIME_FIELD, lastUpdatedTime) if (includeFullUser) { // if we're storing a Note into an internal index, include full User @@ -96,7 +107,8 @@ data class Note( companion object { const val ALERT_ID_FIELD = "alert_id" const val NOTE_CONTENT_FIELD = "content" - const val NOTE_TIME_FIELD = "time" + const val NOTE_CREATED_TIME_FIELD = "created_time" + const val NOTE_LAST_UPDATED_TIME_FIELD = "last_updated_time" const val NOTE_USER_FIELD = "user" const val NO_ID = "" @@ -106,10 +118,10 @@ data class Note( fun parse(xcp: XContentParser, id: String = NO_ID): Note { lateinit var alertId: String var content = "" - lateinit var time: Instant + lateinit var createdTime: Instant + var lastUpdatedTime: Instant? = null var user: User? = null - log.info("note parse curr token: ${xcp.currentToken()}") ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { val fieldName = xcp.currentName() @@ -118,7 +130,8 @@ data class Note( when (fieldName) { ALERT_ID_FIELD -> alertId = xcp.text() NOTE_CONTENT_FIELD -> content = xcp.text() - NOTE_TIME_FIELD -> time = requireNotNull(xcp.instant()) + NOTE_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant()) + NOTE_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant() NOTE_USER_FIELD -> user = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { null @@ -132,7 +145,8 @@ data class Note( id = id, alertId = requireNotNull(alertId) { "Alert ID is null" }, content = content, - time = requireNotNull(time), + createdTime = createdTime, + lastUpdatedTime = lastUpdatedTime, user = user ) } From 4e5a983775e6c7ddb0509b7e00d50c6ec1299fff Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Wed, 29 May 2024 11:53:29 -0700 Subject: [PATCH 03/10] changed name from alert id to entity id Signed-off-by: Dennis Toepker --- .../alerting/action/IndexNoteRequest.kt | 10 ++++----- .../opensearch/commons/alerting/model/Note.kt | 22 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt index 40d4c7e5..b01ccdf9 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt @@ -8,7 +8,7 @@ import org.opensearch.rest.RestRequest import java.io.IOException class IndexNoteRequest : ActionRequest { - val alertId: String + val entityId: String val noteId: String val seqNo: Long val primaryTerm: Long @@ -16,14 +16,14 @@ class IndexNoteRequest : ActionRequest { var content: String constructor( - alertId: String, + entityId: String, noteId: String, seqNo: Long, primaryTerm: Long, method: RestRequest.Method, content: String ) : super() { - this.alertId = alertId + this.entityId = entityId this.noteId = noteId this.seqNo = seqNo this.primaryTerm = primaryTerm @@ -33,7 +33,7 @@ class IndexNoteRequest : ActionRequest { @Throws(IOException::class) constructor(sin: StreamInput) : this( - alertId = sin.readString(), + entityId = sin.readString(), noteId = sin.readString(), seqNo = sin.readLong(), primaryTerm = sin.readLong(), @@ -47,7 +47,7 @@ class IndexNoteRequest : ActionRequest { @Throws(IOException::class) override fun writeTo(out: StreamOutput) { - out.writeString(alertId) + out.writeString(entityId) out.writeString(noteId) out.writeLong(seqNo) out.writeLong(primaryTerm) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt index 797e861b..11f62b1a 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt @@ -19,7 +19,7 @@ import org.opensearch.commons.authuser.User data class Note( val id: String = NO_ID, - val alertId: String = NO_ID, + val entityId: String = NO_ID, val content: String, val createdTime: Instant, val lastUpdatedTime: Instant?, @@ -29,7 +29,7 @@ data class Note( @Throws(IOException::class) constructor(sin: StreamInput) : this( id = sin.readString(), - alertId = sin.readString(), + entityId = sin.readString(), content = sin.readString(), createdTime = sin.readInstant(), lastUpdatedTime = sin.readOptionalInstant(), @@ -41,12 +41,12 @@ data class Note( ) constructor( - alertId: String, + entityId: String, content: String, createdTime: Instant, user: User? ) : this ( - alertId = alertId, + entityId = entityId, content = content, createdTime = createdTime, lastUpdatedTime = null, @@ -56,7 +56,7 @@ data class Note( @Throws(IOException::class) override fun writeTo(out: StreamOutput) { out.writeString(id) - out.writeString(alertId) + out.writeString(entityId) out.writeString(content) out.writeInstant(createdTime) out.writeOptionalInstant(lastUpdatedTime) @@ -67,7 +67,7 @@ data class Note( fun asTemplateArg(): Map { return mapOf( _ID to id, - ALERT_ID_FIELD to alertId, + ENTITY_ID_FIELD to entityId, NOTE_CONTENT_FIELD to content, NOTE_CREATED_TIME_FIELD to createdTime, NOTE_LAST_UPDATED_TIME_FIELD to lastUpdatedTime, @@ -87,7 +87,7 @@ data class Note( private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder { builder.startObject() - .field(ALERT_ID_FIELD, alertId) + .field(ENTITY_ID_FIELD, entityId) .field(NOTE_CONTENT_FIELD, content) .optionalTimeField(NOTE_CREATED_TIME_FIELD, createdTime) .optionalTimeField(NOTE_LAST_UPDATED_TIME_FIELD, lastUpdatedTime) @@ -105,7 +105,7 @@ data class Note( } companion object { - const val ALERT_ID_FIELD = "alert_id" + const val ENTITY_ID_FIELD = "entity_id" const val NOTE_CONTENT_FIELD = "content" const val NOTE_CREATED_TIME_FIELD = "created_time" const val NOTE_LAST_UPDATED_TIME_FIELD = "last_updated_time" @@ -116,7 +116,7 @@ data class Note( @JvmOverloads @Throws(IOException::class) fun parse(xcp: XContentParser, id: String = NO_ID): Note { - lateinit var alertId: String + lateinit var entityId: String var content = "" lateinit var createdTime: Instant var lastUpdatedTime: Instant? = null @@ -128,7 +128,7 @@ data class Note( xcp.nextToken() when (fieldName) { - ALERT_ID_FIELD -> alertId = xcp.text() + ENTITY_ID_FIELD -> entityId = xcp.text() NOTE_CONTENT_FIELD -> content = xcp.text() NOTE_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant()) NOTE_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant() @@ -143,7 +143,7 @@ data class Note( return Note( id = id, - alertId = requireNotNull(alertId) { "Alert ID is null" }, + entityId = entityId, content = content, createdTime = createdTime, lastUpdatedTime = lastUpdatedTime, From 2c80bc18c45a14795708371710e6660931aa84b9 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Wed, 29 May 2024 13:39:23 -0700 Subject: [PATCH 04/10] import cleanup Signed-off-by: Dennis Toepker --- .../kotlin/org/opensearch/commons/alerting/model/Note.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt index 11f62b1a..8ac2e616 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt @@ -3,7 +3,9 @@ package org.opensearch.commons.alerting.model import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID import org.opensearch.commons.alerting.util.instant import org.opensearch.commons.alerting.util.optionalTimeField +import org.opensearch.commons.alerting.util.optionalUserField import org.opensearch.commons.alerting.util.optionalUsernameField +import org.opensearch.commons.authuser.User import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.core.common.io.stream.Writeable @@ -13,9 +15,6 @@ import org.opensearch.core.xcontent.XContentParser import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken import java.io.IOException import java.time.Instant -import org.apache.logging.log4j.LogManager -import org.opensearch.commons.alerting.util.optionalUserField -import org.opensearch.commons.authuser.User data class Note( val id: String = NO_ID, From e770578fdf58f92c52b476c74937137b5d5ccc0e Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Tue, 4 Jun 2024 16:37:19 -0700 Subject: [PATCH 05/10] changing name from Alerting Notes to Alerting Comments Signed-off-by: Dennis Toepker --- .../alerting/action/AlertingActions.kt | 18 +++---- ...NoteRequest.kt => DeleteCommentRequest.kt} | 12 ++--- ...teResponse.kt => DeleteCommentResponse.kt} | 2 +- ...xNoteRequest.kt => IndexCommentRequest.kt} | 12 ++--- ...oteResponse.kt => IndexCommentResponse.kt} | 16 +++--- ...NoteRequest.kt => SearchCommentRequest.kt} | 2 +- .../alerting/model/{Note.kt => Comment.kt} | 52 +++++++++---------- .../commons/alerting/model/DataSources.kt | 31 ++++++++--- 8 files changed, 80 insertions(+), 65 deletions(-) rename src/main/kotlin/org/opensearch/commons/alerting/action/{DeleteNoteRequest.kt => DeleteCommentRequest.kt} (71%) rename src/main/kotlin/org/opensearch/commons/alerting/action/{DeleteNoteResponse.kt => DeleteCommentResponse.kt} (94%) rename src/main/kotlin/org/opensearch/commons/alerting/action/{IndexNoteRequest.kt => IndexCommentRequest.kt} (87%) rename src/main/kotlin/org/opensearch/commons/alerting/action/{IndexNoteResponse.kt => IndexCommentResponse.kt} (84%) rename src/main/kotlin/org/opensearch/commons/alerting/action/{SearchNoteRequest.kt => SearchCommentRequest.kt} (94%) rename src/main/kotlin/org/opensearch/commons/alerting/model/{Note.kt => Comment.kt} (71%) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt index 8b4f9d38..fbd798b7 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt @@ -21,9 +21,9 @@ object AlertingActions { const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe" const val GET_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/get" const val SEARCH_MONITORS_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/search" - const val INDEX_NOTE_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/write" - const val SEARCH_NOTES_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/search" - const val DELETE_NOTES_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/notes/delete" + const val INDEX_COMMENT_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/write" + const val SEARCH_COMMENTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/search" + const val DELETE_COMMENT_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/delete" @JvmField val INDEX_MONITOR_ACTION_TYPE = @@ -78,14 +78,14 @@ object AlertingActions { ActionType(SEARCH_MONITORS_ACTION_NAME, ::SearchResponse) @JvmField - val INDEX_NOTE_ACTION_TYPE = - ActionType(INDEX_NOTE_ACTION_NAME, ::IndexNoteResponse) + val INDEX_COMMENT_ACTION_TYPE = + ActionType(INDEX_COMMENT_ACTION_NAME, ::IndexCommentResponse) @JvmField - val SEARCH_NOTES_ACTION_TYPE = - ActionType(SEARCH_NOTES_ACTION_NAME, ::SearchResponse) + val SEARCH_COMMENTS_ACTION_TYPE = + ActionType(SEARCH_COMMENTS_ACTION_NAME, ::SearchResponse) @JvmField - val DELETE_NOTES_ACTION_TYPE = - ActionType(DELETE_NOTES_ACTION_NAME, ::DeleteNoteResponse) + val DELETE_COMMENT_ACTION_TYPE = + ActionType(DELETE_COMMENT_ACTION_NAME, ::DeleteCommentResponse) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt similarity index 71% rename from src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt rename to src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt index 4e91f6be..48c904bf 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt @@ -6,16 +6,16 @@ import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput import java.io.IOException -class DeleteNoteRequest : ActionRequest { - val noteId: String +class DeleteCommentRequest : ActionRequest { + val commentId: String - constructor(noteId: String) : super() { - this.noteId = noteId + constructor(commentId: String) : super() { + this.commentId = commentId } @Throws(IOException::class) constructor(sin: StreamInput) : this( - noteId = sin.readString() + commentId = sin.readString() ) override fun validate(): ActionRequestValidationException? { @@ -24,6 +24,6 @@ class DeleteNoteRequest : ActionRequest { @Throws(IOException::class) override fun writeTo(out: StreamOutput) { - out.writeString(noteId) + out.writeString(commentId) } } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt similarity index 94% rename from src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt rename to src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt index db6b7d0a..44bb558c 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteNoteResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt @@ -7,7 +7,7 @@ import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder -class DeleteNoteResponse : BaseResponse { +class DeleteCommentResponse : BaseResponse { var id: String constructor( diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt similarity index 87% rename from src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt rename to src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt index b01ccdf9..ad7ad9d5 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt @@ -7,9 +7,9 @@ import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.rest.RestRequest import java.io.IOException -class IndexNoteRequest : ActionRequest { +class IndexCommentRequest : ActionRequest { val entityId: String - val noteId: String + val commentId: String val seqNo: Long val primaryTerm: Long val method: RestRequest.Method @@ -17,14 +17,14 @@ class IndexNoteRequest : ActionRequest { constructor( entityId: String, - noteId: String, + commentId: String, seqNo: Long, primaryTerm: Long, method: RestRequest.Method, content: String ) : super() { this.entityId = entityId - this.noteId = noteId + this.commentId = commentId this.seqNo = seqNo this.primaryTerm = primaryTerm this.method = method @@ -34,7 +34,7 @@ class IndexNoteRequest : ActionRequest { @Throws(IOException::class) constructor(sin: StreamInput) : this( entityId = sin.readString(), - noteId = sin.readString(), + commentId = sin.readString(), seqNo = sin.readLong(), primaryTerm = sin.readLong(), method = sin.readEnum(RestRequest.Method::class.java), @@ -48,7 +48,7 @@ class IndexNoteRequest : ActionRequest { @Throws(IOException::class) override fun writeTo(out: StreamOutput) { out.writeString(entityId) - out.writeString(noteId) + out.writeString(commentId) out.writeLong(seqNo) out.writeLong(primaryTerm) out.writeEnum(method) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt similarity index 84% rename from src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt rename to src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt index 55bb7be8..7eb41487 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexNoteResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt @@ -1,6 +1,5 @@ package org.opensearch.commons.alerting.action -import org.opensearch.commons.alerting.model.Note import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO @@ -10,24 +9,25 @@ import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import java.io.IOException +import org.opensearch.commons.alerting.model.Comment -class IndexNoteResponse : BaseResponse { +class IndexCommentResponse : BaseResponse { // TODO: do we really need sequence num and primary term? probs delete em var id: String var seqNo: Long var primaryTerm: Long - var note: Note + var comment: Comment constructor( id: String, seqNo: Long, primaryTerm: Long, - note: Note + comment: Comment ) : super() { this.id = id this.seqNo = seqNo this.primaryTerm = primaryTerm - this.note = note + this.comment = comment } @Throws(IOException::class) @@ -35,7 +35,7 @@ class IndexNoteResponse : BaseResponse { sin.readString(), // id sin.readLong(), // seqNo sin.readLong(), // primaryTerm - Note.readFrom(sin) // note + Comment.readFrom(sin) // comment ) @Throws(IOException::class) @@ -43,7 +43,7 @@ class IndexNoteResponse : BaseResponse { out.writeString(id) out.writeLong(seqNo) out.writeLong(primaryTerm) - note.writeTo(out) + comment.writeTo(out) } @Throws(IOException::class) @@ -52,7 +52,7 @@ class IndexNoteResponse : BaseResponse { .field(_ID, id) .field(_SEQ_NO, seqNo) .field(_PRIMARY_TERM, primaryTerm) - .field("note", note) + .field("comment", comment) .endObject() } } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequest.kt similarity index 94% rename from src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt rename to src/main/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequest.kt index 4b7d671a..e0d150d0 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/SearchNoteRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequest.kt @@ -7,7 +7,7 @@ import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput import java.io.IOException -class SearchNoteRequest : ActionRequest { +class SearchCommentRequest : ActionRequest { val searchRequest: SearchRequest diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt similarity index 71% rename from src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt rename to src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt index 8ac2e616..c605c500 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Note.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt @@ -16,7 +16,7 @@ import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken import java.io.IOException import java.time.Instant -data class Note( +data class Comment( val id: String = NO_ID, val entityId: String = NO_ID, val content: String, @@ -67,19 +67,19 @@ data class Note( return mapOf( _ID to id, ENTITY_ID_FIELD to entityId, - NOTE_CONTENT_FIELD to content, - NOTE_CREATED_TIME_FIELD to createdTime, - NOTE_LAST_UPDATED_TIME_FIELD to lastUpdatedTime, - NOTE_USER_FIELD to user?.name + COMMENT_CONTENT_FIELD to content, + COMMENT_CREATED_TIME_FIELD to createdTime, + COMMENT_LAST_UPDATED_TIME_FIELD to lastUpdatedTime, + COMMENT_USER_FIELD to user?.name ) } - // used to create the Note JSON object for an API response (displayed to user) + // used to create the Comment JSON object for an API response (displayed to user) override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { return createXContentBuilder(builder, false) } - // used to create the Note JSON object for indexing a doc into an index (not displayed to user) + // used to create the Comment JSON object for indexing a doc into an index (not displayed to user) fun toXContentWithUser(builder: XContentBuilder): XContentBuilder { return createXContentBuilder(builder, true) } @@ -87,16 +87,16 @@ data class Note( private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder { builder.startObject() .field(ENTITY_ID_FIELD, entityId) - .field(NOTE_CONTENT_FIELD, content) - .optionalTimeField(NOTE_CREATED_TIME_FIELD, createdTime) - .optionalTimeField(NOTE_LAST_UPDATED_TIME_FIELD, lastUpdatedTime) + .field(COMMENT_CONTENT_FIELD, content) + .optionalTimeField(COMMENT_CREATED_TIME_FIELD, createdTime) + .optionalTimeField(COMMENT_LAST_UPDATED_TIME_FIELD, lastUpdatedTime) if (includeFullUser) { - // if we're storing a Note into an internal index, include full User - builder.optionalUserField(NOTE_USER_FIELD, user) + // if we're storing a Comment into an internal index, include full User + builder.optionalUserField(COMMENT_USER_FIELD, user) } else { - // if we're displaying the Note as part of an API call response, only include username - builder.optionalUsernameField(NOTE_USER_FIELD, user) + // if we're displaying the Comment as part of an API call response, only include username + builder.optionalUsernameField(COMMENT_USER_FIELD, user) } builder.endObject() @@ -105,16 +105,16 @@ data class Note( companion object { const val ENTITY_ID_FIELD = "entity_id" - const val NOTE_CONTENT_FIELD = "content" - const val NOTE_CREATED_TIME_FIELD = "created_time" - const val NOTE_LAST_UPDATED_TIME_FIELD = "last_updated_time" - const val NOTE_USER_FIELD = "user" + const val COMMENT_CONTENT_FIELD = "content" + const val COMMENT_CREATED_TIME_FIELD = "created_time" + const val COMMENT_LAST_UPDATED_TIME_FIELD = "last_updated_time" + const val COMMENT_USER_FIELD = "user" const val NO_ID = "" @JvmStatic @JvmOverloads @Throws(IOException::class) - fun parse(xcp: XContentParser, id: String = NO_ID): Note { + fun parse(xcp: XContentParser, id: String = NO_ID): Comment { lateinit var entityId: String var content = "" lateinit var createdTime: Instant @@ -128,10 +128,10 @@ data class Note( when (fieldName) { ENTITY_ID_FIELD -> entityId = xcp.text() - NOTE_CONTENT_FIELD -> content = xcp.text() - NOTE_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant()) - NOTE_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant() - NOTE_USER_FIELD -> + COMMENT_CONTENT_FIELD -> content = xcp.text() + COMMENT_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant()) + COMMENT_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant() + COMMENT_USER_FIELD -> user = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { null } else { @@ -140,7 +140,7 @@ data class Note( } } - return Note( + return Comment( id = id, entityId = entityId, content = content, @@ -152,8 +152,8 @@ data class Note( @JvmStatic @Throws(IOException::class) - fun readFrom(sin: StreamInput): Note { - return Note(sin) + fun readFrom(sin: StreamInput): Comment { + return Comment(sin) } } } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt index 4695c241..8ece4127 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt @@ -31,15 +31,11 @@ data class DataSources( /** Configures a custom index pattern for alertHistoryIndex alias.*/ val alertsHistoryIndexPattern: String? = "<.opendistro-alerting-alert-history-{now/d}-1>", // AlertIndices.ALERT_HISTORY_INDEX_PATTERN - /** Configures a custom index to store notes associated with an alert. Creates a new index if index with given name not present. - * If index is pre-existing, mapping is updated. */ - val notesIndex: String = ".opensearch-alerting-notes", // NotesIndices.NOTES_INDEX - - /** Configures a custom index alias to store historical notes associated with historical alerts.*/ - val notesHistoryIndex: String? = ".opensearch-alerting-notes-history-write", // AlertIndices.NOTES_HISTORY_WRITE_INDEX + /** Configures a custom index alias to store historical comments associated with historical alerts.*/ + val commentsIndex: String = ".opensearch-alerting-comments-history-write", // AlertIndices.COMMENTS_HISTORY_WRITE_INDEX - /** Configures a custom index pattern for notesHistoryIndex alias.*/ - val notesHistoryIndexPattern: String? = "<.opensearch-alerting-notes-history-{now/d}-1>", // AlertIndices.NOTES_HISTORY_INDEX_PATTERN + /** Configures a custom index pattern for commentsHistoryIndex alias.*/ + val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // AlertIndices.COMMENTS_HISTORY_INDEX_PATTERN /** Configures custom mappings by field type for query index. * Custom query index mappings are configurable, only if a custom query index is configured too. */ @@ -60,6 +56,9 @@ data class DataSources( require(alertsIndex.isNotEmpty()) { "Alerts index cannot be empty" } + require(commentsIndex.isNotEmpty()) { + "Comments index cannot be empty" + } if (queryIndexMappingsByType.isNotEmpty()) { require(queryIndex != ScheduledJob.DOC_LEVEL_QUERIES_INDEX) { "Custom query index mappings are configurable only if a custom query index is configured too." @@ -84,6 +83,8 @@ data class DataSources( alertsIndex = sin.readString(), alertsHistoryIndex = sin.readOptionalString(), alertsHistoryIndexPattern = sin.readOptionalString(), + commentsIndex = sin.readString(), + commentsIndexPattern = sin.readOptionalString(), queryIndexMappingsByType = sin.readMap() as Map>, findingsEnabled = sin.readOptionalBoolean() ) @@ -97,6 +98,8 @@ data class DataSources( ALERTS_INDEX_FIELD to alertsIndex, ALERTS_HISTORY_INDEX_FIELD to alertsHistoryIndex, ALERTS_HISTORY_INDEX_PATTERN_FIELD to alertsHistoryIndexPattern, + COMMENTS_INDEX_FIELD to commentsIndex, + COMMENTS_INDEX_PATTERN_FIELD to commentsIndexPattern, QUERY_INDEX_MAPPINGS_BY_TYPE to queryIndexMappingsByType, FINDINGS_ENABLED_FIELD to findingsEnabled ) @@ -110,6 +113,8 @@ data class DataSources( builder.field(ALERTS_INDEX_FIELD, alertsIndex) builder.field(ALERTS_HISTORY_INDEX_FIELD, alertsHistoryIndex) builder.field(ALERTS_HISTORY_INDEX_PATTERN_FIELD, alertsHistoryIndexPattern) + builder.field(COMMENTS_INDEX_FIELD, commentsIndex) + builder.field(COMMENTS_INDEX_PATTERN_FIELD, commentsIndexPattern) builder.field(QUERY_INDEX_MAPPINGS_BY_TYPE, queryIndexMappingsByType as Map) builder.field(FINDINGS_ENABLED_FIELD, findingsEnabled) builder.endObject() @@ -123,6 +128,8 @@ data class DataSources( const val ALERTS_INDEX_FIELD = "alerts_index" const val ALERTS_HISTORY_INDEX_FIELD = "alerts_history_index" const val ALERTS_HISTORY_INDEX_PATTERN_FIELD = "alerts_history_index_pattern" + const val COMMENTS_INDEX_FIELD = "comments_index" + const val COMMENTS_INDEX_PATTERN_FIELD = "comments_index_pattern" const val QUERY_INDEX_MAPPINGS_BY_TYPE = "query_index_mappings_by_type" const val FINDINGS_ENABLED_FIELD = "findings_enabled" @@ -136,6 +143,8 @@ data class DataSources( var alertsIndex = "" var alertsHistoryIndex = "" var alertsHistoryIndexPattern = "" + var commentsIndex = "" + var commentsIndexPattern = "" var queryIndexMappingsByType: Map> = mapOf() var findingsEnabled = false @@ -151,6 +160,8 @@ data class DataSources( ALERTS_INDEX_FIELD -> alertsIndex = xcp.text() ALERTS_HISTORY_INDEX_FIELD -> alertsHistoryIndex = xcp.text() ALERTS_HISTORY_INDEX_PATTERN_FIELD -> alertsHistoryIndexPattern = xcp.text() + COMMENTS_INDEX_FIELD -> commentsIndex = xcp.text() + COMMENTS_INDEX_PATTERN_FIELD -> commentsIndexPattern = xcp.text() QUERY_INDEX_MAPPINGS_BY_TYPE -> queryIndexMappingsByType = xcp.map() as Map> FINDINGS_ENABLED_FIELD -> findingsEnabled = xcp.booleanValue() } @@ -162,6 +173,8 @@ data class DataSources( alertsIndex = alertsIndex, alertsHistoryIndex = alertsHistoryIndex, alertsHistoryIndexPattern = alertsHistoryIndexPattern, + commentsIndex = commentsIndex, + commentsIndexPattern = commentsIndexPattern, queryIndexMappingsByType = queryIndexMappingsByType, findingsEnabled = findingsEnabled ) @@ -176,6 +189,8 @@ data class DataSources( out.writeString(alertsIndex) out.writeOptionalString(alertsHistoryIndex) out.writeOptionalString(alertsHistoryIndexPattern) + out.writeString(commentsIndex) + out.writeString(commentsIndexPattern) out.writeMap(queryIndexMappingsByType as Map) out.writeOptionalBoolean(findingsEnabled) } From 0ac563e5e113ec0e5d766feed08b018a4f778255 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Tue, 4 Jun 2024 16:56:13 -0700 Subject: [PATCH 06/10] misc fixes and cleanup Signed-off-by: Dennis Toepker --- .../commons/alerting/action/IndexCommentResponse.kt | 2 +- .../org/opensearch/commons/alerting/model/DataSources.kt | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt index 7eb41487..dacf771c 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt @@ -1,5 +1,6 @@ package org.opensearch.commons.alerting.action +import org.opensearch.commons.alerting.model.Comment import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO @@ -9,7 +10,6 @@ import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import java.io.IOException -import org.opensearch.commons.alerting.model.Comment class IndexCommentResponse : BaseResponse { // TODO: do we really need sequence num and primary term? probs delete em diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt index 8ece4127..6ff38548 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt @@ -31,10 +31,10 @@ data class DataSources( /** Configures a custom index pattern for alertHistoryIndex alias.*/ val alertsHistoryIndexPattern: String? = "<.opendistro-alerting-alert-history-{now/d}-1>", // AlertIndices.ALERT_HISTORY_INDEX_PATTERN - /** Configures a custom index alias to store historical comments associated with historical alerts.*/ + /** Configures a custom index alias to store comments associated with alerts.*/ val commentsIndex: String = ".opensearch-alerting-comments-history-write", // AlertIndices.COMMENTS_HISTORY_WRITE_INDEX - /** Configures a custom index pattern for commentsHistoryIndex alias.*/ + /** Configures a custom index pattern for commentsIndex alias.*/ val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // AlertIndices.COMMENTS_HISTORY_INDEX_PATTERN /** Configures custom mappings by field type for query index. @@ -190,7 +190,7 @@ data class DataSources( out.writeOptionalString(alertsHistoryIndex) out.writeOptionalString(alertsHistoryIndexPattern) out.writeString(commentsIndex) - out.writeString(commentsIndexPattern) + out.writeOptionalString(commentsIndexPattern) out.writeMap(queryIndexMappingsByType as Map) out.writeOptionalBoolean(findingsEnabled) } From ae3754c1f7037bc8b98b7896b537ddc6c29a65d1 Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Wed, 5 Jun 2024 08:24:46 -0700 Subject: [PATCH 07/10] adding unit test coverage Signed-off-by: Dennis Toepker --- .../action/DeleteCommentRequestTests.kt | 22 ++++++++++ .../action/DeleteCommentResponseTests.kt | 22 ++++++++++ .../action/IndexCommentRequestTests.kt | 42 +++++++++++++++++++ .../action/IndexCommentResponseTests.kt | 34 +++++++++++++++ .../action/SearchCommentRequestTests.kt | 27 ++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequestTests.kt create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponseTests.kt create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequestTests.kt diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt new file mode 100644 index 00000000..e5d4ad77 --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt @@ -0,0 +1,22 @@ +package org.opensearch.commons.alerting.action + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.opensearch.common.io.stream.BytesStreamOutput +import org.opensearch.core.common.io.stream.StreamInput + +class DeleteCommentRequestTests { + @Test + fun `test delete comment request`() { + val req = DeleteCommentRequest("1234") + assertNotNull(req) + assertEquals("1234", req.commentId) + + val out = BytesStreamOutput() + req.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newReq = DeleteCommentRequest(sin) + assertEquals("1234", newReq.commentId) + } +} diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt new file mode 100644 index 00000000..7aef6cdf --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt @@ -0,0 +1,22 @@ +package org.opensearch.commons.alerting.action + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.opensearch.common.io.stream.BytesStreamOutput +import org.opensearch.core.common.io.stream.StreamInput + +class DeleteCommentResponseTests { + @Test + fun `test delete comment response`() { + val res = DeleteCommentResponse(id = "123") + assertNotNull(res) + assertEquals("123", res.id) + + val out = BytesStreamOutput() + res.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newRes = DeleteCommentResponse(sin) + assertEquals("123", newRes.id) + } +} diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequestTests.kt new file mode 100644 index 00000000..1ed540c4 --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequestTests.kt @@ -0,0 +1,42 @@ +package org.opensearch.commons.alerting.action + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test +import org.opensearch.common.io.stream.BytesStreamOutput +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.rest.RestRequest + +class IndexCommentRequestTests { + @Test + fun `test index comment post request`() { + val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.POST, "comment") + assertNotNull(req) + val out = BytesStreamOutput() + req.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newReq = IndexCommentRequest(sin) + assertEquals("123", newReq.entityId) + assertEquals("456", newReq.commentId) + assertEquals(1L, newReq.seqNo) + assertEquals(2L, newReq.primaryTerm) + assertEquals(RestRequest.Method.POST, newReq.method) + assertEquals("comment", newReq.content) + } + + @Test + fun `test index comment put request`() { + val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.PUT, "comment") + assertNotNull(req) + val out = BytesStreamOutput() + req.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newReq = IndexCommentRequest(sin) + assertEquals("123", newReq.entityId) + assertEquals("456", newReq.commentId) + assertEquals(1L, newReq.seqNo) + assertEquals(2L, newReq.primaryTerm) + assertEquals(RestRequest.Method.PUT, newReq.method) + assertEquals("comment", newReq.content) + } +} diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponseTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponseTests.kt new file mode 100644 index 00000000..834cc972 --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponseTests.kt @@ -0,0 +1,34 @@ +package org.opensearch.commons.alerting.action + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.opensearch.common.io.stream.BytesStreamOutput +import org.opensearch.commons.alerting.model.Comment +import org.opensearch.commons.alerting.randomUser +import org.opensearch.core.common.io.stream.StreamInput +import java.time.Instant + +class IndexCommentResponseTests { + @Test + fun `test index comment response with comment`() { + val comment = Comment( + "123", + "456", + "comment", + Instant.now(), + Instant.now(), + randomUser() + ) + val req = IndexCommentResponse("1234", 1L, 2L, comment) + Assertions.assertNotNull(req) + + val out = BytesStreamOutput() + req.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newReq = IndexCommentResponse(sin) + Assertions.assertEquals("1234", newReq.id) + Assertions.assertEquals(1L, newReq.seqNo) + Assertions.assertEquals(2L, newReq.primaryTerm) + Assertions.assertNotNull(newReq.comment) + } +} diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequestTests.kt new file mode 100644 index 00000000..596d16c4 --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/SearchCommentRequestTests.kt @@ -0,0 +1,27 @@ +package org.opensearch.commons.alerting.action + +import org.opensearch.action.search.SearchRequest +import org.opensearch.common.io.stream.BytesStreamOutput +import org.opensearch.common.unit.TimeValue +import org.opensearch.core.common.io.stream.StreamInput +import org.opensearch.search.builder.SearchSourceBuilder +import org.opensearch.test.OpenSearchTestCase +import org.opensearch.test.rest.OpenSearchRestTestCase +import java.util.concurrent.TimeUnit + +class SearchCommentRequestTests : OpenSearchTestCase() { + fun `test search comments request`() { + val searchSourceBuilder = SearchSourceBuilder().from(0).size(100).timeout(TimeValue(60, TimeUnit.SECONDS)) + val searchRequest = SearchRequest().indices(OpenSearchRestTestCase.randomAlphaOfLength(10)).source(searchSourceBuilder) + val searchCommentRequest = SearchCommentRequest(searchRequest) + assertNotNull(searchCommentRequest) + + val out = BytesStreamOutput() + searchCommentRequest.writeTo(out) + val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) + val newReq = SearchCommentRequest(sin) + + assertNotNull(newReq.searchRequest) + assertEquals(1, newReq.searchRequest.indices().size) + } +} From 3b3566e4dc5eacd109a935de8af77e8406bbf0ca Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Wed, 5 Jun 2024 16:06:07 -0700 Subject: [PATCH 08/10] misc cleanup Signed-off-by: Dennis Toepker --- .../alerting/action/DeleteCommentRequest.kt | 3 +++ .../alerting/action/DeleteCommentResponse.kt | 10 +++++----- .../commons/alerting/action/IndexCommentRequest.kt | 14 ++++++++++++++ .../alerting/action/IndexCommentResponse.kt | 1 - .../alerting/action/DeleteCommentRequestTests.kt | 2 +- .../alerting/action/DeleteCommentResponseTests.kt | 6 +++--- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt index 48c904bf..096938b8 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt @@ -19,6 +19,9 @@ class DeleteCommentRequest : ActionRequest { ) override fun validate(): ActionRequestValidationException? { + if (commentId.isBlank()) { + return ActionRequestValidationException() + } return null } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt index 44bb558c..f00fe266 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponse.kt @@ -8,25 +8,25 @@ import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder class DeleteCommentResponse : BaseResponse { - var id: String + var commentId: String constructor( id: String ) : super() { - this.id = id + this.commentId = id } constructor(sin: StreamInput) : this( - sin.readString() // id + sin.readString() // commentId ) override fun writeTo(out: StreamOutput) { - out.writeString(id) + out.writeString(commentId) } override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { return builder.startObject() - .field(IndexUtils._ID, id) + .field(IndexUtils._ID, commentId) .endObject() } } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt index ad7ad9d5..ab62599d 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt @@ -7,6 +7,15 @@ import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.rest.RestRequest import java.io.IOException +/** + * Request to index/create a Comment + * + * entityId: the entity that the Comment is attached to and therefore associated with (e.g. in Alerting, + * the entity is an Alert). This field is expected to be non-blank if the request is to create a new Comment. + * + * commentId: the ID of an existing Comment. This field is expected to be non-blank if the request is to + * update an existing Comment. + */ class IndexCommentRequest : ActionRequest { val entityId: String val commentId: String @@ -42,6 +51,11 @@ class IndexCommentRequest : ActionRequest { ) override fun validate(): ActionRequestValidationException? { + if (method == RestRequest.Method.POST && entityId.isBlank() || + method == RestRequest.Method.PUT && commentId.isBlank() + ) { + return ActionRequestValidationException() + } return null } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt index dacf771c..7c9bb9b7 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentResponse.kt @@ -12,7 +12,6 @@ import org.opensearch.core.xcontent.XContentBuilder import java.io.IOException class IndexCommentResponse : BaseResponse { - // TODO: do we really need sequence num and primary term? probs delete em var id: String var seqNo: Long var primaryTerm: Long diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt index e5d4ad77..70a22953 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequestTests.kt @@ -8,7 +8,7 @@ import org.opensearch.core.common.io.stream.StreamInput class DeleteCommentRequestTests { @Test - fun `test delete comment request`() { + fun `test delete comment request writing and parsing`() { val req = DeleteCommentRequest("1234") assertNotNull(req) assertEquals("1234", req.commentId) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt index 7aef6cdf..f10067ac 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteCommentResponseTests.kt @@ -8,15 +8,15 @@ import org.opensearch.core.common.io.stream.StreamInput class DeleteCommentResponseTests { @Test - fun `test delete comment response`() { + fun `test delete comment response writing and parsing`() { val res = DeleteCommentResponse(id = "123") assertNotNull(res) - assertEquals("123", res.id) + assertEquals("123", res.commentId) val out = BytesStreamOutput() res.writeTo(out) val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) val newRes = DeleteCommentResponse(sin) - assertEquals("123", newRes.id) + assertEquals("123", newRes.commentId) } } From 9ad5a1537dc19400276116c83629f500683764ac Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Thu, 6 Jun 2024 11:58:17 -0700 Subject: [PATCH 09/10] misc review-based cleanup Signed-off-by: Dennis Toepker --- .../opensearch/commons/alerting/action/AlertingActions.kt | 6 +++--- .../kotlin/org/opensearch/commons/alerting/model/Comment.kt | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt index fbd798b7..ba4e33fe 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt @@ -21,9 +21,9 @@ object AlertingActions { const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe" const val GET_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/get" const val SEARCH_MONITORS_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/search" - const val INDEX_COMMENT_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/write" - const val SEARCH_COMMENTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/search" - const val DELETE_COMMENT_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/comments/delete" + const val INDEX_COMMENT_ACTION_NAME = "cluster:admin/opensearch/alerting/alerts/comments/write" + const val SEARCH_COMMENTS_ACTION_NAME = "cluster:admin/opensearch/alerting/alerts/comments/search" + const val DELETE_COMMENT_ACTION_NAME = "cluster:admin/opensearch/alerting/alerts/comments/delete" @JvmField val INDEX_MONITOR_ACTION_TYPE = diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt index c605c500..68ae7bf5 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt @@ -32,11 +32,7 @@ data class Comment( content = sin.readString(), createdTime = sin.readInstant(), lastUpdatedTime = sin.readOptionalInstant(), - user = if (sin.readBoolean()) { - User(sin) - } else { - null - } + user = if (sin.readBoolean()) User(sin) else null ) constructor( From a5d8a477a91763959b35c3aa5dc850bcbe7d74bb Mon Sep 17 00:00:00 2001 From: Dennis Toepker Date: Fri, 7 Jun 2024 11:39:15 -0700 Subject: [PATCH 10/10] added validation exception messages Signed-off-by: Dennis Toepker --- .../commons/alerting/action/DeleteCommentRequest.kt | 4 +++- .../opensearch/commons/alerting/action/IndexCommentRequest.kt | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt index 096938b8..811dcd9e 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/DeleteCommentRequest.kt @@ -20,7 +20,9 @@ class DeleteCommentRequest : ActionRequest { override fun validate(): ActionRequestValidationException? { if (commentId.isBlank()) { - return ActionRequestValidationException() + val exception = ActionRequestValidationException() + exception.addValidationError("comment id must not be blank") + return exception } return null } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt index ab62599d..6b66cb64 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/IndexCommentRequest.kt @@ -54,7 +54,9 @@ class IndexCommentRequest : ActionRequest { if (method == RestRequest.Method.POST && entityId.isBlank() || method == RestRequest.Method.PUT && commentId.isBlank() ) { - return ActionRequestValidationException() + val exception = ActionRequestValidationException() + exception.addValidationError("id must not be blank") + return exception } return null }