Apache Solr open-source search software
SOLR-15749: Create v2 equivalent for 'rename' collection API (#1080)
Anakhe Ajayied93594380f3
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 6e91d5008d8..7a2e5aedb37 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -62,6 +62,8 @@ Improvements* SOLR-13626: Document the SystemInfoHandler in the Ref Guide. (Tony Cook via Eric Pugh)+* SOLR-15479: A v2 equivalent of the RENAME command is now available at `POST /api/collections/collName/rename`. (Anakhe Ajayi via Jason Gerlowski)+Optimizations---------------------diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.javaindex d5b5ea2eb1b..d2ecf550061 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java@@ -217,6 +217,7 @@ import org.apache.solr.handler.admin.api.ModifyCollectionAPI;import org.apache.solr.handler.admin.api.MoveReplicaAPI;import org.apache.solr.handler.admin.api.RebalanceLeadersAPI;import org.apache.solr.handler.admin.api.ReloadCollectionAPI;+import org.apache.solr.handler.admin.api.RenameCollectionAPI;import org.apache.solr.handler.admin.api.SetCollectionPropertyAPI;import org.apache.solr.handler.admin.api.SplitShardAPI;import org.apache.solr.handler.admin.api.SyncShardAPI;@@ -2088,6 +2089,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permissionapis.addAll(AnnotatedApi.getApis(new ReloadCollectionAPI(this)));apis.addAll(AnnotatedApi.getApis(new SetCollectionPropertyAPI(this)));apis.addAll(AnnotatedApi.getApis(new CollectionStatusAPI(this)));+ apis.addAll(AnnotatedApi.getApis(new RenameCollectionAPI(this)));return apis;}diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.java b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.javanew file mode 100644index 00000000000..7f92593ff79--- /dev/null+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.java@@ -0,0 +1,99 @@+/*+ * 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.client.solrj.SolrRequest.METHOD.POST;+import static org.apache.solr.common.params.CollectionAdminParams.FOLLOW_ALIASES;+import static org.apache.solr.common.params.CollectionAdminParams.TARGET;+import static org.apache.solr.common.params.CommonAdminParams.ASYNC;+import static org.apache.solr.common.params.CommonParams.ACTION;+import static org.apache.solr.common.params.CommonParams.NAME;+import static org.apache.solr.handler.ClusterAPI.wrapParams;+import static org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;++import com.fasterxml.jackson.databind.DeserializationFeature;+import com.fasterxml.jackson.databind.MapperFeature;+import com.fasterxml.jackson.databind.ObjectMapper;+import java.io.IOException;+import java.util.Locale;+import org.apache.commons.collections4.IterableUtils;+import org.apache.solr.api.EndPoint;+import org.apache.solr.client.solrj.request.beans.RenameCollectionPayload;+import org.apache.solr.common.params.CollectionAdminParams;+import org.apache.solr.common.params.CollectionParams;+import org.apache.solr.common.util.ContentStream;+import org.apache.solr.handler.admin.CollectionsHandler;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;+import org.apache.solr.util.SolrJacksonAnnotationInspector;++/**+ * V2 API for "renaming" an existing collection+ *+ * <p>This API is analogous to the v1 /admin/collections?action=RENAME command.+ */+public class RenameCollectionAPI {++ private final CollectionsHandler collectionsHandler;+ private static final ObjectMapper REQUEST_BODY_PARSER =+ SolrJacksonAnnotationInspector.createObjectMapper()+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)+ .disable(MapperFeature.AUTO_DETECT_FIELDS);++ public RenameCollectionAPI(CollectionsHandler collectionsHandler) {+ this.collectionsHandler = collectionsHandler;+ }++ @EndPoint(+ path = {"/collections/{collection}/rename"},+ method = POST,+ permission = COLL_EDIT_PERM)+ public void renameCollection(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {+ final RenameCollectionPayload v2Body = parseRenameParamsFromRequestBody(req);++ req =+ wrapParams(+ req,+ ACTION,+ CollectionParams.CollectionAction.RENAME.name().toLowerCase(Locale.ROOT),+ NAME,+ req.getPathTemplateValues().get(CollectionAdminParams.COLLECTION),+ TARGET,+ v2Body.to,+ ASYNC,+ v2Body.async,+ FOLLOW_ALIASES,+ v2Body.followAliases);+ collectionsHandler.handleRequestBody(req, rsp);+ }++ // TODO This is a bit hacky, but it's not worth investing in the request-body parsing code much+ // here, as it's+ // something that's already somewhat built-in when this eventually moves to JAX-RS+ private RenameCollectionPayload parseRenameParamsFromRequestBody(+ SolrQueryRequest solrQueryRequest) throws IOException {+ if (IterableUtils.isEmpty(solrQueryRequest.getContentStreams())) {+ // An empty request-body is invalid (the 'to' field is required at a minimum), but we'll lean+ // on the input-validation in CollectionsHandler to+ // catch this, rather than duplicating the check for that here+ return new RenameCollectionPayload();+ }++ final ContentStream cs = solrQueryRequest.getContentStreams().iterator().next();+ return REQUEST_BODY_PARSER.readValue(cs.getStream(), RenameCollectionPayload.class);+ }+}diff --git a/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java b/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.javaindex 1cdbdc9f0fb..78ac3dbd342 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java@@ -22,6 +22,7 @@ import static org.mockito.Mockito.mock;import static org.mockito.Mockito.verify;import com.google.common.collect.Maps;+import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.List;@@ -32,6 +33,7 @@ import org.apache.solr.api.Api;import org.apache.solr.api.ApiBag;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.CommandOperation;+import org.apache.solr.common.util.ContentStream;import org.apache.solr.common.util.ContentStreamBase;import org.apache.solr.handler.RequestHandlerBase;import org.apache.solr.request.LocalSolrQueryRequest;@@ -119,6 +121,11 @@ public abstract class V2ApiMappingTest<T extends RequestHandlerBase> extends Solnew ContentStreamBase.StringStream(v2RequestBody), api.getCommandSchema(), true);}+ @Override+ public Collection<ContentStream> getContentStreams() {+ return List.of(new ContentStreamBase.StringStream(v2RequestBody));+ }+@Overridepublic Map<String, String> getPathTemplateValues() {return parts;diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java b/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.javaindex 99e2f17c933..cebbaee2227 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java@@ -19,6 +19,7 @@ package org.apache.solr.handler.admin.api;import static org.apache.solr.common.params.CollectionAdminParams.COLLECTION;import static org.apache.solr.common.params.CollectionAdminParams.COLL_CONF;+import static org.apache.solr.common.params.CollectionAdminParams.TARGET;import static org.apache.solr.common.params.CommonAdminParams.ASYNC;import static org.apache.solr.common.params.CommonParams.ACTION;import static org.apache.solr.common.params.CommonParams.NAME;@@ -49,6 +50,7 @@ import org.junit.Test;* are never expected in the same request.*/public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHandler> {+@Overridepublic void populateApiBag() {final CollectionsHandler collectionsHandler = getRequestHandler();@@ -62,6 +64,7 @@ public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHandapiBag.registerObject(new ReloadCollectionAPI(collectionsHandler));apiBag.registerObject(new SetCollectionPropertyAPI(collectionsHandler));apiBag.registerObject(new CollectionStatusAPI(collectionsHandler));+ apiBag.registerObject(new RenameCollectionAPI(collectionsHandler));}@Override@@ -85,6 +88,21 @@ public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHandassertEquals("shard2", v1Params.get(SHARD));}+ @Test+ public void testRenameCollectionAllParams() throws Exception {+ final SolrParams v1Params =+ captureConvertedV1Params(+ "/collections/collName/rename",+ "POST",+ "{\"to\": \"targetColl\", \"async\": \"requestTrackingId\", \"followAliases\": true}");++ assertEquals("rename", v1Params.get(ACTION));+ assertEquals("collName", v1Params.get(NAME));+ assertEquals("targetColl", v1Params.get(TARGET));+ assertEquals("requestTrackingId", v1Params.get(ASYNC));+ assertEquals(true, v1Params.getPrimitiveBool("followAliases"));+ }+@Testpublic void testModifyCollectionAllProperties() throws Exception {final SolrParams v1Params =diff --git a/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc b/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adocindex 50e4da67bc0..cc330d9f36b 100644--- a/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc+++ b/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc@@ -535,8 +535,14 @@ http://localhost:8983/solr/admin/collections?action=RENAME&name=techproducts_v2&====[.tab-label]*V2 API*-We do not currently have a V2 equivalent.-+[source,bash]+----+curl -X POST http://localhost:8983/api/collections/techproducts/rename -H 'Content-Type: application/json' -d '+ {+ "to": "new_name"+ }+'+----====--@@ -552,7 +558,7 @@ s|Required |Default: none+Name of the existing SolrCloud collection or an alias that refers to exactly one collection and is not a Routed Alias.-`target`::+`target` (v1), `to` (v2)::+[%autowidth,frame=none]|===diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.javanew file mode 100644index 00000000000..c85fd42daee--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.java@@ -0,0 +1,29 @@+/*+ * 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.solrj.request.beans;++import org.apache.solr.common.annotation.JsonProperty;+import org.apache.solr.common.util.ReflectMapWriter;++public class RenameCollectionPayload implements ReflectMapWriter {+ @JsonProperty public String async;++ @JsonProperty public Boolean followAliases;++ @JsonProperty(required = true)+ public String to;+}