From 1e459fdf5421ae794259a4afb9ceae0984f9b519 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 21 Jul 2021 09:34:23 +0200 Subject: [PATCH] Fix the use of wildcard expressions for data streams in update aliases api (#75526) Prior to this change, supplying a wildcard expression in the `indices` field of an alias action would always result in a 404, despite data streams existing that could match with the provided wildcard expression. Closes #75456 --- .../indices/alias/IndicesAliasesRequest.java | 20 ++++++++- .../IndexNameExpressionResolverTests.java | 15 +++---- .../data_stream/140_data_stream_aliases.yml | 42 +++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java index 42644a9260056..7d2646e25ec69 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java @@ -11,6 +11,7 @@ import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.AliasesRequest; +import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.cluster.metadata.AliasAction; @@ -48,7 +49,7 @@ /** * A request to add/remove aliases for one or more indices. */ -public class IndicesAliasesRequest extends AcknowledgedRequest implements ToXContentObject { +public class IndicesAliasesRequest extends AcknowledgedRequest implements IndicesRequest, ToXContentObject { private List allAliasActions = new ArrayList<>(); private String origin = ""; @@ -482,6 +483,11 @@ public String[] indices() { return indices; } + @Override + public boolean includeDataStreams() { + return true; + } + @Override public IndicesOptions indicesOptions() { return INDICES_OPTIONS; @@ -615,6 +621,18 @@ public IndicesOptions indicesOptions() { return INDICES_OPTIONS; } + @Override + public String[] indices() { + return allAliasActions.stream() + .flatMap(aliasActions -> Arrays.stream(aliasActions.indices())) + .toArray(String[]::new); + } + + @Override + public boolean includeDataStreams() { + return true; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java index 5adeabc3cd471..2d46154f5aba5 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java @@ -1796,24 +1796,21 @@ public void testIndicesAliasesRequestTargetDataStreams() { { IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName); - IndexNotFoundException iae = expectThrows(IndexNotFoundException.class, - () -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions)); - assertEquals("no such index [" + dataStreamName + "]", iae.getMessage()); + assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions), + arrayContaining(backingIndexEqualTo(dataStreamName, 1))); } { IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index("my-data-*").alias("my-data"); - IndexNotFoundException iae = expectThrows(IndexNotFoundException.class, - () -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions)); - assertEquals("no such index [my-data-*]", iae.getMessage()); + assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions), + arrayContaining(backingIndexEqualTo(dataStreamName, 1))); } { IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName) .alias("my-data"); - IndexNotFoundException iae = expectThrows(IndexNotFoundException.class, - () -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions)); - assertEquals("no such index [" + dataStreamName + "]", iae.getMessage()); + assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions), + arrayContaining(backingIndexEqualTo(dataStreamName, 1))); } } diff --git a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml index 0bf6ffefda027..cf7826f584eff 100644 --- a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml +++ b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml @@ -152,3 +152,45 @@ search: index: app1-zone-b - length: { hits.hits: 1 } + +--- +"Create data stream aliases using wildcard expression": + - skip: + version: " - 7.99.99" + reason: "bugfix has not yet backported to the 7.x branch" + features: allowed_warnings + + - do: + allowed_warnings: + - "index template [my-template] has index patterns [log-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template] will take precedence during new index creation" + indices.put_index_template: + name: my-template + body: + index_patterns: [ log-* ] + template: + settings: + index.number_of_replicas: 0 + data_stream: { } + + - do: + indices.create_data_stream: + name: log-foobar + - is_true: acknowledged + + - do: + indices.update_aliases: + body: + actions: + - add: + index: log-* + alias: my-alias + - is_true: acknowledged + + - do: + indices.get_data_stream: + name: "*" + - match: { data_streams.0.name: log-foobar } + + - do: + indices.get_alias: {} + - match: {log-foobar.aliases.my-alias: {}}