Skip to content

Commit

Permalink
[improve][cli] Add current option in the Clusters list cmd (#21139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technoboy- authored Sep 7, 2023
1 parent 3cb7926 commit d1f8f96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> clusters = getAdmin().clusters().getClusters();
String clusterName = getAdmin().brokers().getRuntimeConfigurations().get("clusterName");
final java.util.List<String> result = clusters.stream().map(c ->
c.equals(clusterName) ? (current ? c + "(*)" : c) : c
).collect(Collectors.toList());
print(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,4 +93,33 @@ public ClusterData buildClusterData() {
.brokerClientTlsTrustStorePassword("clientpw")
.build();
}

@Test
public void testListCmd() throws Exception {
List<String> clusterList = Lists.newArrayList("us-west", "us-east", "us-cent");
List<String> clusterResultList = Lists.newArrayList("us-west", "us-east", "us-cent(*)");
Map<String, String> 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);
}
}
}

0 comments on commit d1f8f96

Please sign in to comment.