Added getNodes withDetails()

This commit is contained in:
Ivan Meredith 2010-04-18 20:29:18 +12:00
parent 31d2b08b71
commit 4b8b0e8770
11 changed files with 133 additions and 12 deletions

View File

@ -63,6 +63,7 @@ import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl; import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.predicates.RunScriptRunning; import org.jclouds.compute.predicates.RunScriptRunning;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.DestroyNodeStrategy; import org.jclouds.compute.strategy.DestroyNodeStrategy;
@ -124,7 +125,7 @@ public class EC2ComputeServiceContextModule extends EC2ContextModule {
} }
@Override @Override
public Iterable<? extends ComputeMetadata> execute() { public Iterable<? extends ComputeMetadata> execute(GetNodesOptions options) {
Set<NodeMetadata> nodes = Sets.newHashSet(); Set<NodeMetadata> nodes = Sets.newHashSet();
for (Region region : ImmutableSet.of(Region.US_EAST_1, Region.US_WEST_1, Region.EU_WEST_1)) { for (Region region : ImmutableSet.of(Region.US_EAST_1, Region.US_WEST_1, Region.EU_WEST_1)) {
Iterables.addAll(nodes, Iterables.transform(Iterables.concat(client Iterables.addAll(nodes, Iterables.transform(Iterables.concat(client

View File

@ -27,6 +27,7 @@ import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.BaseComputeService; import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.ssh.ExecResponse; import org.jclouds.ssh.ExecResponse;
@ -77,6 +78,12 @@ public interface ComputeService {
*/ */
Map<String, ? extends ComputeMetadata> getNodes(); Map<String, ? extends ComputeMetadata> getNodes();
/**
* all nodes available to the current user by id. If possible, the returned set will include
* {@link NodeMetadata} objects.
*/
Map<String, ? extends ComputeMetadata> getNodes(GetNodesOptions options);
/** /**
* The get locations command returns all the valid locations for nodes. A location has a scope, * The get locations command returns all the valid locations for nodes. A location has a scope,
* which is typically region or zone. A region is a general area, like eu-west, where a zone is * which is typically region or zone. A region is a general area, like eu-west, where a zone is

View File

@ -53,6 +53,7 @@ import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.Size; import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.DestroyNodeStrategy; import org.jclouds.compute.strategy.DestroyNodeStrategy;
@ -172,7 +173,7 @@ public class BaseComputeService implements ComputeService {
"starting nodes"); "starting nodes");
if (exceptions.size() > 0 && template.getOptions().shouldDestroyOnError()) { if (exceptions.size() > 0 && template.getOptions().shouldDestroyOnError()) {
ImmutableMap<?, ? extends ComputeMetadata> currentNodes = Maps.uniqueIndex( ImmutableMap<?, ? extends ComputeMetadata> currentNodes = Maps.uniqueIndex(
listNodesStrategy.execute(), METADATA_TO_ID); listNodesStrategy.execute(GetNodesOptions.NONE), METADATA_TO_ID);
for (Entry<?, Exception> entry : exceptions.entrySet()) { for (Entry<?, Exception> entry : exceptions.entrySet()) {
logger.error(entry.getValue(), "<< error applying nodes(%s) [%s] destroying ", entry logger.error(entry.getValue(), "<< error applying nodes(%s) [%s] destroying ", entry
.getKey(), entry.getValue().getMessage()); .getKey(), entry.getValue().getMessage());
@ -221,18 +222,28 @@ public class BaseComputeService implements ComputeService {
public Map<String, ? extends ComputeMetadata> getNodes() { public Map<String, ? extends ComputeMetadata> getNodes() {
logger.debug(">> listing servers"); logger.debug(">> listing servers");
ImmutableMap<String, ? extends ComputeMetadata> map = Maps.uniqueIndex(listNodesStrategy ImmutableMap<String, ? extends ComputeMetadata> map = Maps.uniqueIndex(listNodesStrategy
.execute(), METADATA_TO_ID); .execute(GetNodesOptions.NONE), METADATA_TO_ID);
logger.debug("<< list(%d)", map.size());
return map;
}
@Override
public Map<String, ? extends ComputeMetadata> getNodes(GetNodesOptions options) {
logger.debug(">> listing servers");
if(options == null){
options = GetNodesOptions.NONE;
}
ImmutableMap<String, ? extends ComputeMetadata> map = Maps.uniqueIndex(listNodesStrategy
.execute(options), METADATA_TO_ID);
logger.debug("<< list(%d)", map.size()); logger.debug("<< list(%d)", map.size());
return map; return map;
} }
/** /**
* If the result of {@link ListNodesStrategy#execute} is a set of nodes, then return them. * If the result of {@link ListNodesStrategy#execute} is a set of nodes, then return them.
* Otherwise iteratively call {@link #getNodeMetadata} * Otherwise iteratively call {@link #getNodeMetadata}
*/ */
protected Map<String, ? extends NodeMetadata> doGetNodesWithTag(final String tag) { protected Map<String, ? extends NodeMetadata> doGetNodesWithTag(final String tag) {
Iterable<? extends NodeMetadata> nodes = Iterables.filter(Iterables.transform( Iterable<? extends NodeMetadata> nodes = Iterables.filter(Iterables.transform(
listNodesStrategy.execute(), new Function<ComputeMetadata, NodeMetadata>() { listNodesStrategy.execute(GetNodesOptions.NONE), new Function<ComputeMetadata, NodeMetadata>() {
@Override @Override
public NodeMetadata apply(ComputeMetadata from) { public NodeMetadata apply(ComputeMetadata from) {

View File

@ -0,0 +1,82 @@
package org.jclouds.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Ivan Meredith
*/
public class GetNodesOptions implements Cloneable {
public static final ImmutableGetNodesOptions NONE = new ImmutableGetNodesOptions(
new GetNodesOptions());
private boolean detailed;
public GetNodesOptions() {
}
GetNodesOptions(boolean detailed) {
this.detailed = detailed;
}
public static class ImmutableGetNodesOptions extends GetNodesOptions {
private final GetNodesOptions delegate;
public ImmutableGetNodesOptions(GetNodesOptions delegate) {
this.delegate = delegate;
}
@Override
public boolean isDetailed() {
return delegate.isDetailed();
}
@Override
public GetNodesOptions clone() {
return delegate.clone();
}
@Override
public String toString() {
return delegate.toString();
}
}
public boolean isDetailed() {
return detailed;
}
/**
* populate each result with detailed such as metadata even if it incurs extra requests to the
* service.
*/
public GetNodesOptions withDetails() {
this.detailed = true;
return this;
}
public static class Builder {
/**
* @see GetNodesOptions#withDetails()
*/
public static GetNodesOptions withDetails() {
GetNodesOptions options = new GetNodesOptions();
return options.withDetails();
}
}
@Override
public GetNodesOptions clone() {
return new GetNodesOptions(detailed);
}
@Override
public String toString() {
return "[detailed=" + detailed + "]";
}
}

View File

@ -20,6 +20,7 @@
package org.jclouds.compute.strategy; package org.jclouds.compute.strategy;
import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.options.GetNodesOptions;
/** /**
* *
@ -27,6 +28,5 @@ import org.jclouds.compute.domain.ComputeMetadata;
*/ */
public interface ListNodesStrategy { public interface ListNodesStrategy {
Iterable<? extends ComputeMetadata> execute(); Iterable<? extends ComputeMetadata> execute(GetNodesOptions options);
} }

View File

@ -37,6 +37,7 @@ import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.internal.BaseComputeService; import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.AddNodeWithTagStrategy; import org.jclouds.compute.strategy.AddNodeWithTagStrategy;
import org.jclouds.compute.strategy.ListNodesStrategy; import org.jclouds.compute.strategy.ListNodesStrategy;
@ -107,7 +108,7 @@ public class OneByOneRunNodesAndAddToSetStrategy implements RunNodesAndAddToSetS
Set<String> names = Sets.newHashSet(); Set<String> names = Sets.newHashSet();
int nodeIndex = new SecureRandom().nextInt(8096); int nodeIndex = new SecureRandom().nextInt(8096);
Map<String, ? extends ComputeMetadata> currentNodes = Maps.uniqueIndex(listNodesStrategy Map<String, ? extends ComputeMetadata> currentNodes = Maps.uniqueIndex(listNodesStrategy
.execute(), BaseComputeService.METADATA_TO_NAME); .execute(GetNodesOptions.NONE), BaseComputeService.METADATA_TO_NAME);
while (names.size() < count) { while (names.size() < count) {
String name = String.format(nodeNamingConvention, tag, nodeIndex++); String name = String.format(nodeNamingConvention, tag, nodeIndex++);
if (!currentNodes.containsKey(name)) { if (!currentNodes.containsKey(name)) {

View File

@ -44,6 +44,7 @@ import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Size; import org.jclouds.compute.domain.Size;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
@ -302,6 +303,20 @@ public abstract class BaseComputeServiceLiveTest {
} }
} }
public void testGetNodesWithDetails() throws Exception {
for (Entry<String, ? extends ComputeMetadata> node : client.getNodes(new GetNodesOptions().withDetails()).entrySet()) {
assertEquals(node.getKey(), node.getValue().getId());
assert node.getValue().getId() != null;
assert node.getValue().getLocationId() != null;
assertEquals(node.getValue().getType(), ComputeType.NODE);
assert node.getValue() instanceof NodeMetadata;
NodeMetadata nodeMetadata = (NodeMetadata)node.getValue();
assertNotNull(nodeMetadata.getName());
assertNotNull(nodeMetadata.getPublicAddresses());
assert nodeMetadata.getPublicAddresses().size() > 1;
assertNotNull(nodeMetadata.getPrivateAddresses());
}
}
public void testListImages() throws Exception { public void testListImages() throws Exception {
for (Entry<String, ? extends Image> image : client.getImages().entrySet()) { for (Entry<String, ? extends Image> image : client.getImages().entrySet()) {
assertEquals(image.getKey(), image.getValue().getId()); assertEquals(image.getKey(), image.getValue().getId());

View File

@ -52,6 +52,7 @@ import org.jclouds.compute.domain.internal.NodeMetadataImpl;
import org.jclouds.compute.domain.internal.SizeImpl; import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl; import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.predicates.RunScriptRunning; import org.jclouds.compute.predicates.RunScriptRunning;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.*; import org.jclouds.compute.strategy.*;
@ -212,7 +213,7 @@ public class GoGridComputeServiceContextModule extends GoGridContextModule {
} }
@Override @Override
public Iterable<? extends ComputeMetadata> execute() { public Iterable<? extends ComputeMetadata> execute(GetNodesOptions options) {
return Iterables.transform(client.getServerServices().getServerList(), return Iterables.transform(client.getServerServices().getServerList(),
serverToNodeMetadata); serverToNodeMetadata);
} }

View File

@ -52,6 +52,7 @@ import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.internal.BaseComputeService; import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl; import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.predicates.RunScriptRunning; import org.jclouds.compute.predicates.RunScriptRunning;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.AddNodeWithTagStrategy; import org.jclouds.compute.strategy.AddNodeWithTagStrategy;
@ -206,7 +207,7 @@ public class CloudServersComputeServiceContextModule extends CloudServersContext
} }
@Override @Override
public Iterable<? extends ComputeMetadata> execute() { public Iterable<? extends ComputeMetadata> execute(GetNodesOptions options) {
return Iterables.transform(client.listServers(ListOptions.Builder.withDetails()), return Iterables.transform(client.listServers(ListOptions.Builder.withDetails()),
serverToNodeMetadata); serverToNodeMetadata);
} }

View File

@ -54,6 +54,7 @@ import org.jclouds.compute.domain.internal.NodeMetadataImpl;
import org.jclouds.compute.domain.internal.SizeImpl; import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl; import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.predicates.RunScriptRunning; import org.jclouds.compute.predicates.RunScriptRunning;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.AddNodeWithTagStrategy; import org.jclouds.compute.strategy.AddNodeWithTagStrategy;
@ -213,7 +214,7 @@ public class RimuHostingComputeServiceContextModule extends RimuHostingContextMo
} }
@Override @Override
public Iterable<? extends ComputeMetadata> execute() { public Iterable<? extends ComputeMetadata> execute(GetNodesOptions options) {
return Iterables.transform(client.getServerList(), serverToNodeMetadata); return Iterables.transform(client.getServerList(), serverToNodeMetadata);
} }

View File

@ -53,6 +53,7 @@ import org.jclouds.compute.domain.internal.NodeMetadataImpl;
import org.jclouds.compute.domain.internal.SizeImpl; import org.jclouds.compute.domain.internal.SizeImpl;
import org.jclouds.compute.internal.ComputeServiceContextImpl; import org.jclouds.compute.internal.ComputeServiceContextImpl;
import org.jclouds.compute.internal.TemplateBuilderImpl; import org.jclouds.compute.internal.TemplateBuilderImpl;
import org.jclouds.compute.options.GetNodesOptions;
import org.jclouds.compute.predicates.RunScriptRunning; import org.jclouds.compute.predicates.RunScriptRunning;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.strategy.AddNodeWithTagStrategy; import org.jclouds.compute.strategy.AddNodeWithTagStrategy;
@ -222,7 +223,7 @@ public class VCloudComputeServiceContextModule extends VCloudContextModule {
} }
@Override @Override
public Iterable<? extends ComputeMetadata> execute() { public Iterable<? extends ComputeMetadata> execute(GetNodesOptions options) {
Set<ComputeMetadata> nodes = Sets.newHashSet(); Set<ComputeMetadata> nodes = Sets.newHashSet();
for (NamedResource vdc : client.getDefaultOrganization().getVDCs().values()) { for (NamedResource vdc : client.getDefaultOrganization().getVDCs().values()) {
for (NamedResource resource : client.getVDC(vdc.getId()).getResourceEntities().values()) { for (NamedResource resource : client.getVDC(vdc.getId()).getResourceEntities().values()) {