From d1f8f96ad1dbcd4f0e6f5ed03ad432af88c3f8f1 Mon Sep 17 00:00:00 2001 From: Jiwei Guo Date: Thu, 7 Sep 2023 03:10:41 -0500 Subject: [PATCH] [improve][cli] Add current option in the Clusters list cmd (#21139) --- .../apache/pulsar/admin/cli/CmdClusters.java | 13 ++++++- .../pulsar/admin/cli/TestCmdClusters.java | 39 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdClusters.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdClusters.java index 6d6b4e72268cb..1653de93a738c 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdClusters.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdClusters.java @@ -24,6 +24,7 @@ import com.google.common.collect.Sets; import java.util.Arrays; import java.util.function.Supplier; +import java.util.stream.Collectors; import lombok.Getter; import org.apache.commons.lang3.StringUtils; import org.apache.pulsar.admin.cli.utils.CmdUtils; @@ -41,8 +42,18 @@ public class CmdClusters extends CmdBase { @Parameters(commandDescription = "List the existing clusters") private class List extends CliCommand { + + @Parameter(names = { "-c", "--current" }, + description = "Print the current cluster with (*)", required = false) + private boolean current = false; + void run() throws PulsarAdminException { - print(getAdmin().clusters().getClusters()); + java.util.List clusters = getAdmin().clusters().getClusters(); + String clusterName = getAdmin().brokers().getRuntimeConfigurations().get("clusterName"); + final java.util.List result = clusters.stream().map(c -> + c.equals(clusterName) ? (current ? c + "(*)" : c) : c + ).collect(Collectors.toList()); + print(result); } } diff --git a/pulsar-client-tools/src/test/java/org/apache/pulsar/admin/cli/TestCmdClusters.java b/pulsar-client-tools/src/test/java/org/apache/pulsar/admin/cli/TestCmdClusters.java index 86faabccc80ea..a09dce8cd8516 100644 --- a/pulsar-client-tools/src/test/java/org/apache/pulsar/admin/cli/TestCmdClusters.java +++ b/pulsar-client-tools/src/test/java/org/apache/pulsar/admin/cli/TestCmdClusters.java @@ -18,21 +18,27 @@ */ package org.apache.pulsar.admin.cli; +import com.google.common.collect.Lists; +import org.apache.pulsar.client.admin.Brokers; import org.apache.pulsar.client.api.ProxyProtocol; - import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.PrintStream; import java.nio.file.Files; +import java.util.List; +import java.util.Map; import org.apache.pulsar.admin.cli.utils.CmdUtils; import org.apache.pulsar.common.policies.data.ClusterData; import org.apache.pulsar.client.admin.Clusters; import org.apache.pulsar.client.admin.PulsarAdmin; import org.apache.pulsar.common.util.ObjectMapperFactory; +import org.assertj.core.util.Maps; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -87,4 +93,33 @@ public ClusterData buildClusterData() { .brokerClientTlsTrustStorePassword("clientpw") .build(); } + + @Test + public void testListCmd() throws Exception { + List clusterList = Lists.newArrayList("us-west", "us-east", "us-cent"); + List clusterResultList = Lists.newArrayList("us-west", "us-east", "us-cent(*)"); + Map configurations = Maps.newHashMap("clusterName", "us-cent"); + + Clusters clusters = mock(Clusters.class); + Brokers brokers = mock(Brokers.class); + PulsarAdmin admin = mock(PulsarAdmin.class); + when(admin.clusters()).thenReturn(clusters); + when(admin.brokers()).thenReturn(brokers); + doReturn(clusterList).when(clusters).getClusters(); + doReturn(configurations).when(brokers).getRuntimeConfigurations(); + + CmdClusters cmd = new CmdClusters(() -> admin); + + PrintStream defaultSystemOut = System.out; + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out)) { + System.setOut(ps); + cmd.run("list".split("\\s+")); + Assert.assertEquals(out.toString(), String.join("\n", clusterList) + "\n"); + out.reset(); + cmd.run("list -c".split("\\s+")); + Assert.assertEquals(out.toString(), String.join("\n", clusterResultList) + "\n"); + } finally { + System.setOut(defaultSystemOut); + } + } }