Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-13950 - NiFi CLI - add commands to list branch, bucket, flows, versions through NiFi API #9473

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum CommandOption {
FLOW_NAME("fn", "flowName", "A flow name", true),
FLOW_DESC("fd", "flowDesc", "A flow description", true),
FLOW_VERSION("fv", "flowVersion", "A version of a flow", true),
FLOW_BRANCH("fb", "flowBranch", "A branch for the flow", true),

FLOW_VERSION_1("fv1", "flowVersion1", "A version of a flow", true),
FLOW_VERSION_2("fv2", "flowVersion2", "A version of a flow", true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
import org.apache.nifi.toolkit.cli.impl.command.nifi.processors.ChangeVersionProcessor;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.CreateRegistryClient;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.GetRegistryClientId;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListBranches;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListBuckets;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListFlowVersions;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListFlows;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListRegistryClients;
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.UpdateRegistryClient;
import org.apache.nifi.toolkit.cli.impl.command.nifi.reporting.DeleteReportingTask;
Expand Down Expand Up @@ -145,6 +149,10 @@ protected List<Command> createCommands() {
commands.add(new CreateRegistryClient());
commands.add(new UpdateRegistryClient());
commands.add(new GetRegistryClientId());
commands.add(new ListBranches());
commands.add(new ListBuckets());
commands.add(new ListFlows());
commands.add(new ListFlowVersions());
commands.add(new PGImport());
commands.add(new PGConnect());
commands.add(new PGStart());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.registry;

import org.apache.commons.cli.MissingOptionException;
import org.apache.nifi.toolkit.cli.api.Context;
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.RegistryBranchesResult;
import org.apache.nifi.toolkit.client.NiFiClient;
import org.apache.nifi.toolkit.client.NiFiClientException;
import org.apache.nifi.web.api.entity.FlowRegistryBranchesEntity;

import java.io.IOException;
import java.util.Properties;

/**
* Lists the branches seen by the specified registry client.
*/
public class ListBranches extends AbstractNiFiCommand<RegistryBranchesResult> {

public ListBranches() {
super("list-branches", RegistryBranchesResult.class);
}

@Override
protected void doInitialize(Context context) {
super.doInitialize(context);
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
}

@Override
public String getDescription() {
return "Returns the list of branches seen by the specified registry client.";
}

@Override
public RegistryBranchesResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
final FlowRegistryBranchesEntity branches = client.getFlowClient().getFlowRegistryBranches(regClientId);
return new RegistryBranchesResult(getResultType(properties), branches);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.registry;

import org.apache.commons.cli.MissingOptionException;
import org.apache.nifi.toolkit.cli.api.Context;
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.RegistryBucketsResult;
import org.apache.nifi.toolkit.client.NiFiClient;
import org.apache.nifi.toolkit.client.NiFiClientException;
import org.apache.nifi.web.api.entity.FlowRegistryBucketsEntity;

import java.io.IOException;
import java.util.Properties;

/**
* Lists buckets for a given branch seen by a given registry client
*/
public class ListBuckets extends AbstractNiFiCommand<RegistryBucketsResult> {

public ListBuckets() {
super("list-buckets", RegistryBucketsResult.class);
}

@Override
protected void doInitialize(Context context) {
super.doInitialize(context);
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
addOption(CommandOption.FLOW_BRANCH.createOption());
}

@Override
public String getDescription() {
return "Returns the list of branches seen by the specified registry client.";
}

@Override
public RegistryBucketsResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
final FlowRegistryBucketsEntity buckets = client.getFlowClient().getFlowRegistryBuckets(regClientId, branchName);
return new RegistryBucketsResult(getResultType(properties), buckets);
}

}
Original file line number Diff line number Diff line change
@@ -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.nifi.toolkit.cli.impl.command.nifi.registry;

import org.apache.commons.cli.MissingOptionException;
import org.apache.nifi.toolkit.cli.api.Context;
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.RegistryFlowVersionsResult;
import org.apache.nifi.toolkit.client.NiFiClient;
import org.apache.nifi.toolkit.client.NiFiClientException;
import org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity;

import java.io.IOException;
import java.util.Properties;

/**
* Lists flow versions for a given flow in a given branch and bucket seen by a
* given registry client
*/
public class ListFlowVersions extends AbstractNiFiCommand<RegistryFlowVersionsResult> {

public ListFlowVersions() {
super("list-flow-versions", RegistryFlowVersionsResult.class);
}

@Override
protected void doInitialize(Context context) {
super.doInitialize(context);
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
addOption(CommandOption.FLOW_BRANCH.createOption());
addOption(CommandOption.BUCKET_ID.createOption());
addOption(CommandOption.FLOW_ID.createOption());
}

@Override
public String getDescription() {
return "Returns the list of flow versions for a given flow in a given branch and bucket seen by the specified registry client.";
}

@Override
public RegistryFlowVersionsResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
final VersionedFlowSnapshotMetadataSetEntity flowVersions = client.getFlowClient().getVersions(regClientId, bucketId, flowId, branchName);
return new RegistryFlowVersionsResult(getResultType(properties), flowVersions);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.registry;

import org.apache.commons.cli.MissingOptionException;
import org.apache.nifi.toolkit.cli.api.Context;
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.RegistryFlowsResult;
import org.apache.nifi.toolkit.client.NiFiClient;
import org.apache.nifi.toolkit.client.NiFiClientException;
import org.apache.nifi.web.api.entity.VersionedFlowsEntity;

import java.io.IOException;
import java.util.Properties;

/**
* Lists flows for a given branch and bucket seen by a given registry client
*/
public class ListFlows extends AbstractNiFiCommand<RegistryFlowsResult> {

public ListFlows() {
super("list-flows", RegistryFlowsResult.class);
}

@Override
protected void doInitialize(Context context) {
super.doInitialize(context);
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
addOption(CommandOption.FLOW_BRANCH.createOption());
addOption(CommandOption.BUCKET_ID.createOption());
}

@Override
public String getDescription() {
return "Returns the list of flows in a given branch and bucket seen by the specified registry client.";
}

@Override
public RegistryFlowsResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
final VersionedFlowsEntity flows = client.getFlowClient().getFlowRegistryFlows(regClientId, branchName, bucketId);
return new RegistryFlowsResult(getResultType(properties), flows);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.ResultType;
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.FlowRegistryBranchDTO;
import org.apache.nifi.web.api.entity.FlowRegistryBranchEntity;
import org.apache.nifi.web.api.entity.FlowRegistryBranchesEntity;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* Result for a FlowRegistryBranchesEntity.
*/
public class RegistryBranchesResult extends AbstractWritableResult<FlowRegistryBranchesEntity> {

final FlowRegistryBranchesEntity branchesEntity;

public RegistryBranchesResult(final ResultType resultType, final FlowRegistryBranchesEntity branchesEntity) {
super(resultType);
this.branchesEntity = Objects.requireNonNull(branchesEntity);
}

@Override
public FlowRegistryBranchesEntity getResult() {
return this.branchesEntity;
}

@Override
protected void writeSimpleResult(final PrintStream output) {
final Set<FlowRegistryBranchEntity> branches = branchesEntity.getBranches();
if (branches == null || branches.isEmpty()) {
return;
}

final List<FlowRegistryBranchDTO> branchesDTO = branches.stream()
.map(b -> b.getBranch())
.sorted(Comparator.comparing(FlowRegistryBranchDTO::getName))
.toList();

final Table table = new Table.Builder()
.column("#", 3, 3, false)
.column("Name", 20, 36, true)
.build();

for (int i = 0; i < branchesDTO.size(); i++) {
FlowRegistryBranchDTO branch = branchesDTO.get(i);
table.addRow("" + (i + 1), branch.getName());
}

final TableWriter tableWriter = new DynamicTableWriter();
tableWriter.write(table, output);
}

}
Loading