Skip to content

Commit d2bb6eb

Browse files
authored
Get-templates APIs don't support lists (#78989)
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 #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.
1 parent 6d2a899 commit d2bb6eb

File tree

9 files changed

+118
-153
lines changed

9 files changed

+118
-153
lines changed

docs/reference/cat/templates.asciidoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ and <<mapping,field mappings>> to new indices at creation.
2626
==== {api-path-parms-title}
2727

2828
`<template_name>`::
29-
(Optional, string) Comma-separated list of index template names used to limit
30-
the request. Accepts wildcard expressions.
31-
29+
(Optional, string) The name of the template to return. Accepts wildcard
30+
expressions. If omitted, all templates are returned.
3231

3332
[[cat-templates-query-params]]
3433
==== {api-query-parms-title}

docs/reference/indices/get-index-template.asciidoc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ privilege>> to use this API.
5151
[[get-template-api-path-params]]
5252
==== {api-path-parms-title}
5353

54-
include::{docdir}/rest-api/common-parms.asciidoc[tag=index-template]
55-
+
56-
To retrieve all index templates, omit this parameter or use a value of `*`.
57-
54+
(Optional, string) The name of the template to return. Accepts wildcard
55+
expressions. If omitted, all templates are returned.
5856

5957
[[get-template-api-query-params]]
6058
==== {api-query-parms-title}

rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_index_template.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
],
2525
"parts":{
2626
"name":{
27-
"type":"list",
28-
"description":"The comma separated names of the index templates"
27+
"type":"string",
28+
"description":"A pattern that returned template names must match"
2929
}
3030
}
3131
}

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/cat.templates/20_matching.yml

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,6 @@ setup:
7575
- match:
7676
$body: /test-composable-1\ntest-composable-2\ntest-legacy-1\ntest-legacy-2\n/
7777

78-
---
79-
"Matching all templates with other patterns":
80-
- skip:
81-
version: " - 7.99.99"
82-
reason: "support for multiple patterns added in 8.0.0"
83-
84-
- do:
85-
cat.templates:
86-
name: "nonexistent*,*,other-name"
87-
h: [name]
88-
s: [name]
89-
90-
- match:
91-
$body: /test-composable-1\ntest-composable-2\ntest-legacy-1\ntest-legacy-2\n/
92-
9378
---
9479
"Matching no templates":
9580

@@ -146,41 +131,13 @@ setup:
146131
- match:
147132
$body: /^test-composable-2\ntest-legacy-2\n$/
148133

149-
---
150-
"Matching lists of names":
151-
- skip:
152-
version: " - 7.99.99"
153-
reason: "support for multiple patterns added in 8.0.0"
154-
155-
- do:
156-
cat.templates:
157-
name: "test-legacy-1,test-composable-2"
158-
h: [name]
159-
s: [name]
160-
161-
- match:
162-
$body: /^test-composable-2\ntest-legacy-1\n$/
163134

164135
---
165-
"Matching names and wildcards":
166-
- skip:
167-
version: " - 7.99.99"
168-
reason: "support for multiple patterns added in 8.0.0"
169-
170-
- do:
171-
cat.templates:
172-
name: "test-legacy-1,test-composable-*"
173-
h: [name]
174-
s: [name]
175-
176-
- match:
177-
$body: /^test-composable-1\ntest-composable-2\ntest-legacy-1\n$/
178-
179-
- do:
180-
cat.templates:
181-
name: "test-legacy-*,test-composable-2"
182-
h: [name]
183-
s: [name]
184-
185-
- match:
186-
$body: /^test-composable-2\ntest-legacy-1\ntest-legacy-2\n$/
136+
"Reject request containing comma":
137+
- skip:
138+
version: " - 7.99.99"
139+
reason: "validation only added in 8.0.0"
140+
- do:
141+
catch: bad_request
142+
cat.templates:
143+
name: test1,test2

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.get_index_template/10_basic.yml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,43 @@ setup:
5959
- is_true: index_templates.0.name
6060
- is_true: index_templates.1.name
6161

62+
---
63+
"Pattern matching in index templates":
64+
- skip:
65+
version: " - 7.7.99"
66+
reason: "index template v2 API unavailable before 7.8"
67+
features: allowed_warnings
68+
69+
- do:
70+
allowed_warnings:
71+
- "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"
72+
indices.put_index_template:
73+
name: test2
74+
body:
75+
index_patterns: test2-*
76+
template:
77+
settings:
78+
number_of_shards: 1
79+
80+
- do:
81+
indices.get_index_template:
82+
name: "*"
83+
84+
- is_true: index_templates.0.name
85+
- is_true: index_templates.1.name
86+
87+
- do:
88+
indices.get_index_template:
89+
name: "test*"
90+
91+
- match: { index_templates.0.name: "/test.*/" }
92+
- match: { index_templates.1.name: "/test.*/" }
93+
6294
---
6395
"Get index template with local flag":
6496
- skip:
65-
version: " - 7.99.99"
66-
reason: "index template v2 API has not been backported"
97+
version: " - 7.7.99"
98+
reason: "index template v2 API unavailable before 7.8"
6799

68100
- do:
69101
indices.get_index_template:
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
setup:
22
- skip:
3-
version: " - 7.99.99"
4-
reason: "index template v2 API has not been backported"
3+
version: " - 7.7.99"
4+
reason: "index template v2 API unavailable before 7.8"
55

66
- do:
77
indices.delete_index_template:
88
name: '*'
99
ignore: 404
10+
1011
---
1112
"Get missing template":
1213
- skip:
13-
version: " - 7.99.99"
14-
reason: "index template v2 API has not been backported"
14+
version: " - 7.7.99"
15+
reason: "index template v2 API unavailable before 7.8"
1516

1617
- do:
1718
catch: missing
1819
indices.get_index_template:
19-
name: test
20+
name: nonexistent
21+
22+
- match: { error.reason: "index template matching [nonexistent] not found" }
23+
24+
---
25+
"Get non-matching wildcard":
26+
- skip:
27+
version: " - 7.7.99"
28+
reason: "index template v2 API unavailable before 7.8"
29+
30+
- do:
31+
catch: missing
32+
indices.get_index_template:
33+
name: "non-matching-wildcard*"
2034

35+
- is_false: error
36+
- is_true: index_templates
37+
38+
---
39+
"Reject request containing comma":
40+
- skip:
41+
version: " - 7.99.99"
42+
reason: "validation only added in 8.0.0"
43+
- do:
44+
catch: bad_request
45+
indices.get_index_template:
46+
name: test1,test2

server/src/main/java/org/elasticsearch/action/admin/indices/template/get/GetComposableIndexTemplateAction.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import org.elasticsearch.action.ActionType;
1414
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
1515
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
16-
import org.elasticsearch.core.Nullable;
17-
import org.elasticsearch.xcontent.ParseField;
1816
import org.elasticsearch.common.io.stream.StreamInput;
1917
import org.elasticsearch.common.io.stream.StreamOutput;
18+
import org.elasticsearch.core.Nullable;
19+
import org.elasticsearch.xcontent.ParseField;
2020
import org.elasticsearch.xcontent.ToXContentObject;
2121
import org.elasticsearch.xcontent.XContentBuilder;
2222

@@ -39,11 +39,15 @@ private GetComposableIndexTemplateAction() {
3939
public static class Request extends MasterNodeReadRequest<Request> {
4040

4141
@Nullable
42-
private String name;
43-
44-
public Request() { }
42+
private final String name;
4543

44+
/**
45+
* @param name A template name or pattern, or {@code null} to retrieve all templates.
46+
*/
4647
public Request(@Nullable String name) {
48+
if (name != null && name.contains(",")) {
49+
throw new IllegalArgumentException("template name may not contain ','");
50+
}
4751
this.name = name;
4852
}
4953

@@ -63,14 +67,6 @@ public ActionRequestValidationException validate() {
6367
return null;
6468
}
6569

66-
/**
67-
* Sets the name of the index template.
68-
*/
69-
public Request name(String name) {
70-
this.name = name;
71-
return this;
72-
}
73-
7470
/**
7571
* The name of the index templates.
7672
*/

server/src/main/java/org/elasticsearch/action/admin/indices/template/get/GetIndexTemplatesRequest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
*/
2323
public class GetIndexTemplatesRequest extends MasterNodeReadRequest<GetIndexTemplatesRequest> {
2424

25-
private String[] names;
26-
27-
public GetIndexTemplatesRequest() {
28-
}
25+
private final String[] names;
2926

3027
public GetIndexTemplatesRequest(String... names) {
3128
this.names = names;
@@ -57,14 +54,6 @@ public ActionRequestValidationException validate() {
5754
return validationException;
5855
}
5956

60-
/**
61-
* Sets the names of the index templates.
62-
*/
63-
public GetIndexTemplatesRequest names(String... names) {
64-
this.names = names;
65-
return this;
66-
}
67-
6857
/**
6958
* The names of the index templates.
7059
*/

0 commit comments

Comments
 (0)