Skip to content

Commit

Permalink
Get-templates APIs don't support lists
Browse files Browse the repository at this point in the history
We document that `GET /_index_template/...` accepts a comma-separated
list of template names but in fact today this API accepts only a single
name or pattern. Likewise `GET /_cat/templates/...` (at least it didn't
until elastic#78829 but that's not released yet). This commit fixes the docs to
indicate these APIs accept only a single template name and also adds
some extra validation to reject requests containing a `,` since such a
request cannot match any actual templates.

It also adjusts `GET /_cat/templates` to use the filtering built into
`TransportGetComposableIndexTemplateAction` rather than retrieving all
templates and then filtering them on the coordinating node.

Backport of elastic#78989
  • Loading branch information
DaveCTurner committed Oct 13, 2021
1 parent 8913822 commit b72583b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 147 deletions.
5 changes: 2 additions & 3 deletions docs/reference/cat/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ and <<mapping,field mappings>> to new indices at creation.
==== {api-path-parms-title}

`<template_name>`::
(Optional, string) Comma-separated list of index template names used to limit
the request. Accepts wildcard expressions.

(Optional, string) The name of the template to return. Accepts wildcard
expressions. If omitted, all templates are returned.

[[cat-templates-query-params]]
==== {api-query-parms-title}
Expand Down
6 changes: 2 additions & 4 deletions docs/reference/indices/get-index-template.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ privilege>> to use this API.
[[get-template-api-path-params]]
==== {api-path-parms-title}

include::{docdir}/rest-api/common-parms.asciidoc[tag=index-template]
+
To retrieve all index templates, omit this parameter or use a value of `*`.

(Optional, string) The name of the template to return. Accepts wildcard
expressions. If omitted, all templates are returned.

[[get-template-api-query-params]]
==== {api-query-parms-title}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
],
"parts":{
"name":{
"type":"list",
"description":"The comma separated names of the index templates"
"type":"string",
"description":"A pattern that returned template names must match"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ setup:
- match:
$body: /test-composable-1\ntest-composable-2\ntest-legacy-1\ntest-legacy-2\n/

---
"Matching all templates with other patterns":
- skip:
version: " - 7.99.99"
reason: "support for multiple patterns added in 8.0.0"

- do:
cat.templates:
name: "nonexistent*,*,other-name"
h: [name]
s: [name]

- match:
$body: /test-composable-1\ntest-composable-2\ntest-legacy-1\ntest-legacy-2\n/

---
"Matching no templates":

Expand Down Expand Up @@ -148,41 +133,13 @@ setup:
- match:
$body: /^test-composable-2\ntest-legacy-2\n$/

---
"Matching lists of names":
- skip:
version: " - 7.99.99"
reason: "support for multiple patterns added in 8.0.0"

- do:
cat.templates:
name: "test-legacy-1,test-composable-2"
h: [name]
s: [name]

- match:
$body: /^test-composable-2\ntest-legacy-1\n$/

---
"Matching names and wildcards":
- skip:
version: " - 7.99.99"
reason: "support for multiple patterns added in 8.0.0"

- do:
cat.templates:
name: "test-legacy-1,test-composable-*"
h: [name]
s: [name]

- match:
$body: /^test-composable-1\ntest-composable-2\ntest-legacy-1\n$/

- do:
cat.templates:
name: "test-legacy-*,test-composable-2"
h: [name]
s: [name]

- match:
$body: /^test-composable-2\ntest-legacy-1\ntest-legacy-2\n$/
"Reject request containing comma":
- skip:
version: " - 7.15.99"
reason: "validation only added in 7.16.0"
- do:
catch: bad_request
cat.templates:
name: test1,test2
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ setup:
- is_true: index_templates.0.name
- is_true: index_templates.1.name

---
"Pattern matching in index templates":
- skip:
version: " - 7.7.99"
reason: "index template v2 API unavailable before 7.8"
features: allowed_warnings

- do:
allowed_warnings:
- "index template [test2] has index patterns [test2-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [test2] will take precedence during new index creation"
indices.put_index_template:
name: test2
body:
index_patterns: test2-*
template:
settings:
number_of_shards: 1

- do:
indices.get_index_template:
name: "*"

- is_true: index_templates.0.name
- is_true: index_templates.1.name

- do:
indices.get_index_template:
name: "test*"

- match: { index_templates.0.name: "/test.*/" }
- match: { index_templates.1.name: "/test.*/" }

---
"Get index template with local flag":
- skip:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ setup:
indices.delete_index_template:
name: '*'
ignore: 404

---
"Get missing template":
- skip:
Expand All @@ -16,5 +17,30 @@ setup:
- do:
catch: missing
indices.get_index_template:
name: test
name: nonexistent

- match: { error.reason: "index template matching [nonexistent] not found" }

---
"Get non-matching wildcard":
- skip:
version: " - 7.7.99"
reason: "index template v2 API unavailable before 7.8"

- do:
catch: missing
indices.get_index_template:
name: "non-matching-wildcard*"

- is_false: error
- is_true: index_templates

---
"Reject request containing comma":
- skip:
version: " - 7.15.99"
reason: "validation only added in 7.16.0"
- do:
catch: bad_request
indices.get_index_template:
name: test1,test2
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.common.xcontent.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ParseField;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.core.Nullable;

import java.io.IOException;
import java.util.Map;
Expand All @@ -39,11 +39,15 @@ private GetComposableIndexTemplateAction() {
public static class Request extends MasterNodeReadRequest<Request> {

@Nullable
private String name;

public Request() { }
private final String name;

/**
* @param name A template name or pattern, or {@code null} to retrieve all templates.
*/
public Request(@Nullable String name) {
if (name != null && name.contains(",")) {
throw new IllegalArgumentException("template name may not contain ','");
}
this.name = name;
}

Expand All @@ -63,14 +67,6 @@ public ActionRequestValidationException validate() {
return null;
}

/**
* Sets the name of the index template.
*/
public Request name(String name) {
this.name = name;
return this;
}

/**
* The name of the index templates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
public class GetIndexTemplatesRequest extends MasterNodeReadRequest<GetIndexTemplatesRequest> {

private String[] names;

public GetIndexTemplatesRequest() {
}
private final String[] names;

public GetIndexTemplatesRequest(String... names) {
this.names = names;
Expand Down Expand Up @@ -57,14 +54,6 @@ public ActionRequestValidationException validate() {
return validationException;
}

/**
* Sets the names of the index templates.
*/
public GetIndexTemplatesRequest names(String... names) {
this.names = names;
return this;
}

/**
* The names of the index templates.
*/
Expand Down
Loading

0 comments on commit b72583b

Please sign in to comment.