mirror of https://github.com/apache/jclouds.git
Issue 77: added get flavor details
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1645 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
5a548f5a7e
commit
4842055443
|
@ -34,10 +34,12 @@ 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.ParseFlavorFromGsonResponse;
|
||||
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.ReturnFlavorNotFoundOn404;
|
||||
import org.jclouds.rackspace.cloudservers.functions.ReturnImageNotFoundOn404;
|
||||
import org.jclouds.rackspace.filters.AuthenticateRequest;
|
||||
import org.jclouds.rest.ExceptionParser;
|
||||
|
@ -156,4 +158,19 @@ public interface CloudServersConnection {
|
|||
// (400)
|
||||
Image getImageDetails(@PathParam("id") int id);
|
||||
|
||||
/**
|
||||
*
|
||||
* This operation returns details of the specified flavor.
|
||||
*
|
||||
* @see Flavor
|
||||
*/
|
||||
@GET
|
||||
@ResponseParser(ParseFlavorFromGsonResponse.class)
|
||||
@Query(key = "format", value = "json")
|
||||
@ExceptionParser(ReturnFlavorNotFoundOn404.class)
|
||||
@Path("/flavors/{id}")
|
||||
// TODO: cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest
|
||||
// (400)
|
||||
Flavor getFlavorDetails(@PathParam("id") int id);
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ package org.jclouds.rackspace.cloudservers.domain;
|
|||
*/
|
||||
public class Flavor {
|
||||
|
||||
public static final Flavor NOT_FOUND = new Flavor(-1, "NOT_FOUND");
|
||||
|
||||
public Flavor() {
|
||||
}
|
||||
|
||||
|
|
|
@ -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.Flavor;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* This parses {@link Flavor} from a gson string.
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class ParseFlavorFromGsonResponse extends ParseJson<Flavor> {
|
||||
|
||||
@Inject
|
||||
public ParseFlavorFromGsonResponse(Gson gson) {
|
||||
super(gson);
|
||||
}
|
||||
|
||||
private static class FlavorListResponse {
|
||||
Flavor flavor;
|
||||
}
|
||||
|
||||
public Flavor apply(InputStream stream) {
|
||||
|
||||
try {
|
||||
return gson.fromJson(new InputStreamReader(stream, "UTF-8"), FlavorListResponse.class).flavor;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.jclouds.rackspace.cloudservers.functions;
|
||||
|
||||
import org.jclouds.http.HttpResponseException;
|
||||
import org.jclouds.rackspace.cloudservers.domain.Flavor;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class ReturnFlavorNotFoundOn404 implements Function<Exception, Flavor> {
|
||||
|
||||
public Flavor apply(Exception from) {
|
||||
if (from instanceof HttpResponseException) {
|
||||
HttpResponseException responseException = (HttpResponseException) from;
|
||||
if (responseException.getResponse().getStatusCode() == 404) {
|
||||
return Flavor.NOT_FOUND;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -140,4 +140,17 @@ public class CloudServersConnectionLiveTest {
|
|||
assert image.equals(newDetails) : String.format("%s doesn't equal %2", newDetails, image);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFlavorDetails() throws Exception {
|
||||
List<Flavor> response = connection.listFlavorDetails();
|
||||
assert null != response;
|
||||
long flavorCount = response.size();
|
||||
assertTrue(flavorCount >= 0);
|
||||
for (Flavor flavor : response) {
|
||||
Flavor newDetails = connection.getFlavorDetails(flavor.getId());
|
||||
assert flavor.equals(newDetails) : String
|
||||
.format("%s doesn't equal %2", newDetails, flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.jclouds.http.HttpMethod;
|
|||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
|
||||
import org.jclouds.rackspace.Authentication;
|
||||
import org.jclouds.rackspace.cloudservers.functions.ParseFlavorFromGsonResponse;
|
||||
import org.jclouds.rackspace.cloudservers.functions.ParseFlavorListFromGsonResponse;
|
||||
import org.jclouds.rackspace.cloudservers.functions.ParseImageFromGsonResponse;
|
||||
import org.jclouds.rackspace.cloudservers.functions.ParseImageListFromGsonResponse;
|
||||
|
@ -169,6 +170,22 @@ public class CloudServersConnectionTest {
|
|||
|
||||
}
|
||||
|
||||
public void testGetFlavorDetails() throws SecurityException, NoSuchMethodException {
|
||||
Method method = CloudServersConnection.class.getMethod("getFlavorDetails", 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(), "/flavors/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),
|
||||
ParseFlavorFromGsonResponse.class);
|
||||
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
void setupFactory() {
|
||||
factory = Guice.createInjector(new AbstractModule() {
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
*
|
||||
* 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.Flavor;
|
||||
import org.jclouds.util.DateService;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* Tests behavior of {@code ParseFlavorFromGsonResponse}
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Test(groups = "unit", testName = "cloudservers.ParseFlavorFromGsonResponseTest")
|
||||
public class ParseFlavorFromGsonResponseTest {
|
||||
|
||||
Injector i = Guice.createInjector(new ParserModule());
|
||||
DateService dateService = new DateService();
|
||||
|
||||
public void testApplyInputStreamDetails() throws UnknownHostException {
|
||||
InputStream is = getClass().getResourceAsStream("/test_get_flavor_details.json");
|
||||
|
||||
ParseFlavorFromGsonResponse parser = new ParseFlavorFromGsonResponse(i
|
||||
.getInstance(Gson.class));
|
||||
Flavor response = parser.apply(is);
|
||||
assertEquals(response.getId(), 1);
|
||||
assertEquals(response.getName(), "256 MB Server");
|
||||
assertEquals(response.getRam(), new Integer(256));
|
||||
assertEquals(response.getDisk(), new Integer(10));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"flavor" : {
|
||||
"id" : 1,
|
||||
"name" : "256 MB Server",
|
||||
"ram" : 256,
|
||||
"disk" : 10
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue