Merge pull request #129 from mattstep/master

Fixes to nova client to support proofpoint/cloud-management project
This commit is contained in:
Adrian Cole 2011-11-11 00:52:51 -08:00
commit 8e00d9d7d3
9 changed files with 417 additions and 21 deletions

View File

@ -73,14 +73,35 @@ public interface NovaAsyncClient {
@Path("/servers/{id}")
ListenableFuture<Server> getServer(@PathParam("id") int id);
/**
* @see NovaClient#getServer
*/
@GET
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
@QueryParams(keys = "format", values = "json")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@Path("/servers/{uuid}")
ListenableFuture<Server> getServer(@PathParam("uuid") String uuid);
/**
* @see NovaClient#deleteServer
*/
@DELETE
@Consumes
@ExceptionParser(ReturnFalseOnNotFoundOr404.class)
@Path("/servers/{id}")
ListenableFuture<Boolean> deleteServer(@PathParam("id") int id);
/**
* @see NovaClient#deleteServer
*/
@DELETE
@Consumes
@ExceptionParser(ReturnFalseOnNotFoundOr404.class)
@Path("/servers/{uuid}")
ListenableFuture<Boolean> deleteServer(@PathParam("uuid") String uuid);
/**
* @see NovaClient#rebootServer
*/
@ -183,6 +204,17 @@ public interface NovaAsyncClient {
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Flavor> getFlavor(@PathParam("id") int id);
/**
* @see NovaClient#getFlavor
*/
@GET
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
@QueryParams(keys = "format", values = "json")
@Path("/flavors/{uuid}")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Flavor> getFlavor(@PathParam("uuid") String uuid);
/**
* @see NovaClient#listImages
*/
@ -205,6 +237,17 @@ public interface NovaAsyncClient {
@Path("/images/{id}")
ListenableFuture<Image> getImage(@PathParam("id") int id);
/**
* @see NovaClient#getImage
*/
@GET
@Unwrap
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
@QueryParams(keys = "format", values = "json")
@Path("/images/{uuid}")
ListenableFuture<Image> getImage(@PathParam("uuid") String uuid);
/**
* @see NovaClient#deleteImage
*/

View File

@ -70,7 +70,8 @@ public interface NovaClient {
* @return null, if the server is not found
* @see Server
*/
Server getServer(@PathParam("id") int id);
Server getServer(int id);
Server getServer(String uuid);
/**
*
@ -81,7 +82,8 @@ public interface NovaClient {
* @return false if the server is not found
* @see Server
*/
boolean deleteServer(@PathParam("id") int id);
boolean deleteServer(int id);
boolean deleteServer(String id);
/**
* The reboot function allows for either a soft or hard reboot of a server.
@ -204,6 +206,7 @@ public interface NovaClient {
* @see Flavor
*/
Flavor getFlavor(int id);
Flavor getFlavor(String uuid);
/**
*
@ -223,6 +226,7 @@ public interface NovaClient {
* @see Image
*/
Image getImage(int id);
Image getImage(String id);
/**
*

View File

@ -22,31 +22,81 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import sun.awt.image.ImageWatched;
import javax.annotation.Nullable;
/**
* @author Dmitri Babaev
* @author Matt Stephenson
*/
public class Resource {
private List<Map<String, String>> links = Lists.newArrayList();
public URI getURI() {
for (Map<String, String> linkProperties : links) {
try {
if (!Functions.forMap(linkProperties, "").apply("rel").equals("bookmark"))
continue;
if (!Functions.forMap(linkProperties, "").apply("type").contains("json"))
continue;
//This is the preference order for returning a URI in getURI
private enum LinkType {
BOOKMARK_JSON(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("bookmark") &&
Functions.forMap(linkMap, "").apply("type").contains("json");
}
}),
BOOKMARK_ANY(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("bookmark");
}
}),
SELF(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("self");
}
});
return new URI(linkProperties.get("href"));
Predicate<Map<String,String>> linkPredicate;
LinkType(Predicate<Map<String,String>> linkPredicate) {
this.linkPredicate = linkPredicate;
};
}
private final ConcurrentSkipListMap<LinkType,URI> orderedSelfReferences = new ConcurrentSkipListMap<LinkType,URI>();
private void populateOrderedSelfReferences() {
for (Map<String, String> linkProperties : links) {
for (LinkType type : LinkType.values()) {
if(type.linkPredicate.apply(linkProperties)) {
try {
orderedSelfReferences.put(type, new URI(linkProperties.get("href")));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
}
if(orderedSelfReferences.isEmpty())
throw new IllegalStateException("URI is not available");
}
public URI getURI() {
if(orderedSelfReferences.isEmpty())
populateOrderedSelfReferences();
return orderedSelfReferences.firstEntry().getValue();
}
public URI getSelfURI() {
if(orderedSelfReferences.isEmpty())
populateOrderedSelfReferences();
return orderedSelfReferences.get(LinkType.SELF);
}
}

View File

@ -41,6 +41,9 @@ public class Server extends Resource {
private String hostId;
private String imageRef;
private String affinityId;
private String uuid;
private Flavor flavor;
private Image image;
private Date created;
private Date updated;
@ -101,6 +104,10 @@ public class Server extends Resource {
this.flavorRef = flavorRef;
}
/**
* @deprecated in nova 1.1 api at the Diablo release, replaced by {@link #getFlavor()}
*/
@Deprecated
public String getFlavorRef() {
return flavorRef;
}
@ -129,6 +136,10 @@ public class Server extends Resource {
this.imageRef = imageRef;
}
/**
* @deprecated in nova 1.1 api at the Diablo release, replaced by {@link #getImage()}.
*/
@Deprecated
public String getImageRef() {
return imageRef;
}
@ -157,6 +168,30 @@ public class Server extends Resource {
return status;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public Flavor getFlavor() {
return flavor;
}
public void setFlavor(Flavor flavor) {
this.flavor = flavor;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
@Override
public int hashCode() {
final int prime = 31;
@ -168,7 +203,10 @@ public class Server extends Resource {
result = prime * result + id;
result = prime * result + ((imageRef == null) ? 0 : imageRef.hashCode());
result = prime * result + ((metadata == null) ? 0 : metadata.hashCode());
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((flavor == null) ? 0 : flavor.hashCode());
result = prime * result + ((image == null) ? 0 : image.hashCode());
return result;
}
@ -213,11 +251,26 @@ public class Server extends Resource {
return false;
} else if (!metadata.equals(other.metadata))
return false;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (flavor == null) {
if (other.flavor != null)
return false;
} else if (!flavor.equals(other.flavor))
return false;
if (image == null) {
if (other.image != null)
return false;
} else if (!image.equals(other.image))
return false;
return true;
}
@ -229,7 +282,7 @@ public class Server extends Resource {
public String toString() {
return "Server [addresses=" + addresses + ", adminPass=" + adminPass + ", flavorRef="
+ flavorRef + ", hostId=" + hostId + ", id=" + id + ", imageRef=" + imageRef
+ ", metadata=" + metadata + ", name=" + name + "]";
+ ", metadata=" + metadata + ", uuid=" + uuid + ", name=" + name + "]";
}
}

View File

@ -212,6 +212,21 @@ public class NovaAsyncClientTest extends RestClientTest<NovaAsyncClient> {
checkFilters(request);
}
public void testGetServerByUUID() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("getServer", String.class);
HttpRequest request = processor.createRequest(method, "dfdcd0a6-0a2f-11e1-8505-2837371c69ae");
assertRequestLineEquals(request, "GET http://endpoint/vapiversion/servers/dfdcd0a6-0a2f-11e1-8505-2837371c69ae?format=json HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/json\n");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
public void testListFlavors() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("listFlavors", listOptionsVarargsClass);
HttpRequest request = processor.createRequest(method);
@ -289,6 +304,21 @@ public class NovaAsyncClientTest extends RestClientTest<NovaAsyncClient> {
checkFilters(request);
}
public void testGetFlavorByUUID() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("getFlavor", String.class);
HttpRequest request = processor.createRequest(method, "209904b6-0a30-11e1-a0f0-2837371c69ae");
assertRequestLineEquals(request, "GET http://endpoint/vapiversion/flavors/209904b6-0a30-11e1-a0f0-2837371c69ae?format=json HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/json\n");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
public void testListImages() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("listImages", listOptionsVarargsClass);
HttpRequest request = processor.createRequest(method);
@ -366,12 +396,42 @@ public class NovaAsyncClientTest extends RestClientTest<NovaAsyncClient> {
checkFilters(request);
}
public void testGetImageByUUID() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("getImage", int.class);
HttpRequest request = processor.createRequest(method, "3bd52d90-0a30-11e1-83f5-2837371c69ae");
assertRequestLineEquals(request, "GET http://endpoint/vapiversion/images/3bd52d90-0a30-11e1-83f5-2837371c69ae?format=json HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/json\n");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, UnwrapOnlyJsonValue.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(request);
}
public void testDeleteServer() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("deleteServer", int.class);
HttpRequest request = processor.createRequest(method, 2);
assertRequestLineEquals(request, "DELETE http://endpoint/vapiversion/servers/2 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertNonPayloadHeadersEqual(request, "Accept: */*\n");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ReturnTrueIf2xx.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnFalseOnNotFoundOr404.class);
checkFilters(request);
}
public void testDeleteServerByUUID() throws IOException, SecurityException, NoSuchMethodException {
Method method = NovaAsyncClient.class.getMethod("deleteServer", String.class);
HttpRequest request = processor.createRequest(method, "db8a1ac6-0a35-11e1-a42f-2837371c69ae");
assertRequestLineEquals(request, "DELETE http://endpoint/vapiversion/servers/db8a1ac6-0a35-11e1-a42f-2837371c69ae HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: */*\n");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ReturnTrueIf2xx.class);

View File

@ -0,0 +1,108 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.openstack.nova.functions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.nova.domain.Address;
import org.jclouds.openstack.nova.domain.Addresses;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.testng.annotations.Test;
import java.io.InputStream;
import java.net.URI;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.SimpleTimeZone;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ParseServerFromJsonResponse} for the transitional nova api 1.1 in the Diablo release
*
* @author Matt Stephenson
*/
@Test(groups = "unit")
public class ParseServerFromJsonResponseDiabloTest {
@Test
public void testApplyInputStreamDetails() throws Exception {
Server response = parseServer();
assertEquals(response.getId(), 15);
assertEquals(response.getName(), "sample-server");
assertEquals(response.getImage().getURI(), new URI("http://servers.api.openstack.org/1234/images/1"));
assertEquals(response.getFlavor().getURI(), new URI("http://servers.api.openstack.org/1234/flavors/1"));
assertEquals(response.getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(response.getStatus(), ServerStatus.BUILD);
assertEquals(response.getProgress(), new Integer(60));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
assertEquals(response.getCreated(),
dateFormat.parse("2010-08-10T12:00:00Z"));
assertEquals(response.getUpdated(),
dateFormat.parse("2010-10-10T12:00:00Z"));
List<Address> publicAddresses = ImmutableList.copyOf(Iterables.transform(
ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"),
Address.newString2AddressFunction()));
List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(
ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"),
Address.newString2AddressFunction()));
Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses));
assertEquals(response.getAddresses(), addresses1);
assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.getAddresses(), addresses1);
}
public static Server parseServer() throws NoSuchMethodException, ClassNotFoundException {
Injector i = Guice.createInjector(new GsonModule() {
@Override
protected void configure() {
super.configure();
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
});
InputStream is = ParseServerFromJsonResponseDiabloTest.class.getResourceAsStream("/test_get_server_detail_diablo.json");
UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() {
}));
return parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
}
}

View File

@ -87,6 +87,7 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(0).getImageRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.get(0).getFlavorRef(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.get(0).getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(response.get(0).getUuid(), "d84e2086-fc0d-11e0-8e08-2837371c69ae");
assertEquals(response.get(0).getStatus(), ServerStatus.BUILD);
assertEquals(response.get(0).getProgress(), new Integer(60));
@ -106,6 +107,7 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(1).getFlavorRef(), "1");
assertEquals(response.get(1).getAffinityId(), "b414fa41cb37b97dcb58d6c76112af1258e9eae2");
assertEquals(response.get(1).getHostId(), "9e107d9d372bb6826bd81d3542a419d6");
assertEquals(response.get(1).getUuid(), "e66d54d4-fc0d-11e0-b93f-2837371c69ae");
assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE);
assertEquals(response.get(1).getProgress(), null);

View File

@ -0,0 +1,80 @@
{
"server": {
"id": 15,
"uuid": "52415800-8b69-11e0-9b19-734f000004d2",
"tenant_id": "1234",
"user_id": "5678",
"name": "sample-server",
"updated": "2010-10-10T12:00:00Z",
"created": "2010-08-10T12:00:00Z",
"hostId": "e4d909c290d0fb1ca068ffaddf22cbd0",
"accessIPv4" : "67.23.10.132",
"accessIPv6" : "::babe:67.23.10.132",
"status": "BUILD",
"progress": 60,
"key_name": null,
"config_drive": "",
"image" : {
"id": "1",
"links": [
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/images/1"
}
]
},
"flavor" : {
"id": "1",
"links": [
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/flavors/1"
}
]
},
"addresses": {
"public" : [
{
"version": 4,
"addr": "67.23.10.132"
},
{
"version": 6,
"addr": "::babe:67.23.10.132"
},
{
"version": 4,
"addr": "67.23.10.131"
},
{
"version": 6,
"addr": "::babe:4317:0A83"
}
],
"private" : [
{
"version": 4,
"addr": "10.176.42.16"
},
{
"version": 6,
"addr": "::babe:10.176.42.16"
}
]
},
"metadata": {
"Server Label": "Web Head 1",
"Image Version": "2.1"
},
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/1234/servers/52415800-8b69-11e0-9b19-734f000004d2"
},
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/servers/52415800-8b69-11e0-9b19-734f000004d2"
}
]
}
}

View File

@ -9,6 +9,7 @@
"created" : "2010-08-10T12:00:00Z",
"hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",
"affinityId" : "fc88bcf8394db9c8d0564e08ca6a9724188a84d1",
"uuid" : "d84e2086-fc0d-11e0-8e08-2837371c69ae",
"status" : "BUILD",
"progress" : 60,
"addresses" : {
@ -53,6 +54,7 @@
"created" : "2010-08-10T12:00:00Z",
"hostId" : "9e107d9d372bb6826bd81d3542a419d6",
"affinityId" : "b414fa41cb37b97dcb58d6c76112af1258e9eae2",
"uuid" : "e66d54d4-fc0d-11e0-b93f-2837371c69ae",
"status" : "ACTIVE",
"addresses" : {
"public" : [
@ -74,12 +76,6 @@
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+xml",
"href" : "http://servers.api.openstack.org/1234/servers/5678"
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+json",
"href" : "http://servers.api.openstack.org/1234/servers/56789"
}
]