From ae8308252b69e116f47ab82365f5e1c47c2b8bed Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Fri, 10 Jan 2020 09:26:53 -0800 Subject: [PATCH] Ensure we emit a warning when using the deprecated 'template' field. (#50831) Since 6.0, the 'template' field has been deprecated in put template requests in favour of index_patterns. Previously, the PutIndexTemplateRequest would accept the 'template' field in its 'source' methods and silently convert it to 'index_patterns'. This meant that users specifying 'template' in the source would not receive a deprecation warning from the server. This PR makes a small change to no longer silently convert 'template' to 'index_patterns', which ensures that users receive a deprecation warning. Follow-up to #49460. --- .../indices/PutIndexTemplateRequest.java | 23 +++++++++++++--- .../elasticsearch/client/IndicesClientIT.java | 27 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java index 7008a719b7b36..5e10ae1b8f7ce 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.MasterNodeRequest; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -64,6 +65,15 @@ public class PutIndexTemplateRequest extends MasterNodeRequest indexPatterns; + /** + * This field corresponds to the deprecated 'template' parameter, which was replaced by + * 'index_patterns' in 6.0. It is stored and rendered to xContent separately from + * 'index_patterns' to ensure that the server emits a deprecation warning when it's been set. + */ + @Deprecated + @Nullable + private String template; + private int order; private boolean create; @@ -86,7 +96,7 @@ public PutIndexTemplateRequest(String name) { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (indexPatterns == null || indexPatterns.size() == 0) { + if (template == null && (indexPatterns == null || indexPatterns.size() == 0)) { validationException = addValidationError("index patterns are missing", validationException); } return validationException; @@ -288,7 +298,7 @@ public PutIndexTemplateRequest source(Map templateSource) { String name = entry.getKey(); if (name.equals("template")) { if(entry.getValue() instanceof String) { - patterns(Collections.singletonList((String) entry.getValue())); + this.template = (String) entry.getValue(); } } else if (name.equals("index_patterns")) { if(entry.getValue() instanceof String) { @@ -297,7 +307,7 @@ public PutIndexTemplateRequest source(Map templateSource) { List elements = ((List) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList()); patterns(elements); } else { - throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings"); + throw new IllegalArgumentException("Malformed [index_patterns] value, should be a string or a list of strings"); } } else if (name.equals("order")) { order(XContentMapValues.nodeIntegerValue(entry.getValue(), order())); @@ -424,7 +434,12 @@ public IndicesOptions indicesOptions() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("index_patterns", indexPatterns); + if (template != null) { + builder.field("template", template); + } else { + builder.field("index_patterns", indexPatterns); + } + builder.field("order", order); if (version != null) { builder.field("version", version); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 2990627b3af68..3b1e62f42184a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -92,6 +92,7 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; @@ -1578,6 +1579,32 @@ public void testPutTemplate() throws Exception { assertThat((Map) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz")); } + public void testPutTemplateWithDeprecatedTemplateField() throws Exception { + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template") + .source(XContentFactory.jsonBuilder() + .startObject() + .field("template", "name-*") + .field("order", 10) + .startObject("settings") + .field("number_of_shards", 3) + .field("number_of_replicas", 0) + .endObject() + .endObject()); + + AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, + highLevelClient().indices()::putTemplate, + highLevelClient().indices()::putTemplateAsync, + expectWarnings("Deprecated field [template] used, replaced by [index_patterns]")); + assertThat(putTemplateResponse.isAcknowledged(), equalTo(true)); + + Map templates = getAsMap("/_template/my-template"); + assertThat(templates.keySet(), hasSize(1)); + assertThat(extractValue("my-template.order", templates), equalTo(10)); + assertThat(extractRawValues("my-template.index_patterns", templates), contains("name-*")); + assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3")); + assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0")); + } + public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception { PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template") .patterns(Arrays.asList("pattern-1", "name-*"))