Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we emit a warning when using the deprecated 'template' field. #50831

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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