Skip to content

Commit 777caf0

Browse files
authored
[7.x] Add support for V2 index templates to /_cat/templates (#55829) (#55866)
Backports the following commits to 7.x: - Add support for V2 index templates to /_cat/templates (#55829)
1 parent bebbc37 commit 777caf0

File tree

4 files changed

+121
-14
lines changed

4 files changed

+121
-14
lines changed

docs/reference/cat/templates.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The API returns the following response:
6565

6666
[source,txt]
6767
--------------------------------------------------
68-
name index_patterns order version
68+
name index_patterns order version composed_of
6969
template0 [te*] 0
7070
template1 [tea*] 1
7171
template2 [teak*] 2 7

rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
---
22
"Help":
3+
- skip:
4+
version: " - 7.7.99"
5+
reason: "format changed in 7.8 to accomodate V2 index templates"
6+
37
- do:
48
cat.templates:
59
help: true
@@ -10,6 +14,7 @@
1014
index_patterns .+ \n
1115
order .+ \n
1216
version .+ \n
17+
composed_of .+ \n
1318
$/
1419
1520
---
@@ -26,6 +31,9 @@
2631
2732
---
2833
"Normal templates":
34+
- skip:
35+
version: " - 7.7.99"
36+
reason: "format changed in 7.8 to accomodate V2 index templates"
2937

3038
- do:
3139
indices.put_template:
@@ -58,7 +66,7 @@
5866
(^|\n)test \s+
5967
\[test-\*\] \s+
6068
0 \s+
61-
1
69+
1 \s+
6270
(\n|$)
6371
/
6472
@@ -68,12 +76,15 @@
6876
(^|\n)test_2 \s+
6977
\[test-2\*\] \s+
7078
1 \s+
71-
2
79+
2 \s+
7280
(\n|$)
7381
/
7482
7583
---
7684
"Filtered templates":
85+
- skip:
86+
version: " - 7.7.99"
87+
reason: "format changed in 7.8 to accomodate V2 index templates"
7788

7889
- do:
7990
indices.put_template:
@@ -107,12 +118,16 @@
107118
test \s+
108119
\[t\*\] \s+
109120
0 \s+
110-
1
121+
1 \s*
111122
\n
112123
$/
113124
114125
---
115126
"Column headers":
127+
- skip:
128+
version: " - 7.7.99"
129+
reason: "format changed in 7.8 to accomodate V2 index templates"
130+
116131
- do:
117132
indices.put_template:
118133
name: test
@@ -135,17 +150,22 @@
135150
name \s+
136151
index_patterns \s+
137152
order \s+
138-
version
153+
version \s+
154+
composed_of
139155
\n
140156
test \s+
141157
\[t\*\] \s+
142158
0 \s+
143-
1
159+
1 \s*
144160
\n
145161
$/
146162
147163
---
148164
"Select columns":
165+
- skip:
166+
version: " - 7.7.99"
167+
reason: "format changed in 7.8 to accomodate V2 index templates"
168+
149169
- do:
150170
indices.put_template:
151171
name: test
@@ -177,7 +197,10 @@
177197
---
178198
"Sort templates":
179199
- skip:
200+
version: " - 7.7.99"
201+
reason: "format changed in 7.8 to accomodate V2 index templates"
180202
features: default_shards, no_xpack
203+
181204
- do:
182205
indices.put_template:
183206
name: test
@@ -207,8 +230,8 @@
207230
- match:
208231
$body: |
209232
/^
210-
test \s+ \[t\*\] \s+ \n
211-
test_1 \s+ \[te\*\] \s+ 1 \n
233+
test \s+ \[t\*\] \s+ \n \n
234+
test_1 \s+ \[te\*\] \s+ 1 \n \n
212235
$/
213236
214237
- do:
@@ -219,15 +242,18 @@
219242
- match:
220243
$body: |
221244
/^
222-
test_1 \s+ \[te\*\] \s+ 1\n
223-
test \s+ \[t\*\] \s+ \n
245+
test_1 \s+ \[te\*\] \s+ 1\n \n
246+
test \s+ \[t\*\] \s+ \n \n
224247
225248
$/
226249
227250
---
228251
"Multiple template":
229252
- skip:
253+
version: " - 7.7.99"
254+
reason: "format changed in 7.8 to accomodate V2 index templates"
230255
features: default_shards, no_xpack
256+
231257
- do:
232258
indices.put_template:
233259
name: test_1
@@ -254,4 +280,57 @@
254280
test_1 \s+
255281
\[t\*,\ te\*\]
256282
\n
283+
\n
257284
$/
285+
286+
---
287+
"Mixture of V1 and V2 templates":
288+
- skip:
289+
version: " - 7.7.99"
290+
reason: "format changed in 7.8 to accomodate V2 index templates"
291+
features: allowed_warnings
292+
293+
- do:
294+
indices.put_template:
295+
name: test
296+
body:
297+
order: 0
298+
version: 1
299+
index_patterns: test-*
300+
settings:
301+
number_of_shards: 1
302+
number_of_replicas: 0
303+
304+
- do:
305+
allowed_warnings:
306+
- "index template [testv2] has index patterns [v2-test] matching patterns from existing older templates [global] with patterns (global => [*]); this template [testv2] will take precedence during new index creation"
307+
indices.put_index_template:
308+
name: testv2
309+
body:
310+
index_patterns: [v2-test]
311+
priority: 4
312+
version: 3
313+
composed_of: [foo, bar]
314+
315+
- do:
316+
cat.templates: {}
317+
318+
- match:
319+
$body: >
320+
/
321+
(^|\n)test \s+
322+
\[test-\*\] \s+
323+
0 \s+
324+
1 \s+
325+
(\n|$)
326+
/
327+
328+
- match:
329+
$body: >
330+
/
331+
(^|\n)testv2 \s+
332+
\[v2-test\] \s+
333+
4 \s+
334+
3 \s+
335+
\[foo,\ bar\]
336+
/

server/src/main/java/org/elasticsearch/rest/action/cat/RestTemplatesAction.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
2525
import org.elasticsearch.client.node.NodeClient;
2626
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
27+
import org.elasticsearch.cluster.metadata.IndexTemplateV2;
2728
import org.elasticsearch.cluster.metadata.Metadata;
2829
import org.elasticsearch.common.Table;
2930
import org.elasticsearch.common.regex.Regex;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.rest.action.RestResponseListener;
3334

3435
import java.util.List;
36+
import java.util.Map;
3537

3638
import static java.util.Arrays.asList;
3739
import static java.util.Collections.unmodifiableList;
@@ -78,8 +80,9 @@ protected Table getTableWithHeader(RestRequest request) {
7880
table.startHeaders();
7981
table.addCell("name", "alias:n;desc:template name");
8082
table.addCell("index_patterns", "alias:t;desc:template index patterns");
81-
table.addCell("order", "alias:o;desc:template application order number");
83+
table.addCell("order", "alias:o,p;desc:template application order/priority number");
8284
table.addCell("version", "alias:v;desc:version");
85+
table.addCell("composed_of", "alias:c;desc:component templates comprising index template");
8386
table.endHeaders();
8487
return table;
8588
}
@@ -95,6 +98,21 @@ private Table buildTable(RestRequest request, ClusterStateResponse clusterStateR
9598
table.addCell("[" + String.join(", ", indexData.patterns()) + "]");
9699
table.addCell(indexData.getOrder());
97100
table.addCell(indexData.getVersion());
101+
table.addCell("");
102+
table.endRow();
103+
}
104+
}
105+
106+
for (Map.Entry<String, IndexTemplateV2> entry : metadata.templatesV2().entrySet()) {
107+
String name = entry.getKey();
108+
IndexTemplateV2 template = entry.getValue();
109+
if (patternString == null || Regex.simpleMatch(patternString, name)) {
110+
table.startRow();
111+
table.addCell(name);
112+
table.addCell("[" + String.join(", ", template.indexPatterns()) + "]");
113+
table.addCell(template.priority());
114+
table.addCell(template.version());
115+
table.addCell("[" + String.join(", ", template.composedOf()) + "]");
98116
table.endRow();
99117
}
100118
}

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import static java.util.Collections.unmodifiableList;
9999
import static org.hamcrest.Matchers.anEmptyMap;
100100
import static org.hamcrest.Matchers.anyOf;
101+
import static org.hamcrest.Matchers.containsString;
101102
import static org.hamcrest.Matchers.equalTo;
102103
import static org.hamcrest.Matchers.everyItem;
103104
import static org.hamcrest.Matchers.in;
@@ -588,12 +589,21 @@ private void wipeCluster() throws Exception {
588589
if ("".equals(template)) {
589590
throw new IllegalStateException("empty template in templates list:\n" + templates);
590591
}
591-
logger.debug("Clearing template [{}]", template);
592-
adminClient().performRequest(new Request("DELETE", "_template/" + template));
592+
logger.info("Clearing template [{}]", template);
593+
try {
594+
adminClient().performRequest(new Request("DELETE", "_template/" + template));
595+
} catch (ResponseException e) {
596+
// This is fine, it could be a V2 template
597+
assertThat(e.getMessage(), containsString("index_template [" + template + "] missing"));
598+
try {
599+
adminClient().performRequest(new Request("DELETE", "_index_template/" + template));
600+
} catch (ResponseException e2) {
601+
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
602+
}
603+
}
593604
}
594605
}
595606
try {
596-
adminClient().performRequest(new Request("DELETE", "_index_template/*"));
597607
adminClient().performRequest(new Request("DELETE", "_component_template/*"));
598608
} catch (ResponseException e) {
599609
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore

0 commit comments

Comments
 (0)