From e457a928371044eadf349666baa6a57b506acc85 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Tue, 14 Jul 2020 14:40:41 +0200 Subject: [PATCH] Compatible Delete and Update rest actions (#58246) based on compat/search branch fixed tests from delete and update directories. Delete test is still not fixed CompatRestIT. test {yaml=delete/70_mix_typeless_typeful/DELETE with typeless API on an index that has types} current state 1306tests | 174failures previously 1306tests | 197failures relates #54160 --- .../compat/RestCompatPlugin.java | 8 ++- .../action/document/RestDeleteActionV7.java | 62 +++++++++++++++++++ .../action/document/RestUpdateActionV7.java | 62 +++++++++++++++++++ .../document/RestDeleteActionV7Tests.java | 46 ++++++++++++++ .../document/RestUpdateActionV7Tests.java | 46 ++++++++++++++ .../elasticsearch/rest/BaseRestHandler.java | 8 +++ 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestDeleteActionV7.java create mode 100644 modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestUpdateActionV7.java create mode 100644 modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestDeleteActionV7Tests.java create mode 100644 modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionV7Tests.java diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java index 563562debad14..e846c925166d4 100644 --- a/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/compat/RestCompatPlugin.java @@ -7,7 +7,7 @@ * not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -33,10 +33,12 @@ import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.action.admin.indices.RestCreateIndexActionV7; +import org.elasticsearch.rest.action.document.RestDeleteActionV7; import org.elasticsearch.rest.action.document.RestGetActionV7; import org.elasticsearch.rest.action.document.RestIndexActionV7; import org.elasticsearch.rest.action.document.RestMultiTermVectorsActionV7; import org.elasticsearch.rest.action.document.RestTermVectorsActionV7; +import org.elasticsearch.rest.action.document.RestUpdateActionV7; import org.elasticsearch.rest.action.search.RestMultiSearchActionV7; import org.elasticsearch.rest.action.search.RestSearchActionV7; import org.elasticsearch.script.mustache.RestMultiSearchTemplateActionV7; @@ -74,7 +76,9 @@ public List getRestHandlers( new RestSearchActionV7(), new RestMultiSearchActionV7(settings), new RestSearchTemplateActionV7(), - new RestMultiSearchTemplateActionV7(settings) + new RestMultiSearchTemplateActionV7(settings), + new RestDeleteActionV7(), + new RestUpdateActionV7() ); } return Collections.emptyList(); diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestDeleteActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestDeleteActionV7.java new file mode 100644 index 0000000000000..efc5150ab02c3 --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestDeleteActionV7.java @@ -0,0 +1,62 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.rest.action.document; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.DELETE; + +public class RestDeleteActionV7 extends RestDeleteAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestDeleteActionV7.class); + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in " + + "document index requests is deprecated, use the /{index}/_doc/{id} endpoint instead."; + + @Override + public List routes() { + return List.of(new Route(DELETE, "/{index}/{type}/{id}")); + } + + @Override + public String getName() { + return "document_delete_action_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + if (request.hasParam("type")) { + request.param("type"); + deprecationLogger.deprecate("delete_with_types", TYPES_DEPRECATION_MESSAGE); + // todo compatible log + } + + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestUpdateActionV7.java b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestUpdateActionV7.java new file mode 100644 index 0000000000000..8881cb744030b --- /dev/null +++ b/modules/rest-compatibility/src/main/java/org/elasticsearch/rest/action/document/RestUpdateActionV7.java @@ -0,0 +1,62 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.rest.action.document; + +import org.elasticsearch.Version; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.rest.RestRequest; + +import java.io.IOException; +import java.util.List; + +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestUpdateActionV7 extends RestUpdateAction { + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestGetActionV7.class); + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in " + + "document update requests is deprecated, use the endpoint /{index}/_update/{id} instead."; + + @Override + public List routes() { + return List.of(new Route(POST, "/{index}/{type}/{id}/_update")); + } + + @Override + public String getName() { + return "document_update_action_v7"; + } + + @Override + public Version compatibleWithVersion() { + return Version.V_7_0_0; + } + + @Override + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { + if (request.hasParam("type")) { + request.param("type"); + deprecationLogger.deprecate("update_with_types", TYPES_DEPRECATION_MESSAGE); + // todo compatible log + } + + return super.prepareRequest(request, client); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestDeleteActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestDeleteActionV7Tests.java new file mode 100644 index 0000000000000..4f0cf4ab318cb --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestDeleteActionV7Tests.java @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.rest.action.document; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +public class RestDeleteActionV7Tests extends RestActionTestCase { + @Before + public void setUpAction() { + controller().registerHandler(new RestDeleteActionV7()); + controller().registerHandler(new RestDeleteAction()); + } + + public void testTypeInPath() { + RestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.DELETE) + .withPath("/some_index/some_type/some_id") + .build(); + dispatchRequest(deprecatedRequest); + assertWarnings(RestDeleteActionV7.TYPES_DEPRECATION_MESSAGE); + + RestRequest validRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.DELETE) + .withPath("/some_index/_doc/some_id") + .build(); + dispatchRequest(validRequest); + } +} diff --git a/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionV7Tests.java b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionV7Tests.java new file mode 100644 index 0000000000000..cf659140af9b9 --- /dev/null +++ b/modules/rest-compatibility/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionV7Tests.java @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.rest.action.document; + +import org.elasticsearch.compat.FakeCompatRestRequestBuilder; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.test.rest.RestActionTestCase; +import org.junit.Before; + +public class RestUpdateActionV7Tests extends RestActionTestCase { + @Before + public void setUpAction() { + controller().registerHandler(new RestUpdateActionV7()); + controller().registerHandler(new RestUpdateAction()); + } + + public void testTypeInPath() { + RestRequest deprecatedRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/some_index/some_type/some_id/_update") + .build(); + dispatchRequest(deprecatedRequest); + assertWarnings(RestUpdateActionV7.TYPES_DEPRECATION_MESSAGE); + + RestRequest validRequest = new FakeCompatRestRequestBuilder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/some_index/_update/some_id") + .build(); + dispatchRequest(validRequest); + } +} diff --git a/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java b/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java index 101c3182fbb62..27a804507ff7a 100644 --- a/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java +++ b/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java @@ -148,6 +148,14 @@ protected final String unrecognized( return message.toString(); } + @Override + public String toString() { + return this.getClass()+"{" + + "name=" + this.getName() + ", " + + "compatibleWithVersion=" + this.compatibleWithVersion() + + '}'; + } + /** * REST requests are handled by preparing a channel consumer that represents the execution of * the request against a channel.