Add Flavor functionality to openstack-nova

This commit is contained in:
Jeremy Daggett 2012-02-13 12:35:06 -08:00
parent e88a32a75d
commit b52ab879c1
12 changed files with 652 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import java.util.Set;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Region;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.openstack.nova.v1_1.features.FlavorClient;
import org.jclouds.openstack.nova.v1_1.features.ServerAsyncClient;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;
@ -53,5 +54,12 @@ public interface NovaAsyncClient {
@Delegate
ServerAsyncClient getServerClientForRegion(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region);
/**
* Provides asynchronous access to Flavor features.
*/
@Delegate
FlavorClient getFlavorClientForRegion(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region);
}

View File

@ -25,6 +25,7 @@ import org.jclouds.concurrent.Timeout;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Region;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.openstack.nova.v1_1.features.FlavorClient;
import org.jclouds.openstack.nova.v1_1.features.ServerClient;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;
@ -56,4 +57,11 @@ public interface NovaClient {
ServerClient getServerClientForRegion(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region);
/**
* Provides synchronous access to Flavor features.
*/
@Delegate
FlavorClient getFlavorClientForRegion(
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region);
}

View File

@ -28,6 +28,8 @@ import org.jclouds.http.annotation.ServerError;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneAuthenticationModule;
import org.jclouds.openstack.nova.v1_1.NovaAsyncClient;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.features.FlavorAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.FlavorClient;
import org.jclouds.openstack.nova.v1_1.features.ServerAsyncClient;
import org.jclouds.openstack.nova.v1_1.features.ServerClient;
import org.jclouds.openstack.nova.v1_1.handlers.NovaErrorHandler;
@ -47,6 +49,7 @@ public class NovaRestClientModule extends RestClientModule<NovaClient, NovaAsync
public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()//
.put(ServerClient.class, ServerAsyncClient.class)//
.put(FlavorClient.class, FlavorAsyncClient.class)
.build();
public NovaRestClientModule() {

View File

@ -0,0 +1,139 @@
/**
* 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.v1_1.domain;
import static com.google.common.base.Objects.toStringHelper;
import java.util.Set;
import org.jclouds.openstack.domain.Link;
import org.jclouds.openstack.domain.Resource;
/**
* A flavor is an available hardware configuration for a server. Each flavor has
* a unique combination of disk space and memory capacity.
*
* @author Jeremy Daggett
* @see <a href=
* "http://docs.openstack.org/api/openstack-compute/1.1/content/Flavors-d1e4180.html"
* />
*/
public class Flavor extends Resource {
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return builder().fromFlavor(this);
}
public static class Builder extends Resource.Builder {
private int ram;
private int disk;
private int vcpus;
public Builder ram(int ram) {
this.ram = ram;
return this;
}
public Builder disk(int disk) {
this.disk = disk;
return this;
}
public Builder vcpus(int vcpus) {
this.vcpus = vcpus;
return this;
}
public Flavor build() {
return new Flavor(id, name, links, ram, disk, vcpus);
}
public Builder fromFlavor(Flavor in) {
return fromResource(in).ram(in.getRam()).disk(in.getDisk()).vcpus(in.getVcpus());
}
/**
* {@inheritDoc}
*/
@Override
public Builder id(String id) {
return Builder.class.cast(super.id(id));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource in) {
return Builder.class.cast(super.fromResource(in));
}
}
private int ram;
private int disk;
private int vcpus;
protected Flavor(String id, String name, Set<Link> links, int ram, int disk,
int vcpus) {
super(id, name, links);
this.ram = ram;
this.disk = disk;
this.vcpus = vcpus;
}
public int getRam() {
return this.ram;
}
public int getDisk() {
return this.disk;
}
public int getVcpus() {
return this.vcpus;
}
@Override
public String toString() {
return toStringHelper("").add("id", id).add("name", name)
.add("links", links).add("ram", ram).add("disk", disk)
.add("vcpus", vcpus).toString();
}
}

View File

@ -0,0 +1,85 @@
/**
* 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.v1_1.features;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import com.google.common.util.concurrent.ListenableFuture;
/**
* Provides asynchronous access to Flavors via their REST API.
* <p/>
*
* @see FlavorClient
* @see <a href="http://docs.openstack.org/api/openstack-compute/1.1/content/Flavors-d1e4180.html"
* />
* @author Jeremy Daggett
* TODO: Need a ListFlavorOptions class minDisk=minDiskInGB& minRam=minRamInMB& marker=markerID&limit=int
*/
@SkipEncoding({ '/', '=' })
@RequestFilters(AuthenticateRequest.class)
public interface FlavorAsyncClient {
/**
* @see FlavorClient#listFlavors
*/
@GET
@SelectJson("flavors")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/flavors")
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Resource>> listFlavors();
/**
* @see FlavorClient#listFlavorsInDetail
*/
@GET
@SelectJson("flavors")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/flavors/detail")
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Flavor>> listFlavorsInDetail();
/**
* @see FlavorClient#getFlavor
*/
@GET
@SelectJson("flavor")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/flavors/{id}")
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Flavor> getFlavor(@PathParam("id") String id);
}

View File

@ -0,0 +1,66 @@
/**
* 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.v1_1.features;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SkipEncoding;
/**
* Provides asynchronous access to Flavors via their REST API.
* <p/>
*
* @see FlavorClient
* @see <a href="http://docs.openstack.org/api/openstack-compute/1.1/content/Flavors-d1e4180.html"
* />
* @author Jeremy Daggett
*/
@Timeout(duration = 30, timeUnit = TimeUnit.SECONDS)
public interface FlavorClient {
/**
* List all flavors (IDs, names, links)
*
* @return all flavors (IDs, names, links)
*/
Set<Resource> listFlavors();
/**
* List all flavors (all details)
*
* @return all flavors (all details)
*/
Set<Flavor> listFlavorsInDetail();
/**
* List details of the specified flavor
*
* @param id
* id of the flavor
* @return flavorr or null if not found
*/
Flavor getFlavor(String id);
}

View File

@ -0,0 +1,76 @@
package org.jclouds.openstack.nova.v1_1.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaRestClientExpectTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseFlavorListTest;
import org.jclouds.openstack.nova.v1_1.parse.ParseFlavorTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
* Tests annotation parsing of {@code FlavorAsyncClient}
*
* @author Jeremy Daggett
*/
@Test(groups = "unit", testName = "FlavorAsyncClientTest")
public class FlavorClientExpectTest extends BaseNovaRestClientExpectTest {
public void testListFlavorsWhenResponseIs2xx() throws Exception {
HttpRequest listServers = HttpRequest.builder().method("GET").endpoint(
URI.create("https://compute.north.host/v1.1/3456/flavors")).headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
authToken).build()).build();
HttpResponse listFlavorsResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/flavor_list.json")).build();
NovaClient clientWhenFlavorsExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
responseWithKeystoneAccess, listServers, listFlavorsResponse);
assertEquals(clientWhenFlavorsExist.getConfiguredRegions(), ImmutableSet.of("North"));
assertEquals(clientWhenFlavorsExist.getFlavorClientForRegion("North").listFlavors().toString(),
new ParseFlavorListTest().expected().toString());
}
public void testListFlavorsWhenReponseIs404IsEmpty() throws Exception {
HttpRequest listFlavors = HttpRequest.builder().method("GET").endpoint(
URI.create("https://compute.north.host/v1.1/3456/flavors")).headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
authToken).build()).build();
HttpResponse listFlavorsResponse = HttpResponse.builder().statusCode(404).build();
NovaClient clientWhenNoServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
responseWithKeystoneAccess, listFlavors, listFlavorsResponse);
assertTrue(clientWhenNoServersExist.getFlavorClientForRegion("North").listFlavors().isEmpty());
}
// TODO: gson deserializer for Multimap
public void testGetFlavorWhenResponseIs2xx() throws Exception {
HttpRequest getFlavor = HttpRequest.builder().method("GET").endpoint(
URI.create("https://compute.north.host/v1.1/3456/flavors/foo")).headers(
ImmutableMultimap.<String, String> builder().put("Accept", "application/json").put("X-Auth-Token",
authToken).build()).build();
HttpResponse getFlavorResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/flavor_details.json")).build();
NovaClient clientWhenServersExist = requestsSendResponses(keystoneAuthWithAccessKeyAndSecretKey,
responseWithKeystoneAccess, getFlavor, getFlavorResponse);
assertEquals(clientWhenServersExist.getFlavorClientForRegion("North").getFlavor("foo").toString(),
new ParseFlavorTest().expected().toString());
}
}

View File

@ -0,0 +1,56 @@
/**
* 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.v1_1.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code FlavorClient}
*
* @author Jeremy Daggett
*/
@Test(groups = "live", testName = "FlavorClientLiveTest")
public class FlavorClientLiveTest extends BaseNovaClientLiveTest {
@Test
public void testListFlavors() throws Exception {
for (String regionId : context.getApi().getConfiguredRegions()) {
FlavorClient client = context.getApi().getFlavorClientForRegion(regionId);
Set<Resource> response = client.listFlavors();
assert null != response;
assertTrue(response.size() >= 0);
for (Resource flavor : response) {
Flavor newDetails = client.getFlavor(flavor.getId());
assertEquals(newDetails.getId(), flavor.getId());
assertEquals(newDetails.getName(), flavor.getName());
assertEquals(newDetails.getLinks(), flavor.getLinks());
}
}
}
}

View File

@ -0,0 +1,87 @@
/**
* 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.v1_1.parse;
import java.net.URI;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.domain.Link;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.domain.Link.Relation;
import org.jclouds.openstack.nova.v1_1.config.NovaParserModule;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
*
* @author Jeremy Daggett
*/
@Test(groups = "unit", testName = "ParseFlavorListTest")
public class ParseFlavorListTest extends BaseSetParserTest<Resource> {
@Override
public String resource() {
return "/flavor_list.json";
}
@Override
@SelectJson("flavors")
@Consumes(MediaType.APPLICATION_JSON)
public Set<Resource> expected() {
return ImmutableSet
.of(Resource
.builder()
.id("52415800-8b69-11e0-9b19-734f1195ff37")
.name("256 MB Server")
.links(
Link.create(
Relation.SELF,
URI.create("http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37")),
Link.create(
Relation.BOOKMARK,
URI.create("http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37")))
.build(),
Resource
.builder()
.id("52415800-8b69-11e0-9b19-734f216543fd")
.name("512 MB Server")
.links(
Link.create(
Relation.SELF,
URI.create("http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd")),
Link.create(
Relation.BOOKMARK,
URI.create("http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd")))
.build());
}
protected Injector injector() {
return Guice.createInjector(new NovaParserModule(), new GsonModule());
}
}

View File

@ -0,0 +1,73 @@
/**
* 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.v1_1.parse;
import java.net.URI;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import org.jclouds.json.BaseItemParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.domain.Link;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.domain.Link.Relation;
import org.jclouds.openstack.nova.v1_1.config.NovaParserModule;
import org.jclouds.openstack.nova.v1_1.domain.Flavor;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Jeremy Daggett
*/
@Test(groups = "unit", testName = "ParseFlavorTest")
public class ParseFlavorTest extends BaseItemParserTest<Flavor> {
@Override
public String resource() {
return "/flavor_details.json";
}
@Override
@SelectJson("flavor")
@Consumes(MediaType.APPLICATION_JSON)
public Flavor expected() {
return Flavor
.builder()
.id("52415800-8b69-11e0-9b19-734f1195ff37")
.name("256 MB Server")
.links(
ImmutableSet.of(
Link.create(
Relation.SELF,
URI.create("http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37")),
Link.create(
Relation.BOOKMARK,
URI.create("http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37"))))
.ram(256).disk(10).vcpus(1).build();
}
protected Injector injector() {
return Guice.createInjector(new NovaParserModule(), new GsonModule());
}
}

View File

@ -0,0 +1,19 @@
{
"flavor" : {
"id" : "52415800-8b69-11e0-9b19-734f1195ff37",
"name" : "256 MB Server",
"ram" : 256,
"disk" : 10,
"vcpus" : 1,
"links": [
{
"rel" : "self",
"href" : "http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37"
},
{
"rel" : "bookmark",
"href" : "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37"
}
]
}
}

View File

@ -0,0 +1,32 @@
{
"flavors": [
{
"id": "52415800-8b69-11e0-9b19-734f1195ff37",
"name": "256 MB Server",
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37"
},
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f1195ff37"
}
]
},
{
"id": "52415800-8b69-11e0-9b19-734f216543fd",
"name": "512 MB Server",
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd"
},
{
"rel": "bookmark",
"href": "http://servers.api.openstack.org/1234/flavors/52415800-8b69-11e0-9b19-734f216543fd"
}
]
}
]
}