Skip to content

Commit 3508560

Browse files
authored
NIFI-13950 Add commands to list branch, bucket, flows, versions to CLI (#9473)
Signed-off-by: David Handermann <[email protected]>
1 parent e082b60 commit 3508560

File tree

12 files changed

+712
-9
lines changed

12 files changed

+712
-9
lines changed

nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/CommandOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum CommandOption {
4747
FLOW_NAME("fn", "flowName", "A flow name", true),
4848
FLOW_DESC("fd", "flowDesc", "A flow description", true),
4949
FLOW_VERSION("fv", "flowVersion", "A version of a flow", true),
50+
FLOW_BRANCH("fb", "flowBranch", "A branch for the flow", true),
5051

5152
FLOW_VERSION_1("fv1", "flowVersion1", "A version of a flow", true),
5253
FLOW_VERSION_2("fv2", "flowVersion2", "A version of a flow", true),

nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/NiFiCommandGroup.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
import org.apache.nifi.toolkit.cli.impl.command.nifi.processors.ProcessorStart;
111111
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.CreateRegistryClient;
112112
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.GetRegistryClientId;
113+
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListBranches;
114+
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListBuckets;
115+
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListFlowVersions;
116+
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListFlows;
113117
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.ListRegistryClients;
114118
import org.apache.nifi.toolkit.cli.impl.command.nifi.registry.UpdateRegistryClient;
115119
import org.apache.nifi.toolkit.cli.impl.command.nifi.reporting.DeleteReportingTask;
@@ -149,6 +153,10 @@ protected List<Command> createCommands() {
149153
commands.add(new CreateRegistryClient());
150154
commands.add(new UpdateRegistryClient());
151155
commands.add(new GetRegistryClientId());
156+
commands.add(new ListBranches());
157+
commands.add(new ListBuckets());
158+
commands.add(new ListFlows());
159+
commands.add(new ListFlowVersions());
152160
commands.add(new PGImport());
153161
commands.add(new PGConnect());
154162
commands.add(new PGStart());
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.toolkit.cli.impl.command.nifi.registry;
18+
19+
import org.apache.commons.cli.MissingOptionException;
20+
import org.apache.nifi.toolkit.cli.api.Context;
21+
import org.apache.nifi.toolkit.cli.impl.command.CommandOption;
22+
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
23+
import org.apache.nifi.toolkit.cli.impl.result.nifi.RegistryBranchesResult;
24+
import org.apache.nifi.toolkit.client.NiFiClient;
25+
import org.apache.nifi.toolkit.client.NiFiClientException;
26+
import org.apache.nifi.web.api.entity.FlowRegistryBranchesEntity;
27+
28+
import java.io.IOException;
29+
import java.util.Properties;
30+
31+
/**
32+
* Lists the branches seen by the specified registry client.
33+
*/
34+
public class ListBranches extends AbstractNiFiCommand<RegistryBranchesResult> {
35+
36+
public ListBranches() {
37+
super("list-branches", RegistryBranchesResult.class);
38+
}
39+
40+
@Override
41+
protected void doInitialize(Context context) {
42+
super.doInitialize(context);
43+
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
44+
}
45+
46+
@Override
47+
public String getDescription() {
48+
return "Returns the list of branches seen by the specified registry client.";
49+
}
50+
51+
@Override
52+
public RegistryBranchesResult doExecute(final NiFiClient client, final Properties properties)
53+
throws NiFiClientException, IOException, MissingOptionException {
54+
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
55+
final FlowRegistryBranchesEntity branches = client.getFlowClient().getFlowRegistryBranches(regClientId);
56+
return new RegistryBranchesResult(getResultType(properties), branches);
57+
}
58+
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.toolkit.cli.impl.command.nifi.registry;
18+
19+
import org.apache.commons.cli.MissingOptionException;
20+
import org.apache.nifi.toolkit.cli.api.Context;
21+
import org.apache.nifi.toolkit.cli.impl.command.CommandOption;
22+
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
23+
import org.apache.nifi.toolkit.cli.impl.result.nifi.RegistryBucketsResult;
24+
import org.apache.nifi.toolkit.client.NiFiClient;
25+
import org.apache.nifi.toolkit.client.NiFiClientException;
26+
import org.apache.nifi.web.api.entity.FlowRegistryBucketsEntity;
27+
28+
import java.io.IOException;
29+
import java.util.Properties;
30+
31+
/**
32+
* Lists buckets for a given branch seen by a given registry client
33+
*/
34+
public class ListBuckets extends AbstractNiFiCommand<RegistryBucketsResult> {
35+
36+
public ListBuckets() {
37+
super("list-buckets", RegistryBucketsResult.class);
38+
}
39+
40+
@Override
41+
protected void doInitialize(Context context) {
42+
super.doInitialize(context);
43+
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
44+
addOption(CommandOption.FLOW_BRANCH.createOption());
45+
}
46+
47+
@Override
48+
public String getDescription() {
49+
return "Returns the list of branches seen by the specified registry client.";
50+
}
51+
52+
@Override
53+
public RegistryBucketsResult doExecute(final NiFiClient client, final Properties properties)
54+
throws NiFiClientException, IOException, MissingOptionException {
55+
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
56+
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
57+
final FlowRegistryBucketsEntity buckets = client.getFlowClient().getFlowRegistryBuckets(regClientId, branchName);
58+
return new RegistryBucketsResult(getResultType(properties), buckets);
59+
}
60+
61+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.toolkit.cli.impl.command.nifi.registry;
18+
19+
import org.apache.commons.cli.MissingOptionException;
20+
import org.apache.nifi.toolkit.cli.api.Context;
21+
import org.apache.nifi.toolkit.cli.impl.command.CommandOption;
22+
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
23+
import org.apache.nifi.toolkit.cli.impl.result.nifi.RegistryFlowVersionsResult;
24+
import org.apache.nifi.toolkit.client.NiFiClient;
25+
import org.apache.nifi.toolkit.client.NiFiClientException;
26+
import org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity;
27+
28+
import java.io.IOException;
29+
import java.util.Properties;
30+
31+
/**
32+
* Lists flow versions for a given flow in a given branch and bucket seen by a
33+
* given registry client
34+
*/
35+
public class ListFlowVersions extends AbstractNiFiCommand<RegistryFlowVersionsResult> {
36+
37+
public ListFlowVersions() {
38+
super("list-flow-versions", RegistryFlowVersionsResult.class);
39+
}
40+
41+
@Override
42+
protected void doInitialize(Context context) {
43+
super.doInitialize(context);
44+
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
45+
addOption(CommandOption.FLOW_BRANCH.createOption());
46+
addOption(CommandOption.BUCKET_ID.createOption());
47+
addOption(CommandOption.FLOW_ID.createOption());
48+
}
49+
50+
@Override
51+
public String getDescription() {
52+
return "Returns the list of flow versions for a given flow in a given branch and bucket seen by the specified registry client.";
53+
}
54+
55+
@Override
56+
public RegistryFlowVersionsResult doExecute(final NiFiClient client, final Properties properties)
57+
throws NiFiClientException, IOException, MissingOptionException {
58+
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
59+
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
60+
final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
61+
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
62+
final VersionedFlowSnapshotMetadataSetEntity flowVersions = client.getFlowClient().getVersions(regClientId, bucketId, flowId, branchName);
63+
return new RegistryFlowVersionsResult(getResultType(properties), flowVersions);
64+
}
65+
66+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.toolkit.cli.impl.command.nifi.registry;
18+
19+
import org.apache.commons.cli.MissingOptionException;
20+
import org.apache.nifi.toolkit.cli.api.Context;
21+
import org.apache.nifi.toolkit.cli.impl.command.CommandOption;
22+
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand;
23+
import org.apache.nifi.toolkit.cli.impl.result.nifi.RegistryFlowsResult;
24+
import org.apache.nifi.toolkit.client.NiFiClient;
25+
import org.apache.nifi.toolkit.client.NiFiClientException;
26+
import org.apache.nifi.web.api.entity.VersionedFlowsEntity;
27+
28+
import java.io.IOException;
29+
import java.util.Properties;
30+
31+
/**
32+
* Lists flows for a given branch and bucket seen by a given registry client
33+
*/
34+
public class ListFlows extends AbstractNiFiCommand<RegistryFlowsResult> {
35+
36+
public ListFlows() {
37+
super("list-flows", RegistryFlowsResult.class);
38+
}
39+
40+
@Override
41+
protected void doInitialize(Context context) {
42+
super.doInitialize(context);
43+
addOption(CommandOption.REGISTRY_CLIENT_ID.createOption());
44+
addOption(CommandOption.FLOW_BRANCH.createOption());
45+
addOption(CommandOption.BUCKET_ID.createOption());
46+
}
47+
48+
@Override
49+
public String getDescription() {
50+
return "Returns the list of flows in a given branch and bucket seen by the specified registry client.";
51+
}
52+
53+
@Override
54+
public RegistryFlowsResult doExecute(final NiFiClient client, final Properties properties)
55+
throws NiFiClientException, IOException, MissingOptionException {
56+
final String regClientId = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
57+
final String branchName = getRequiredArg(properties, CommandOption.FLOW_BRANCH);
58+
final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
59+
final VersionedFlowsEntity flows = client.getFlowClient().getFlowRegistryFlows(regClientId, branchName, bucketId);
60+
return new RegistryFlowsResult(getResultType(properties), flows);
61+
}
62+
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.toolkit.cli.impl.result.nifi;
18+
19+
import org.apache.nifi.toolkit.cli.api.ResultType;
20+
import org.apache.nifi.toolkit.cli.impl.result.AbstractWritableResult;
21+
import org.apache.nifi.toolkit.cli.impl.result.writer.DynamicTableWriter;
22+
import org.apache.nifi.toolkit.cli.impl.result.writer.Table;
23+
import org.apache.nifi.toolkit.cli.impl.result.writer.TableWriter;
24+
import org.apache.nifi.web.api.dto.FlowRegistryBranchDTO;
25+
import org.apache.nifi.web.api.entity.FlowRegistryBranchEntity;
26+
import org.apache.nifi.web.api.entity.FlowRegistryBranchesEntity;
27+
28+
import java.io.PrintStream;
29+
import java.util.Comparator;
30+
import java.util.List;
31+
import java.util.Objects;
32+
import java.util.Set;
33+
34+
/**
35+
* Result for a FlowRegistryBranchesEntity.
36+
*/
37+
public class RegistryBranchesResult extends AbstractWritableResult<FlowRegistryBranchesEntity> {
38+
39+
final FlowRegistryBranchesEntity branchesEntity;
40+
41+
public RegistryBranchesResult(final ResultType resultType, final FlowRegistryBranchesEntity branchesEntity) {
42+
super(resultType);
43+
this.branchesEntity = Objects.requireNonNull(branchesEntity);
44+
}
45+
46+
@Override
47+
public FlowRegistryBranchesEntity getResult() {
48+
return this.branchesEntity;
49+
}
50+
51+
@Override
52+
protected void writeSimpleResult(final PrintStream output) {
53+
final Set<FlowRegistryBranchEntity> branches = branchesEntity.getBranches();
54+
if (branches == null || branches.isEmpty()) {
55+
return;
56+
}
57+
58+
final List<FlowRegistryBranchDTO> branchesDTO = branches.stream()
59+
.map(b -> b.getBranch())
60+
.sorted(Comparator.comparing(FlowRegistryBranchDTO::getName))
61+
.toList();
62+
63+
final Table table = new Table.Builder()
64+
.column("#", 3, 3, false)
65+
.column("Name", 20, 36, true)
66+
.build();
67+
68+
for (int i = 0; i < branchesDTO.size(); i++) {
69+
FlowRegistryBranchDTO branch = branchesDTO.get(i);
70+
table.addRow("" + (i + 1), branch.getName());
71+
}
72+
73+
final TableWriter tableWriter = new DynamicTableWriter();
74+
tableWriter.write(table, output);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)