From dd9fdf1f245da03f257c096a49c17494e18c6025 Mon Sep 17 00:00:00 2001 From: toepkerd <120457569+toepkerd@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:21:13 -0700 Subject: [PATCH] Fixing DataSources Parsing Errors (#678) (#681) * attempt to fix DataSources breaking change Signed-off-by: Dennis Toepker * adding default comments index values instead of passing null into constructor Signed-off-by: Dennis Toepker --------- Signed-off-by: Dennis Toepker Co-authored-by: Dennis Toepker (cherry picked from commit f4b2e1b9de2b660b66815610b214ffd1f4ec579c) Signed-off-by: toepkerd <120457569+toepkerd@users.noreply.github.com> --- .../commons/alerting/model/DataSources.kt | 12 ++++++++---- .../commons/alerting/model/XContentTests.kt | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) 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 110cf36f..7e995b53 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt @@ -32,10 +32,11 @@ 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", // CommentsIndices.COMMENTS_HISTORY_WRITE_INDEX + + val commentsIndex: String? = DEFAULT_COMMENTS_INDEX, // CommentsIndices.COMMENTS_HISTORY_WRITE_INDEX /** Configures a custom index pattern for commentsIndex alias.*/ - val commentsIndexPattern: String? = "<.opensearch-alerting-comments-history-{now/d}-1>", // CommentsIndices.COMMENTS_HISTORY_INDEX_PATTERN + val commentsIndexPattern: String? = DEFAULT_COMMENTS_INDEX_PATTERN, // 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. */ @@ -102,8 +103,8 @@ data class DataSources( alertsIndex = alertsIndex, alertsHistoryIndex = alertsHistoryIndex, alertsHistoryIndexPattern = alertsHistoryIndexPattern, - commentsIndex = null, - commentsIndexPattern = null, + commentsIndex = DEFAULT_COMMENTS_INDEX, + commentsIndexPattern = DEFAULT_COMMENTS_INDEX_PATTERN, queryIndexMappingsByType = queryIndexMappingsByType, findingsEnabled = findingsEnabled ) @@ -152,6 +153,9 @@ data class DataSources( const val QUERY_INDEX_MAPPINGS_BY_TYPE = "query_index_mappings_by_type" const val FINDINGS_ENABLED_FIELD = "findings_enabled" + const val DEFAULT_COMMENTS_INDEX = ".opensearch-alerting-comments-history-write" + const val DEFAULT_COMMENTS_INDEX_PATTERN = "<.opensearch-alerting-comments-history-{now/d}-1>" + @JvmStatic @Throws(IOException::class) @Suppress("UNCHECKED_CAST") diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt index 065191fb..ba0d5180 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt @@ -506,4 +506,23 @@ class XContentTests { assertEquals("Round tripping alert doesn't work", actionExecutionResult, parsedActionExecutionResultString) } + + @Test + fun `test DataSources parsing`() { + 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 dataSourcesString = dataSources.toXContent(builder(), ToXContent.EMPTY_PARAMS).string() + val parsedDataSources = DataSources.parse(parser(dataSourcesString)) + Assertions.assertEquals(dataSources, parsedDataSources, "Round tripping DataSources doesn't work") + } }