diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/EC2ComputeService.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/EC2ComputeService.java index b71cfdcc97..3d7c3d2acf 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/EC2ComputeService.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/EC2ComputeService.java @@ -37,6 +37,7 @@ import javax.inject.Singleton; import org.jclouds.aws.AWSResponseException; 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.IpProtocol; 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.ServerIdentity; 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.ServerIdentityImpl; import org.jclouds.compute.domain.internal.ServerMetadataImpl; @@ -87,6 +89,12 @@ public class EC2ComputeService implements ComputeService { private Map profileInstanceTypeMap = ImmutableMap . builder().put(Profile.SMALLEST, InstanceType.M1_SMALL).build(); + private Map instanceToServerState = ImmutableMap + . builder().put(InstanceState.PENDING, ServerState.PENDING) + .put(InstanceState.RUNNING, ServerState.RUNNING).put(InstanceState.SHUTTING_DOWN, + ServerState.PENDING).put(InstanceState.TERMINATED, ServerState.TERMINATED) + .build(); + @Override public CreateServerResponse createServer(String name, Profile profile, Image image) { String ami = checkNotNull(imageAmiIdMap.get(image), "image not supported: " + image); @@ -117,7 +125,8 @@ public class EC2ComputeService implements ComputeService { Set privateAddresses = runningInstance.getPrivateIpAddress() == null ? ImmutableSet . of() : ImmutableSet. 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 .getKeyMaterial())); } @@ -166,10 +175,10 @@ public class EC2ComputeService implements ComputeService { @Override public ServerMetadata getServerMetadata(String id) { RunningInstance runningInstance = getRunningInstance(id); - return new ServerMetadataImpl(runningInstance.getInstanceId(), runningInstance - .getInstanceId(), ImmutableSet. of(runningInstance.getIpAddress()), - ImmutableSet. of(runningInstance.getPrivateIpAddress()), 22, - LoginType.SSH); + return new ServerMetadataImpl(runningInstance.getInstanceId(), runningInstance.getKeyName(), + instanceToServerState.get(runningInstance.getInstanceState()), ImmutableSet + . of(runningInstance.getIpAddress()), ImmutableSet + . of(runningInstance.getPrivateIpAddress()), 22, LoginType.SSH); } private RunningInstance getRunningInstance(String id) { diff --git a/core/src/main/java/org/jclouds/compute/domain/ServerMetadata.java b/core/src/main/java/org/jclouds/compute/domain/ServerMetadata.java index 39d8557e3e..92f6cdbea3 100644 --- a/core/src/main/java/org/jclouds/compute/domain/ServerMetadata.java +++ b/core/src/main/java/org/jclouds/compute/domain/ServerMetadata.java @@ -36,6 +36,7 @@ import com.google.inject.ImplementedBy; */ @ImplementedBy(ServerMetadataImpl.class) public interface ServerMetadata extends ServerIdentity { + ServerState getState(); SortedSet getPublicAddresses(); diff --git a/core/src/main/java/org/jclouds/compute/domain/ServerState.java b/core/src/main/java/org/jclouds/compute/domain/ServerState.java new file mode 100644 index 0000000000..f000e43405 --- /dev/null +++ b/core/src/main/java/org/jclouds/compute/domain/ServerState.java @@ -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; + +} \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/compute/domain/internal/CreateServerResponseImpl.java b/core/src/main/java/org/jclouds/compute/domain/internal/CreateServerResponseImpl.java index 1c78225e8e..fe82c3d4ac 100644 --- a/core/src/main/java/org/jclouds/compute/domain/internal/CreateServerResponseImpl.java +++ b/core/src/main/java/org/jclouds/compute/domain/internal/CreateServerResponseImpl.java @@ -27,6 +27,7 @@ import java.net.InetAddress; import org.jclouds.compute.domain.CreateServerResponse; import org.jclouds.compute.domain.LoginType; +import org.jclouds.compute.domain.ServerState; import org.jclouds.domain.Credentials; /** @@ -34,23 +35,20 @@ import org.jclouds.domain.Credentials; * @author Ivan Meredith */ public class CreateServerResponseImpl extends ServerMetadataImpl implements CreateServerResponse { - + private final Credentials credentials; - - public CreateServerResponseImpl(String id, String name, Iterable publicAddresses, - Iterable privateAddresses, int loginPort, LoginType loginType, - Credentials credentials) { - super(id, name, publicAddresses, privateAddresses, loginPort, loginType); + public CreateServerResponseImpl(String id, String name, ServerState state, + Iterable publicAddresses, Iterable privateAddresses, + int loginPort, LoginType loginType, Credentials credentials) { + super(id, name, state, publicAddresses, privateAddresses, loginPort, loginType); this.credentials = credentials; } - public Credentials getCredentials() { return credentials; } - @Override public int hashCode() { final int prime = 31; @@ -59,7 +57,6 @@ public class CreateServerResponseImpl extends ServerMetadataImpl implements Crea return result; } - @Override public boolean equals(Object obj) { if (this == obj) @@ -77,5 +74,4 @@ public class CreateServerResponseImpl extends ServerMetadataImpl implements Crea return true; } - } diff --git a/core/src/main/java/org/jclouds/compute/domain/internal/ServerMetadataImpl.java b/core/src/main/java/org/jclouds/compute/domain/internal/ServerMetadataImpl.java index 9049ed7365..bcef74ac9c 100644 --- a/core/src/main/java/org/jclouds/compute/domain/internal/ServerMetadataImpl.java +++ b/core/src/main/java/org/jclouds/compute/domain/internal/ServerMetadataImpl.java @@ -29,6 +29,7 @@ import java.util.SortedSet; import org.jclouds.compute.domain.LoginType; import org.jclouds.compute.domain.ServerMetadata; +import org.jclouds.compute.domain.ServerState; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; @@ -46,14 +47,17 @@ public class ServerMetadataImpl extends ServerIdentityImpl implements ServerMeta } }; + private final ServerState state; private final SortedSet publicAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR); private final SortedSet privateAddresses = Sets.newTreeSet(ADDRESS_COMPARATOR); private final int loginPort; private final LoginType loginType; - public ServerMetadataImpl(String id, String name, Iterable publicAddresses, - Iterable privateAddresses, int loginPort, LoginType loginType) { + public ServerMetadataImpl(String id, String name, ServerState state, + Iterable publicAddresses, Iterable privateAddresses, + int loginPort, LoginType loginType) { super(id, name); + this.state = state; Iterables.addAll(this.publicAddresses, publicAddresses); Iterables.addAll(this.privateAddresses, privateAddresses); this.loginPort = loginPort; @@ -128,4 +132,8 @@ public class ServerMetadataImpl extends ServerIdentityImpl implements ServerMeta return true; } + public ServerState getState() { + return state; + } + } diff --git a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/servers/RimuHostingCreateServerResponse.java b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/servers/RimuHostingCreateServerResponse.java index 921d672794..1bc0ddef30 100644 --- a/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/servers/RimuHostingCreateServerResponse.java +++ b/rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/servers/RimuHostingCreateServerResponse.java @@ -27,6 +27,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import org.jclouds.compute.domain.LoginType; +import org.jclouds.compute.domain.ServerState; import org.jclouds.compute.domain.internal.CreateServerResponseImpl; import org.jclouds.domain.Credentials; import org.jclouds.rimuhosting.miro.domain.NewServerResponse; @@ -44,10 +45,11 @@ public class RimuHostingCreateServerResponse extends CreateServerResponseImpl { public RimuHostingCreateServerResponse(NewServerResponse rhServerResponse) { super(rhServerResponse.getServer().getId().toString(), - rhServerResponse.getServer().getName(), getPublicAddresses(rhServerResponse - .getServer()), ImmutableList. of(), 22, LoginType.SSH, - new Credentials("root", rhServerResponse.getNewInstanceRequest().getCreateOptions() - .getPassword())); + rhServerResponse.getServer().getName(), + ServerState.PENDING,// TODO need a real state! + getPublicAddresses(rhServerResponse.getServer()), ImmutableList. of(), + 22, LoginType.SSH, new Credentials("root", rhServerResponse.getNewInstanceRequest() + .getCreateOptions().getPassword())); } @VisibleForTesting diff --git a/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/compute/HostingDotComVCloudComputeService.java b/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/compute/HostingDotComVCloudComputeService.java index 236f0e04d4..a7705f35b5 100644 --- a/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/compute/HostingDotComVCloudComputeService.java +++ b/vcloud/hostingdotcom/src/main/java/org/jclouds/vcloud/hostingdotcom/compute/HostingDotComVCloudComputeService.java @@ -38,6 +38,7 @@ import org.jclouds.compute.domain.LoginType; import org.jclouds.compute.domain.Profile; import org.jclouds.compute.domain.ServerIdentity; 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.ServerMetadataImpl; import org.jclouds.domain.Credentials; @@ -45,9 +46,11 @@ import org.jclouds.logging.Logger; import org.jclouds.rest.domain.NamedResource; import org.jclouds.vcloud.VCloudMediaType; import org.jclouds.vcloud.domain.VApp; +import org.jclouds.vcloud.domain.VAppStatus; import org.jclouds.vcloud.hostingdotcom.HostingDotComVCloudClient; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.inject.internal.ImmutableSet; @@ -60,12 +63,18 @@ public class HostingDotComVCloudComputeService implements ComputeService { @Resource protected Logger logger = Logger.NULL; private final HostingDotComVCloudComputeClient computeClient; - private final HostingDotComVCloudClient tmClient; + private final HostingDotComVCloudClient hostingClient; + + private static final Map vAppStatusToServerState = ImmutableMap + . 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 public HostingDotComVCloudComputeService(HostingDotComVCloudClient tmClient, HostingDotComVCloudComputeClient computeClient) { - this.tmClient = tmClient; + this.hostingClient = tmClient; this.computeClient = computeClient; } @@ -73,17 +82,19 @@ public class HostingDotComVCloudComputeService implements ComputeService { @Override public CreateServerResponse createServer(String name, Profile profile, Image image) { Map metaMap = computeClient.start(name, 1, 512, image); - VApp vApp = tmClient.getVApp(metaMap.get("id")); - return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vApp - .getNetworkToAddresses().values(), ImmutableSet. of(), 22, - LoginType.SSH, new Credentials(metaMap.get("username"), metaMap.get("password"))); + VApp vApp = hostingClient.getVApp(metaMap.get("id")); + return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState + .get(vApp.getStatus()), vApp.getNetworkToAddresses().values(), ImmutableSet + . of(), 22, LoginType.SSH, new Credentials(metaMap.get("username"), + metaMap.get("password"))); } @Override public ServerMetadata getServerMetadata(String id) { - VApp vApp = tmClient.getVApp(id); - return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vApp.getNetworkToAddresses() - .values(), ImmutableSet. of(), 22, LoginType.SSH); + VApp vApp = hostingClient.getVApp(id); + return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState.get(vApp + .getStatus()), vApp.getNetworkToAddresses().values(), ImmutableSet + . of(), 22, LoginType.SSH); } @Override @@ -99,7 +110,7 @@ public class HostingDotComVCloudComputeService implements ComputeService { @Override public SortedSet listServers() { SortedSet servers = Sets.newTreeSet(); - for (NamedResource resource : tmClient.getDefaultVDC().getResourceEntities().values()) { + for (NamedResource resource : hostingClient.getDefaultVDC().getResourceEntities().values()) { if (resource.getType().equals(VCloudMediaType.VAPP_XML)) { servers.add(getServerMetadata(resource.getId())); } diff --git a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/compute/TerremarkVCloudComputeService.java b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/compute/TerremarkVCloudComputeService.java index e1b3d5c3ab..b3b9049269 100644 --- a/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/compute/TerremarkVCloudComputeService.java +++ b/vcloud/terremark/src/main/java/org/jclouds/vcloud/terremark/compute/TerremarkVCloudComputeService.java @@ -24,6 +24,7 @@ package org.jclouds.vcloud.terremark.compute; import java.net.InetAddress; +import java.util.Map; import java.util.Set; 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.ServerIdentity; 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.ServerMetadataImpl; import org.jclouds.domain.Credentials; @@ -45,10 +47,12 @@ import org.jclouds.logging.Logger; import org.jclouds.rest.domain.NamedResource; import org.jclouds.vcloud.VCloudMediaType; import org.jclouds.vcloud.domain.VApp; +import org.jclouds.vcloud.domain.VAppStatus; import org.jclouds.vcloud.terremark.TerremarkVCloudClient; import org.jclouds.vcloud.terremark.domain.InternetService; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.inject.internal.ImmutableSet; @@ -63,6 +67,12 @@ public class TerremarkVCloudComputeService implements ComputeService { private final TerremarkVCloudComputeClient computeClient; private final TerremarkVCloudClient tmClient; + private static final Map vAppStatusToServerState = ImmutableMap + . 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 public TerremarkVCloudComputeService(TerremarkVCloudClient tmClient, TerremarkVCloudComputeClient computeClient) { @@ -78,9 +88,10 @@ public class TerremarkVCloudComputeService implements ComputeService { // 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); - return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), ImmutableSet - . of(publicIp), vApp.getNetworkToAddresses().values(), 22, - LoginType.SSH, new Credentials("vcloud", "p4ssw0rd")); + return new CreateServerResponseImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState + .get(vApp.getStatus()), ImmutableSet. of(publicIp), vApp + .getNetworkToAddresses().values(), 22, LoginType.SSH, new Credentials("vcloud", + "p4ssw0rd")); } @Override @@ -88,8 +99,9 @@ public class TerremarkVCloudComputeService implements ComputeService { VApp vApp = tmClient.getVApp(id); // TODO Set publicAddresses = ImmutableSet. of(); - return new ServerMetadataImpl(vApp.getId(), vApp.getName(), publicAddresses, vApp - .getNetworkToAddresses().values(), 22, LoginType.SSH); + return new ServerMetadataImpl(vApp.getId(), vApp.getName(), vAppStatusToServerState.get(vApp + .getStatus()), publicAddresses, vApp.getNetworkToAddresses().values(), 22, + LoginType.SSH); } public SortedSet getInternetServicesByName(final String name) {