diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionParser.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionParser.kt index d38c17496..d74e65bb6 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionParser.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionParser.kt @@ -54,14 +54,12 @@ class ShrinkActionParser : ActionParser() { TARGET_INDEX_TEMPLATE_FIELD -> targetIndexTemplate = Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG) ALIASES_FIELD -> { if (xcp.currentToken() != XContentParser.Token.VALUE_NULL) { + ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp) aliases = mutableListOf() - when (xcp.currentToken()) { - XContentParser.Token.START_OBJECT -> { - while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { - aliases.add(Alias.fromXContent(xcp)) - } - } - else -> ensureExpectedToken(XContentParser.Token.START_ARRAY, xcp.currentToken(), xcp) + while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { + ensureExpectedToken(XContentParser.Token.FIELD_NAME, xcp.nextToken(), xcp) + aliases.add(Alias.fromXContent(xcp)) + ensureExpectedToken(XContentParser.Token.END_OBJECT, xcp.nextToken(), xcp) } } } diff --git a/src/main/kotlin/org/opensearch/indexmanagement/opensearchapi/OpenSearchExtensions.kt b/src/main/kotlin/org/opensearch/indexmanagement/opensearchapi/OpenSearchExtensions.kt index 8d6c23a6e..4288c88cf 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/opensearchapi/OpenSearchExtensions.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/opensearchapi/OpenSearchExtensions.kt @@ -83,9 +83,13 @@ fun XContentParser.instant(): Instant? { } fun XContentBuilder.aliasesField(aliases: List): XContentBuilder { - val builder = this.startObject(ShrinkAction.ALIASES_FIELD) - aliases.forEach { it.toXContent(builder, ToXContent.EMPTY_PARAMS) } - return builder.endObject() + val builder = this.startArray(ShrinkAction.ALIASES_FIELD) + aliases.forEach { + builder.startObject() + it.toXContent(builder, ToXContent.EMPTY_PARAMS) + builder.endObject() + } + return builder.endArray() } fun XContentBuilder.optionalTimeField(name: String, instant: Instant?): XContentBuilder { diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt index 1f563ed50..686a09265 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt @@ -6,9 +6,11 @@ package org.opensearch.indexmanagement.indexstatemanagement.action import org.apache.logging.log4j.LogManager +import org.opensearch.action.admin.indices.alias.Alias import org.opensearch.cluster.metadata.IndexMetadata import org.opensearch.common.settings.Settings import org.opensearch.common.unit.ByteSizeValue +import org.opensearch.index.query.QueryBuilders import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase import org.opensearch.indexmanagement.indexstatemanagement.model.Policy import org.opensearch.indexmanagement.indexstatemanagement.model.State @@ -37,7 +39,7 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { maxShardSize = null, percentageOfSourceShards = null, targetIndexTemplate = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}$testIndexSuffix", mapOf()), - aliases = null, + aliases = listOf(Alias("test-alias1"), Alias("test-alias2").filter(QueryBuilders.termQuery("foo", "bar")).writeIndex(true)), forceUnsafe = true, index = 0 ) @@ -116,6 +118,8 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { getExplainManagedIndexMetaData(indexName).info?.get("message") ) assertEquals("Write block setting was not reset after successful shrink", "true", getIndexBlocksWriteSetting(indexName)) + val aliases = getAlias(targetIndexName, "") + assertTrue("Aliases were not added to shrunken index", aliases.containsKey("test-alias1") && aliases.containsKey("test-alias2")) } } @@ -130,7 +134,7 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { maxShardSize = testMaxShardSize, percentageOfSourceShards = null, targetIndexTemplate = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}$testIndexSuffix", mapOf()), - aliases = null, + aliases = listOf(Alias("max-shard-alias")), forceUnsafe = true, index = 0 ) @@ -206,6 +210,8 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { val indexSettings = getIndexSettings(indexName) as Map>> val writeBlock = indexSettings[indexName]!!["settings"]!![IndexMetadata.SETTING_BLOCKS_WRITE] as String? assertNull("Write block setting was not reset after successful shrink", writeBlock) + val aliases = getAlias(targetIndexName, "") + assertTrue("Alias was not added to shrunken index", aliases.containsKey("max-shard-alias")) } }