Skip to content

Commit

Permalink
Ensure we emit a warning when using the deprecated 'template' field. (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jtibshirani authored Jan 10, 2020
1 parent 5afa0b7 commit ae83082
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,15 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR

private List<String> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -288,7 +298,7 @@ public PutIndexTemplateRequest source(Map<String, Object> 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) {
Expand All @@ -297,7 +307,7 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
List<String> 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()));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1578,6 +1579,32 @@ public void testPutTemplate() throws Exception {
assertThat((Map<String, String>) 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<String, Object> 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-*"))
Expand Down

0 comments on commit ae83082

Please sign in to comment.