Skip to content

Commit

Permalink
[REST Compatible API] 'template' parameter and field on PUT index tem…
Browse files Browse the repository at this point in the history
…plate (#71238)
  • Loading branch information
joegallo authored Apr 2, 2021
1 parent 14aba7b commit 4ff17c1
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"indices.put_template_with_param":{
"documentation":{
"url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html",
"description":"Creates or updates an index template."
},
"stability":"stable",
"visibility":"public",
"headers":{
"accept": [ "application/vnd.elasticsearch+json;compatible-with=7"],
"content_type": ["application/vnd.elasticsearch+json;compatible-with=7"]
},
"url":{
"paths":[
{
"path":"/_template/{name}",
"methods":[
"PUT",
"POST"
],
"parts":{
"name":{
"type":"string",
"description":"The name of the template"
}
}
}
]
},
"params":{
"template":{
"type":"string",
"description":"The indices that this template should apply to, replaced by index_patterns within the template definition."
},
"order":{
"type":"number",
"description":"The order for this template when merging multiple matching ones (higher numbers are merged later, overriding the lower numbers)"
},
"create":{
"type":"boolean",
"description":"Whether the index template should only be added if new or can also replace an existing one",
"default":false
},
"master_timeout":{
"type":"time",
"description":"Specify timeout for connection to master"
}
},
"body":{
"description":"The template definition",
"required":true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
setup:
- skip:
version: "9.0.0 - "
reason: "compatible from 8.x to 7.x"
features:
- "headers"
- "warnings"

---
"Put template":

- do:
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
warnings:
- "Deprecated field [template] used, replaced by [index_patterns]"
indices.put_template:
name: test
body:
template: test-*
settings:
number_of_shards: 1
number_of_replicas: 0
mappings:
properties:
field:
type: keyword

- do:
indices.get_template:
name: test
flat_settings: true

- match: {test.index_patterns: ["test-*"]}
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
- match: {test.mappings: {properties: {field: {type: keyword}}}}

---
"Put template (with template parameter)":

- do:
headers:
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
warnings:
- "Deprecated parameter [template] used, replaced by [index_patterns]"
indices.put_template_with_param:
name: test
template: "test-*"
body:
settings:
number_of_shards: 1
number_of_replicas: 0
mappings:
properties:
field:
type: keyword

- do:
indices.get_template:
name: test
flat_settings: true

- match: {test.index_patterns: ["test-*"]}
- match: {test.settings: {index.number_of_shards: '1', index.number_of_replicas: '0'}}
- match: {test.mappings: {properties: {field: {type: keyword}}}}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.RestApiVersion;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
Expand All @@ -26,6 +28,8 @@

public class RestPutIndexTemplateAction extends BaseRestHandler {

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestPutIndexTemplateAction.class);

@Override
public List<Route> routes() {
return List.of(
Expand All @@ -40,9 +44,14 @@ public String getName() {

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {

PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));
putRequest.patterns(asList(request.paramAsStringArray("index_patterns", Strings.EMPTY_ARRAY)));
if (request.getRestApiVersion() == RestApiVersion.V_7 && request.hasParam("template")) {
deprecationLogger.compatibleApiWarning("template_parameter_deprecation",
"Deprecated parameter [template] used, replaced by [index_patterns]");
putRequest.patterns(List.of(request.param("template")));
} else {
putRequest.patterns(asList(request.paramAsStringArray("index_patterns", Strings.EMPTY_ARRAY)));
}
putRequest.order(request.paramAsInt("order", putRequest.order()));
putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
putRequest.create(request.paramAsBoolean("create", false));
Expand All @@ -51,6 +60,11 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false,
request.getXContentType()).v2();
sourceAsMap = RestCreateIndexAction.prepareMappings(sourceAsMap);
if (request.getRestApiVersion() == RestApiVersion.V_7 && sourceAsMap.containsKey("template")) {
deprecationLogger.compatibleApiWarning("template_field_deprecation",
"Deprecated field [template] used, replaced by [index_patterns]");
putRequest.patterns(List.of((String) sourceAsMap.remove("template")));
}
putRequest.source(sourceAsMap);

return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel));
Expand Down

0 comments on commit 4ff17c1

Please sign in to comment.