Explorar o código

[REST Compatible API] 'template' parameter and field on PUT index template (#71238)

Joe Gallo %!s(int64=4) %!d(string=hai) anos
pai
achega
4ff17c1b7a

+ 54 - 0
rest-api-spec/src/yamlRestCompatTest/resources/rest-api-spec/api/indices.put_template_with_param.json

@@ -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
+    }
+  }
+}

+ 68 - 0
rest-api-spec/src/yamlRestCompatTest/resources/rest-api-spec/test/v7compat/indices.put_template/10_basic_compat.yml

@@ -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}}}}

+ 16 - 2
server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java

@@ -10,7 +10,9 @@ package org.elasticsearch.rest.action.admin.indices;
 
 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;
@@ -26,6 +28,8 @@ import static org.elasticsearch.rest.RestRequest.Method.PUT;
 
 public class RestPutIndexTemplateAction extends BaseRestHandler {
 
+    private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestPutIndexTemplateAction.class);
+
     @Override
     public List<Route> routes() {
         return List.of(
@@ -40,9 +44,14 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
 
     @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));
@@ -51,6 +60,11 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
         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));