Apache Solr open-source search software
SOLR-16397: Tweak v2 'renamecore' API to be more REST-ful (#2072)
Sanjay Dutt4c4ef2ea1e51
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c6a7ecb81f1..cbf1e020914 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -98,6 +98,9 @@ Improvements* SOLR-17041: Make CommitTracker currentTlogSize lazy (Alex Deparvu)+* SOLR-16397: The rename-core v2 endpoint has been updated to be more REST-ful.+ RENAME is now available at `POST /api/cores/coreName/rename` (Sanjay Dutt via Jason Gerlowski)+Optimizations---------------------(No changes)diff --git a/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.java b/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.javanew file mode 100644index 00000000000..115505fd894--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.java@@ -0,0 +1,39 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.apache.solr.client.api.endpoint;++import io.swagger.v3.oas.annotations.Operation;+import io.swagger.v3.oas.annotations.parameters.RequestBody;+import javax.ws.rs.POST;+import javax.ws.rs.Path;+import javax.ws.rs.PathParam;+import org.apache.solr.client.api.model.RenameCoreRequestBody;+import org.apache.solr.client.api.model.SolrJerseyResponse;++/** V2 API definition for renaming a Solr core. */+@Path("/cores/{coreName}/rename")+public interface RenameCoreApi {+ @POST+ @Operation(+ summary = "The RENAME action changes the name of a Solr core",+ tags = {"cores"})+ SolrJerseyResponse renameCore(+ @PathParam("coreName") String coreName,+ @RequestBody(description = "Additional properties related to the core renaming")+ RenameCoreRequestBody requestBody)+ throws Exception;+}diff --git a/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.java b/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.javanew file mode 100644index 00000000000..55037117fe8--- /dev/null+++ b/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.java@@ -0,0 +1,30 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.apache.solr.client.api.model;++import com.fasterxml.jackson.annotation.JsonProperty;+import io.swagger.v3.oas.annotations.media.Schema;++public class RenameCoreRequestBody {+ @Schema(description = "The new name for the Solr core.", required = true)+ @JsonProperty+ public String to;++ @Schema(description = "Request ID to track this action which will be processed asynchronously.")+ @JsonProperty+ public String async;+}diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex 60c83d10d15..6ea7fe8e7aa 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -64,7 +64,7 @@ import org.apache.solr.handler.admin.api.OverseerOperationAPI;import org.apache.solr.handler.admin.api.PrepareCoreRecoveryAPI;import org.apache.solr.handler.admin.api.RejoinLeaderElectionAPI;import org.apache.solr.handler.admin.api.ReloadCore;-import org.apache.solr.handler.admin.api.RenameCoreAPI;+import org.apache.solr.handler.admin.api.RenameCore;import org.apache.solr.handler.admin.api.RequestApplyCoreUpdatesAPI;import org.apache.solr.handler.admin.api.RequestBufferUpdatesAPI;import org.apache.solr.handler.admin.api.RequestCoreCommandStatusAPI;@@ -383,7 +383,6 @@ public class CoreAdminHandler extends RequestHandlerBase implements PermissionNaapis.addAll(AnnotatedApi.getApis(new CreateCoreAPI(this)));apis.addAll(AnnotatedApi.getApis(new RejoinLeaderElectionAPI(this)));apis.addAll(AnnotatedApi.getApis(new OverseerOperationAPI(this)));- apis.addAll(AnnotatedApi.getApis(new RenameCoreAPI(this)));apis.addAll(AnnotatedApi.getApis(new MergeIndexesAPI(this)));apis.addAll(AnnotatedApi.getApis(new SplitCoreAPI(this)));apis.addAll(AnnotatedApi.getApis(new RequestCoreCommandStatusAPI(this)));@@ -406,7 +405,8 @@ public class CoreAdminHandler extends RequestHandlerBase implements PermissionNaRestoreCore.class,ReloadCore.class,UnloadCore.class,- SwapCores.class);+ SwapCores.class,+ RenameCore.class);}public interface CoreAdminOp {diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.javaindex c8947803400..1f6deee32ca 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java@@ -61,6 +61,7 @@ import java.util.Map;import org.apache.solr.client.api.endpoint.SwapCoresApi;import org.apache.solr.client.api.model.ListCoreSnapshotsResponse;import org.apache.solr.client.api.model.ReloadCoreRequestBody;+import org.apache.solr.client.api.model.RenameCoreRequestBody;import org.apache.solr.client.api.model.SolrJerseyResponse;import org.apache.solr.client.api.model.SwapCoresRequestBody;import org.apache.solr.client.api.model.UnloadCoreRequestBody;@@ -79,6 +80,7 @@ import org.apache.solr.core.SolrCore;import org.apache.solr.handler.admin.CoreAdminHandler.CoreAdminOp;import org.apache.solr.handler.admin.api.CoreSnapshot;import org.apache.solr.handler.admin.api.ReloadCore;+import org.apache.solr.handler.admin.api.RenameCore;import org.apache.solr.handler.admin.api.SwapCores;import org.apache.solr.handler.admin.api.UnloadCore;import org.apache.solr.handler.api.V2ApiUtils;@@ -172,12 +174,15 @@ public enum CoreAdminOperation implements CoreAdminOp {RENAME,it -> {SolrParams params = it.req.getParams();- String name = params.required().get(CoreAdminParams.OTHER);- String cname = params.required().get(CoreAdminParams.CORE);-- if (cname.equals(name)) return;-- it.handler.coreContainer.rename(cname, name);+ final String cname = params.required().get(CoreAdminParams.CORE);+ final String name = params.required().get(CoreAdminParams.OTHER);+ final var renameCoreRequestBody = new RenameCoreRequestBody();+ renameCoreRequestBody.to = name;+ final var renameCoreApi =+ new RenameCore(+ it.handler.coreContainer, it.handler.getCoreAdminAsyncTracker(), it.req, it.rsp);+ SolrJerseyResponse response = renameCoreApi.renameCore(cname, renameCoreRequestBody);+ V2ApiUtils.squashIntoSolrResponseWithoutHeader(it.rsp, response);}),MERGEINDEXES_OP(MERGEINDEXES, new MergeIndexesOp()),diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.java b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.javanew file mode 100644index 00000000000..03b96b57659--- /dev/null+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.java@@ -0,0 +1,66 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF 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.apache.solr.handler.admin.api;++import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM;++import javax.inject.Inject;+import org.apache.solr.client.api.endpoint.RenameCoreApi;+import org.apache.solr.client.api.model.RenameCoreRequestBody;+import org.apache.solr.client.api.model.SolrJerseyResponse;+import org.apache.solr.core.CoreContainer;+import org.apache.solr.handler.admin.CoreAdminHandler;+import org.apache.solr.jersey.PermissionName;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;++/**+ * V2 API for renaming an existing Solr core.+ *+ * <p>The new API (POST /v2/cores/coreName/rename is equivalent to the v1 /admin/cores?action=rename+ * command.+ */+public class RenameCore extends CoreAdminAPIBase implements RenameCoreApi {+ @Inject+ public RenameCore(+ CoreContainer coreContainer,+ CoreAdminHandler.CoreAdminAsyncTracker coreAdminAsyncTracker,+ SolrQueryRequest solrQueryRequest,+ SolrQueryResponse solrQueryResponse) {+ super(coreContainer, coreAdminAsyncTracker, solrQueryRequest, solrQueryResponse);+ }++ @PermissionName(CORE_EDIT_PERM)+ @Override+ public SolrJerseyResponse renameCore(String coreName, RenameCoreRequestBody requestBody)+ throws Exception {+ ensureRequiredParameterProvided("coreName", coreName);+ ensureRequiredRequestBodyProvided(requestBody);+ ensureRequiredParameterProvided("to", requestBody.to);+ SolrJerseyResponse solrJerseyResponse = instantiateJerseyResponse(SolrJerseyResponse.class);+ if (coreName.equals(requestBody.to)) return solrJerseyResponse;+ return handlePotentiallyAsynchronousTask(+ solrJerseyResponse,+ coreName,+ requestBody.async,+ "rename",+ () -> {+ coreContainer.rename(coreName, requestBody.to);+ return solrJerseyResponse;+ });+ }+}diff --git a/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java b/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.javaindex 825cb996a5e..8a8ac0652bf 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java@@ -57,12 +57,6 @@ public class TestCoreAdminApis extends SolrTestCaseJ4 {Object[] params = calls.get("create");assertEquals("hello", params[0]);assertEquals(fromJSONString("{schema : schema.xml}"), params[2]);-- TestCollectionAPIs.makeCall(- apiBag, "/cores/core1", SolrRequest.METHOD.POST, "{rename:{to: core2}}");- params = calls.get("rename");- assertEquals("core1", params[0]);- assertEquals("core2", params[1]);}@SuppressWarnings({"unchecked"})diff --git a/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc b/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adocindex f2cf158ce95..b0af69e2d93 100644--- a/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc+++ b/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc@@ -349,7 +349,30 @@ This parameter is required in v1, and part of the url in the v2 API.The `RENAME` action changes the name of a Solr core.-`admin/cores?action=RENAME&core=_core-name_&other=_other-core-name_`+[.dynamic-tabs]+--+[example.tab-pane#v1coreadmin-rename]+====+[.tab-label]*V1 API*+[source,bash]+----+curl -X GET "http://localhost:8983/solr/admin/cores?action=RENAME&core=currentCoreName&other=newCoreName"+----+====++[example.tab-pane#v2coreadmin-rename]+====+[.tab-label]*V2 API*+[source,bash]+----+curl -X POST http://localhost:8983/api/cores/currentCoreName/rename -H 'Content-Type: application/json' -d '+ {+ "to": "newCoreName"+ }+'+----+====+--=== RENAME Parameters@@ -360,9 +383,10 @@ The `RENAME` action changes the name of a Solr core.s|Required |Default: none|===+-The name of the Solr core to be renamed.+The name of an existing Solr core to rename.+Specified as a query parameter if making a v1 request, or as a path parameter if using the v2 API.-`other`::+`other` (v1), `to` (v2)::+[%autowidth,frame=none]|===@@ -370,6 +394,7 @@ s|Required |Default: none|===+The new name for the Solr core.+Specified as a query parameter if making a v1 request, or as a property in the request body if using the v2 API.If the persistent attribute of `<solr>` is `true`, the new name will be written to `solr.xml` as the `name` attribute of the `<core>` attribute.`async`::@@ -381,7 +406,6 @@ If the persistent attribute of `<solr>` is `true`, the new name will be written+Request ID to track this action which will be processed asynchronously.-[[coreadmin-swap]]== SWAP@@ -402,19 +426,20 @@ Each core will be known by the name of the other, after the swap.----====-[example.tab-pane#v2coreadmin-reload]+[example.tab-pane#v2coreadmin-swap]====[.tab-label]*V2 API*[source,bash]-----`curl -X POST http://localhost:8983/api/cores/_core-name_/swap -H 'Content-Type: application/json' -d '+curl -X POST http://localhost:8983/api/cores/_core-name_/swap -H 'Content-Type: application/json' -d '{"with": "_other-core-name_"}-'`+'----====+--[IMPORTANT]====