Issue 130: added server state

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2462 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-18 07:44:52 +00:00
parent 9253e755bd
commit ce1b6dea27
8 changed files with 101 additions and 36 deletions

View File

@ -37,6 +37,7 @@ import javax.inject.Singleton;
import org.jclouds.aws.AWSResponseException; import org.jclouds.aws.AWSResponseException;
import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.domain.InstanceState;
import org.jclouds.aws.ec2.domain.InstanceType; import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.domain.IpProtocol; import org.jclouds.aws.ec2.domain.IpProtocol;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
@ -49,6 +50,7 @@ import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.Profile; import org.jclouds.compute.domain.Profile;
import org.jclouds.compute.domain.ServerIdentity; import org.jclouds.compute.domain.ServerIdentity;
import org.jclouds.compute.domain.ServerMetadata; import org.jclouds.compute.domain.ServerMetadata;
import org.jclouds.compute.domain.ServerState;
import org.jclouds.compute.domain.internal.CreateServerResponseImpl; import org.jclouds.compute.domain.internal.CreateServerResponseImpl;
import org.jclouds.compute.domain.internal.ServerIdentityImpl; import org.jclouds.compute.domain.internal.ServerIdentityImpl;
import org.jclouds.compute.domain.internal.ServerMetadataImpl; import org.jclouds.compute.domain.internal.ServerMetadataImpl;
@ -87,6 +89,12 @@ public class EC2ComputeService implements ComputeService {
private Map<Profile, InstanceType> profileInstanceTypeMap = ImmutableMap private Map<Profile, InstanceType> profileInstanceTypeMap = ImmutableMap
.<Profile, InstanceType> builder().put(Profile.SMALLEST, InstanceType.M1_SMALL).build(); .<Profile, InstanceType> builder().put(Profile.SMALLEST, InstanceType.M1_SMALL).build();
private Map<InstanceState, ServerState> instanceToServerState = ImmutableMap
.<InstanceState, ServerState> builder().put(InstanceState.PENDING, ServerState.PENDING)
.put(InstanceState.RUNNING, ServerState.RUNNING).put(InstanceState.SHUTTING_DOWN,
ServerState.PENDING).put(InstanceState.TERMINATED, ServerState.TERMINATED)
.build();
@Override @Override
public CreateServerResponse createServer(String name, Profile profile, Image image) { public CreateServerResponse createServer(String name, Profile profile, Image image) {
String ami = checkNotNull(imageAmiIdMap.get(image), "image not supported: " + image); String ami = checkNotNull(imageAmiIdMap.get(image), "image not supported: " + image);
@ -117,7 +125,8 @@ public class EC2ComputeService implements ComputeService {
Set<InetAddress> privateAddresses = runningInstance.getPrivateIpAddress() == null ? ImmutableSet Set<InetAddress> privateAddresses = runningInstance.getPrivateIpAddress() == null ? ImmutableSet
.<InetAddress> of() .<InetAddress> of()
: ImmutableSet.<InetAddress> of(runningInstance.getPrivateIpAddress()); : ImmutableSet.<InetAddress> of(runningInstance.getPrivateIpAddress());
return new CreateServerResponseImpl(runningInstance.getInstanceId(), name, publicAddresses, return new CreateServerResponseImpl(runningInstance.getInstanceId(), name,
instanceToServerState.get(runningInstance.getInstanceState()), publicAddresses,
privateAddresses, 22, LoginType.SSH, new Credentials("root", keyPair privateAddresses, 22, LoginType.SSH, new Credentials("root", keyPair
.getKeyMaterial())); .getKeyMaterial()));
} }
@ -166,10 +175,10 @@ public class EC2ComputeService implements ComputeService {
@Override @Override
public ServerMetadata getServerMetadata(String id) { public ServerMetadata getServerMetadata(String id) {
RunningInstance runningInstance = getRunningInstance(id); RunningInstance runningInstance = getRunningInstance(id);
return new ServerMetadataImpl(runningInstance.getInstanceId(), runningInstance return new ServerMetadataImpl(runningInstance.getInstanceId(), runningInstance.getKeyName(),
.getInstanceId(), ImmutableSet.<InetAddress> of(runningInstance.getIpAddress()), instanceToServerState.get(runningInstance.getInstanceState()), ImmutableSet
ImmutableSet.<InetAddress> of(runningInstance.getPrivateIpAddress()), 22, .<InetAddress> of(runningInstance.getIpAddress()), ImmutableSet
LoginType.SSH); .<InetAddress> of(runningInstance.getPrivateIpAddress()), 22, LoginType.SSH);
} }
private RunningInstance getRunningInstance(String id) { private RunningInstance getRunningInstance(String id) {

View File

@ -36,6 +36,7 @@ import com.google.inject.ImplementedBy;
*/ */
@ImplementedBy(ServerMetadataImpl.class) @ImplementedBy(ServerMetadataImpl.class)
public interface ServerMetadata extends ServerIdentity { public interface ServerMetadata extends ServerIdentity {
ServerState getState();
SortedSet<InetAddress> getPublicAddresses(); SortedSet<InetAddress> getPublicAddresses();

View File

@ -0,0 +1,26 @@
package org.jclouds.compute.domain;
/**
* Indicates the status of a server
*
* @author Adrian Cole
*/
public enum ServerState {
/**
* The server is in transition
*/
PENDING,
/**
* The server is not running
*/
TERMINATED,
/**
* The server is deployed, but suspended
*/
SUSPENDED,
/**
* The server is available for requests
*/
RUNNING;
}

View File

@ -27,6 +27,7 @@ import java.net.InetAddress;
import org.jclouds.compute.domain.CreateServerResponse; import org.jclouds.compute.domain.CreateServerResponse;
import org.jclouds.compute.domain.LoginType; import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.ServerState;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
/** /**
@ -37,20 +38,17 @@ public class CreateServerResponseImpl extends ServerMetadataImpl implements Crea
private final Credentials credentials; private final Credentials credentials;
public CreateServerResponseImpl(String id, String name, ServerState state,
public CreateServerResponseImpl(String id, String name, Iterable<InetAddress> publicAddresses, Iterable<InetAddress> publicAddresses, Iterable<InetAddress> privateAddresses,
Iterable<InetAddress> privateAddresses, int loginPort, LoginType loginType, int loginPort, LoginType loginType, Credentials credentials) {
Credentials credentials) { super(id, name, state, publicAddresses, privateAddresses, loginPort, loginType);
super(id, name, publicAddresses, privateAddresses, loginPort, loginType);
this.credentials = credentials; this.credentials = credentials;
} }
public Credentials getCredentials() { public Credentials getCredentials() {
return credentials; return credentials;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -59,7 +57,6 @@ public class CreateServerResponseImpl extends ServerMetadataImpl implements Crea
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
@ -77,5 +74,4 @@ public class CreateServerResponseImpl extends ServerMetadataImpl implements Crea
return true; return true;
} }
} }

View File

@ -29,6 +29,7 @@ import java.util.SortedSet;
import org.jclouds.compute.domain.LoginType; import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.ServerMetadata; import org.jclouds.compute.domain.ServerMetadata;
import org.jclouds.compute.domain.ServerState;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -46,14 +47,17 @@ public class ServerMetadataImpl extends ServerIdentityImpl implements ServerMeta
} }
}; };
private final ServerState state;
private final SortedSet<InetAddress> publicAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR); private final SortedSet<InetAddress> publicAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR);
private final SortedSet<InetAddress> privateAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR); private final SortedSet<InetAddress> privateAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR);
private final int loginPort; private final int loginPort;
private final LoginType loginType; private final LoginType loginType;
public ServerMetadataImpl(String id, String name, Iterable<InetAddress> publicAddresses, public ServerMetadataImpl(String id, String name, ServerState state,
Iterable<InetAddress> privateAddresses, int loginPort, LoginType loginType) { Iterable<InetAddress> publicAddresses, Iterable<InetAddress> privateAddresses,
int loginPort, LoginType loginType) {
super(id, name); super(id, name);
this.state = state;
Iterables.addAll(this.publicAddresses, publicAddresses); Iterables.addAll(this.publicAddresses, publicAddresses);
Iterables.addAll(this.privateAddresses, privateAddresses); Iterables.addAll(this.privateAddresses, privateAddresses);
this.loginPort = loginPort; this.loginPort = loginPort;
@ -128,4 +132,8 @@ public class ServerMetadataImpl extends ServerIdentityImpl implements ServerMeta
return true; return true;
} }
public ServerState getState() {
return state;
}
} }

View File

@ -27,6 +27,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.jclouds.compute.domain.LoginType; import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.ServerState;
import org.jclouds.compute.domain.internal.CreateServerResponseImpl; import org.jclouds.compute.domain.internal.CreateServerResponseImpl;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.rimuhosting.miro.domain.NewServerResponse; import org.jclouds.rimuhosting.miro.domain.NewServerResponse;
@ -44,10 +45,11 @@ public class RimuHostingCreateServerResponse extends CreateServerResponseImpl {
public RimuHostingCreateServerResponse(NewServerResponse rhServerResponse) { public RimuHostingCreateServerResponse(NewServerResponse rhServerResponse) {
super(rhServerResponse.getServer().getId().toString(), super(rhServerResponse.getServer().getId().toString(),
rhServerResponse.getServer().getName(), getPublicAddresses(rhServerResponse rhServerResponse.getServer().getName(),
.getServer()), ImmutableList.<InetAddress> of(), 22, LoginType.SSH, ServerState.PENDING,// TODO need a real state!
new Credentials("root", rhServerResponse.getNewInstanceRequest().getCreateOptions() getPublicAddresses(rhServerResponse.getServer()), ImmutableList.<InetAddress> of(),
.getPassword())); 22, LoginType.SSH, new Credentials("root", rhServerResponse.getNewInstanceRequest()
.getCreateOptions().getPassword()));
} }
@VisibleForTesting @VisibleForTesting

View File

@ -38,6 +38,7 @@ import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.Profile; import org.jclouds.compute.domain.Profile;
import org.jclouds.compute.domain.ServerIdentity; import org.jclouds.compute.domain.ServerIdentity;
import org.jclouds.compute.domain.ServerMetadata; import org.jclouds.compute.domain.ServerMetadata;
import org.jclouds.compute.domain.ServerState;
import org.jclouds.compute.domain.internal.CreateServerResponseImpl; import org.jclouds.compute.domain.internal.CreateServerResponseImpl;
import org.jclouds.compute.domain.internal.ServerMetadataImpl; import org.jclouds.compute.domain.internal.ServerMetadataImpl;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
@ -45,9 +46,11 @@ import org.jclouds.logging.Logger;
import org.jclouds.rest.domain.NamedResource; import org.jclouds.rest.domain.NamedResource;
import org.jclouds.vcloud.VCloudMediaType; import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.VApp; import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.hostingdotcom.HostingDotComVCloudClient; import org.jclouds.vcloud.hostingdotcom.HostingDotComVCloudClient;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.internal.ImmutableSet; import com.google.inject.internal.ImmutableSet;
@ -60,12 +63,18 @@ public class HostingDotComVCloudComputeService implements ComputeService {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final HostingDotComVCloudComputeClient computeClient; private final HostingDotComVCloudComputeClient computeClient;
private final HostingDotComVCloudClient tmClient; private final HostingDotComVCloudClient hostingClient;
private static final Map<VAppStatus, ServerState> vAppStatusToServerState = ImmutableMap
.<VAppStatus, ServerState> builder().put(VAppStatus.OFF, ServerState.TERMINATED).put(
VAppStatus.ON, ServerState.RUNNING).put(VAppStatus.RESOLVED,
ServerState.PENDING).put(VAppStatus.SUSPENDED, ServerState.SUSPENDED).put(
VAppStatus.UNRESOLVED, ServerState.PENDING).build();
@Inject @Inject
public HostingDotComVCloudComputeService(HostingDotComVCloudClient tmClient, public HostingDotComVCloudComputeService(HostingDotComVCloudClient tmClient,
HostingDotComVCloudComputeClient computeClient) { HostingDotComVCloudComputeClient computeClient) {
this.tmClient = tmClient; this.hostingClient = tmClient;
this.computeClient = computeClient; this.computeClient = computeClient;
} }
@ -73,17 +82,19 @@ public class HostingDotComVCloudComputeService implements ComputeService {
@Override @Override
public CreateServerResponse createServer(String name, Profile profile, Image image) { public CreateServerResponse createServer(String name, Profile profile, Image image) {
Map<String, String> metaMap = computeClient.start(name, 1, 512, image); Map<String, String> metaMap = computeClient.start(name, 1, 512, image);
VApp vApp = tmClient.getVApp(metaMap.get("id")); VApp vApp = hostingClient.getVApp(metaMap.get("id"));
return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vApp return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState
.getNetworkToAddresses().values(), ImmutableSet.<InetAddress> of(), 22, .get(vApp.getStatus()), vApp.getNetworkToAddresses().values(), ImmutableSet
LoginType.SSH, new Credentials(metaMap.get("username"), metaMap.get("password"))); .<InetAddress> of(), 22, LoginType.SSH, new Credentials(metaMap.get("username"),
metaMap.get("password")));
} }
@Override @Override
public ServerMetadata getServerMetadata(String id) { public ServerMetadata getServerMetadata(String id) {
VApp vApp = tmClient.getVApp(id); VApp vApp = hostingClient.getVApp(id);
return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vApp.getNetworkToAddresses() return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState.get(vApp
.values(), ImmutableSet.<InetAddress> of(), 22, LoginType.SSH); .getStatus()), vApp.getNetworkToAddresses().values(), ImmutableSet
.<InetAddress> of(), 22, LoginType.SSH);
} }
@Override @Override
@ -99,7 +110,7 @@ public class HostingDotComVCloudComputeService implements ComputeService {
@Override @Override
public SortedSet<ServerIdentity> listServers() { public SortedSet<ServerIdentity> listServers() {
SortedSet<ServerIdentity> servers = Sets.newTreeSet(); SortedSet<ServerIdentity> servers = Sets.newTreeSet();
for (NamedResource resource : tmClient.getDefaultVDC().getResourceEntities().values()) { for (NamedResource resource : hostingClient.getDefaultVDC().getResourceEntities().values()) {
if (resource.getType().equals(VCloudMediaType.VAPP_XML)) { if (resource.getType().equals(VCloudMediaType.VAPP_XML)) {
servers.add(getServerMetadata(resource.getId())); servers.add(getServerMetadata(resource.getId()));
} }

View File

@ -24,6 +24,7 @@
package org.jclouds.vcloud.terremark.compute; package org.jclouds.vcloud.terremark.compute;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
@ -38,6 +39,7 @@ import org.jclouds.compute.domain.LoginType;
import org.jclouds.compute.domain.Profile; import org.jclouds.compute.domain.Profile;
import org.jclouds.compute.domain.ServerIdentity; import org.jclouds.compute.domain.ServerIdentity;
import org.jclouds.compute.domain.ServerMetadata; import org.jclouds.compute.domain.ServerMetadata;
import org.jclouds.compute.domain.ServerState;
import org.jclouds.compute.domain.internal.CreateServerResponseImpl; import org.jclouds.compute.domain.internal.CreateServerResponseImpl;
import org.jclouds.compute.domain.internal.ServerMetadataImpl; import org.jclouds.compute.domain.internal.ServerMetadataImpl;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
@ -45,10 +47,12 @@ import org.jclouds.logging.Logger;
import org.jclouds.rest.domain.NamedResource; import org.jclouds.rest.domain.NamedResource;
import org.jclouds.vcloud.VCloudMediaType; import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.VApp; import org.jclouds.vcloud.domain.VApp;
import org.jclouds.vcloud.domain.VAppStatus;
import org.jclouds.vcloud.terremark.TerremarkVCloudClient; import org.jclouds.vcloud.terremark.TerremarkVCloudClient;
import org.jclouds.vcloud.terremark.domain.InternetService; import org.jclouds.vcloud.terremark.domain.InternetService;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.internal.ImmutableSet; import com.google.inject.internal.ImmutableSet;
@ -63,6 +67,12 @@ public class TerremarkVCloudComputeService implements ComputeService {
private final TerremarkVCloudComputeClient computeClient; private final TerremarkVCloudComputeClient computeClient;
private final TerremarkVCloudClient tmClient; private final TerremarkVCloudClient tmClient;
private static final Map<VAppStatus, ServerState> vAppStatusToServerState = ImmutableMap
.<VAppStatus, ServerState> builder().put(VAppStatus.OFF, ServerState.TERMINATED).put(
VAppStatus.ON, ServerState.RUNNING).put(VAppStatus.RESOLVED,
ServerState.PENDING).put(VAppStatus.SUSPENDED, ServerState.SUSPENDED).put(
VAppStatus.UNRESOLVED, ServerState.PENDING).build();
@Inject @Inject
public TerremarkVCloudComputeService(TerremarkVCloudClient tmClient, public TerremarkVCloudComputeService(TerremarkVCloudClient tmClient,
TerremarkVCloudComputeClient computeClient) { TerremarkVCloudComputeClient computeClient) {
@ -78,9 +88,10 @@ public class TerremarkVCloudComputeService implements ComputeService {
// bug creating more than one internet service returns 503 or 500 // bug creating more than one internet service returns 503 or 500
// InetAddress publicIp = computeClient.createPublicAddressMappedToPorts(vApp, 22, 80, 8080); // InetAddress publicIp = computeClient.createPublicAddressMappedToPorts(vApp, 22, 80, 8080);
InetAddress publicIp = computeClient.createPublicAddressMappedToPorts(vApp, 22); InetAddress publicIp = computeClient.createPublicAddressMappedToPorts(vApp, 22);
return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), ImmutableSet return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState
.<InetAddress> of(publicIp), vApp.getNetworkToAddresses().values(), 22, .get(vApp.getStatus()), ImmutableSet.<InetAddress> of(publicIp), vApp
LoginType.SSH, new Credentials("vcloud", "p4ssw0rd")); .getNetworkToAddresses().values(), 22, LoginType.SSH, new Credentials("vcloud",
"p4ssw0rd"));
} }
@Override @Override
@ -88,8 +99,9 @@ public class TerremarkVCloudComputeService implements ComputeService {
VApp vApp = tmClient.getVApp(id); VApp vApp = tmClient.getVApp(id);
// TODO // TODO
Set<InetAddress> publicAddresses = ImmutableSet.<InetAddress> of(); Set<InetAddress> publicAddresses = ImmutableSet.<InetAddress> of();
return new ServerMetadataImpl(vApp.getId(), vApp.getName(), publicAddresses, vApp return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState.get(vApp
.getNetworkToAddresses().values(), 22, LoginType.SSH); .getStatus()), publicAddresses, vApp.getNetworkToAddresses().values(), 22,
LoginType.SSH);
} }
public SortedSet<InternetService> getInternetServicesByName(final String name) { public SortedSet<InternetService> getInternetServicesByName(final String name) {