Skip to content

Commit

Permalink
Fix the use of wildcard expressions for data streams in update aliase…
Browse files Browse the repository at this point in the history
…s api (elastic#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 elastic#75456
  • Loading branch information
martijnvg authored and ywangd committed Jul 30, 2021
1 parent 6181a6d commit 1e459fd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,7 @@
/**
* A request to add/remove aliases for one or more indices.
*/
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements ToXContentObject {
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest, ToXContentObject {

private List<AliasActions> allAliasActions = new ArrayList<>();
private String origin = "";
Expand Down Expand Up @@ -482,6 +483,11 @@ public String[] indices() {
return indices;
}

@Override
public boolean includeDataStreams() {
return true;
}

@Override
public IndicesOptions indicesOptions() {
return INDICES_OPTIONS;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}}

0 comments on commit 1e459fd

Please sign in to comment.