From 486ba283605ed6b14bb11d81e2411d6cf106c2fd Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 21 Nov 2010 18:55:46 +0100 Subject: [PATCH] added setDriveData to elastichosts impl --- .../elastichosts/ElasticHostsAsyncClient.java | 12 +++ .../elastichosts/ElasticHostsClient.java | 12 +++ .../BindDriveDataToPlainTextString.java | 61 ++++++++++++ .../elastichosts/domain/DriveData.java | 93 +++++++++++++++++++ .../functions/DriveDataToMap.java | 51 ++++++++++ .../ElasticHostsAsyncClientTest.java | 18 ++++ .../ElasticHostsClientLiveTest.java | 47 +++++++--- .../BindDriveDataToPlainTextStringTest.java | 73 +++++++++++++++ .../functions/DriveDataToMapTest.java | 66 +++++++++++++ .../src/test/resources/drive_data.txt | 7 ++ 10 files changed, 427 insertions(+), 13 deletions(-) create mode 100644 sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextString.java create mode 100644 sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/domain/DriveData.java create mode 100644 sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/functions/DriveDataToMap.java create mode 100644 sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextStringTest.java create mode 100644 sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/functions/DriveDataToMapTest.java create mode 100644 sandbox/elastichosts/src/test/resources/drive_data.txt diff --git a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsAsyncClient.java b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsAsyncClient.java index d20bc90698..1b365dd31d 100644 --- a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsAsyncClient.java +++ b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsAsyncClient.java @@ -29,7 +29,9 @@ import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import org.jclouds.elastichosts.binders.BindCreateDriveRequestToPlainTextString; +import org.jclouds.elastichosts.binders.BindDriveDataToPlainTextString; import org.jclouds.elastichosts.domain.CreateDriveRequest; +import org.jclouds.elastichosts.domain.DriveData; import org.jclouds.elastichosts.domain.DriveInfo; import org.jclouds.elastichosts.functions.KeyValuesDelimitedByBlankLinesToDriveInfo; import org.jclouds.elastichosts.functions.ListOfKeyValuesDelimitedByBlankLinesToDriveInfoSet; @@ -99,6 +101,16 @@ public interface ElasticHostsAsyncClient { ListenableFuture createDrive( @BinderParam(BindCreateDriveRequestToPlainTextString.class) CreateDriveRequest createDrive); + /** + * @see ElasticHostsClient#setDriveData + */ + @POST + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @ResponseParser(KeyValuesDelimitedByBlankLinesToDriveInfo.class) + @Path("/drives/{uuid}/set") + ListenableFuture setDriveData(@PathParam("uuid") String uuid, + @BinderParam(BindDriveDataToPlainTextString.class) DriveData createDrive); + /** * @see ElasticHostsClient#deleteDrive */ diff --git a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsClient.java b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsClient.java index af5ddc0de4..fa67c9f3dc 100644 --- a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsClient.java +++ b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/ElasticHostsClient.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; import org.jclouds.elastichosts.domain.CreateDriveRequest; +import org.jclouds.elastichosts.domain.DriveData; import org.jclouds.elastichosts.domain.DriveInfo; /** @@ -73,6 +74,17 @@ public interface ElasticHostsClient { */ DriveInfo createDrive(CreateDriveRequest createDrive); + /** + * set extra drive data + * + * @param uuid + * what drive to change + * @param driveData + * what values to change + * @return new data + */ + DriveInfo setDriveData(String uuid, DriveData driveData); + /** * Destroy a drive * diff --git a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextString.java b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextString.java new file mode 100644 index 0000000000..cb62e165a7 --- /dev/null +++ b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextString.java @@ -0,0 +1,61 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.elastichosts.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.elastichosts.domain.DriveData; +import org.jclouds.elastichosts.functions.DriveDataToMap; +import org.jclouds.elastichosts.functions.ListOfMapsToListOfKeyValuesDelimitedByBlankLines; +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; + +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class BindDriveDataToPlainTextString implements Binder { + private final DriveDataToMap createDriveRequestToMap; + private final ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines; + + @Inject + public BindDriveDataToPlainTextString(DriveDataToMap createDriveRequestToMap, + ListOfMapsToListOfKeyValuesDelimitedByBlankLines listOfMapsToListOfKeyValuesDelimitedByBlankLines) { + this.createDriveRequestToMap = createDriveRequestToMap; + this.listOfMapsToListOfKeyValuesDelimitedByBlankLines = listOfMapsToListOfKeyValuesDelimitedByBlankLines; + } + + public void bindToRequest(HttpRequest request, Object payload) { + checkArgument(payload instanceof DriveData, "this binder is only valid for DriveData!"); + DriveData create = DriveData.class.cast(payload); + Map map = createDriveRequestToMap.apply(create); + request.setPayload(listOfMapsToListOfKeyValuesDelimitedByBlankLines.apply(ImmutableSet.of(map))); + request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_PLAIN); + } +} diff --git a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/domain/DriveData.java b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/domain/DriveData.java new file mode 100644 index 0000000000..74bd811299 --- /dev/null +++ b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/domain/DriveData.java @@ -0,0 +1,93 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.elastichosts.domain; + +import java.util.Map; + +import javax.annotation.Nullable; + +import org.jclouds.elastichosts.domain.internal.BaseDrive; + +/** + * + * @author Adrian Cole + */ +public class DriveData extends BaseDrive { + public static class Builder extends BaseDrive.Builder { + + /** + * {@inheritDoc} + */ + @Override + public Builder claimType(ClaimType claimType) { + return Builder.class.cast(super.claimType(claimType)); + } + + + /** + * {@inheritDoc} + */ + @Override + public Builder name(String name) { + return Builder.class.cast(super.name(name)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder readers(Iterable readers) { + return Builder.class.cast(super.readers(readers)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder size(long size) { + return Builder.class.cast(super.size(size)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder tags(Iterable tags) { + return Builder.class.cast(super.tags(tags)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder userMetadata(Map userMetadata) { + return Builder.class.cast(super.userMetadata(userMetadata)); + } + + public DriveData build() { + return new DriveData(name, size, claimType, readers, tags, userMetadata); + } + } + + public DriveData(String name, long size, @Nullable ClaimType claimType, Iterable readers, + Iterable tags, Map userMetadata) { + super(name, size, claimType, readers, tags, userMetadata); + } +} \ No newline at end of file diff --git a/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/functions/DriveDataToMap.java b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/functions/DriveDataToMap.java new file mode 100644 index 0000000000..9a2c3b5e38 --- /dev/null +++ b/sandbox/elastichosts/src/main/java/org/jclouds/elastichosts/functions/DriveDataToMap.java @@ -0,0 +1,51 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.elastichosts.functions; + +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.elastichosts.domain.DriveData; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class DriveDataToMap implements Function> { + private final BaseDriveToMap baseDriveToMap; + + @Inject + public DriveDataToMap(BaseDriveToMap baseDriveToMap) { + this.baseDriveToMap = baseDriveToMap; + } + + @Override + public Map apply(DriveData from) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.putAll(baseDriveToMap.apply(from)); + return builder.build(); + } +} \ No newline at end of file diff --git a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsAsyncClientTest.java b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsAsyncClientTest.java index 7302eab71c..e39405af1f 100644 --- a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsAsyncClientTest.java +++ b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsAsyncClientTest.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.lang.reflect.Method; import org.jclouds.elastichosts.domain.CreateDriveRequest; +import org.jclouds.elastichosts.domain.DriveData; import org.jclouds.elastichosts.functions.KeyValuesDelimitedByBlankLinesToDriveInfo; import org.jclouds.elastichosts.functions.ListOfKeyValuesDelimitedByBlankLinesToDriveInfoSet; import org.jclouds.elastichosts.functions.SplitNewlines; @@ -142,6 +143,23 @@ public class ElasticHostsAsyncClientTest extends RestClientTest httpRequest = processor.createRequest(method, "100", + new DriveData.Builder().name("foo").size(10000l).build()); + + assertRequestLineEquals(httpRequest, "POST https://api.elastichosts.com/drives/100/set HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: text/plain\n"); + assertPayloadEquals(httpRequest, "name foo\nsize 10000", "text/plain", false); + + assertResponseParserClassEquals(method, httpRequest, KeyValuesDelimitedByBlankLinesToDriveInfo.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + public void testDestroyDrive() throws SecurityException, NoSuchMethodException, IOException { Method method = ElasticHostsAsyncClient.class.getMethod("destroyDrive", String.class); GeneratedHttpRequest httpRequest = processor.createRequest(method, "uuid"); diff --git a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsClientLiveTest.java b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsClientLiveTest.java index 50f26dd2d9..f71b66a968 100644 --- a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsClientLiveTest.java +++ b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/ElasticHostsClientLiveTest.java @@ -29,7 +29,9 @@ import java.util.Properties; import java.util.Set; import org.jclouds.Constants; +import org.jclouds.elastichosts.domain.ClaimType; import org.jclouds.elastichosts.domain.CreateDriveRequest; +import org.jclouds.elastichosts.domain.DriveData; import org.jclouds.elastichosts.domain.DriveInfo; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.rest.RestContext; @@ -38,6 +40,7 @@ import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.inject.Module; @@ -128,27 +131,45 @@ public class ElasticHostsClientLiveTest { } private String prefix = System.getProperty("user.name") + ".test"; + private DriveInfo info; @Test - public void testCreateDestroy() throws Exception { + public void testCreate() throws Exception { try { findAndDestroyDrive(); } catch (Exception e) { } - String uuid = null; - try { - DriveInfo info = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(1024 * 1024l).build()); - assertNotNull(uuid = info.getUuid()); - assertEquals(info.getName(), prefix); - assertEquals(info.getSize(), 1024 * 1024l); - assertEquals(info, client.getDriveInfo(info.getUuid())); - } finally { - findAndDestroyDrive(); - } - if (uuid != null) - assertEquals(client.getDriveInfo(uuid), null); + info = client.createDrive(new CreateDriveRequest.Builder().name(prefix).size(1024 * 1024l).build()); + assertNotNull(info.getUuid()); + assertEquals(info.getName(), prefix); + assertEquals(info.getSize(), 1024 * 1024l); + assertEquals(info, client.getDriveInfo(info.getUuid())); + + } + + @Test(dependsOnMethods = "testCreate") + public void testSetDriveData() throws Exception { + + DriveInfo info2 = client.setDriveData( + info.getUuid(), + new DriveData.Builder().claimType(ClaimType.SHARED).name("rediculous") + .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")) + .tags(ImmutableSet.of("tag1", "tag2")).userMetadata(ImmutableMap.of("foo", "bar")).build()); + assertNotNull(info2.getUuid(), info.getUuid()); + assertEquals(info.getName(), "rediculous"); + assertEquals(info.getClaimType(), ClaimType.SHARED); + assertEquals(info.getReaders(), ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff")); + assertEquals(info.getTags(), ImmutableSet.of("tag1", "tag2")); + assertEquals(info.getUserMetadata(), ImmutableMap.of("foo", "bar")); + } + + @Test(dependsOnMethods = "testSetDriveData") + public void testDestroyDrive() throws Exception { + + findAndDestroyDrive(); + assertEquals(client.getDriveInfo(info.getUuid()), null); } diff --git a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextStringTest.java b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextStringTest.java new file mode 100644 index 0000000000..9209275ec7 --- /dev/null +++ b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/binders/BindDriveDataToPlainTextStringTest.java @@ -0,0 +1,73 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.elastichosts.binders; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.net.URI; + +import javax.ws.rs.core.MediaType; + +import org.jclouds.elastichosts.domain.ClaimType; +import org.jclouds.elastichosts.domain.DriveData; +import org.jclouds.http.HttpRequest; +import org.jclouds.util.Utils; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; + +/** + * + * @author Adrian Cole + */ +@Test(groups = { "unit" }) +public class BindDriveDataToPlainTextStringTest { + + private static final BindDriveDataToPlainTextString FN = Guice.createInjector().getInstance( + BindDriveDataToPlainTextString.class); + + public void testSimple() { + HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create")); + FN.bindToRequest(request, new DriveData.Builder().name("foo").size(100l).build()); + assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN); + assertEquals(request.getPayload().getRawContent(), "name foo\nsize 100"); + } + + public void testComplete() throws IOException { + DriveData input = new DriveData.Builder().name("Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System") + // + .size(8589934592l)// + .claimType(ClaimType.SHARED)// + .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"))// + .tags(ImmutableSet.of("tag1", "tag2")).userMetadata(ImmutableMap.of("foo", "bar", "baz", "raz"))// + .build(); + + HttpRequest request = new HttpRequest("POST", URI.create("https://host/drives/create")); + FN.bindToRequest(request, input); + assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.TEXT_PLAIN); + assertEquals(request.getPayload().getRawContent(), + Utils.toStringAndClose(BindDriveDataToPlainTextStringTest.class + .getResourceAsStream("/drive_data.txt"))); + + } +} \ No newline at end of file diff --git a/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/functions/DriveDataToMapTest.java b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/functions/DriveDataToMapTest.java new file mode 100644 index 0000000000..5ec2f96c47 --- /dev/null +++ b/sandbox/elastichosts/src/test/java/org/jclouds/elastichosts/functions/DriveDataToMapTest.java @@ -0,0 +1,66 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.elastichosts.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; + +import org.jclouds.elastichosts.domain.ClaimType; +import org.jclouds.elastichosts.domain.DriveData; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; + +/** + * + * @author Adrian Cole + */ +@Test(groups = { "unit" }) +public class DriveDataToMapTest { + + private static final DriveDataToMap BASEDRIVE_TO_MAP = Guice.createInjector().getInstance(DriveDataToMap.class); + + public void testBasics() { + assertEquals(BASEDRIVE_TO_MAP.apply(new DriveData.Builder().name("foo").size(100l).build()), + ImmutableMap.of("name", "foo", "size", "100")); + } + + public void testComplete() throws IOException { + DriveData one = new DriveData.Builder().name("Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System") + // + .size(8589934592l)// + .claimType(ClaimType.SHARED)// + .readers(ImmutableSet.of("ffffffff-ffff-ffff-ffff-ffffffffffff"))// + .tags(ImmutableSet.of("tag1", "tag2")).userMetadata(ImmutableMap.of("foo", "bar", "baz", "raz"))// + .build(); + assertEquals( + BASEDRIVE_TO_MAP.apply(one), + ImmutableMap.builder().put("name", "Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System") + .put("size", "8589934592").put("claim:type", "shared") + .put("readers", "ffffffff-ffff-ffff-ffff-ffffffffffff").put("tags", "tag1 tag2") + .put("user:foo", "bar").put("user:baz", "raz").build() + + ); + + } +} \ No newline at end of file diff --git a/sandbox/elastichosts/src/test/resources/drive_data.txt b/sandbox/elastichosts/src/test/resources/drive_data.txt new file mode 100644 index 0000000000..b96bc0afd3 --- /dev/null +++ b/sandbox/elastichosts/src/test/resources/drive_data.txt @@ -0,0 +1,7 @@ +name Ubuntu 10.10 Server Edition Linux 64bit Preinstalled System +size 8589934592 +claim:type shared +readers ffffffff-ffff-ffff-ffff-ffffffffffff +tags tag1 tag2 +user:foo bar +user:baz raz \ No newline at end of file