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
This commit is contained in:
adrian.f.cole 2009-07-17 18:37:52 +00:00
parent ffac49c5b7
commit 14053f0341
8 changed files with 223 additions and 10 deletions

View File

@ -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;
@ -109,8 +113,6 @@ public interface CloudServersConnection {
// (400)
List<Flavor> listFlavorDetails();
/**
*
* List available images (IDs and names only)
@ -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<Image> 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<Image> 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);
}

View File

@ -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;

View File

@ -0,0 +1,60 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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<Image> {
@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);
}
}
}

View File

@ -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<Exception, Image> {
public Image apply(Exception from) {
if (from instanceof HttpResponseException) {
HttpResponseException responseException = (HttpResponseException) from;
if (responseException.getResponse().getStatusCode() == 404) {
return Image.NOT_FOUND;
}
}
return null;
}
}

View File

@ -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<Image> 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);
}
}
}

View File

@ -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;
@ -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");
@ -153,6 +153,22 @@ public class CloudServersConnectionTest {
}
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() {

View File

@ -0,0 +1,67 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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"));
}
}

View File

@ -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
}
}