bbende commented on a change in pull request #3366: NIFI-6112: Add some useful 
commands to NiFi Toolkit for automating NiFi cluster construction.
URL: https://github.com/apache/nifi/pull/3366#discussion_r264827731
 
 

 ##########
 File path: 
nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/policies/UpdateAccessPolicy.java
 ##########
 @@ -0,0 +1,192 @@
+/*
+ * 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.nifi.toolkit.cli.impl.command.nifi.policies;
+
+import org.apache.commons.cli.MissingOptionException;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.toolkit.cli.api.AccessPolicyAction;
+import org.apache.nifi.toolkit.cli.api.CommandException;
+import org.apache.nifi.toolkit.cli.api.Context;
+import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient;
+import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException;
+import org.apache.nifi.toolkit.cli.impl.client.nifi.PoliciesClient;
+import org.apache.nifi.toolkit.cli.impl.client.nifi.TenantsClient;
+import org.apache.nifi.toolkit.cli.impl.command.CommandOption;
+import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
+import org.apache.nifi.toolkit.cli.impl.result.VoidResult;
+import org.apache.nifi.web.api.dto.AccessPolicyDTO;
+import org.apache.nifi.web.api.entity.AccessPolicyEntity;
+import org.apache.nifi.web.api.entity.TenantEntity;
+import org.apache.nifi.web.api.entity.UserEntity;
+import org.apache.nifi.web.api.entity.UserGroupEntity;
+import org.apache.nifi.web.api.entity.UserGroupsEntity;
+import org.apache.nifi.web.api.entity.UsersEntity;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Stream;
+
+/**
+ * Command for updating an access policy.
+ */
+public class UpdateAccessPolicy extends AbstractNiFiCommand<VoidResult> {
+
+    public UpdateAccessPolicy() {
+        super("update-policy", VoidResult.class);
+    }
+
+    @Override
+    public String getDescription() {
+        return "Updates an access policy.";
+    }
+
+    @Override
+    public void doInitialize(final Context context) {
+        addOption(CommandOption.POLICY_RESOURCE.createOption());
+        addOption(CommandOption.POLICY_ACTION.createOption());
+        addOption(CommandOption.USER_LIST.createOption());
+        addOption(CommandOption.GROUP_LIST.createOption());
+        addOption(CommandOption.OVERWRITE_POLICY.createOption());
+    }
+
+    @Override
+    public VoidResult doExecute(final NiFiClient client, final Properties 
properties)
+            throws NiFiClientException, IOException, MissingOptionException, 
CommandException {
+        final PoliciesClient policiesClient = client.getPoliciesClient();
+        final TenantsClient tenantsClient = client.getTenantsClient();
+
+        final String resource = "/" + 
StringUtils.removeStart(getRequiredArg(properties, 
CommandOption.POLICY_RESOURCE), "/");
+        final AccessPolicyAction actionType = AccessPolicyAction.valueOf(
+                getRequiredArg(properties, 
CommandOption.POLICY_ACTION).toUpperCase().trim());
+
+        final String users = getArg(properties, CommandOption.USER_LIST);
+        final String groups = getArg(properties, CommandOption.GROUP_LIST);
+
+        if (StringUtils.isBlank(users) && StringUtils.isBlank(groups)) {
+            throw new CommandException("Users and groups were blank, nothing 
to update");
+        }
+
+        final boolean overwrite = 
properties.containsKey(CommandOption.OVERWRITE_POLICY.getLongName());
+
+        AccessPolicyEntity policyEntity;
+        try {
+            policyEntity = policiesClient.getAccessPolicy(resource, 
actionType.toString().toLowerCase());
+        } catch (NiFiClientException e) {
+            policyEntity = null;
+        }
+
+        if (policyEntity == null) {
+            println("Access policy not found" +
 
 Review comment:
   In a couple of other commands we wrapped any println calls in a check to see 
`if isInteractive() || isVerbose(properties)`, with the idea that the user 
probably wants the output when in the interactive shell, but not when writing a 
script to chain together multiple calls.
   
   Do you think that makes sense here and possibly in a few other commands?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to