Skip to content

Commit

Permalink
adding ctor with optional comments indices args (opensearch-project#674)
Browse files Browse the repository at this point in the history
* adding ctor with optional comments indices args

Signed-off-by: Dennis Toepker <[email protected]>

* removing throws IOException declaration above new ctor

Signed-off-by: Dennis Toepker <[email protected]>

* making reads and writes optional

Signed-off-by: Dennis Toepker <[email protected]>

* quick comment change

Signed-off-by: Dennis Toepker <[email protected]>

* adding test for new ctor

Signed-off-by: Dennis Toepker <[email protected]>

---------

Signed-off-by: Dennis Toepker <[email protected]>
Co-authored-by: Dennis Toepker <[email protected]>
  • Loading branch information
toepkerd and toepkerd-zz authored Jun 10, 2024
1 parent 480590d commit bc215fd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ data class DataSources(
val alertsHistoryIndexPattern: String? = "<.opendistro-alerting-alert-history-{now/d}-1>", // AlertIndices.ALERT_HISTORY_INDEX_PATTERN

/** Configures a custom index alias to store comments associated with alerts.*/
val commentsIndex: String = ".opensearch-alerting-comments-history-write", // AlertIndices.COMMENTS_HISTORY_WRITE_INDEX
val commentsIndex: String? = ".opensearch-alerting-comments-history-write", // CommentsIndices.COMMENTS_HISTORY_WRITE_INDEX

/** Configures a custom index pattern for commentsIndex alias.*/
val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // AlertIndices.COMMENTS_HISTORY_INDEX_PATTERN
val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // CommentsIndices.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. */
Expand All @@ -56,9 +56,6 @@ 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."
Expand All @@ -83,12 +80,34 @@ data class DataSources(
alertsIndex = sin.readString(),
alertsHistoryIndex = sin.readOptionalString(),
alertsHistoryIndexPattern = sin.readOptionalString(),
commentsIndex = sin.readString(),
commentsIndex = sin.readOptionalString(),
commentsIndexPattern = sin.readOptionalString(),
queryIndexMappingsByType = sin.readMap() as Map<String, Map<String, String>>,
findingsEnabled = sin.readOptionalBoolean()
)

constructor(
queryIndex: String,
findingsIndex: String,
findingsIndexPattern: String?,
alertsIndex: String,
alertsHistoryIndex: String?,
alertsHistoryIndexPattern: String?,
queryIndexMappingsByType: Map<String, Map<String, String>>,
findingsEnabled: Boolean?
) : this(
queryIndex = queryIndex,
findingsIndex = findingsIndex,
findingsIndexPattern = findingsIndexPattern,
alertsIndex = alertsIndex,
alertsHistoryIndex = alertsHistoryIndex,
alertsHistoryIndexPattern = alertsHistoryIndexPattern,
commentsIndex = null,
commentsIndexPattern = null,
queryIndexMappingsByType = queryIndexMappingsByType,
findingsEnabled = findingsEnabled
)

@Suppress("UNCHECKED_CAST")
fun asTemplateArg(): Map<String, Any?> {
return mapOf(
Expand Down Expand Up @@ -189,7 +208,7 @@ data class DataSources(
out.writeString(alertsIndex)
out.writeOptionalString(alertsHistoryIndex)
out.writeOptionalString(alertsHistoryIndexPattern)
out.writeString(commentsIndex)
out.writeOptionalString(commentsIndex)
out.writeOptionalString(commentsIndexPattern)
out.writeMap(queryIndexMappingsByType as Map<String, Any>)
out.writeOptionalBoolean(findingsEnabled)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.opensearch.commons.alerting.model

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.opensearch.common.io.stream.BytesStreamOutput
import org.opensearch.core.common.io.stream.StreamInput

class DataSourcesTests {
@Test
fun `Test DataSources construction with no comments indices`() {
val dataSources = DataSources(
ScheduledJob.DOC_LEVEL_QUERIES_INDEX,
".opensearch-alerting-finding-history-write",
"<.opensearch-alerting-finding-history-{now/d}-1>",
".opendistro-alerting-alerts",
".opendistro-alerting-alert-history-write",
"<.opendistro-alerting-alert-history-{now/d}-1>",
mapOf(),
false
)
Assertions.assertNotNull(dataSources)

val out = BytesStreamOutput()
dataSources.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newDataSources = DataSources(sin)
Assertions.assertEquals(ScheduledJob.DOC_LEVEL_QUERIES_INDEX, newDataSources.queryIndex)
Assertions.assertEquals(".opensearch-alerting-finding-history-write", newDataSources.findingsIndex)
Assertions.assertEquals("<.opensearch-alerting-finding-history-{now/d}-1>", newDataSources.findingsIndexPattern)
Assertions.assertEquals(".opendistro-alerting-alerts", newDataSources.alertsIndex)
Assertions.assertEquals(".opendistro-alerting-alert-history-write", newDataSources.alertsHistoryIndex)
Assertions.assertEquals("<.opendistro-alerting-alert-history-{now/d}-1>", newDataSources.alertsHistoryIndexPattern)
Assertions.assertEquals(mapOf<String, Map<String, String>>(), newDataSources.queryIndexMappingsByType)
Assertions.assertEquals(false, newDataSources.findingsEnabled)
}
}

0 comments on commit bc215fd

Please sign in to comment.