Skip to content

Commit adce3d5

Browse files
committed
Fix the use of wildcard expressions for data streams in update aliases api
Backporting elastic#75526 to 7.x branch. 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 elastic#75456
1 parent 4407fe5 commit adce3d5

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
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.action.ActionRequestValidationException;
1414
import org.elasticsearch.action.AliasesRequest;
15+
import org.elasticsearch.action.IndicesRequest;
1516
import org.elasticsearch.action.support.IndicesOptions;
1617
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1718
import org.elasticsearch.cluster.metadata.AliasAction;
@@ -49,7 +50,7 @@
4950
/**
5051
* A request to add/remove aliases for one or more indices.
5152
*/
52-
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements ToXContentObject {
53+
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest, ToXContentObject {
5354

5455
private List<AliasActions> allAliasActions = new ArrayList<>();
5556
private String origin = "";
@@ -505,6 +506,11 @@ public String[] indices() {
505506
return indices;
506507
}
507508

509+
@Override
510+
public boolean includeDataStreams() {
511+
return true;
512+
}
513+
508514
@Override
509515
public IndicesOptions indicesOptions() {
510516
return INDICES_OPTIONS;
@@ -643,6 +649,18 @@ public IndicesOptions indicesOptions() {
643649
return INDICES_OPTIONS;
644650
}
645651

652+
@Override
653+
public String[] indices() {
654+
return allAliasActions.stream()
655+
.flatMap(aliasActions -> Arrays.stream(aliasActions.indices()))
656+
.toArray(String[]::new);
657+
}
658+
659+
@Override
660+
public boolean includeDataStreams() {
661+
return true;
662+
}
663+
646664
@Override
647665
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
648666
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
@@ -1757,24 +1757,21 @@ public void testIndicesAliasesRequestTargetDataStreams() {
17571757

17581758
{
17591759
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName);
1760-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1761-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1762-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1760+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1761+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
17631762
}
17641763

17651764
{
17661765
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index("my-data-*").alias("my-data");
1767-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1768-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1769-
assertEquals("no such index [my-data-*]", iae.getMessage());
1766+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1767+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
17701768
}
17711769

17721770
{
17731771
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions.add().index(dataStreamName)
17741772
.alias("my-data");
1775-
IndexNotFoundException iae = expectThrows(IndexNotFoundException.class,
1776-
() -> indexNameExpressionResolver.concreteIndexNames(state, aliasActions));
1777-
assertEquals("no such index [" + dataStreamName + "]", iae.getMessage());
1773+
assertThat(indexNameExpressionResolver.concreteIndexNames(state, aliasActions),
1774+
arrayContaining(backingIndexEqualTo(dataStreamName, 1)));
17781775
}
17791776
}
17801777

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
@@ -71,3 +71,45 @@
7171
index: events
7272
body: { query: { match_all: {} } }
7373
- length: { hits.hits: 2 }
74+
75+
---
76+
"Create data stream aliases using wildcard expression":
77+
- skip:
78+
version: " - 7.99.99"
79+
reason: "bugfix has not yet backported to the 7.x branch"
80+
features: allowed_warnings
81+
82+
- do:
83+
allowed_warnings:
84+
- "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"
85+
indices.put_index_template:
86+
name: my-template
87+
body:
88+
index_patterns: [ log-* ]
89+
template:
90+
settings:
91+
index.number_of_replicas: 0
92+
data_stream: { }
93+
94+
- do:
95+
indices.create_data_stream:
96+
name: log-foobar
97+
- is_true: acknowledged
98+
99+
- do:
100+
indices.update_aliases:
101+
body:
102+
actions:
103+
- add:
104+
index: log-*
105+
alias: my-alias
106+
- is_true: acknowledged
107+
108+
- do:
109+
indices.get_data_stream:
110+
name: "*"
111+
- match: { data_streams.0.name: log-foobar }
112+
113+
- do:
114+
indices.get_alias: {}
115+
- match: {log-foobar.aliases.my-alias: {}}

0 commit comments

Comments
 (0)