From 14053f0341313b8dd3ce9ca4747204abaac3e133 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Fri, 17 Jul 2009 18:37:52 +0000 Subject: [PATCH] Issue 77: getImageDetails, except the test is disabled due to rackspace issue 118856 git-svn-id: http://jclouds.googlecode.com/svn/trunk@1641 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../cloudservers/CloudServersConnection.java | 29 ++++++-- .../rackspace/cloudservers/domain/Image.java | 2 + .../functions/ParseImageFromGsonResponse.java | 60 +++++++++++++++++ .../functions/ReturnImageNotFoundOn404.java | 25 +++++++ .../CloudServersConnectionLiveTest.java | 13 ++++ .../CloudServersConnectionTest.java | 26 +++++-- .../ParseImageFromGsonResponseTest.java | 67 +++++++++++++++++++ .../resources/test_get_image_details.json | 11 +++ 8 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponse.java create mode 100644 rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnImageNotFoundOn404.java create mode 100644 rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponseTest.java create mode 100644 rackspace/cloudservers/core/src/test/resources/test_get_image_details.json diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java index 78270819aa..3d6b0407c5 100755 --- a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/CloudServersConnection.java @@ -29,14 +29,18 @@ import java.util.concurrent.Future; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import org.jclouds.rackspace.cloudservers.domain.Flavor; import org.jclouds.rackspace.cloudservers.domain.Image; import org.jclouds.rackspace.cloudservers.domain.Server; import org.jclouds.rackspace.cloudservers.functions.ParseFlavorListFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseImageFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseImageListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerListFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ReturnImageNotFoundOn404; import org.jclouds.rackspace.filters.AuthenticateRequest; +import org.jclouds.rest.ExceptionParser; import org.jclouds.rest.Query; import org.jclouds.rest.RequestFilters; import org.jclouds.rest.ResponseParser; @@ -71,7 +75,7 @@ public interface CloudServersConnection { /** * This operation provides a list of servers associated with your account. Servers that have been - * deleted are not included in this list. + * deleted are not included in this list. */ @GET @ResponseParser(ParseServerListFromGsonResponse.class) @@ -108,8 +112,6 @@ public interface CloudServersConnection { // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest // (400) List listFlavorDetails(); - - /** * @@ -121,7 +123,8 @@ public interface CloudServersConnection { @ResponseParser(ParseImageListFromGsonResponse.class) @Query(key = "format", value = "json") @Path("/images") - // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest (400) + // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) List listImages(); /** @@ -134,7 +137,23 @@ public interface CloudServersConnection { @ResponseParser(ParseImageListFromGsonResponse.class) @Query(key = "format", value = "json") @Path("/images/detail") - // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest (400) + // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) List listImageDetails(); + /** + * + * This operation returns details of the specified image. + * + * @see Image + */ + @GET + @ResponseParser(ParseImageFromGsonResponse.class) + @Query(key = "format", value = "json") + @ExceptionParser(ReturnImageNotFoundOn404.class) + @Path("/images/{id}") + // TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest + // (400) + Image getImageDetails(@PathParam("id") int id); + } diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/Image.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/Image.java index 72d7a688b4..4120561c66 100644 --- a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/Image.java +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/domain/Image.java @@ -33,6 +33,8 @@ import org.joda.time.DateTime; */ public class Image { + public static final Image NOT_FOUND = new Image(-1,"NOT_FOUND"); + private DateTime created; private int id; private String name; diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponse.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponse.java new file mode 100644 index 0000000000..26a788e6a0 --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponse.java @@ -0,0 +1,60 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.rackspace.cloudservers.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rackspace.cloudservers.domain.Image; + +import com.google.gson.Gson; +import com.google.inject.Inject; + +/** + * This parses {@link Image} from a gson string. + * + * @author Adrian Cole + */ +public class ParseImageFromGsonResponse extends ParseJson { + + @Inject + public ParseImageFromGsonResponse(Gson gson) { + super(gson); + } + + private static class ImageListResponse { + Image image; + } + + public Image apply(InputStream stream) { + + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), ImageListResponse.class).image; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnImageNotFoundOn404.java b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnImageNotFoundOn404.java new file mode 100644 index 0000000000..ea4c7d10a3 --- /dev/null +++ b/rackspace/cloudservers/core/src/main/java/org/jclouds/rackspace/cloudservers/functions/ReturnImageNotFoundOn404.java @@ -0,0 +1,25 @@ +package org.jclouds.rackspace.cloudservers.functions; + +import org.jclouds.http.HttpResponseException; +import org.jclouds.rackspace.cloudservers.domain.Image; + +import com.google.common.base.Function; + +/** + * + * + * @author Adrian Cole + */ +public class ReturnImageNotFoundOn404 implements Function { + + public Image apply(Exception from) { + if (from instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) from; + if (responseException.getResponse().getStatusCode() == 404) { + return Image.NOT_FOUND; + } + } + return null; + } + +} diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java index af7df23729..a278122e74 100755 --- a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionLiveTest.java @@ -127,4 +127,17 @@ public class CloudServersConnectionLiveTest { assert null != image.getStatus() : image; } } + + @Test(enabled = false) + // Rackspace Web Hosting issue #118856 + public void testGetImageDetails() throws Exception { + List response = connection.listImageDetails(); + assert null != response; + long imageCount = response.size(); + assertTrue(imageCount >= 0); + for (Image image : response) { + Image newDetails = connection.getImageDetails(image.getId()); + assert image.equals(newDetails) : String.format("%s doesn't equal %2", newDetails, image); + } + } } diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java index 3f0c93d2d4..969023fae5 100755 --- a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java @@ -35,6 +35,7 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.rackspace.Authentication; import org.jclouds.rackspace.cloudservers.functions.ParseFlavorListFromGsonResponse; +import org.jclouds.rackspace.cloudservers.functions.ParseImageFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseImageListFromGsonResponse; import org.jclouds.rackspace.cloudservers.functions.ParseServerListFromGsonResponse; import org.jclouds.rest.JaxrsAnnotationProcessor; @@ -71,7 +72,7 @@ public class CloudServersConnectionTest { ParseServerListFromGsonResponse.class); } - + public void testListServersDetail() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listServerDetails"); URI endpoint = URI.create("http://localhost"); @@ -103,7 +104,7 @@ public class CloudServersConnectionTest { ParseFlavorListFromGsonResponse.class); } - + public void testListFlavorsDetail() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listFlavorDetails"); URI endpoint = URI.create("http://localhost"); @@ -120,7 +121,6 @@ public class CloudServersConnectionTest { } - public void testListImages() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listImages"); URI endpoint = URI.create("http://localhost"); @@ -136,7 +136,7 @@ public class CloudServersConnectionTest { ParseImageListFromGsonResponse.class); } - + public void testListImagesDetail() throws SecurityException, NoSuchMethodException { Method method = CloudServersConnection.class.getMethod("listImageDetails"); URI endpoint = URI.create("http://localhost"); @@ -152,7 +152,23 @@ public class CloudServersConnectionTest { ParseImageListFromGsonResponse.class); } - + + public void testGetImageDetails() throws SecurityException, NoSuchMethodException { + Method method = CloudServersConnection.class.getMethod("getImageDetails", int.class); + URI endpoint = URI.create("http://localhost"); + HttpRequest httpMethod = factory.create(CloudServersConnection.class).createRequest(endpoint, + method, new Object[] { 2 }); + assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); + assertEquals(httpMethod.getEndpoint().getPath(), "/images/2"); + assertEquals(httpMethod.getEndpoint().getQuery(), "format=json"); + assertEquals(httpMethod.getMethod(), HttpMethod.GET); + assertEquals(httpMethod.getHeaders().size(), 0); + factory.create(CloudServersConnection.class); + assertEquals(JaxrsAnnotationProcessor.getParserOrThrowException(method), + ParseImageFromGsonResponse.class); + + } + @BeforeClass void setupFactory() { factory = Guice.createInjector(new AbstractModule() { diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponseTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponseTest.java new file mode 100644 index 0000000000..2505b99015 --- /dev/null +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/functions/ParseImageFromGsonResponseTest.java @@ -0,0 +1,67 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.rackspace.cloudservers.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.UnknownHostException; + +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.rackspace.cloudservers.domain.Image; +import org.jclouds.rackspace.cloudservers.domain.ImageStatus; +import org.jclouds.util.DateService; +import org.joda.time.DateTime; +import org.testng.annotations.Test; + +import com.google.gson.Gson; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseImageFromGsonResponse} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "cloudservers.ParseImageFromGsonResponseTest") +public class ParseImageFromGsonResponseTest { + + Injector i = Guice.createInjector(new ParserModule()); + DateService dateService = new DateService(); + + public void testApplyInputStreamDetails() throws UnknownHostException { + InputStream is = getClass().getResourceAsStream("/test_get_image_details.json"); + + ParseImageFromGsonResponse parser = new ParseImageFromGsonResponse(i.getInstance(Gson.class)); + Image response = parser.apply(is); + assertEquals(response.getId(), 2); + assertEquals(response.getName(), "CentOS 5.2"); + assertEquals(response.getCreated(), new DateTime("2010-08-10T12:00:00Z")); + assertEquals(response.getProgress(), new Integer(80)); + assertEquals(response.getServerId(), new Integer(12)); + assertEquals(response.getStatus(), ImageStatus.SAVING); + assertEquals(response.getUpdated(), new DateTime("2010-10-10T12:00:00Z")); + } + +} diff --git a/rackspace/cloudservers/core/src/test/resources/test_get_image_details.json b/rackspace/cloudservers/core/src/test/resources/test_get_image_details.json new file mode 100644 index 0000000000..e73095bb77 --- /dev/null +++ b/rackspace/cloudservers/core/src/test/resources/test_get_image_details.json @@ -0,0 +1,11 @@ +{ + "image" : { + "id" : 2, + "name" : "CentOS 5.2", + "serverId" : 12, + "updated" : "2010-10-10T12:00:00Z", + "created" : "2010-08-10T12:00:00Z", + "status" : "SAVING", + "progress" : 80 + } +} \ No newline at end of file