From 605d6580de92a603f42c5e3d18081aa2ad2f61ae Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 16 Sep 2012 17:07:42 -0700 Subject: [PATCH] metadata for nova --- .../binders/BindMetadataToJsonPayload.java | 49 +++++++++++ .../openstack/nova/v2_0/domain/Quota.java | 6 +- .../nova/v2_0/features/ImageApi.java | 14 ++-- .../nova/v2_0/features/ImageAsyncApi.java | 27 +++--- .../nova/v2_0/features/ServerApi.java | 14 ++-- .../nova/v2_0/features/ServerAsyncApi.java | 56 +++++++------ .../internal/OnlyMetadataValueOrNull.java | 65 +++++++++++++++ .../v2_0/extensions/QuotaApiLiveTest.java | 2 +- .../v2_0/features/ImageApiExpectTest.java | 82 +++++++------------ .../v2_0/features/ServerApiExpectTest.java | 47 ++++++----- 10 files changed, 230 insertions(+), 132 deletions(-) create mode 100644 apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindMetadataToJsonPayload.java create mode 100644 apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/functions/internal/OnlyMetadataValueOrNull.java diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindMetadataToJsonPayload.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindMetadataToJsonPayload.java new file mode 100644 index 0000000000..7030261e04 --- /dev/null +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/binders/BindMetadataToJsonPayload.java @@ -0,0 +1,49 @@ +/** + * 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.v2_0.binders; + +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.HttpRequest; +import org.jclouds.json.Json; +import org.jclouds.rest.binders.BindToJsonPayload; + +import com.google.common.collect.ImmutableMap; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class BindMetadataToJsonPayload extends BindToJsonPayload { + + @Inject + public BindMetadataToJsonPayload(Json jsonBinder) { + super(jsonBinder); + } + + @Override + public R bindToRequest(R request, Map postParams) { + return bindToRequest(request, + (Object) ImmutableMap.of("metadata", ImmutableMap.of(postParams.get("key"), postParams.get("value")))); + } +} \ No newline at end of file diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Quota.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Quota.java index 6915511377..f52c13708e 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Quota.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Quota.java @@ -68,7 +68,7 @@ public class Quota { } /** - * @see Quota#getMetadataItems() + * @see Quota#getMetadatas() */ public T metadataItems(int metadataItems) { this.metadataItems = metadataItems; @@ -170,7 +170,7 @@ public class Quota { public T fromQuotas(Quota in) { return this .id(in.getId()) - .metadataItems(in.getMetadataItems()) + .metadataItems(in.getMetadatas()) .injectedFileContentBytes(in.getInjectedFileContentBytes()) .volumes(in.getVolumes()) .gigabytes(in.getGigabytes()) @@ -242,7 +242,7 @@ public class Quota { /** * The limit of the number of metadata items for the tenant */ - public int getMetadataItems() { + public int getMetadatas() { return this.metadataItems; } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageApi.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageApi.java index 24dee30e05..5afb755fee 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageApi.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageApi.java @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; import org.jclouds.collect.PagedIterable; import org.jclouds.concurrent.Timeout; +import org.jclouds.javax.annotation.Nullable; import org.jclouds.openstack.keystone.v2_0.domain.PaginatedCollection; import org.jclouds.openstack.nova.v2_0.domain.Image; import org.jclouds.openstack.v2_0.domain.Resource; @@ -84,7 +85,7 @@ public interface ImageApi { * id of the image * @return the metadata as a Map */ - Map listMetadata(String id); + Map getMetadata(String id); /** * Sets the metadata for an image. @@ -115,9 +116,10 @@ public interface ImageApi { * id of the image * @param metadata * a Map containing the metadata - * @return the metadata as a Map + * @return the value or null if not present */ - Map getMetadataItem(String id, String key); + @Nullable + String getMetadata(String id, String key); /** @@ -129,9 +131,9 @@ public interface ImageApi { * the name of the metadata item * @param value * the value of the metadata item - * @return the metadata as a Map + * @return the value you updated */ - Map setMetadataItem(String id, String key, String value); + String updateMetadata(String id, String key, String value); /** * Delete a metadata item from an image. @@ -141,6 +143,6 @@ public interface ImageApi { * @param key * the name of the metadata item */ - void deleteMetadataItem(String id, String key); + void deleteMetadata(String id, String key); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageAsyncApi.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageAsyncApi.java index 79dfef9f8a..0d69ab3402 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageAsyncApi.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ImageAsyncApi.java @@ -34,14 +34,15 @@ import org.jclouds.collect.PagedIterable; import org.jclouds.openstack.keystone.v2_0.domain.PaginatedCollection; import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; import org.jclouds.openstack.keystone.v2_0.functions.ReturnEmptyPaginatedCollectionOnNotFoundOr404; +import org.jclouds.openstack.nova.v2_0.binders.BindMetadataToJsonPayload; import org.jclouds.openstack.nova.v2_0.domain.Image; +import org.jclouds.openstack.nova.v2_0.functions.internal.OnlyMetadataValueOrNull; import org.jclouds.openstack.nova.v2_0.functions.internal.ParseImageDetails; import org.jclouds.openstack.nova.v2_0.functions.internal.ParseImages; import org.jclouds.openstack.v2_0.domain.Resource; import org.jclouds.openstack.v2_0.options.PaginationOptions; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.MapBinder; -import org.jclouds.rest.annotations.Payload; import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; @@ -129,14 +130,14 @@ public interface ImageAsyncApi { ListenableFuture delete(@PathParam("id") String id); /** - * @see ImageApi#listMetadata + * @see ImageApi#getMetadata */ @GET @SelectJson("metadata") @Path("/images/{id}/metadata") @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) - ListenableFuture> listMetadata(@PathParam("id") String id); + ListenableFuture> getMetadata(@PathParam("id") String id); /** * @see ImageApi#setMetadata @@ -163,34 +164,34 @@ public interface ImageAsyncApi { ListenableFuture> updateMetadata(@PathParam("id") String id, @PayloadParam("metadata") Map metadata); /** - * @see ImageApi#getMetadataItem + * @see ImageApi#getMetadata */ @GET - @SelectJson("metadata") @Path("/images/{id}/metadata/{key}") @Consumes(MediaType.APPLICATION_JSON) + @ResponseParser(OnlyMetadataValueOrNull.class) @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture> getMetadataItem(@PathParam("id") String id, @PathParam("key") String key); + ListenableFuture getMetadata(@PathParam("id") String id, @PathParam("key") String key); /** - * @see ImageApi#setMetadataItem + * @see ImageApi#updateMetadata */ @PUT - @SelectJson("metadata") @Path("/images/{id}/metadata/{key}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) - @Payload("%7B\"metadata\":%7B\"{key}\":\"{value}\"%7D%7D") - ListenableFuture> setMetadataItem(@PathParam("id") String id, @PathParam("key") String key, @PathParam("value") String value); + @ResponseParser(OnlyMetadataValueOrNull.class) + @MapBinder(BindMetadataToJsonPayload.class) + ListenableFuture updateMetadata(@PathParam("id") String id, + @PathParam("key") @PayloadParam("key") String key, @PathParam("value") @PayloadParam("value") String value); /** - * @see ImageApi#deleteMetadataItem + * @see ImageApi#deleteMetadata */ @DELETE @Consumes @Path("/images/{id}/metadata/{key}") @ExceptionParser(ReturnVoidOnNotFoundOr404.class) - ListenableFuture deleteMetadataItem(@PathParam("id") String id, @PathParam("key") String key); + ListenableFuture deleteMetadata(@PathParam("id") String id, @PathParam("key") String key); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerApi.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerApi.java index f8454004a3..f7ea8b251d 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerApi.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerApi.java @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; import org.jclouds.collect.PagedIterable; import org.jclouds.concurrent.Timeout; +import org.jclouds.javax.annotation.Nullable; import org.jclouds.openstack.keystone.v2_0.domain.PaginatedCollection; import org.jclouds.openstack.nova.v2_0.domain.RebootType; import org.jclouds.openstack.nova.v2_0.domain.Server; @@ -201,7 +202,7 @@ public interface ServerApi { * * @return the metadata as a Map */ - Map listMetadata(String id); + Map getMetadata(String id); /** * Set the metadata for a server. @@ -232,9 +233,10 @@ public interface ServerApi { * id of the image * @param metadata * a Map containing the metadata - * @return the metadata as a Map + * @return the value or null if not present */ - Map getMetadataItem(String id, String key); + @Nullable + String getMetadata(String id, String key); /** * Set a metadata item for a server. @@ -245,9 +247,9 @@ public interface ServerApi { * the name of the metadata item * @param value * the value of the metadata item - * @return the metadata as a Map + * @return the value you updated */ - Map setMetadataItem(String id, String key, String value); + String updateMetadata(String id, String key, String value); /** * Delete a metadata item from a server. @@ -257,6 +259,6 @@ public interface ServerApi { * @param key * the name of the metadata item */ - void deleteMetadataItem(String id, String key); + void deleteMetadata(String id, String key); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerAsyncApi.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerAsyncApi.java index e58e879baf..ef3070728b 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerAsyncApi.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/features/ServerAsyncApi.java @@ -34,10 +34,12 @@ import org.jclouds.collect.PagedIterable; import org.jclouds.openstack.keystone.v2_0.domain.PaginatedCollection; import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; import org.jclouds.openstack.keystone.v2_0.functions.ReturnEmptyPaginatedCollectionOnNotFoundOr404; +import org.jclouds.openstack.nova.v2_0.binders.BindMetadataToJsonPayload; import org.jclouds.openstack.nova.v2_0.domain.RebootType; import org.jclouds.openstack.nova.v2_0.domain.Server; import org.jclouds.openstack.nova.v2_0.domain.ServerCreated; import org.jclouds.openstack.nova.v2_0.functions.ParseImageIdFromLocationHeader; +import org.jclouds.openstack.nova.v2_0.functions.internal.OnlyMetadataValueOrNull; import org.jclouds.openstack.nova.v2_0.functions.internal.ParseServerDetails; import org.jclouds.openstack.nova.v2_0.functions.internal.ParseServers; import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions; @@ -69,8 +71,7 @@ import com.google.common.util.concurrent.ListenableFuture; *

* * @see ServerApi - * @see * @author Adrian Cole */ @@ -120,7 +121,6 @@ public interface ServerAsyncApi { @ExceptionParser(ReturnEmptyPaginatedCollectionOnNotFoundOr404.class) ListenableFuture> listInDetail(PaginationOptions options); - /** * @see ServerApi#get */ @@ -159,7 +159,7 @@ public interface ServerAsyncApi { @Produces(MediaType.APPLICATION_JSON) @Payload("{\"os-stop\":null}") ListenableFuture stop(@PathParam("id") String id); - + /** * @see ServerApi#reboot */ @@ -209,7 +209,7 @@ public interface ServerAsyncApi { @Path("/servers") @MapBinder(CreateServerOptions.class) ListenableFuture create(@PayloadParam("name") String name, @PayloadParam("imageRef") String imageRef, - @PayloadParam("flavorRef") String flavorRef, CreateServerOptions... options); + @PayloadParam("flavorRef") String flavorRef, CreateServerOptions... options); /** * @see ServerApi#rebuild @@ -253,15 +253,15 @@ public interface ServerAsyncApi { ListenableFuture createImageFromServer(@PayloadParam("name") String name, @PathParam("id") String id); /** - * @see ServerApi#listMetadata + * @see ServerApi#getMetadata */ @GET @SelectJson("metadata") @Path("/servers/{id}/metadata") @Consumes(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) - ListenableFuture> listMetadata(@PathParam("id") String id); - + ListenableFuture> getMetadata(@PathParam("id") String id); + /** * @see ServerApi#setMetadata */ @@ -272,7 +272,8 @@ public interface ServerAsyncApi { @Produces(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) @MapBinder(BindToJsonPayload.class) - ListenableFuture> setMetadata(@PathParam("id") String id, @PayloadParam("metadata") Map metadata); + ListenableFuture> setMetadata(@PathParam("id") String id, + @PayloadParam("metadata") Map metadata); /** * @see ServerApi#updateMetadata @@ -284,37 +285,38 @@ public interface ServerAsyncApi { @Produces(MediaType.APPLICATION_JSON) @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) @MapBinder(BindToJsonPayload.class) - ListenableFuture> updateMetadata(@PathParam("id") String id, @PayloadParam("metadata") Map metadata); - - /** - * @see ServerApi#getMetadataItem - */ - @GET - @SelectJson("metadata") - @Path("/servers/{id}/metadata/{key}") - @Consumes(MediaType.APPLICATION_JSON) - @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture> getMetadataItem(@PathParam("id") String id, @PathParam("key") String key); + ListenableFuture> updateMetadata(@PathParam("id") String id, + @PayloadParam("metadata") Map metadata); /** - * @see ServerApi#setMetadataItem + * @see ServerApi#getMetadata + */ + @GET + @Path("/servers/{id}/metadata/{key}") + @Consumes(MediaType.APPLICATION_JSON) + @ResponseParser(OnlyMetadataValueOrNull.class) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getMetadata(@PathParam("id") String id, @PathParam("key") String key); + + /** + * @see ServerApi#updateMetadata */ @PUT - @SelectJson("metadata") @Path("/servers/{id}/metadata/{key}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) - @Payload("%7B\"metadata\":%7B\"{key}\":\"{value}\"%7D%7D") - ListenableFuture> setMetadataItem(@PathParam("id") String id, @PathParam("key") String key, @PathParam("value") String value); + @ResponseParser(OnlyMetadataValueOrNull.class) + @MapBinder(BindMetadataToJsonPayload.class) + ListenableFuture updateMetadata(@PathParam("id") String id, + @PathParam("key") @PayloadParam("key") String key, @PathParam("value") @PayloadParam("value") String value); /** - * @see ServerApi#deleteMetadataItem + * @see ServerApi#deleteMetadata */ @DELETE @Consumes @Path("/servers/{id}/metadata/{key}") @ExceptionParser(ReturnVoidOnNotFoundOr404.class) - ListenableFuture deleteMetadataItem(@PathParam("id") String id, @PathParam("key") String key); + ListenableFuture deleteMetadata(@PathParam("id") String id, @PathParam("key") String key); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/functions/internal/OnlyMetadataValueOrNull.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/functions/internal/OnlyMetadataValueOrNull.java new file mode 100644 index 0000000000..d00c2e7ea6 --- /dev/null +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/functions/internal/OnlyMetadataValueOrNull.java @@ -0,0 +1,65 @@ +/** + * 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.v2_0.functions.internal; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import javax.inject.Singleton; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseJson; +import org.jclouds.json.internal.GsonWrapper; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.Iterables; +import com.google.inject.Inject; +import com.google.inject.TypeLiteral; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class OnlyMetadataValueOrNull implements Function { + private final ParseJson parser; + + private static class Wrapper implements Supplier { + private Map metadata; + + @Override + public String get() { + return metadata == null ? null : Iterables.get(metadata.values(), 0, null); + } + + } + + @Inject + public OnlyMetadataValueOrNull(GsonWrapper gsonView) { + this.parser = new ParseJson(checkNotNull(gsonView, "gsonView"), new TypeLiteral() { + }); + } + + public String apply(HttpResponse response) { + checkNotNull(response, "response"); + return parser.apply(response).get(); + } +} diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/extensions/QuotaApiLiveTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/extensions/QuotaApiLiveTest.java index ec771e4a52..2cb2bb2412 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/extensions/QuotaApiLiveTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/extensions/QuotaApiLiveTest.java @@ -71,7 +71,7 @@ public class QuotaApiLiveTest extends BaseNovaApiLiveTest { Quota modified = before.toBuilder() .cores(before.getCores() - 1) .instances(before.getInstances() - 1) - .metadataItems(before.getMetadataItems() - 1) + .metadataItems(before.getMetadatas() - 1) .ram(before.getRam() - 1) .volumes(before.getVolumes() - 1) .build(); diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ImageApiExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ImageApiExpectTest.java index aea70f0496..81b8403713 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ImageApiExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ImageApiExpectTest.java @@ -119,26 +119,26 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { public void testListMetadataWhenResponseIs2xx() throws Exception { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; - HttpRequest listMetadata = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken).build(); - HttpResponse listMetadataResponse = HttpResponse.builder().statusCode(200) + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/metadata_list.json")).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, listMetadata, listMetadataResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); - assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").listMetadata(imageId).toString(), + assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").getMetadata(imageId).toString(), new ParseMetadataListTest().expected().toString()); } public void testListMetadataWhenResponseIs404() throws Exception { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; - HttpRequest listMetadata = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata") @@ -146,13 +146,13 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse listMetadataResponse = HttpResponse.builder().statusCode(404).build(); + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(404).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, listMetadata, listMetadataResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); try { - apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").listMetadata(imageId); + apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").getMetadata(imageId); fail("Expected an exception."); } catch (Exception e) { ; @@ -224,7 +224,7 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { HttpRequest setMetadata = HttpRequest .builder() .method("POST") - .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + imageId + "/metadata") + .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken) .payload(payloadFromStringWithContentType("{\"metadata\":{\"Server Label\":\"Web Head 2\",\"Server Description\":\"Simple Server\"}}","application/json")) @@ -274,7 +274,7 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; String key = "Image%20Version"; - HttpRequest getMetadataItem = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + key) @@ -282,15 +282,14 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse getMetadataItemResponse = HttpResponse.builder().statusCode(200) + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromString("{\"metadata\":{\"Image Version\":\"2.5\"}}")).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, getMetadataItem, getMetadataItemResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); - System.out.println(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").getMetadataItem(imageId, "Image Version").toString()); - assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").getMetadataItem(imageId, "Image Version").toString(), - "{Image Version=2.5}"); + assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").getMetadata(imageId, "Image Version").toString(), + "2.5"); } public void testGetMetadataItemWhenResponseIs404() throws Exception { @@ -311,58 +310,37 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { NovaApi apiWhenImageExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, responseWithKeystoneAccess, getMetadata, getMetadataResponse); - assertNull(apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").getMetadataItem(imageId, key)); + assertNull(apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").getMetadata(imageId, key)); } public void testSetMetadataItemWhenResponseIs2xx() throws Exception { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; - String key = "Image%20Version"; + String key = "Image Version"; - HttpRequest setMetadataItem = HttpRequest + HttpRequest updateMetadata = HttpRequest .builder() .method("PUT") - .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + key) + .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + "Image%20Version") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken) .payload(payloadFromStringWithContentType("{\"metadata\":{\"Image Version\":\"2.5\"}}", "application/json")) .build(); - HttpResponse setMetadataItemResponse = HttpResponse.builder().statusCode(200) + HttpResponse updateMetadataResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromStringWithContentType("{\"metadata\":{\"Image Version\":\"2.5\"}}", "application/json")).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, setMetadataItem, setMetadataItemResponse); + responseWithKeystoneAccess, updateMetadata, updateMetadataResponse); - assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").setMetadataItem(imageId, key, "2.5").toString(), - "Image Version=2.5"); - } - - public void testSetMetadataItemWhenResponseIs404() throws Exception { - String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; - String key = "Image%20Version"; - - HttpRequest setMetadataItem = HttpRequest - .builder() - .method("PUT") - .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + key) - .addHeader("Accept", "application/json") - .addHeader("X-Auth-Token", authToken) - .build(); - - HttpResponse setMetadataItemResponse = HttpResponse.builder().statusCode(404).build(); - - NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, setMetadataItem, setMetadataItemResponse); - - assertNull(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").setMetadataItem(imageId, key, "2.5")); - + assertEquals(apiWhenServerExists.getImageApiForZone("az-1.region-a.geo-1").updateMetadata(imageId, key, "2.5").toString(), + "2.5"); } public void testDeleteMetadataItemWhenResponseIs2xx() throws Exception { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; String key = "Image%20Version"; - HttpRequest deleteMetadataItem = HttpRequest + HttpRequest deleteMetadata = HttpRequest .builder() .method("DELETE") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + key) @@ -370,19 +348,19 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse deleteMetadataItemResponse = HttpResponse.builder().statusCode(204).build(); + HttpResponse deleteMetadataResponse = HttpResponse.builder().statusCode(204).build(); NovaApi apiWhenImageExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, deleteMetadataItem, deleteMetadataItemResponse); + responseWithKeystoneAccess, deleteMetadata, deleteMetadataResponse); - apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").deleteMetadataItem(imageId, key); + apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").deleteMetadata(imageId, key); } public void testDeleteMetadataItemWhenResponseIs404() throws Exception { String imageId = "52415800-8b69-11e0-9b19-734f5736d2a2"; String key = "Image%20Version"; - HttpRequest deleteMetadataItem = HttpRequest + HttpRequest deleteMetadata = HttpRequest .builder() .method("DELETE") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/images/" + imageId + "/metadata/" + key) @@ -390,12 +368,12 @@ public class ImageApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse deleteMetadataItemResponse = HttpResponse.builder().statusCode(404).build(); + HttpResponse deleteMetadataResponse = HttpResponse.builder().statusCode(404).build(); NovaApi apiWhenImageExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, deleteMetadataItem, deleteMetadataItemResponse); + responseWithKeystoneAccess, deleteMetadata, deleteMetadataResponse); - apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").deleteMetadataItem(imageId, key); + apiWhenImageExists.getImageApiForZone("az-1.region-a.geo-1").deleteMetadata(imageId, key); } } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java index 0292c75e4d..dde200258b 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/features/ServerApiExpectTest.java @@ -28,7 +28,6 @@ import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.openstack.nova.v2_0.internal.BaseNovaApiExpectTest; import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions; import org.jclouds.openstack.nova.v2_0.parse.ParseCreatedServerTest; -import org.jclouds.openstack.nova.v2_0.parse.ParseMetadataItemTest; import org.jclouds.openstack.nova.v2_0.parse.ParseMetadataListTest; import org.jclouds.openstack.nova.v2_0.parse.ParseMetadataUpdateTest; import org.jclouds.openstack.nova.v2_0.parse.ParseServerListTest; @@ -278,27 +277,27 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { public void testListMetadataWhenResponseIs2xx() throws Exception { String serverId = "123"; - HttpRequest listMetadata = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken).build(); - HttpResponse listMetadataResponse = HttpResponse.builder().statusCode(200) + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/metadata_list.json")).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, listMetadata, listMetadataResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); - assertEquals(apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").listMetadata(serverId).toString(), + assertEquals(apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").getMetadata(serverId).toString(), new ParseMetadataListTest().expected().toString()); } public void testListMetadataWhenResponseIs404() throws Exception { String serverId = "123"; - HttpRequest listMetadata = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata") @@ -306,13 +305,13 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse listMetadataResponse = HttpResponse.builder().statusCode(404).build(); + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(404).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, listMetadata, listMetadataResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); try { - apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").listMetadata(serverId); + apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").getMetadata(serverId); fail("Expected an exception."); } catch (Exception e) { ; @@ -432,24 +431,24 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { public void testGetMetadataItemWhenResponseIs2xx() throws Exception { String serverId = "123"; - String key = "Server%20Label"; + String key = "Server Label"; - HttpRequest getMetadataItem = HttpRequest + HttpRequest getMetadata = HttpRequest .builder() .method("GET") - .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata/" + key) + .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata/" + "Server%20Label") .addHeader("Accept", "application/json") .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse getMetadataItemResponse = HttpResponse.builder().statusCode(200) + HttpResponse getMetadataResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResource("/metadata_item.json")).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, getMetadataItem, getMetadataItemResponse); + responseWithKeystoneAccess, getMetadata, getMetadataResponse); - assertEquals(apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").getMetadataItem(serverId, "Server Label").toString(), - new ParseMetadataItemTest().expected().toString()); + assertEquals(apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").getMetadata(serverId, key).toString(), + "Web Head 1"); } public void testGetMetadataItemWhenResponseIs404() throws Exception { @@ -541,7 +540,7 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { String serverId = "123"; String key = "Server%20Label"; - HttpRequest setMetadataItem = HttpRequest + HttpRequest updateMetadata = HttpRequest .builder() .method("DELETE") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata/" + key) @@ -549,12 +548,12 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse setMetadataItemResponse = HttpResponse.builder().statusCode(204).build(); + HttpResponse updateMetadataResponse = HttpResponse.builder().statusCode(204).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, setMetadataItem, setMetadataItemResponse); + responseWithKeystoneAccess, updateMetadata, updateMetadataResponse); - apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").deleteMetadataItem(serverId, key); + apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").deleteMetadata(serverId, key); } @@ -562,7 +561,7 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { String serverId = "123"; String key = "Server%20Label"; - HttpRequest deleteMetadataItem = HttpRequest + HttpRequest deleteMetadata = HttpRequest .builder() .method("DELETE") .endpoint("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456/servers/" + serverId + "/metadata/" + key) @@ -570,12 +569,12 @@ public class ServerApiExpectTest extends BaseNovaApiExpectTest { .addHeader("X-Auth-Token", authToken) .build(); - HttpResponse deleteMetadataItemResponse = HttpResponse.builder().statusCode(404).build(); + HttpResponse deleteMetadataResponse = HttpResponse.builder().statusCode(404).build(); NovaApi apiWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPasswordAndTenantName, - responseWithKeystoneAccess, deleteMetadataItem, deleteMetadataItemResponse); + responseWithKeystoneAccess, deleteMetadata, deleteMetadataResponse); - apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").deleteMetadataItem(serverId, key); + apiWhenServerExists.getServerApiForZone("az-1.region-a.geo-1").deleteMetadata(serverId, key); } }