gresockj commented on code in PR #8611: URL: https://github.com/apache/nifi/pull/8611#discussion_r1568880051
########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.pg; + +import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.lang3.StringUtils; +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.FlowClient; +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.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; +import org.apache.nifi.web.api.entity.VersionControlInformationEntity; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Command to change the version of a version controlled process group. + */ +public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { + + public PGChangeAllVersions() { + super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); + } + + @Override + public String getDescription() { + return "Changes the version for all of the controlled process group instances for a given flow ID. " + + "This can be used to upgrade all the instances of a versioned flow to a new version, or " + + "revert to a previous version. If no version is specified, the latest version will be used. " + + "If no process group ID is provided, the root process group will be used to recursively " + + "search for all instances of the Flow ID. It is possible to force the recursive operation " + + "and not stop the operation in case the upgrade of a process group fails."; + } + + @Override + protected void doInitialize(final Context context) { + addOption(CommandOption.FLOW_ID.createOption()); + addOption(CommandOption.FLOW_VERSION.createOption()); + addOption(CommandOption.PG_ID.createOption()); + addOption(CommandOption.FORCE.createOption()); + } + + @Override + public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) + throws NiFiClientException, IOException, MissingOptionException, CommandException { + + final FlowClient flowClient = client.getFlowClient(); + final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); + + // get the optional id of the parent PG, otherwise fallback to the root group + String parentPgId = getArg(properties, CommandOption.PG_ID); + if (StringUtils.isBlank(parentPgId)) { + parentPgId = flowClient.getRootGroupId(); + } + + PGList doPGList = new PGList(); Review Comment: We can have a few more `final` variables here, such as `doPGList`, `pgList`, and `doPGChangeVersion`. ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeVersion.java: ########## @@ -97,10 +105,13 @@ public VoidResult doExecute(final NiFiClient client, final Properties properties final VersionedFlowUpdateRequestEntity updateRequest = versionsClient.getUpdateRequest(updateRequestId); if (updateRequest != null && updateRequest.getRequest().isComplete()) { completed = true; + if(updateRequest.getRequest().getFailureReason() != null) { Review Comment: Space after `if` ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/NiFiCommandGroup.java: ########## @@ -133,6 +134,7 @@ protected List<Command> createCommands() { commands.add(new PGGetVersion()); commands.add(new PGStopVersionControl()); commands.add(new PGChangeVersion()); + commands.add(new PGChangeAllVersions()); Review Comment: The new command should be added to the toolkit guide as well ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.pg; + +import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.lang3.StringUtils; +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.FlowClient; +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.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; +import org.apache.nifi.web.api.entity.VersionControlInformationEntity; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Command to change the version of a version controlled process group. + */ +public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { + + public PGChangeAllVersions() { + super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); + } + + @Override + public String getDescription() { + return "Changes the version for all of the controlled process group instances for a given flow ID. " + + "This can be used to upgrade all the instances of a versioned flow to a new version, or " + + "revert to a previous version. If no version is specified, the latest version will be used. " + + "If no process group ID is provided, the root process group will be used to recursively " + + "search for all instances of the Flow ID. It is possible to force the recursive operation " + + "and not stop the operation in case the upgrade of a process group fails."; + } + + @Override + protected void doInitialize(final Context context) { + addOption(CommandOption.FLOW_ID.createOption()); + addOption(CommandOption.FLOW_VERSION.createOption()); + addOption(CommandOption.PG_ID.createOption()); + addOption(CommandOption.FORCE.createOption()); + } + + @Override + public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) + throws NiFiClientException, IOException, MissingOptionException, CommandException { + + final FlowClient flowClient = client.getFlowClient(); + final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); + + // get the optional id of the parent PG, otherwise fallback to the root group + String parentPgId = getArg(properties, CommandOption.PG_ID); + if (StringUtils.isBlank(parentPgId)) { + parentPgId = flowClient.getRootGroupId(); + } + + PGList doPGList = new PGList(); + List<ProcessGroupDTO> pgList = new ArrayList<ProcessGroupDTO>(); + recursivePGList(pgList, doPGList, client, properties, parentPgId); + + PGChangeVersion doPGChangeVersion = new PGChangeVersion(); + + // new version, if specified in the arguments + Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION); + + // force operation, if specified in the arguments + final boolean forceOperation = properties.containsKey(CommandOption.FORCE.getLongName()); + + final List<ProcessGroupDTO> processGroups = new ArrayList<>(); + final Map<String, ChangeVersionResult> changeVersionResults = new HashMap<String, ChangeVersionResult>(); + + for (ProcessGroupDTO pgDTO : pgList) { + final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgDTO.getId()); + + if(entity.getVersionControlInformation() == null || !entity.getVersionControlInformation().getFlowId().equals(flowId)) { + continue; // the process group is not version controlled or does not match the provided Flow ID + } + + if(newVersion == null) { + newVersion = doPGChangeVersion.getLatestVersion(client, entity.getVersionControlInformation()); + } + + processGroups.add(pgDTO); + + final Integer previousVersion = pgDTO.getVersionControlInformation().getVersion(); + if(previousVersion == newVersion) { + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(newVersion, newVersion, "Process group already at desired version")); + continue; // Process group already at desired version + } + + try { + doPGChangeVersion.changeVersion(client, entity, newVersion, pgDTO.getId(), getContext()); + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, newVersion, "SUCCESS")); + } catch (Exception e) { + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, null, e.getMessage())); + if(forceOperation) { + continue; + } else { + e.printStackTrace(); + break; + } + } + } + + return new ProcessGroupsVersionChangeResult(getResultType(properties), processGroups, changeVersionResults); + } + + private void recursivePGList(final List<ProcessGroupDTO> pgList, final PGList doPGList, final NiFiClient client, + final Properties properties, final String pgId) throws NiFiClientException, IOException { + ProcessGroupsResult result = doPGList.getList(client, properties, pgId); Review Comment: `final` ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/result/nifi/ChangeVersionResult.java: ########## @@ -0,0 +1,44 @@ +/* + * 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.result.nifi; + +/** + * Object to help with the result of a change version operation + */ +public class ChangeVersionResult { + Integer previousVersion; + Integer newVersion; + String message; + + public ChangeVersionResult(Integer previousVersion, Integer newVersion, String message) { Review Comment: Arguments could be `final` ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeVersion.java: ########## @@ -121,7 +132,7 @@ public VoidResult doExecute(final NiFiClient client, final Properties properties return VoidResult.getInstance(); } - private int getLatestVersion(final NiFiClient client, final VersionControlInformationDTO existingVersionControlDTO) + public int getLatestVersion(final NiFiClient client, final VersionControlInformationDTO existingVersionControlDTO) Review Comment: If I'm not mistaken, this could be package scope (just remove the `public`) ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGList.java: ########## @@ -66,7 +66,12 @@ public ProcessGroupsResult doExecute(final NiFiClient client, final Properties p parentPgId = flowClient.getRootGroupId(); } - final ProcessGroupFlowEntity processGroupFlowEntity = flowClient.getProcessGroup(parentPgId); + return getList(client, properties, parentPgId); + } + + public ProcessGroupsResult getList(final NiFiClient client, final Properties properties, String pgID) Review Comment: `final pgID` ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.pg; + +import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.lang3.StringUtils; +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.FlowClient; +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.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; +import org.apache.nifi.web.api.entity.VersionControlInformationEntity; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Command to change the version of a version controlled process group. + */ +public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { + + public PGChangeAllVersions() { + super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); + } + + @Override + public String getDescription() { + return "Changes the version for all of the controlled process group instances for a given flow ID. " + + "This can be used to upgrade all the instances of a versioned flow to a new version, or " + + "revert to a previous version. If no version is specified, the latest version will be used. " + + "If no process group ID is provided, the root process group will be used to recursively " + + "search for all instances of the Flow ID. It is possible to force the recursive operation " + + "and not stop the operation in case the upgrade of a process group fails."; + } + + @Override + protected void doInitialize(final Context context) { + addOption(CommandOption.FLOW_ID.createOption()); + addOption(CommandOption.FLOW_VERSION.createOption()); + addOption(CommandOption.PG_ID.createOption()); + addOption(CommandOption.FORCE.createOption()); + } + + @Override + public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) + throws NiFiClientException, IOException, MissingOptionException, CommandException { + + final FlowClient flowClient = client.getFlowClient(); + final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); + + // get the optional id of the parent PG, otherwise fallback to the root group + String parentPgId = getArg(properties, CommandOption.PG_ID); + if (StringUtils.isBlank(parentPgId)) { + parentPgId = flowClient.getRootGroupId(); + } + + PGList doPGList = new PGList(); + List<ProcessGroupDTO> pgList = new ArrayList<ProcessGroupDTO>(); + recursivePGList(pgList, doPGList, client, properties, parentPgId); + + PGChangeVersion doPGChangeVersion = new PGChangeVersion(); + + // new version, if specified in the arguments + Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION); + + // force operation, if specified in the arguments + final boolean forceOperation = properties.containsKey(CommandOption.FORCE.getLongName()); + + final List<ProcessGroupDTO> processGroups = new ArrayList<>(); + final Map<String, ChangeVersionResult> changeVersionResults = new HashMap<String, ChangeVersionResult>(); + + for (ProcessGroupDTO pgDTO : pgList) { + final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgDTO.getId()); + + if(entity.getVersionControlInformation() == null || !entity.getVersionControlInformation().getFlowId().equals(flowId)) { + continue; // the process group is not version controlled or does not match the provided Flow ID + } + + if(newVersion == null) { + newVersion = doPGChangeVersion.getLatestVersion(client, entity.getVersionControlInformation()); + } + + processGroups.add(pgDTO); + + final Integer previousVersion = pgDTO.getVersionControlInformation().getVersion(); + if(previousVersion == newVersion) { + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(newVersion, newVersion, "Process group already at desired version")); + continue; // Process group already at desired version + } + + try { + doPGChangeVersion.changeVersion(client, entity, newVersion, pgDTO.getId(), getContext()); + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, newVersion, "SUCCESS")); + } catch (Exception e) { + changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, null, e.getMessage())); + if(forceOperation) { Review Comment: Space after the `if` ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/result/nifi/ProcessGroupsVersionChangeResult.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.result.nifi; + +import org.apache.nifi.toolkit.cli.api.Context; +import org.apache.nifi.toolkit.cli.api.ReferenceResolver; +import org.apache.nifi.toolkit.cli.api.Referenceable; +import org.apache.nifi.toolkit.cli.api.ResolvedReference; +import org.apache.nifi.toolkit.cli.api.ResultType; +import org.apache.nifi.toolkit.cli.impl.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.result.AbstractWritableResult; +import org.apache.nifi.toolkit.cli.impl.result.writer.DynamicTableWriter; +import org.apache.nifi.toolkit.cli.impl.result.writer.Table; +import org.apache.nifi.toolkit.cli.impl.result.writer.TableWriter; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; + +import java.io.PrintStream; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Result for a list of ProcessGroupEntities. + */ +public class ProcessGroupsVersionChangeResult extends AbstractWritableResult<List<ProcessGroupDTO>> implements Referenceable { + + private final List<ProcessGroupDTO> processGroups; + private final Map<String, ChangeVersionResult> changeVersionResults; + + public ProcessGroupsVersionChangeResult(final ResultType resultType, final List<ProcessGroupDTO> processGroups, + final Map<String, ChangeVersionResult> changeVersionResults) { + super(resultType); + this.processGroups = Objects.requireNonNull(processGroups); + this.processGroups.sort(Comparator.comparing(ProcessGroupDTO::getName)); + this.changeVersionResults = Objects.requireNonNull(changeVersionResults); + } + + @Override + public List<ProcessGroupDTO> getResult() { + return processGroups; + } + + @Override + protected void writeSimpleResult(final PrintStream output) { + + final Table table = new Table.Builder() + .column("#", 3, 3, false) + .column("Name", 20, 36, true) + .column("Id", 36, 36, false) + .column("Prev Version", 15, 15, false) + .column("New Version", 15, 15, false) + .column("Message", 100, 100, false) + .build(); + + for (int i=0; i < processGroups.size(); i++) { + final ProcessGroupDTO dto = processGroups.get(i); + table.addRow( + String.valueOf(i+1), + dto.getName(), + dto.getId(), + String.valueOf(changeVersionResults.get(dto.getId()).getPreviousVersion()), + String.valueOf(changeVersionResults.get(dto.getId()).getNewVersion()), + String.valueOf(changeVersionResults.get(dto.getId()).getMessage()) + ); + } + + final TableWriter tableWriter = new DynamicTableWriter(); + tableWriter.write(table, output); + } + + @Override + public ReferenceResolver createReferenceResolver(final Context context) { + final Map<Integer,ProcessGroupDTO> backRefs = new HashMap<>(); Review Comment: Space after the comma ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.pg; + +import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.lang3.StringUtils; +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.FlowClient; +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.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; +import org.apache.nifi.web.api.entity.VersionControlInformationEntity; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Command to change the version of a version controlled process group. + */ +public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { + + public PGChangeAllVersions() { + super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); + } + + @Override + public String getDescription() { + return "Changes the version for all of the controlled process group instances for a given flow ID. " + + "This can be used to upgrade all the instances of a versioned flow to a new version, or " + + "revert to a previous version. If no version is specified, the latest version will be used. " + + "If no process group ID is provided, the root process group will be used to recursively " + + "search for all instances of the Flow ID. It is possible to force the recursive operation " + + "and not stop the operation in case the upgrade of a process group fails."; + } + + @Override + protected void doInitialize(final Context context) { + addOption(CommandOption.FLOW_ID.createOption()); + addOption(CommandOption.FLOW_VERSION.createOption()); + addOption(CommandOption.PG_ID.createOption()); + addOption(CommandOption.FORCE.createOption()); + } + + @Override + public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) + throws NiFiClientException, IOException, MissingOptionException, CommandException { + + final FlowClient flowClient = client.getFlowClient(); + final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); + + // get the optional id of the parent PG, otherwise fallback to the root group + String parentPgId = getArg(properties, CommandOption.PG_ID); + if (StringUtils.isBlank(parentPgId)) { + parentPgId = flowClient.getRootGroupId(); + } + + PGList doPGList = new PGList(); + List<ProcessGroupDTO> pgList = new ArrayList<ProcessGroupDTO>(); + recursivePGList(pgList, doPGList, client, properties, parentPgId); + + PGChangeVersion doPGChangeVersion = new PGChangeVersion(); + + // new version, if specified in the arguments + Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION); + + // force operation, if specified in the arguments + final boolean forceOperation = properties.containsKey(CommandOption.FORCE.getLongName()); + + final List<ProcessGroupDTO> processGroups = new ArrayList<>(); + final Map<String, ChangeVersionResult> changeVersionResults = new HashMap<String, ChangeVersionResult>(); + + for (ProcessGroupDTO pgDTO : pgList) { Review Comment: This can be `final` as well. ########## nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java: ########## @@ -0,0 +1,140 @@ +/* + * 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.pg; + +import org.apache.commons.cli.MissingOptionException; +import org.apache.commons.lang3.StringUtils; +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.FlowClient; +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.command.CommandOption; +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; +import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; +import org.apache.nifi.web.api.dto.ProcessGroupDTO; +import org.apache.nifi.web.api.entity.VersionControlInformationEntity; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Command to change the version of a version controlled process group. + */ +public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { + + public PGChangeAllVersions() { + super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); + } + + @Override + public String getDescription() { + return "Changes the version for all of the controlled process group instances for a given flow ID. " + + "This can be used to upgrade all the instances of a versioned flow to a new version, or " + + "revert to a previous version. If no version is specified, the latest version will be used. " + + "If no process group ID is provided, the root process group will be used to recursively " + + "search for all instances of the Flow ID. It is possible to force the recursive operation " + + "and not stop the operation in case the upgrade of a process group fails."; + } + + @Override + protected void doInitialize(final Context context) { + addOption(CommandOption.FLOW_ID.createOption()); + addOption(CommandOption.FLOW_VERSION.createOption()); + addOption(CommandOption.PG_ID.createOption()); + addOption(CommandOption.FORCE.createOption()); + } + + @Override + public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) + throws NiFiClientException, IOException, MissingOptionException, CommandException { + + final FlowClient flowClient = client.getFlowClient(); + final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); + + // get the optional id of the parent PG, otherwise fallback to the root group + String parentPgId = getArg(properties, CommandOption.PG_ID); + if (StringUtils.isBlank(parentPgId)) { + parentPgId = flowClient.getRootGroupId(); + } + + PGList doPGList = new PGList(); + List<ProcessGroupDTO> pgList = new ArrayList<ProcessGroupDTO>(); + recursivePGList(pgList, doPGList, client, properties, parentPgId); + + PGChangeVersion doPGChangeVersion = new PGChangeVersion(); + + // new version, if specified in the arguments + Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION); + + // force operation, if specified in the arguments + final boolean forceOperation = properties.containsKey(CommandOption.FORCE.getLongName()); + + final List<ProcessGroupDTO> processGroups = new ArrayList<>(); + final Map<String, ChangeVersionResult> changeVersionResults = new HashMap<String, ChangeVersionResult>(); + + for (ProcessGroupDTO pgDTO : pgList) { + final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgDTO.getId()); + + if(entity.getVersionControlInformation() == null || !entity.getVersionControlInformation().getFlowId().equals(flowId)) { + continue; // the process group is not version controlled or does not match the provided Flow ID + } + + if(newVersion == null) { + newVersion = doPGChangeVersion.getLatestVersion(client, entity.getVersionControlInformation()); + } + + processGroups.add(pgDTO); + + final Integer previousVersion = pgDTO.getVersionControlInformation().getVersion(); + if(previousVersion == newVersion) { Review Comment: Let's add a space after the `if` for style consistency. -- 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]
