Skip to content

Commit 641e051

Browse files
authored
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
1 parent c519b93 commit 641e051

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.ElasticsearchGenerationException;
1212
import org.elasticsearch.action.ActionRequestValidationException;
1313
import org.elasticsearch.action.AliasesRequest;
14+
import org.elasticsearch.action.IndicesRequest;
1415
import org.elasticsearch.action.support.IndicesOptions;
1516
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1617
import org.elasticsearch.cluster.metadata.AliasAction;
@@ -48,7 +49,7 @@
4849
/**
4950
* A request to add/remove aliases for one or more indices.
5051
*/
51-
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements ToXContentObject {
52+
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest, ToXContentObject {
5253

5354
private List<AliasActions> allAliasActions = new ArrayList<>();
5455
private String origin = "";
@@ -482,6 +483,11 @@ public String[] indices() {
482483
return indices;
483484
}
484485

486+
@Override
487+
public boolean includeDataStreams() {
488+
return true;
489+
}
490+
485491
@Override
486492
public IndicesOptions indicesOptions() {
487493
return INDICES_OPTIONS;
@@ -615,6 +621,18 @@ public IndicesOptions indicesOptions() {
615621
return INDICES_OPTIONS;
616622
}
617623

624+
@Override
625+
public String[] indices() {
626+
return allAliasActions.stream()
627+
.flatMap(aliasActions -> Arrays.stream(aliasActions.indices()))
628+
.toArray(String[]::new);
629+
}
630+
631+
@Override
632+
public boolean includeDataStreams() {
633+
return true;
634+
}
635+
618636
@Override
619637
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
620638
builder.startObject();

server/src/test/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolverTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,24 +1796,21 @@ public void testIndicesAliasesRequestTargetDataStreams() {
17961796

17971797
{
17981798
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName);
1799-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1800-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1801-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1799+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1800+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18021801
}
18031802

18041803
{
18051804
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index("my-data-*").alias("my-data");
1806-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1807-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1808-
assertEquals("no such index [my-data-*]", iae.getMessage());
1805+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1806+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18091807
}
18101808

18111809
{
18121810
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName)
18131811
.alias("my-data");
1814-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1815-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1816-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1812+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1813+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
18171814
}
18181815
}
18191816

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/data_stream/140_data_stream_aliases.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,45 @@
152152
search:
153153
index: app1-zone-b
154154
- length: { hits.hits: 1 }
155+
156+
---
157+
"Create data stream aliases using wildcard expression":
158+
- skip:
159+
version: " - 7.99.99"
160+
reason: "bugfix has not yet backported to the 7.x branch"
161+
features: allowed_warnings
162+
163+
- do:
164+
allowed_warnings:
165+
- "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"
166+
indices.put_index_template:
167+
name: my-template
168+
body:
169+
index_patterns: [ log-* ]
170+
template:
171+
settings:
172+
index.number_of_replicas: 0
173+
data_stream: { }
174+
175+
- do:
176+
indices.create_data_stream:
177+
name: log-foobar
178+
- is_true: acknowledged
179+
180+
- do:
181+
indices.update_aliases:
182+
body:
183+
actions:
184+
- add:
185+
index: log-*
186+
alias: my-alias
187+
- is_true: acknowledged
188+
189+
- do:
190+
indices.get_data_stream:
191+
name: "*"
192+
- match: { data_streams.0.name: log-foobar }
193+
194+
- do:
195+
indices.get_alias: {}
196+
- match: {log-foobar.aliases.my-alias: {}}

0 commit comments

Comments
 (0)