mirror of https://github.com/apache/jclouds.git
Issue 412: updated syntax for readDrive based on notes from elasticstack
This commit is contained in:
parent
81760ee086
commit
c57e09c5ae
|
@ -20,20 +20,16 @@
|
|||
package org.jclouds.elasticstack;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.jclouds.elasticstack.binders.BindReadDriveOptionsToPath;
|
||||
import org.jclouds.elasticstack.domain.ImageConversionType;
|
||||
import org.jclouds.elasticstack.functions.ReturnPayload;
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.http.filters.BasicAuthentication;
|
||||
import org.jclouds.io.Payload;
|
||||
import org.jclouds.rest.annotations.BinderParam;
|
||||
import org.jclouds.rest.annotations.ExceptionParser;
|
||||
import org.jclouds.rest.annotations.RequestFilters;
|
||||
import org.jclouds.rest.annotations.ResponseParser;
|
||||
|
@ -72,25 +68,15 @@ public interface ElasticStackAsyncClient extends CommonElasticStackAsyncClient {
|
|||
@PathParam("conversion") ImageConversionType conversionType);
|
||||
|
||||
/**
|
||||
* @see ElasticStackClient#readDrive(String)
|
||||
*/
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
|
||||
@Path("/drives/{uuid}/read")
|
||||
@ResponseParser(ReturnPayload.class)
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
ListenableFuture<Payload> readDrive(@PathParam("uuid") String uuid);
|
||||
|
||||
/**
|
||||
* @see ElasticStackClient#readDrive(String,ReadDriveOptions)
|
||||
* @see ElasticStackClient#readDrive
|
||||
*/
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
|
||||
@Path("/drives/{uuid}/read")
|
||||
@Path("/drives/{uuid}/read/{offset}/{size}")
|
||||
@ResponseParser(ReturnPayload.class)
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
ListenableFuture<Payload> readDrive(@PathParam("uuid") String uuid,
|
||||
@BinderParam(BindReadDriveOptionsToPath.class) ReadDriveOptions options);
|
||||
ListenableFuture<Payload> readDrive(@PathParam("uuid") String uuid, @PathParam("offset") long offset,
|
||||
@PathParam("size") long size);
|
||||
|
||||
/**
|
||||
* @see ElasticStackClient#writeDrive(String, Payload)
|
||||
|
|
|
@ -23,7 +23,6 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import org.jclouds.concurrent.Timeout;
|
||||
import org.jclouds.elasticstack.domain.ImageConversionType;
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.io.Payload;
|
||||
|
||||
/**
|
||||
|
@ -60,16 +59,13 @@ public interface ElasticStackClient extends CommonElasticStackClient {
|
|||
*
|
||||
* @param uuid
|
||||
* drive to read
|
||||
* @param offset
|
||||
* start at the specified offset in bytes
|
||||
* @param size
|
||||
* the specified size in bytes; must be <=4096k
|
||||
* @return binary content of the drive.
|
||||
*/
|
||||
Payload readDrive(String uuid);
|
||||
|
||||
/**
|
||||
* @see #readDrive(String)
|
||||
* @param options
|
||||
* controls offset and size of the request
|
||||
*/
|
||||
Payload readDrive(String uuid, ReadDriveOptions options);
|
||||
Payload readDrive(String uuid, long offset, long size);
|
||||
|
||||
/**
|
||||
* Write binary data to a drive
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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 javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.inject.Singleton;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.rest.Binder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class BindReadDriveOptionsToPath implements Binder {
|
||||
private final Provider<UriBuilder> uriBuilderProvider;
|
||||
|
||||
@Inject
|
||||
public BindReadDriveOptionsToPath(Provider<UriBuilder> uriBuilderProvider) {
|
||||
this.uriBuilderProvider = uriBuilderProvider;
|
||||
}
|
||||
|
||||
public void bindToRequest(HttpRequest request, Object payload) {
|
||||
checkArgument(payload instanceof ReadDriveOptions, "this binder is only valid for ReadDriveOptions!");
|
||||
ReadDriveOptions options = ReadDriveOptions.class.cast(payload);
|
||||
if (options.getOffset() != null || options.getSize() != null){
|
||||
UriBuilder builder = uriBuilderProvider.get().uri(request.getEndpoint());
|
||||
if (options.getOffset() != null)
|
||||
builder.path("/"+options.getOffset());
|
||||
if (options.getSize() != null)
|
||||
builder.path("/"+options.getSize());
|
||||
request.setEndpoint(builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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.options;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
/**
|
||||
* Contains options supported for read drive operations. <h2>
|
||||
* Usage</h2> The recommended way to instantiate a ReadDriveOptions object is to statically import
|
||||
* ReadDriveOptions.Builder.* and invoke a static creation method followed by an instance mutator
|
||||
* (if needed):
|
||||
* <p/>
|
||||
* <code>
|
||||
* import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.*;
|
||||
*
|
||||
*
|
||||
* // this will get the first 1024 bytes starting at offset 2048
|
||||
* Payload payload = client.readDrive("drive-uuid",offset(2048l).size(1024l));
|
||||
* <code>
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*
|
||||
*/
|
||||
public class ReadDriveOptions {
|
||||
|
||||
private Long offset;
|
||||
private Long size;
|
||||
|
||||
/**
|
||||
* start at the specified offset in bytes
|
||||
*/
|
||||
public ReadDriveOptions offset(long offset) {
|
||||
checkArgument(offset >= 0, "start must be >= 0");
|
||||
this.offset = offset;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* download the specified size in bytes
|
||||
*/
|
||||
public ReadDriveOptions size(long size) {
|
||||
checkArgument(size >= 0, "start must be >= 0");
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
/**
|
||||
* @see ReadDriveOptions#offset
|
||||
*/
|
||||
public static ReadDriveOptions offset(long offset) {
|
||||
ReadDriveOptions options = new ReadDriveOptions();
|
||||
return options.offset(offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ReadDriveOptions#size
|
||||
*/
|
||||
public static ReadDriveOptions size(long size) {
|
||||
ReadDriveOptions options = new ReadDriveOptions();
|
||||
return options.size(size);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Long getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public Long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
|
@ -246,12 +246,15 @@ public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticSt
|
|||
"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"))
|
||||
// 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());
|
||||
|
@ -273,8 +276,9 @@ public abstract class CommonElasticStackClientLiveTest<S extends CommonElasticSt
|
|||
client.resetServer(server.getUuid());
|
||||
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
|
||||
|
||||
// for some reason shutdown doesn't immediately occur
|
||||
client.shutdownServer(server.getUuid());
|
||||
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.STOPPED);
|
||||
assertEquals(client.getServerInfo(server.getUuid()).getStatus(), ServerStatus.ACTIVE);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testLifeCycle")
|
||||
|
|
|
@ -39,7 +39,6 @@ import org.jclouds.elasticstack.functions.ListOfKeyValuesDelimitedByBlankLinesTo
|
|||
import org.jclouds.elasticstack.functions.ListOfKeyValuesDelimitedByBlankLinesToServerInfoSet;
|
||||
import org.jclouds.elasticstack.functions.ReturnPayload;
|
||||
import org.jclouds.elasticstack.functions.SplitNewlines;
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.filters.BasicAuthentication;
|
||||
import org.jclouds.http.functions.ReleasePayloadAndReturn;
|
||||
|
@ -398,24 +397,8 @@ public class ElasticStackAsyncClientTest extends RestClientTest<ElasticStackAsyn
|
|||
}
|
||||
|
||||
public void testReadDrive() throws SecurityException, NoSuchMethodException, IOException {
|
||||
Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class);
|
||||
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100");
|
||||
|
||||
assertRequestLineEquals(httpRequest, "GET https://api.elasticstack.com/drives/100/read HTTP/1.1");
|
||||
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/octet-stream\n");
|
||||
assertPayloadEquals(httpRequest, null, null, false);
|
||||
|
||||
assertResponseParserClassEquals(method, httpRequest, ReturnPayload.class);
|
||||
assertSaxResponseParserClassEquals(method, null);
|
||||
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
|
||||
|
||||
checkFilters(httpRequest);
|
||||
}
|
||||
|
||||
public void testReadDriveOptions() throws SecurityException, NoSuchMethodException, IOException {
|
||||
Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class, ReadDriveOptions.class);
|
||||
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100",
|
||||
new ReadDriveOptions().offset(1024).size(2048));
|
||||
Method method = ElasticStackAsyncClient.class.getMethod("readDrive", String.class, long.class, long.class);
|
||||
GeneratedHttpRequest<ElasticStackAsyncClient> httpRequest = processor.createRequest(method, "100", 1024, 2048);
|
||||
|
||||
assertRequestLineEquals(httpRequest, "POST https://api.elasticstack.com/drives/100/read/1024/2048 HTTP/1.1");
|
||||
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/octet-stream\n");
|
||||
|
|
|
@ -25,7 +25,6 @@ import java.io.IOException;
|
|||
|
||||
import org.jclouds.elasticstack.domain.CreateDriveRequest;
|
||||
import org.jclouds.elasticstack.domain.DriveInfo;
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.util.Utils;
|
||||
import org.testng.annotations.Test;
|
||||
|
@ -44,8 +43,7 @@ public class ElasticStackClientLiveTest extends
|
|||
public void testWeCanReadAndWriteToDrive() throws IOException {
|
||||
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");
|
||||
assertEquals(Utils.toStringAndClose(client.readDrive(drive2.getUuid(), 0, 3).getInput()), "foo");
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testWeCanReadAndWriteToDrive")
|
||||
|
@ -60,8 +58,7 @@ public class ElasticStackClientLiveTest extends
|
|||
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");
|
||||
assertEquals(Utils.toStringAndClose(client.readDrive(drive3.getUuid(), 0, 3).getInput()), "foo");
|
||||
} finally {
|
||||
client.destroyDrive(drive2.getUuid());
|
||||
client.destroyDrive(drive3.getUuid());
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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.net.URI;
|
||||
|
||||
import org.jclouds.elasticstack.options.ReadDriveOptions;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.logging.config.NullLoggingModule;
|
||||
import org.jclouds.rest.BaseRestClientTest.MockModule;
|
||||
import org.jclouds.rest.config.RestModule;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = { "unit" })
|
||||
public class BindReadDriveOptionsToPathTest {
|
||||
|
||||
private static final BindReadDriveOptionsToPath FN = Guice.createInjector(new RestModule(), new MockModule(),
|
||||
new NullLoggingModule()).getInstance(BindReadDriveOptionsToPath.class);
|
||||
|
||||
public void testSimple() {
|
||||
HttpRequest request = new HttpRequest("POST", URI.create("https://drives/read"));
|
||||
FN.bindToRequest(request, new ReadDriveOptions().offset(1024l).size(2048l));
|
||||
assertEquals(request.getEndpoint().getPath(), "/read/1024/2048");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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.options;
|
||||
|
||||
import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.offset;
|
||||
import static org.jclouds.elasticstack.options.ReadDriveOptions.Builder.size;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNull;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* Tests possible uses of ReadDriveOptions and ReadDriveOptions.Builder.*
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit")
|
||||
public class ReadDriveOptionsTest {
|
||||
|
||||
@Test
|
||||
public void testNullOffset() {
|
||||
ReadDriveOptions options = new ReadDriveOptions();
|
||||
assertNull(options.getOffset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffset() {
|
||||
ReadDriveOptions options = new ReadDriveOptions().offset(1024);
|
||||
assertEquals(options.getOffset(), new Long(1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffsetStatic() {
|
||||
ReadDriveOptions options = offset(1024);
|
||||
assertEquals(options.getOffset(), new Long(1024));
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testOffsetNegative() {
|
||||
offset(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullSize() {
|
||||
ReadDriveOptions options = new ReadDriveOptions();
|
||||
assertNull(options.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
ReadDriveOptions options = new ReadDriveOptions().size(1024);
|
||||
assertEquals(options.getSize(), new Long(1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSizeStatic() {
|
||||
ReadDriveOptions options = size(1024);
|
||||
assertEquals(options.getSize(), new Long(1024));
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||
public void testSizeNegative() {
|
||||
size(-1);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue