dsmiley commented on code in PR #955:
URL: https://github.com/apache/solr/pull/955#discussion_r935104951


##########
solr/core/src/java/org/apache/solr/handler/configsets/UploadConfigSetAPI.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.configsets;
+
+
+import org.apache.commons.io.IOUtils;
+import org.apache.solr.api.EndPoint;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ConfigSetParams;
+import org.apache.solr.core.ConfigSetService;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import static org.apache.solr.client.solrj.SolrRequest.METHOD.PUT;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM;
+
+/**
+ * V2 API for uploading a new configset (or overwriting an existing one).
+ *
+ * <p>This API (PUT /v2/cluster/configs/configsetName) is analogous to the v1
+ * /admin/configs?action=UPLOAD command.
+ *
+ */
+public class UploadConfigSetAPI extends ConfigSetAPIBase {
+
+    public static final String CONFIGSET_NAME_PLACEHOLDER = "name";
+
+
+    private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    public UploadConfigSetAPI(CoreContainer coreContainer) {
+        super(coreContainer);
+    }
+
+    @EndPoint(method = PUT, path = "/cluster/configs/{name}", permission = 
CONFIG_EDIT_PERM)
+    public void uploadConfigSet(SolrQueryRequest req, SolrQueryResponse rsp) 
throws Exception {
+        ensureConfigSetUploadEnabled();
+
+        final String configSetName = req.getPathTemplateValues().get("name");
+        boolean overwritesExisting = 
configSetService.checkConfigExists(configSetName);
+        boolean requestIsTrusted = isTrusted(req.getUserPrincipal(), 
coreContainer.getAuthenticationPlugin());
+        // Get upload parameters
+        boolean allowOverwrite = 
req.getParams().getBool(ConfigSetParams.OVERWRITE, true);
+        boolean cleanup = req.getParams().getBool(ConfigSetParams.CLEANUP, 
false);
+        final InputStream inputStream = ensureNonEmptyInputStream(req);
+
+        if (overwritesExisting && !allowOverwrite) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST,
+                    "The configuration " + configSetName + " already exists in 
zookeeper");

Review Comment:
   No need to reference ZooKeeper in error messages relating to configSets.  
ConfigSetService is pluggable and the backend is more of a detail.



##########
solr/core/src/java/org/apache/solr/handler/configsets/CreateConfigSetAPI.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.configsets;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.solr.api.Command;
+import org.apache.solr.api.EndPoint;
+import org.apache.solr.api.PayloadObj;
+import org.apache.solr.client.solrj.request.beans.CreateConfigPayload;
+import org.apache.solr.cloud.ConfigSetCmds;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ConfigSetParams;
+import org.apache.solr.core.CoreContainer;
+
+import java.util.Map;
+
+import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;
+import static org.apache.solr.common.params.CommonParams.NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DISABLE_CREATE_AUTH_CHECKS;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM;
+
+/**
+ * V2 API for creating a new configset as a copy of an existing one.
+ *
+ * <p>This API (POST /v2/cluster/configs {"create": {...}}) is analogous to
+ * the v1 /admin/configs?action=CREATE command.
+ *
+ */
+@EndPoint(method = POST, path = "/cluster/configs", permission = 
CONFIG_EDIT_PERM)
+public class CreateConfigSetAPI extends ConfigSetAPIBase {
+
+    public CreateConfigSetAPI(CoreContainer coreContainer) {
+        super(coreContainer);
+    }
+
+    @Command(name = "create")
+    @SuppressWarnings("unchecked")
+    public void create(PayloadObj<CreateConfigPayload> obj) throws Exception {
+        final CreateConfigPayload createConfigPayload = obj.get();
+        final String baseConfigSetName = (createConfigPayload.baseConfigSet != 
null) ? createConfigPayload.baseConfigSet : DEFAULT_CONFIGSET_NAME;
+        if (configSetService.checkConfigExists(createConfigPayload.name)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST, "ConfigSet already 
exists: " + createConfigPayload.name);
+        }
+
+        // is there a base config that already exists
+        if (! configSetService.checkConfigExists(baseConfigSetName)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST, "Base ConfigSet does 
not exist: " + baseConfigSetName);
+        }
+
+        if (!DISABLE_CREATE_AUTH_CHECKS
+                && !isTrusted(obj.getRequest().getUserPrincipal(), 
coreContainer.getAuthenticationPlugin())
+                && isCurrentlyTrusted(baseConfigSetName)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.UNAUTHORIZED,
+                    "Can't create a configset with an unauthenticated request 
from a trusted "
+                            + ConfigSetCmds.BASE_CONFIGSET);
+        }
+
+        final Map<String, Object> configsetCommandMsg = Maps.newHashMap();

Review Comment:
   I find it very sad that configsetCommandMsg needs to exist, and must be 
translated from CreateConfigPayload.  Wouldn't it be great if we could send 
"ReflectMapWriter" stuff directly to runConfigSetCommand?  and get the same 
POJO (CreateConfigPayload) back on the other end?



##########
solr/core/src/java/org/apache/solr/handler/configsets/CreateConfigSetAPI.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.configsets;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.solr.api.Command;
+import org.apache.solr.api.EndPoint;
+import org.apache.solr.api.PayloadObj;
+import org.apache.solr.client.solrj.request.beans.CreateConfigPayload;
+import org.apache.solr.cloud.ConfigSetCmds;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ConfigSetParams;
+import org.apache.solr.core.CoreContainer;
+
+import java.util.Map;
+
+import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;
+import static org.apache.solr.common.params.CommonParams.NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DISABLE_CREATE_AUTH_CHECKS;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM;
+
+/**
+ * V2 API for creating a new configset as a copy of an existing one.
+ *
+ * <p>This API (POST /v2/cluster/configs {"create": {...}}) is analogous to
+ * the v1 /admin/configs?action=CREATE command.
+ *
+ */
+@EndPoint(method = POST, path = "/cluster/configs", permission = 
CONFIG_EDIT_PERM)
+public class CreateConfigSetAPI extends ConfigSetAPIBase {
+
+    public CreateConfigSetAPI(CoreContainer coreContainer) {
+        super(coreContainer);
+    }
+
+    @Command(name = "create")
+    @SuppressWarnings("unchecked")
+    public void create(PayloadObj<CreateConfigPayload> obj) throws Exception {
+        final CreateConfigPayload createConfigPayload = obj.get();
+        final String baseConfigSetName = (createConfigPayload.baseConfigSet != 
null) ? createConfigPayload.baseConfigSet : DEFAULT_CONFIGSET_NAME;
+        if (configSetService.checkConfigExists(createConfigPayload.name)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST, "ConfigSet already 
exists: " + createConfigPayload.name);
+        }
+
+        // is there a base config that already exists
+        if (! configSetService.checkConfigExists(baseConfigSetName)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.BAD_REQUEST, "Base ConfigSet does 
not exist: " + baseConfigSetName);
+        }
+
+        if (!DISABLE_CREATE_AUTH_CHECKS
+                && !isTrusted(obj.getRequest().getUserPrincipal(), 
coreContainer.getAuthenticationPlugin())
+                && isCurrentlyTrusted(baseConfigSetName)) {
+            throw new SolrException(
+                    SolrException.ErrorCode.UNAUTHORIZED,
+                    "Can't create a configset with an unauthenticated request 
from a trusted "
+                            + ConfigSetCmds.BASE_CONFIGSET);
+        }
+
+        final Map<String, Object> configsetCommandMsg = Maps.newHashMap();

Review Comment:
   On further reflection, the fact that this is communicated to the Overseer at 
all instead of executed directly is silly (I think).  
   
   If as a project we forgo use of the Overseer for some things like this as I 
indicate in this comment 
https://issues.apache.org/jira/browse/SOLR-15146?focusedCommentId=17574033&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17574033
 it could lead to wonderful simplifications in SolrCloud code.



##########
solr/core/src/java/org/apache/solr/handler/configsets/CreateConfigSetAPI.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.configsets;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.solr.api.Command;
+import org.apache.solr.api.EndPoint;
+import org.apache.solr.api.PayloadObj;
+import org.apache.solr.client.solrj.request.beans.CreateConfigPayload;
+import org.apache.solr.cloud.ConfigSetCmds;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ConfigSetParams;
+import org.apache.solr.core.CoreContainer;
+
+import java.util.Map;
+
+import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;
+import static org.apache.solr.common.params.CommonParams.NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;
+import static 
org.apache.solr.handler.admin.ConfigSetsHandler.DISABLE_CREATE_AUTH_CHECKS;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM;
+
+/**
+ * V2 API for creating a new configset as a copy of an existing one.
+ *
+ * <p>This API (POST /v2/cluster/configs {"create": {...}}) is analogous to
+ * the v1 /admin/configs?action=CREATE command.
+ *
+ */
+@EndPoint(method = POST, path = "/cluster/configs", permission = 
CONFIG_EDIT_PERM)
+public class CreateConfigSetAPI extends ConfigSetAPIBase {
+
+    public CreateConfigSetAPI(CoreContainer coreContainer) {
+        super(coreContainer);
+    }
+
+    @Command(name = "create")
+    @SuppressWarnings("unchecked")
+    public void create(PayloadObj<CreateConfigPayload> obj) throws Exception {
+        final CreateConfigPayload createConfigPayload = obj.get();
+        final String baseConfigSetName = (createConfigPayload.baseConfigSet != 
null) ? createConfigPayload.baseConfigSet : DEFAULT_CONFIGSET_NAME;

Review Comment:
   Maybe this default belongs in CreateConfigPayload where it will be 
self-documenting as a default and keeps the logic here simpler.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to