forked from apache/whirr
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WHIRR-73. Add a list command to the CLI.
git-svn-id: https://svn.apache.org/repos/asf/incubator/whirr/trunk@1003381 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
8 changed files
with
284 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
cli/src/main/java/org/apache/whirr/cli/command/ListClusterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* 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.whirr.cli.command; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.Iterables; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.PrintStream; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import joptsimple.OptionParser; | ||
import joptsimple.OptionSet; | ||
|
||
import org.apache.whirr.service.ClusterSpec; | ||
import org.apache.whirr.service.Service; | ||
import org.apache.whirr.service.ServiceFactory; | ||
import org.jclouds.compute.domain.NodeMetadata; | ||
|
||
/** | ||
* A command to list the nodes in a cluster. | ||
*/ | ||
public class ListClusterCommand extends AbstractClusterSpecCommand { | ||
|
||
public ListClusterCommand() throws IOException { | ||
this(new ServiceFactory()); | ||
} | ||
|
||
public ListClusterCommand(ServiceFactory factory) { | ||
super("list-cluster", "List the nodes in a cluster.", factory); | ||
} | ||
|
||
@Override | ||
public int run(InputStream in, PrintStream out, PrintStream err, | ||
List<String> args) throws Exception { | ||
|
||
OptionSet optionSet = parser.parse(args.toArray(new String[0])); | ||
|
||
if (!optionSet.nonOptionArguments().isEmpty()) { | ||
printUsage(parser, err); | ||
return -1; | ||
} | ||
try { | ||
ClusterSpec clusterSpec = getClusterSpec(optionSet); | ||
|
||
Service service = factory.create(clusterSpec.getServiceName()); | ||
Set<? extends NodeMetadata> nodes = service.getNodes(clusterSpec); | ||
for (NodeMetadata node : nodes) { | ||
out.println(Joiner.on('\t').join(node.getId(), node.getImageId(), | ||
getFirstAddress(node.getPublicAddresses()), | ||
getFirstAddress(node.getPrivateAddresses()), | ||
node.getState(), node.getLocation().getId())); | ||
} | ||
return 0; | ||
} catch (IllegalArgumentException e) { | ||
err.println(e.getMessage()); | ||
printUsage(parser, err); | ||
return -1; | ||
} | ||
} | ||
|
||
private String getFirstAddress(Set<String> addresses) { | ||
return addresses.isEmpty() ? "" : Iterables.get(addresses, 0); | ||
} | ||
|
||
private void printUsage(OptionParser parser, PrintStream stream) throws IOException { | ||
stream.println("Usage: whirr list-cluster [OPTIONS]"); | ||
stream.println(); | ||
parser.printHelpOn(stream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
cli/src/test/java/org/apache/whirr/cli/command/ListClusterCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* 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.whirr.cli.command; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.apache.whirr.service.ClusterSpec; | ||
import org.apache.whirr.service.Service; | ||
import org.apache.whirr.service.ServiceFactory; | ||
import org.hamcrest.Matcher; | ||
import org.jclouds.compute.domain.NodeMetadata; | ||
import org.jclouds.compute.domain.NodeState; | ||
import org.jclouds.compute.domain.internal.NodeMetadataImpl; | ||
import org.jclouds.domain.LocationScope; | ||
import org.jclouds.domain.internal.LocationImpl; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.internal.matchers.StringContains; | ||
|
||
public class ListClusterCommandTest { | ||
|
||
private ByteArrayOutputStream outBytes; | ||
private PrintStream out; | ||
private ByteArrayOutputStream errBytes; | ||
private PrintStream err; | ||
|
||
@Before | ||
public void setUp() { | ||
outBytes = new ByteArrayOutputStream(); | ||
out = new PrintStream(outBytes); | ||
errBytes = new ByteArrayOutputStream(); | ||
err = new PrintStream(errBytes); | ||
} | ||
|
||
@Test | ||
public void testInsufficientOptions() throws Exception { | ||
ListClusterCommand command = new ListClusterCommand(); | ||
int rc = command.run(null, null, err, Collections.<String>emptyList()); | ||
assertThat(rc, is(-1)); | ||
assertThat(errBytes.toString(), containsUsageString()); | ||
} | ||
|
||
private Matcher<String> containsUsageString() { | ||
return StringContains.containsString("Usage: whirr list-cluster [OPTIONS]"); | ||
} | ||
|
||
@Test | ||
public void testAllOptions() throws Exception { | ||
|
||
ServiceFactory factory = mock(ServiceFactory.class); | ||
Service service = mock(Service.class); | ||
when(factory.create((String) any())).thenReturn(service); | ||
NodeMetadata node1 = new NodeMetadataImpl(null, "name1", "id1", | ||
new LocationImpl(LocationScope.PROVIDER, "location-id1", | ||
"location-desc1", null), | ||
null, Collections.<String,String>emptyMap(), null, null, "image-id", | ||
null, NodeState.RUNNING, | ||
Lists.newArrayList("100.0.0.1"), | ||
Lists.newArrayList("10.0.0.1"), null); | ||
NodeMetadata node2 = new NodeMetadataImpl(null, "name2", "id2", | ||
new LocationImpl(LocationScope.PROVIDER, "location-id2", | ||
"location-desc2", null), | ||
null, Collections.<String,String>emptyMap(), null, null, "image-id", | ||
null, NodeState.RUNNING, | ||
Lists.newArrayList("100.0.0.2"), | ||
Lists.newArrayList("10.0.0.2"), null); | ||
when(service.getNodes((ClusterSpec) any())).thenReturn( | ||
(Set) Sets.newLinkedHashSet(Lists.newArrayList(node1, node2))); | ||
|
||
ListClusterCommand command = new ListClusterCommand(factory); | ||
|
||
int rc = command.run(null, out, null, Lists.newArrayList( | ||
"--service-name", "test-service", | ||
"--cluster-name", "test-cluster", | ||
"--identity", "myusername")); | ||
|
||
assertThat(rc, is(0)); | ||
|
||
assertThat(outBytes.toString(), is( | ||
"id1\timage-id\t100.0.0.1\t10.0.0.1\tRUNNING\tlocation-id1\n" + | ||
"id2\timage-id\t100.0.0.2\t10.0.0.2\tRUNNING\tlocation-id2\n")); | ||
|
||
verify(factory).create("test-service"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.