Issue 412: server create now operates

This commit is contained in:
Adrian Cole 2010-12-18 19:51:49 +01:00
parent 0d286b0c60
commit 81760ee086
18 changed files with 886 additions and 78 deletions

View File

@ -67,6 +67,12 @@ public class CloudSigmaClientLiveTest extends CommonElasticStackClientLiveTest<C
@Override
protected void checkCreatedDrive() {
super.checkCreatedDrive();
assertEquals(DriveInfo.class.cast(info).getType(), null);
assertEquals(DriveInfo.class.cast(drive).getType(), null);
}
@Override
protected void prepareDrive() {
// TODO Auto-generated method stub
}
}

View File

@ -30,9 +30,11 @@ import javax.ws.rs.core.MediaType;
import org.jclouds.elasticstack.binders.BindDriveDataToPlainTextString;
import org.jclouds.elasticstack.binders.BindDriveToPlainTextString;
import org.jclouds.elasticstack.binders.BindServerToPlainTextString;
import org.jclouds.elasticstack.domain.Drive;
import org.jclouds.elasticstack.domain.DriveData;
import org.jclouds.elasticstack.domain.DriveInfo;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.ServerInfo;
import org.jclouds.elasticstack.functions.KeyValuesDelimitedByBlankLinesToDriveInfo;
import org.jclouds.elasticstack.functions.KeyValuesDelimitedByBlankLinesToServerInfo;
@ -86,6 +88,72 @@ public interface CommonElasticStackAsyncClient {
@Path("/servers/{uuid}/info")
ListenableFuture<? extends ServerInfo> getServerInfo(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#createAndStartServer
*/
@POST
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@ResponseParser(KeyValuesDelimitedByBlankLinesToServerInfo.class)
@Path("/servers/create")
ListenableFuture<? extends ServerInfo> createAndStartServer(
@BinderParam(BindServerToPlainTextString.class) Server createServer);
/**
* @see ElasticStackClient#createServer
*/
@POST
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@ResponseParser(KeyValuesDelimitedByBlankLinesToServerInfo.class)
@Path("/servers/create/stopped")
ListenableFuture<? extends ServerInfo> createServer(
@BinderParam(BindServerToPlainTextString.class) Server createServer);
/**
* @see ElasticStackClient#setServer
*/
@POST
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@ResponseParser(KeyValuesDelimitedByBlankLinesToServerInfo.class)
@Path("/servers/{uuid}/set")
ListenableFuture<? extends ServerInfo> setServerConfiguration(@PathParam("uuid") String uuid,
@BinderParam(BindServerToPlainTextString.class) Server setServer);
/**
* @see ElasticStackClient#destroyServer
*/
@POST
@Path("/servers/{uuid}/destroy")
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
ListenableFuture<Void> destroyServer(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#startServer
*/
@POST
@Path("/servers/{uuid}/start")
ListenableFuture<Void> startServer(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#stopServer
*/
@POST
@Path("/servers/{uuid}/stop")
ListenableFuture<Void> stopServer(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#shutdownServer
*/
@POST
@Path("/servers/{uuid}/shutdown")
ListenableFuture<Void> shutdownServer(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#resetServer
*/
@POST
@Path("/servers/{uuid}/reset")
ListenableFuture<Void> resetServer(@PathParam("uuid") String uuid);
/**
* @see ElasticStackClient#listDrives()
*/
@ -118,8 +186,7 @@ public interface CommonElasticStackAsyncClient {
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@ResponseParser(KeyValuesDelimitedByBlankLinesToDriveInfo.class)
@Path("/drives/create")
ListenableFuture<? extends DriveInfo> createDrive(
@BinderParam(BindDriveToPlainTextString.class) Drive createDrive);
ListenableFuture<? extends DriveInfo> createDrive(@BinderParam(BindDriveToPlainTextString.class) Drive createDrive);
/**
* @see ElasticStackClient#setDriveData

View File

@ -26,6 +26,7 @@ import org.jclouds.concurrent.Timeout;
import org.jclouds.elasticstack.domain.Drive;
import org.jclouds.elasticstack.domain.DriveData;
import org.jclouds.elasticstack.domain.DriveInfo;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.ServerInfo;
/**
@ -59,6 +60,79 @@ public interface CommonElasticStackClient {
*/
ServerInfo getServerInfo(String uuid);
/**
* create a new server
*
* @param server
* @return newly created server
*/
ServerInfo createServer(Server server);
/**
* create and start a new server
*
* @param server
* @return newly created server
*/
ServerInfo createAndStartServer(Server server);
/**
* set server configuration
*
* @param uuid
* what server to change
* @param serverData
* what values to change
* @return new data
*/
ServerInfo setServerConfiguration(String uuid, Server server);
/**
* Destroy a server
*
* @param uuid
* what to destroy
*/
void destroyServer(String uuid);
/**
* Start a server
*
* @param uuid
* what to start
*/
void startServer(String uuid);
/**
* Stop a server
* <p/>
* Kills the server immediately, equivalent to a power failure. Server reverts to a stopped
* status if it is persistent and is automatically destroyed otherwise.
*
* @param uuid
* what to stop
*/
void stopServer(String uuid);
/**
* Shutdown a server
* <p/>
* Sends the server an ACPI power-down event. Server reverts to a stopped status if it is
* persistent and is automatically destroyed otherwise.
*
* @param uuid
* what to shutdown
*/
void shutdownServer(String uuid);
/**
* Reset a server
*
* @param uuid
* what to reset
*/
void resetServer(String uuid);
/**
* list of drive uuids in your account
*

View File

@ -0,0 +1,61 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.elasticstack.binders;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.core.MediaType;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.functions.ListOfMapsToListOfKeyValuesDelimitedByBlankLines;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Singleton
public class BindServerToPlainTextString implements Binder {
private final Function<Server, Map<String, String>> createServerRequestToMap;
private final ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines;
@Inject
public BindServerToPlainTextString(Function<Server, Map<String, String>> createServerRequestToMap,
ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines) {
this.createServerRequestToMap = createServerRequestToMap;
this.listOfMapsToListOfKeyValuesDelimitedByBlankLines = listOfMapsToListOfKeyValuesDelimitedByBlankLines;
}
public void bindToRequest(HttpRequest request, Object payload) {
checkArgument(payload instanceof Server, "this binder is only valid for Server!");
Server create = Server.class.cast(payload);
Map<String, String> map = createServerRequestToMap.apply(create);
request.setPayload(listOfMapsToListOfKeyValuesDelimitedByBlankLines.apply(ImmutableSet.of(map)));
request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_PLAIN);
}
}

View File

@ -29,10 +29,12 @@ import org.jclouds.elasticstack.domain.Drive;
import org.jclouds.elasticstack.domain.DriveData;
import org.jclouds.elasticstack.domain.DriveMetrics;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.ServerMetrics;
import org.jclouds.elasticstack.functions.CreateDriveRequestToMap;
import org.jclouds.elasticstack.functions.DriveDataToMap;
import org.jclouds.elasticstack.functions.MapToDevices;
import org.jclouds.elasticstack.functions.ServerToMap;
import org.jclouds.elasticstack.functions.MapToDevices.DeviceToId;
import org.jclouds.elasticstack.functions.MapToDriveMetrics;
import org.jclouds.elasticstack.functions.MapToNICs;
@ -79,6 +81,10 @@ public class ElasticStackRestClientModule extends RestClientModule<ElasticStackC
}).to(MapToServerMetrics.class);
bind(new TypeLiteral<Function<Device, String>>() {
}).to(DeviceToId.class);
bind(new TypeLiteral<Function<Server, Map<String, String>>>() {
}).to(ServerToMap.class);
bind(new TypeLiteral<Function<Server, Map<String, String>>>() {
}).to(ServerToMap.class);
}
@Override

View File

@ -127,7 +127,7 @@ public class Server extends Item {
}
public Server build() {
return new Server(uuid, name, cpu, smp, mem, persistent, devices, tags, bootDeviceIds, userMetadata, nics,
return new Server(uuid, name, cpu, smp, mem, persistent, devices, bootDeviceIds, tags, userMetadata, nics,
vnc, description);
}
}

View File

@ -161,7 +161,7 @@ public class ServerInfo extends Server {
}
public ServerInfo build() {
return new ServerInfo(uuid, name, cpu, smp, mem, persistent, devices, tags, bootDeviceIds, userMetadata, nics,
return new ServerInfo(uuid, name, cpu, smp, mem, persistent, devices, bootDeviceIds, tags, userMetadata, nics,
vnc, description, status, started, user, metrics);
}
}

View File

@ -0,0 +1,83 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.elasticstack.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Singleton;
import org.jclouds.elasticstack.domain.Device;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ServerToMap implements Function<Server, Map<String, String>> {
@Override
public Map<String, String> apply(Server from) {
checkNotNull(from, "server");
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
builder.put("name", from.getName());
builder.put("cpu", from.getCpu() + "");
if (from.getSmp() != null)
builder.put("smp", from.getSmp() + "");
else
builder.put("smp", "auto");
builder.put("mem", from.getMem() + "");
builder.put("persistent", from.isPersistent() + "");
if (from.getBootDeviceIds().size() != 0)
builder.put("boot", Joiner.on(' ').join(from.getBootDeviceIds()));
for (Entry<String, ? extends Device> entry : from.getDevices().entrySet()) {
builder.put(entry.getKey(), entry.getValue().getDriveUuid());
builder.put(entry.getKey() + ":media", entry.getValue().getMediaType().toString());
}
int nicId = 0;
for (NIC nic : from.getNics()) {
builder.put("nic:" + nicId + ":model", nic.getModel().toString());
if (nic.getDhcp() != null)
builder.put("nic:" + nicId + ":dhcp", nic.getDhcp());
if (nic.getVlan() != null)
builder.put("nic:" + nicId + ":vlan", nic.getVlan());
if (nic.getMac() != null)
builder.put("nic:" + nicId + ":mac", nic.getMac());
nicId++;
}
builder.put("vnc:ip", from.getVnc().getIp() == null ? "auto" : from.getVnc().getIp());
if (from.getVnc().getPassword() != null)
builder.put("vnc:password", from.getVnc().getPassword());
if (from.getVnc().isTls())
builder.put("vnc:tls", "on");
if (from.getTags().size() != 0)
builder.put("tags", Joiner.on(' ').join(from.getTags()));
for (Entry<String, String> entry : from.getUserMetadata().entrySet())
builder.put("user:" + entry.getKey(), entry.getValue());
return builder.build();
}
}

View File

@ -25,6 +25,7 @@ import static org.testng.Assert.assertNotNull;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.Constants;
import org.jclouds.elasticstack.domain.ClaimType;
@ -32,16 +33,28 @@ import org.jclouds.elasticstack.domain.CreateDriveRequest;
import org.jclouds.elasticstack.domain.DriveData;
import org.jclouds.elasticstack.domain.DriveInfo;
import org.jclouds.elasticstack.domain.DriveStatus;
import org.jclouds.elasticstack.domain.IDEDevice;
import org.jclouds.elasticstack.domain.Model;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.ServerInfo;
import org.jclouds.elasticstack.domain.ServerStatus;
import org.jclouds.elasticstack.domain.VNC;
import org.jclouds.elasticstack.predicates.DriveClaimed;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.rest.RestContext;
import org.jclouds.rest.RestContextFactory;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.inject.Module;
/**
@ -52,6 +65,7 @@ import com.google.inject.Module;
@Test(groups = "live", sequential = true)
public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticStackClient, A extends CommonElasticStackAsyncClient> {
protected static final String VNC_PASSWORD = "XXXXXXXX";
protected S client;
protected RestContext<S, A> context;
@ -60,6 +74,7 @@ public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticSt
protected String credential;
protected String endpoint;
protected String apiversion;
protected RetryablePredicate<DriveInfo> driveNotClaimed;
protected void setupCredentials() {
identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
@ -90,6 +105,8 @@ public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticSt
overrides);
client = context.getApi();
driveNotClaimed = new RetryablePredicate<DriveInfo>(Predicates.not(new DriveClaimed(client)), 30, 1,
TimeUnit.SECONDS);
}
@AfterGroups(groups = "live")
@ -139,59 +156,150 @@ public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticSt
}
protected String prefix = System.getProperty("user.name") + ".test";
protected DriveInfo info;
protected DriveInfo drive;
@Test
public void testCreate() throws Exception {
info = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(4 * 1024 * 1024l).build());
public void testCreateDrive() throws Exception {
drive = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(1 * 1024 * 1024 * 1024l).build());
checkCreatedDrive();
DriveInfo newInfo = client.getDriveInfo(info.getUuid());
DriveInfo newInfo = client.getDriveInfo(drive.getUuid());
checkDriveMatchesGet(newInfo);
}
protected void checkDriveMatchesGet(DriveInfo newInfo) {
assertEquals(newInfo.getUuid(), info.getUuid());
assertEquals(newInfo.getUuid(), drive.getUuid());
}
protected void checkCreatedDrive() {
assertNotNull(info.getUuid());
assertNotNull(info.getUser());
assertEquals(info.getName(), prefix);
assertEquals(info.getSize(), 4 * 1024 * 1024l);
assertEquals(info.getStatus(), DriveStatus.ACTIVE);
assertNotNull(drive.getUuid());
assertNotNull(drive.getUser());
assertEquals(drive.getName(), prefix);
assertEquals(drive.getSize(), 1 * 1024 * 1024 * 1024l);
assertEquals(drive.getStatus(), DriveStatus.ACTIVE);
// for some reason, these occasionally return as 4096,1
// assertEquals(info.getReadBytes(), 0l);
// assertEquals(info.getWriteBytes(), 0l);
// assertEquals(info.getReadRequests(), 0l);
// assertEquals(info.getWriteRequests(), 0l);
assertEquals(info.getEncryptionCipher(), "aes-xts-plain");
assertEquals(drive.getEncryptionCipher(), "aes-xts-plain");
}
@Test(dependsOnMethods = "testCreate")
@Test(dependsOnMethods = "testCreateDrive")
public void testSetDriveData() throws Exception {
DriveInfo info2 = client.setDriveData(
info.getUuid(),
DriveInfo drive2 = client.setDriveData(
drive.getUuid(),
new DriveData.Builder().claimType(ClaimType.SHARED).name("rediculous")
.readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"))
.tags(ImmutableSet.of("networking", "security", "gateway"))
.userMetadata(ImmutableMap.of("foo", "bar")).build());
assertNotNull(info2.getUuid(), info.getUuid());
assertEquals(info2.getName(), "rediculous");
assertEquals(info2.getClaimType(), ClaimType.SHARED);
assertEquals(info2.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"));
assertEquals(info2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
assertEquals(info2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
info = info2;
assertNotNull(drive2.getUuid(), drive.getUuid());
assertEquals(drive2.getName(), "rediculous");
assertEquals(drive2.getClaimType(), ClaimType.SHARED);
assertEquals(drive2.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"));
assertEquals(drive2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
assertEquals(drive2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
drive = drive2;
}
protected ServerInfo server;
protected abstract void prepareDrive();
@Test(dependsOnMethods = "testSetDriveData")
public void testDestroyDrive() throws Exception {
client.destroyDrive(info.getUuid());
assertEquals(client.getDriveInfo(info.getUuid()), null);
}
public void testCreateAndStartServer() throws Exception {
prepareDrive();
Server serverRequest = new Server.Builder().name(prefix).cpu(1000).mem(512).persistent(true)
.devices(ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid(drive.getUuid()).build()))
.bootDeviceIds(ImmutableSet.of("ide:0:0")).nics(ImmutableSet.of(new NIC.Builder().model(Model.E1000).
build())).vnc(new VNC(null, VNC_PASSWORD, false)).build();
server = client.createAndStartServer(serverRequest);
checkCreatedServer();
Server newInfo = client.getServerInfo(server.getUuid());
checkServerMatchesGet(newInfo);
}
protected void checkServerMatchesGet(Server newInfo) {
assertEquals(newInfo.getUuid(), server.getUuid());
}
protected void checkCreatedServer() {
System.out.println(new Gson().toJson(server));
assertNotNull(server.getUuid());
assertNotNull(server.getUser());
assertEquals(server.getName(), prefix);
assertEquals(server.isPersistent(), true);
assertEquals(server.getDevices(),
ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid(drive.getUuid()).build()));
assertEquals(server.getBootDeviceIds(), ImmutableSet.of("ide:0:0"));
assertEquals(server.getNics(), ImmutableSet.of(new NIC.Builder()
.model(Model.E1000)
.block(
ImmutableList.of("tcp/43594", "tcp/5902", "udp/5060", "tcp/5900", "tcp/5901", "tcp/21", "tcp/22",
"tcp/23", "tcp/25", "tcp/110", "tcp/143", "tcp/43595")).build()));
assertEquals(server.getStatus(), ServerStatus.ACTIVE);
}
//TODO
// @Test(dependsOnMethods = "testCreateAndStartServer")
// public void testSetServerConfiguration() throws Exception {
//
// ServerInfo server2 = client.setServerConfiguration(server.getUuid(), new Server.Builder().name("rediculous")
// .tags(ImmutableSet.of("networking", "security", "gateway")).userMetadata(ImmutableMap.of("foo", "bar"))
// .build());
//
// assertNotNull(server2.getUuid(), server.getUuid());
// assertEquals(server2.getName(), "rediculous");
// assertEquals(server2.getTags(), ImmutableSet.of("networking", "security", "gateway"));
// assertEquals(server2.getUserMetadata(), ImmutableMap.of("foo", "bar"));
// server = server2;
// }
// @Test(dependsOnMethods = "testSetServerConfiguration")
@Test(dependsOnMethods = "testCreateAndStartServer")
public void testLifeCycle() throws Exception {
client.stopServer(server.getUuid());
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
client.startServer(server.getUuid());
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
client.resetServer(server.getUuid());
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
client.shutdownServer(server.getUuid());
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
}
@Test(dependsOnMethods = "testLifeCycle")
public void testDestroyServer() throws Exception {
client.destroyServer(server.getUuid());
assertEquals(client.getServerInfo(server.getUuid()), null);
}
@Test(dependsOnMethods = "testDestroyServer")
public void testDestroyDrive() throws Exception {
client.destroyDrive(drive.getUuid());
assertEquals(client.getDriveInfo(drive.getUuid()), null);
}
@AfterTest
public void cleanUp() {
try {
client.destroyServer(server.getUuid());
} catch (Exception e) {
// no need to check null or anything as we swallow all
}
try {
client.destroyDrive(drive.getUuid());
} catch (Exception e) {
}
}
}

View File

@ -27,10 +27,12 @@ import java.util.Properties;
import javax.ws.rs.core.MediaType;
import org.jclouds.elasticstack.binders.BindServerToPlainTextStringTest;
import org.jclouds.elasticstack.domain.CreateDriveRequest;
import org.jclouds.elasticstack.domain.Drive;
import org.jclouds.elasticstack.domain.DriveData;
import org.jclouds.elasticstack.domain.ImageConversionType;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.functions.KeyValuesDelimitedByBlankLinesToDriveInfo;
import org.jclouds.elasticstack.functions.KeyValuesDelimitedByBlankLinesToServerInfo;
import org.jclouds.elasticstack.functions.ListOfKeyValuesDelimitedByBlankLinesToDriveInfoSet;
@ -46,6 +48,7 @@ import org.jclouds.io.Payloads;
import org.jclouds.rest.RestClientTest;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.rest.internal.GeneratedHttpRequest;
@ -121,6 +124,137 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
}
public void testCreateAndStartServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("createAndStartServer", Server.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method,
BindServerToPlainTextStringTest.SERVER);
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/create HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, BindServerToPlainTextStringTest.CREATED_SERVER, "text/plain", false);
assertResponseParserClassEquals(method, httpRequest, KeyValuesDelimitedByBlankLinesToServerInfo.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testCreateServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("createServer", Server.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method,
BindServerToPlainTextStringTest.SERVER);
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/create/stopped HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, BindServerToPlainTextStringTest.CREATED_SERVER, "text/plain", false);
assertResponseParserClassEquals(method, httpRequest, KeyValuesDelimitedByBlankLinesToServerInfo.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testSetServerConfiguration() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("setServerConfiguration", String.class, Server.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100",
BindServerToPlainTextStringTest.SERVER);
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/100/set HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, BindServerToPlainTextStringTest.CREATED_SERVER, "text/plain", false);
assertResponseParserClassEquals(method, httpRequest, KeyValuesDelimitedByBlankLinesToServerInfo.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testDestroyServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("destroyServer", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/uuid/destroy HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testStartServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("startServer", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/uuid/start HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testStopServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("stopServer", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/uuid/stop HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testShutdownServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("shutdownServer", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/uuid/shutdown HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testResetServer() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("resetServer", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/servers/uuid/reset HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, MapHttp4xxCodesToExceptions.class);
checkFilters(httpRequest);
}
public void testListDrives() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("listDrives");
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method);
@ -213,6 +347,22 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
}
public void testDestroyDrive() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("destroyDrive", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/uuid/destroy HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testImageDrive() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("imageDrive", String.class, String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100", "200");
@ -247,22 +397,6 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
}
public void testDestroyDrive() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("destroyDrive", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "uuid");
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/uuid/destroy HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testReadDrive() throws SecurityException, NoSuchMethodException, IOException {
Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class);
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100");

View File

@ -38,50 +38,42 @@ import org.testng.annotations.Test;
@Test(groups = "live", testName = "elasticstack.ElasticStackClientLiveTest")
public class ElasticStackClientLiveTest extends
CommonElasticStackClientLiveTest<ElasticStackClient, ElasticStackAsyncClient> {
private DriveInfo info2;
private DriveInfo drive2;
private DriveInfo drive3;
@Override
public void testGetDrive() throws Exception {
super.testGetDrive();
}
@Override
public void testCreate() throws Exception {
super.testCreate();
}
@Override
public void testSetDriveData() throws Exception {
super.testSetDriveData();
}
@Override
public void testDestroyDrive() throws Exception {
super.testDestroyDrive();
}
@Test(dependsOnMethods = "testCreate")
public void testWeCanReadAndWriteToDrive() throws IOException {
client.writeDrive(info.getUuid(), Payloads.newStringPayload("foo"));
assertEquals(Utils.toStringAndClose(client.readDrive(info.getUuid(), ReadDriveOptions.Builder.offset(0).size(3))
.getInput()), "foo");
drive2 = client.createDrive(new CreateDriveRequest.Builder().name(prefix + "2").size(1 * 1024 * 1024l).build());
client.writeDrive(drive2.getUuid(), Payloads.newStringPayload("foo"));
assertEquals(Utils.toStringAndClose(client
.readDrive(drive2.getUuid(), ReadDriveOptions.Builder.offset(0).size(3)).getInput()), "foo");
}
@Test(dependsOnMethods = "testWeCanReadAndWriteToDrive")
public void testWeCopyADriveContentsViaGzip() throws IOException {
try {
info2 = client.createDrive(new CreateDriveRequest.Builder().name(prefix + "2").size(4 * 1024 * 1024l).build());
client.imageDrive(info.getUuid(), info2.getUuid());
// TODO block until complete
System.err.println("state " + client.getDriveInfo(info2.getUuid()));
assertEquals(Utils.toStringAndClose(client.readDrive(info2.getUuid(),
drive3 = client
.createDrive(new CreateDriveRequest.Builder().name(prefix + "3").size(1 * 1024 * 1024l).build());
System.err.println("before image; drive 2" + client.getDriveInfo(drive2.getUuid()));
System.err.println("before image; drive 3" + client.getDriveInfo(drive3.getUuid()));
client.imageDrive(drive2.getUuid(), drive3.getUuid());
assert driveNotClaimed.apply(drive3) : client.getDriveInfo(drive3.getUuid());
assert driveNotClaimed.apply(drive2) : client.getDriveInfo(drive2.getUuid());
System.err.println("after image; drive 2" + client.getDriveInfo(drive2.getUuid()));
System.err.println("after image; drive 3" + client.getDriveInfo(drive3.getUuid()));
assertEquals(Utils.toStringAndClose(client.readDrive(drive3.getUuid(),
ReadDriveOptions.Builder.offset(0).size(3)).getInput()), "foo");
} finally {
client.destroyDrive(info2.getUuid());
}
client.destroyDrive(drive2.getUuid());
client.destroyDrive(drive3.getUuid());
}
}
@Override
protected void prepareDrive() {
System.err.println("before prepare" + client.getDriveInfo(drive.getUuid()));
client.imageDrive("e6111e4c-67af-4438-b1bc-189747d5a8e5", drive.getUuid());
assert driveNotClaimed.apply(drive) : client.getDriveInfo(drive.getUuid());
System.err.println("after prepare" + client.getDriveInfo(drive.getUuid()));
}
}

View File

@ -0,0 +1,92 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.elasticstack.binders;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.jclouds.elasticstack.domain.IDEDevice;
import org.jclouds.elasticstack.domain.Model;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.VNC;
import org.jclouds.elasticstack.functions.ServerToMap;
import org.jclouds.http.HttpRequest;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.TypeLiteral;
/**
*
* @author Adrian Cole
*/
@Test(groups = { "unit" })
public class BindServerToPlainTextStringTest {
public static String CREATED_SERVER;
static {
try {
CREATED_SERVER = Utils.toStringAndClose(BindServerToPlainTextStringTest.class
.getResourceAsStream("/create_server.txt"));
} catch (IOException e) {
CREATED_SERVER = null;
}
}
public static final Server SERVER = new Server.Builder()
.name("TestServer")
.cpu(2000)
.mem(1024)
.devices(
ImmutableMap.of("ide:0:0", new IDEDevice.Builder(0, 0).uuid("08c92dd5-70a0-4f51-83d2-835919d254df")
.build())).bootDeviceIds(ImmutableSet.of("ide:0:0"))
.nics(ImmutableSet.of(new NIC.Builder().model(Model.E1000).
build())).vnc(new VNC(null, "XXXXXXXX", false)).build();
private static final BindServerToPlainTextString FN = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Function<Server, Map<String, String>>>() {
}).to(ServerToMap.class);
bind(new TypeLiteral<Function<Server, Map<String, String>>>() {
}).to(ServerToMap.class);
}
}).getInstance(BindServerToPlainTextString.class);
public void testSimple() throws IOException {
HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create"));
FN.bindToRequest(request, SERVER);
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN);
assertEquals(request.getPayload().getRawContent(), CREATED_SERVER);
}
}

View File

@ -73,4 +73,9 @@ public class KeyValuesDelimitedByBlankLinesToServerInfoTest {
assertEquals(FN.apply(new HttpResponse(200, "", Payloads.newInputStreamPayload(MapToServerInfoTest.class
.getResourceAsStream("/servers.txt")))), MapToServerInfoTest.ONE);
}
public void testNew() {
assertEquals(FN.apply(new HttpResponse(200, "", Payloads.newInputStreamPayload(MapToServerInfoTest.class
.getResourceAsStream("/new_server.txt")))), MapToServerInfoTest.ONE);
}
}

View File

@ -124,4 +124,39 @@ public class MapToServerInfoTest {
assertEquals(MAP_TO_DRIVE.apply(input), ONE);
}
public static ServerInfo NEW = new ServerInfo.Builder()
.persistent(true)
.uuid("bd98615a-6f74-4d63-ad1e-b13338b9356a")
.cpu(1000)
.bootDeviceIds(ImmutableSet.of("ide:0:0"))
.smp(1)
.mem(512)
.status(ServerStatus.ACTIVE)
.started(new Date(1292695612))
.user("2f6244eb-50bc-4403-847e-f03cc3706a1f")
.name("adriancole.test")
.vnc(new VNC("83.222.249.221", "XXXXXXXX", false))
.nics(ImmutableSet.of(new NIC.Builder()
.model(Model.E1000)
.block(
ImmutableList.of("tcp/43594", "tcp/5902", "udp/5060", "tcp/5900", "tcp/5901", "tcp/21", "tcp/22",
"tcp/23", "tcp/25", "tcp/110", "tcp/143", "tcp/43595")).build()))
.devices(
ImmutableMap.of("ide:0:0",
new IDEDevice.Builder((int) 0, (int) 0).uuid("403c9a86-0aab-4e47-aa95-e9768021c4c1").build()
))
.metrics(
new ServerMetrics.Builder().driveMetrics(ImmutableMap.of("ide:0:0", new DriveMetrics.Builder().build()))
.build()).build();
public void testNew() throws IOException {
Map<String, String> input = new ListOfKeyValuesDelimitedByBlankLinesToListOfMaps().apply(
Utils.toStringAndClose(MapToServerInfoTest.class.getResourceAsStream("/new_server.txt"))).get(0);
assertEquals(MAP_TO_DRIVE.apply(input), NEW);
}
}

View File

@ -0,0 +1,66 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.elasticstack.functions;
import static org.testng.Assert.assertEquals;
import org.jclouds.elasticstack.domain.IDEDevice;
import org.jclouds.elasticstack.domain.Model;
import org.jclouds.elasticstack.domain.NIC;
import org.jclouds.elasticstack.domain.Server;
import org.jclouds.elasticstack.domain.VNC;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Test(groups = { "unit" })
public class ServerToMapTest {
private static final ServerToMap SERVER_TO_MAP = new ServerToMap();
public void testBasics() {
assertEquals(
SERVER_TO_MAP.apply(new Server.Builder()
.name("TestServer")
.cpu(2000)
.mem(1024)
.devices(
ImmutableMap.of("ide:0:0",
new IDEDevice.Builder(0, 0).uuid("08c92dd5-70a0-4f51-83d2-835919d254df").build()))
.bootDeviceIds(ImmutableSet.of("ide:0:0")).nics(ImmutableSet.of(new NIC.Builder().model(Model.E1000).
build())).vnc(new VNC(null, "XXXXXXXX", false)).build()),
ImmutableMap
.builder()
.putAll(ImmutableMap.of("name", "TestServer", "cpu", "2000", "smp", "auto", "mem", "1024"))
.putAll(
ImmutableMap.of("persistent", "false", "boot", "ide:0:0", "ide:0:0",
"08c92dd5-70a0-4f51-83d2-835919d254df"))
.putAll(
ImmutableMap.of("ide:0:0:media", "disk", "nic:0:model", "e1000", "vnc:ip", "auto",
"vnc:password", "XXXXXXXX")).build());
}
}

View File

@ -0,0 +1,44 @@
package org.jclouds.elasticstack.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.elasticstack.CommonElasticStackClient;
import org.jclouds.elasticstack.domain.DriveInfo;
import org.jclouds.logging.Logger;
import com.google.common.base.Predicate;
import com.google.inject.Inject;
/**
*
* @author Adrian Cole
*/
@Singleton
public class DriveClaimed implements Predicate<DriveInfo> {
private final CommonElasticStackClient client;
@Resource
protected Logger logger = Logger.NULL;
@Inject
public DriveClaimed(CommonElasticStackClient client) {
this.client = client;
}
public boolean apply(DriveInfo drive) {
logger.trace("looking for claims on drive %s", checkNotNull(drive, "drive"));
drive = refresh(drive);
if (drive == null)
return false;
logger.trace("%s: looking for drive claims: currently: %s", drive.getUuid(), drive.getClaimed());
return drive.getClaimed().size() > 0;
}
private DriveInfo refresh(DriveInfo drive) {
return client.getDriveInfo(drive.getUuid());
}
}

View File

@ -0,0 +1,11 @@
name TestServer
cpu 2000
smp auto
mem 1024
persistent false
boot ide:0:0
ide:0:0 08c92dd5-70a0-4f51-83d2-835919d254df
ide:0:0:media disk
nic:0:model e1000
vnc:ip auto
vnc:password XXXXXXXX

View File

@ -0,0 +1,24 @@
ide:0:0:write:requests 0
boot ide:0:0
vnc:password XXXXXXXX
ide:0:0 403c9a86-0aab-4e47-aa95-e9768021c4c1
ide:0:0:read:requests 0
ide:0:0:read:bytes 0
vnc:ip 83.222.249.221
tx:packets 0
tx 0
rx 0
smp 1
mem 512
nic:0:model e1000
status active
started 1292695612
rx:packets 0
user 2f6244eb-50bc-4403-847e-f03cc3706a1f
ide:0:0:media disk
name adriancole.test
persistent true
nic:0:block tcp/43594 tcp/5902 udp/5060 tcp/5900 tcp/5901 tcp/21 tcp/22 tcp/23 tcp/25 tcp/110 tcp/143 tcp/43595
server bd98615a-6f74-4d63-ad1e-b13338b9356a
ide:0:0:write:bytes 0
cpu 1000