From f4e030d10f046219e54b12591f1edce90b01f565 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 7 Jun 2010 17:32:47 -0700 Subject: [PATCH] Issue 230: completed ibmdev api from a coding and unit test perspective --- .../ibmdev/IBMDeveloperCloudAsyncClient.java | 218 ++++++++- .../ibmdev/IBMDeveloperCloudClient.java | 294 ++++++++++- .../BindExpirationTimeToJsonPayload.java | 48 ++ ...onPayload.java => SaveInstanceBinder.java} | 25 +- .../org/jclouds/ibmdev/domain/Address.java | 135 +++++ .../java/org/jclouds/ibmdev/domain/Key.java | 148 ++++++ .../org/jclouds/ibmdev/domain/Location.java | 120 +++++ .../org/jclouds/ibmdev/domain/Volume.java | 219 +++++++++ .../functions/ParseAddressFromJson.java | 74 +++ .../functions/ParseAddressesFromJson.java | 80 +++ .../ParseExpirationTimeFromJson.java | 55 +++ .../ibmdev/functions/ParseKeyFromJson.java | 74 +++ .../ibmdev/functions/ParseKeysFromJson.java | 80 +++ .../ibmdev/functions/ParseVolumeFromJson.java | 58 +++ .../functions/ParseVolumesFromJson.java | 65 +++ .../IBMDeveloperCloudErrorHandler.java | 3 + .../ibmdev/options/CreateInstanceOptions.java | 125 +++++ .../options/RestartInstanceOptions.java | 75 +++ .../reference/IBMDeveloperCloudConstants.java | 4 + .../jclouds/ibmdev/xml/LocationHandler.java | 99 ++++ .../jclouds/ibmdev/xml/LocationsHandler.java | 69 +++ .../IBMDeveloperCloudAsyncClientTest.java | 462 ++++++++++++++++++ .../IBMDeveloperCloudClientLiveTest.java | 66 +++ .../functions/ParseAddressFromJsonTest.java | 64 +++ .../functions/ParseAddressesFromJsonTest.java | 64 +++ .../ParseExpirationTimeFromJsonTest.java | 63 +++ .../functions/ParseKeyFromJsonTest.java | 67 +++ .../functions/ParseKeysFromJsonTest.java | 69 +++ .../functions/ParseVolumeFromJsonTest.java | 68 +++ .../functions/ParseVolumesFromJsonTest.java | 70 +++ .../ibmdev/xml/LocationHandlerTest.java | 60 +++ .../ibmdev/xml/LocationsHandlerTest.java | 102 ++++ ibmdev/src/test/resources/address.json | 7 + ibmdev/src/test/resources/addresses.json | 16 + ibmdev/src/test/resources/key.json | 7 + ibmdev/src/test/resources/keys.json | 18 + ibmdev/src/test/resources/location.xml | 39 ++ ibmdev/src/test/resources/volume.json | 12 + ibmdev/src/test/resources/volumes.json | 26 + 39 files changed, 3331 insertions(+), 17 deletions(-) create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindExpirationTimeToJsonPayload.java rename ibmdev/src/main/java/org/jclouds/ibmdev/binders/{BindImageVisibilityToJsonPayload.java => SaveInstanceBinder.java} (52%) create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/domain/Address.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/domain/Key.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/domain/Location.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/domain/Volume.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressesFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeyFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeysFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumeFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumesFromJson.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/options/CreateInstanceOptions.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/options/RestartInstanceOptions.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationHandler.java create mode 100644 ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationsHandler.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeyFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeysFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumeFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumesFromJsonTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationHandlerTest.java create mode 100644 ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationsHandlerTest.java create mode 100644 ibmdev/src/test/resources/address.json create mode 100644 ibmdev/src/test/resources/addresses.json create mode 100644 ibmdev/src/test/resources/key.json create mode 100644 ibmdev/src/test/resources/keys.json create mode 100644 ibmdev/src/test/resources/location.xml create mode 100644 ibmdev/src/test/resources/volume.json create mode 100644 ibmdev/src/test/resources/volumes.json diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClient.java b/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClient.java index 2b0470ab76..432a8cb097 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClient.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClient.java @@ -23,29 +23,51 @@ */ package org.jclouds.ibmdev; +import java.util.Date; import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import org.jclouds.http.filters.BasicAuthentication; -import org.jclouds.ibmdev.binders.BindImageVisibilityToJsonPayload; +import org.jclouds.ibmdev.binders.BindExpirationTimeToJsonPayload; +import org.jclouds.ibmdev.binders.SaveInstanceBinder; +import org.jclouds.ibmdev.domain.Address; import org.jclouds.ibmdev.domain.Image; import org.jclouds.ibmdev.domain.Instance; +import org.jclouds.ibmdev.domain.Key; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.ibmdev.domain.Volume; +import org.jclouds.ibmdev.functions.ParseAddressFromJson; +import org.jclouds.ibmdev.functions.ParseAddressesFromJson; +import org.jclouds.ibmdev.functions.ParseExpirationTimeFromJson; import org.jclouds.ibmdev.functions.ParseImageFromJson; import org.jclouds.ibmdev.functions.ParseImagesFromJson; import org.jclouds.ibmdev.functions.ParseInstanceFromJson; import org.jclouds.ibmdev.functions.ParseInstancesFromJson; +import org.jclouds.ibmdev.functions.ParseKeyFromJson; +import org.jclouds.ibmdev.functions.ParseKeysFromJson; +import org.jclouds.ibmdev.functions.ParseVolumeFromJson; +import org.jclouds.ibmdev.functions.ParseVolumesFromJson; +import org.jclouds.ibmdev.options.CreateInstanceOptions; +import org.jclouds.ibmdev.options.RestartInstanceOptions; +import org.jclouds.ibmdev.xml.LocationHandler; +import org.jclouds.ibmdev.xml.LocationsHandler; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.Endpoint; import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.MapBinder; +import org.jclouds.rest.annotations.MapPayloadParam; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.rest.annotations.XMLResponseParser; +import org.jclouds.rest.binders.BindToJsonPayload; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; @@ -96,8 +118,19 @@ public interface IBMDeveloperCloudAsyncClient { @ExceptionParser(ReturnNullOnNotFoundOr404.class) @Path("/images/{imageId}") @ResponseParser(ParseImageFromJson.class) + @MapBinder(BindToJsonPayload.class) ListenableFuture setImageVisibility(@PathParam("imageId") long id, - @BinderParam(BindImageVisibilityToJsonPayload.class) Image.Visibility visibility); + @MapPayloadParam("visibility") Image.Visibility visibility); + + /** + * @see IBMDeveloperCloudClient#listInstancesFromRequest(long) + */ + @GET + @Path("/requests/{requestId}") + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @ResponseParser(ParseInstancesFromJson.class) + ListenableFuture> listInstancesFromRequest( + @PathParam("requestId") long requestId); /** * @see IBMDeveloperCloudClient#listInstances() @@ -116,6 +149,47 @@ public interface IBMDeveloperCloudAsyncClient { @ResponseParser(ParseInstanceFromJson.class) ListenableFuture getInstance(@PathParam("instanceId") long id); + /** + * @see IBMDeveloperCloudClient#extendReservationForInstance(long,Date) + */ + @PUT + @Path("/instances/{instanceId}") + @ResponseParser(ParseExpirationTimeFromJson.class) + ListenableFuture extendReservationForInstance(@PathParam("instanceId") long id, + @BinderParam(BindExpirationTimeToJsonPayload.class) Date expirationTime); + + /** + * @see IBMDeveloperCloudClient#restartInstance + */ + @PUT + @Path("/instances/{instanceId}") + @MapBinder(RestartInstanceOptions.class) + ListenableFuture restartInstance(@PathParam("instanceId") long id, + RestartInstanceOptions... options); + + /** + * @see IBMDeveloperCloudClient#saveInstanceToImage + */ + @PUT + @Path("/instances/{instanceId}") + @MapBinder(SaveInstanceBinder.class) + @ResponseParser(ParseImageFromJson.class) + ListenableFuture saveInstanceToImage(@PathParam("instanceId") long id, + @MapPayloadParam("name") String toImageName, + @MapPayloadParam("description") String toImageDescription); + + /** + * @see IBMDeveloperCloudClient#createInstanceInLocation + */ + @POST + @Path("/instances") + @MapBinder(CreateInstanceOptions.class) + @ResponseParser(ParseInstanceFromJson.class) + ListenableFuture createInstanceInLocation( + @MapPayloadParam("location") String location, @MapPayloadParam("name") String name, + @MapPayloadParam("imageID") String imageID, + @MapPayloadParam("instanceType") String instanceType, CreateInstanceOptions... options); + /** * @see IBMDeveloperCloudClient#deleteInstance */ @@ -124,4 +198,144 @@ public interface IBMDeveloperCloudAsyncClient { @ExceptionParser(ReturnVoidOnNotFoundOr404.class) ListenableFuture deleteInstance(@PathParam("instanceId") long id); + /** + * @see IBMDeveloperCloudClient#listKeys() + */ + @GET + @Path("/keys") + @ResponseParser(ParseKeysFromJson.class) + ListenableFuture> listKeys(); + + /** + * @see IBMDeveloperCloudClient#generateKeyPair(String) + */ + @POST + @Path("/keys") + @MapBinder(BindToJsonPayload.class) + @ResponseParser(ParseKeyFromJson.class) + ListenableFuture generateKeyPair(@MapPayloadParam("name") String name); + + /** + * @see IBMDeveloperCloudClient#addPublicKey(String, String) + */ + @POST + @Path("/keys") + @MapBinder(BindToJsonPayload.class) + ListenableFuture addPublicKey(@MapPayloadParam("name") String name, + @MapPayloadParam("publicKey") String publicKey); + + /** + * @see IBMDeveloperCloudClient#updatePublicKey(String, String) + */ + @PUT + @Path("/keys/{keyName}") + @MapBinder(BindToJsonPayload.class) + ListenableFuture updatePublicKey(@PathParam("keyName") String name, + @MapPayloadParam("publicKey") String publicKey); + + /** + * @see IBMDeveloperCloudClient#setDefaultStatusOfKey(String, boolean) + */ + @PUT + @Path("/keys/{keyName}") + @MapBinder(BindToJsonPayload.class) + ListenableFuture setDefaultStatusOfKey(@PathParam("keyName") String name, + @MapPayloadParam("default") boolean isDefault); + + /** + * @see IBMDeveloperCloudClient#getKey(String) + */ + @GET + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @Path("/keys/{keyName}") + @ResponseParser(ParseKeyFromJson.class) + ListenableFuture getKey(@PathParam("keyName") String name); + + /** + * @see IBMDeveloperCloudClient#deleteKey + */ + @DELETE + @Path("/keys/{keyName}") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + ListenableFuture deleteKey(@PathParam("keyName") String name); + + /** + * @see IBMDeveloperCloudClient#listVolumes() + */ + @GET + @Path("/storage") + @ResponseParser(ParseVolumesFromJson.class) + ListenableFuture> listVolumes(); + + /** + * @see IBMDeveloperCloudClient#createVolumeInLocation + */ + @POST + @Path("/storage") + @MapBinder(BindToJsonPayload.class) + @ResponseParser(ParseVolumeFromJson.class) + ListenableFuture createVolumeInLocation(@MapPayloadParam("location") String location, + @MapPayloadParam("name") String name, @MapPayloadParam("format") String format, + @MapPayloadParam("size") String size); + + /** + * @see IBMDeveloperCloudClient#getVolume(long) + */ + @GET + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @Path("/storage/{volumeId}") + @ResponseParser(ParseVolumeFromJson.class) + ListenableFuture getVolume(@PathParam("volumeId") long id); + + /** + * @see IBMDeveloperCloudClient#deleteVolume + */ + @DELETE + @Path("/storage/{volumeId}") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + ListenableFuture deleteVolume(@PathParam("volumeId") long id); + + /** + * @see IBMDeveloperCloudClient#listLocations() + */ + @GET + @Path("/locations") + @Consumes(MediaType.TEXT_XML) + @XMLResponseParser(LocationsHandler.class) + ListenableFuture> listLocations(); + + /** + * @see IBMDeveloperCloudClient#getLocation(long) + */ + @GET + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @Path("/locations/{locationId}") + @Consumes(MediaType.TEXT_XML) + @XMLResponseParser(LocationHandler.class) + ListenableFuture getLocation(@PathParam("locationId") long id); + + /** + * @see IBMDeveloperCloudClient#listAddresses() + */ + @GET + @Path("/addresses") + @ResponseParser(ParseAddressesFromJson.class) + ListenableFuture> listAddresses(); + + /** + * @see IBMDeveloperCloudClient#allocateAddressInLocation(long) + */ + @POST + @Path("/addresses") + @MapBinder(BindToJsonPayload.class) + @ResponseParser(ParseAddressFromJson.class) + ListenableFuture
allocateAddressInLocation(@MapPayloadParam("location") long locationId); + + /** + * @see IBMDeveloperCloudClient#releaseAddress(long) + */ + @DELETE + @Path("/addresses/{addressId}") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + ListenableFuture releaseAddress(@PathParam("addressId") long id); } diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudClient.java b/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudClient.java index e434f8eafd..681b996e04 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudClient.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/IBMDeveloperCloudClient.java @@ -41,13 +41,21 @@ */ package org.jclouds.ibmdev; +import java.util.Date; import java.util.Set; import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; +import org.jclouds.ibmdev.domain.Address; import org.jclouds.ibmdev.domain.Image; import org.jclouds.ibmdev.domain.Instance; +import org.jclouds.ibmdev.domain.Key; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.ibmdev.domain.Volume; +import org.jclouds.ibmdev.options.CreateInstanceOptions; +import org.jclouds.ibmdev.options.RestartInstanceOptions; import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.ResourceNotFoundException; /** * Provides synchronous access to IBMDeveloperCloud. @@ -85,7 +93,7 @@ public interface IBMDeveloperCloudClient { void deleteImage(long id); /** - * If set to {@link Image.Visibility#PUBLIC}, makes the Image identified by the supplied Image ID + * If set to {@code Image.Visibility#PUBLIC}, makes the Image identified by the supplied Image ID * publicly available for all users to create Instances of. * * @return modified image or null, if image was not found. @@ -106,6 +114,15 @@ public interface IBMDeveloperCloudClient { */ Set listInstances(); + /** + * + * @return the list of Instances that the authenticated user manages that were created as part of + * the request specified by {@code requestId}, or null if the request was not found + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to view this request + */ + Set listInstancesFromRequest(long requestId); + /** * Returns the Instance that the authenticated user manages with the specified {@code id} * @@ -116,6 +133,99 @@ public interface IBMDeveloperCloudClient { */ Instance getInstance(long id); + /** + * Requests a new Instance to be created. + * + * @param location + * The id of the Location where this instance will be created + * @param name + * The alias to use to reference this instance + * @param imageID + * The ID of the image to create this instance from + * @param instanceType + * The instance type to use for this instance {SMALL, MEDIUM, LARGE} + * @param options + * overrides default public key, mounts a volume, or attaches a static ip + * @throws AuthorizationException + * code 401 if the authenticated user is not authorized to create instances + *

+ * code 402 if payment is required before more instances may be created + * @throws IllegalStateException + * code 409 if there are not enough resources in the cloud to fulfill this request + *

+ * code 412 One or more of the supplied parameters are invalid for this request + */ + Instance createInstanceInLocation(String location, String name, String imageID, + String instanceType, CreateInstanceOptions... options); + + /** + * Sets the expiration time of the instance to the value specified + * + * @throws ResourceNotFoundException + * code 404 The instance specified by {@code name} was not found + * @throws AuthorizationException + * code 401 if the user is not authorized to extend the expiration time of this + * instance + *

+ * code 402 if payment is required before more instances may be created + * @throws IllegalStateException + *

+ * code 406 The provided expiration date is not valid code 409 if there are not enough + * resources in the cloud to fulfill this request + *

+ * code 412 The instance is in an invalid state to perform this operation + */ + Date extendReservationForInstance(long id, Date expirationTime); + + /** + * Restart the instance + * + * @param id + * the instance to restart + * @param options + * allows you to specify a new public key for login + * @throws ResourceNotFoundException + * code 404 The instance specified by {@code name} was not found + * @throws AuthorizationException + * code 401 if the user is not authorized to extend the expiration time of this + * instance + *

+ * code 402 if payment is required before more instances may be created + * @throws IllegalStateException + *

+ * code 406 The provided expiration date is not valid code 409 if there are not enough + * resources in the cloud to fulfill this request + *

+ * code 412 The instance is in an invalid state to perform this operation + */ + void restartInstance(long id, RestartInstanceOptions... options); + + /** + * Saves an instance to a private image + * + * @param id + * the instance to save + * @param toImageName + * The name to associate with the captured image. + * @param toImageDescription + * The description to associate with the capture image. + * @return a private image + * @throws ResourceNotFoundException + * code 404 The instance specified by {@code name} was not found + * @throws AuthorizationException + * code 401 if the user is not authorized to extend the expiration time of this + * instance + *

+ * code 402 if payment is required before more instances may be created + * @throws IllegalStateException + *

+ * code 406 The provided expiration date is not valid code 409 if there are not enough + * resources in the cloud to fulfill this request + *

+ * code 412 The instance is in an invalid state to perform this operation + */ + Image saveInstanceToImage(long id, String toImageName, String toImageDescription); + /** * Deletes the Instance that the authenticated user manages with the specified {@code id} * @@ -125,4 +235,186 @@ public interface IBMDeveloperCloudClient { * code 412 if the instance is in an invalid state to perform this operation */ void deleteInstance(long id); + + /** + * + * @return the set of Public Keys stored for the authenticated user. + */ + Set listKeys(); + + /** + * Returns the key with the specified key name from the set of Public Keys stored for the + * authenticated user. + * + * @return null if key is not found + */ + Key getKey(String name); + + /** + * Used to generate a new SSH Key Pair for the authenticated user. + * + * @param name + * The name to used to identify this key pair. + * @return private key + * + * @throws IllegalStateException + * code 409 A key with the specified {@code name} already exists + * + */ + Key generateKeyPair(String name); + + /** + * Used to generate a new SSH Key Pair for the authenticated user. + * + * @param name + * The name to used to identify this key pair. + * @param publicKey + * The RSA SSH Key to add + * + * @throws IllegalStateException + * code 409 A key with the specified {@code name} already exists + *

+ * code 412 The supplied public key is invalid + */ + void addPublicKey(String name, String publicKey); + + /** + * Used to update the Public Key specified by the supplied key name stored for the authenticated + * user. + * + * @param name + * The name to used to identify this key pair. + * @param publicKey + * The public key to store + * @throws ResourceNotFoundException + * code 404 The key specified by {@code name} was not found + * @throws IllegalStateException + * code 412 The supplied public key is invalid + */ + void updatePublicKey(String name, String publicKey); + + /** + * Used to set the Public Key specified by the supplied key name as the default key. + * + * @param name + * The name to used to identify this key pair. + * @param isDefault + * A boolean representing the default state of this key + * @throws ResourceNotFoundException + * code 404 The key specified by {@code name} was not found + */ + void setDefaultStatusOfKey(String name, boolean isDefault); + + /** + * Deletes Key identified by the supplied key name. + * + * @throws AuthorizationException + * code 401 if the user is not authorized to perform this action + */ + void deleteKey(String name); + + /** + * + * @return the set of storage volumes for the authenticated user. + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to view this + * information + */ + Set listVolumes(); + + /** + * Creates a new storage volume for the authenticated user. + * + * @param location + * The id of the Location where the storage volume will be created + * @param name + * The desired name of the newly created storage volume + * @param format + * The filesystem format for the new storage volume. Valid format is: EXT3 + * @param size + * The size of the new storage volume. Valid values may include SMALL, MEDIUM, and + * LARGE. Actual values may depend on the location used and may be discov- ered via the + * location service + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to create a volume + *

+ * code 402 if payment is required before more storage volumes may be created + * @throws IllegalStateException + * code 409 ifhere are not enough resources in the cloud to fulfill this request + *

+ * code 412 One or more of the supplied parameters are invalid for this request + */ + Volume createVolumeInLocation(String location, String name, String format, String size); + + /** + * Used to retrieve the specified storage volume for the authenticated user. + * + * @return null if volume is not found + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to view this storage + * volume + */ + Volume getVolume(long id); + + /** + * Remove the specified storage volume for the authenticated user. + * + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to remove this + * storage volume + * @throws IllegalStateException + * code 412 if the storage volume is not in the correct state to be deleted + */ + void deleteVolume(long id); + + /** + * + * @return the list of Locations (Data Centers) that the user is entitled to and their + * capabilities + */ + Set listLocations(); + + /** + * Returns the Location identified by the supplied Location ID + * + * @return null if location is not found + * + * @throws AuthorizationException + * code 401 if the user is not authorized + */ + Location getLocation(long id); + + /** + * + * @return the set of static IP addresses for the authenticated user. + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to view this + * information + */ + Set listAddresses(); + + /** + * Allocates a new static IP addresses for the authenticated user. + * + * @param locationId + * the id of the Location where this address will be allocated + * @throws AuthorizationException + * code 401 if the currently authenticated user is not authorized to remove this IP + * address + *

+ * code 402 if payment is required before more addresses may be allocated + * @throws IllegalStateException + * code 409 ifhere are not enough resources in the cloud to fulfill this request + */ + Address allocateAddressInLocation(long locationId); + + /** + * Used to release the specified static IP addresses for the authenticated user. + * + * @throws AuthorizationException + * code 401 if the user is not authorized to release this address + * @throws IllegalStateException + * code 412 address is in an invalid state to perform this operation + */ + void releaseAddress(long id); } diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindExpirationTimeToJsonPayload.java b/ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindExpirationTimeToJsonPayload.java new file mode 100644 index 0000000000..cf43795dd1 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindExpirationTimeToJsonPayload.java @@ -0,0 +1,48 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.binders; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.util.Date; +import java.util.Map; + +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.binders.BindToJsonPayload; + +import com.google.common.collect.ImmutableMap; + +/** + * + * @author Adrian Cole + * + */ +public class BindExpirationTimeToJsonPayload extends BindToJsonPayload { + + @Override + public void bindToRequest(HttpRequest request, Map postParams) { + throw new IllegalStateException("Change Admin Pass is a PUT operation"); + } + + @Override + public void bindToRequest(HttpRequest request, Object toBind) { + checkArgument(toBind instanceof Date, "this binder is only valid for Date!"); + super.bindToRequest(request, ImmutableMap.of("expirationTime", toBind)); + } +} diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindImageVisibilityToJsonPayload.java b/ibmdev/src/main/java/org/jclouds/ibmdev/binders/SaveInstanceBinder.java similarity index 52% rename from ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindImageVisibilityToJsonPayload.java rename to ibmdev/src/main/java/org/jclouds/ibmdev/binders/SaveInstanceBinder.java index 5e3b34539a..5c3f294196 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/binders/BindImageVisibilityToJsonPayload.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/binders/SaveInstanceBinder.java @@ -3,14 +3,14 @@ * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== - * Licensed under the Apache License, Version 2.0 (the ;License;); + * Licensed 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 + * 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, + * 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. @@ -18,26 +18,23 @@ */ package org.jclouds.ibmdev.binders; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; +import java.util.Map; import org.jclouds.http.HttpRequest; -import org.jclouds.rest.binders.BindToStringPayload; - -import com.google.common.collect.ImmutableSet; +import org.jclouds.rest.binders.BindToJsonPayload; /** + * * * @author Adrian Cole * */ -public class BindImageVisibilityToJsonPayload extends BindToStringPayload { +public class SaveInstanceBinder extends BindToJsonPayload { @Override - public void bindToRequest(HttpRequest request, Object payload) { - request.getHeaders().replaceValues(HttpHeaders.CONTENT_TYPE, - ImmutableSet.of(MediaType.APPLICATION_JSON)); - super.bindToRequest(request, String.format("{\"visibility\":\"%s\"}", payload)); + public void bindToRequest(HttpRequest request, Map postParams) { + postParams.put("state", "save"); + bindToRequest(request,(Object) postParams); } -} \ No newline at end of file +} diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Address.java b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Address.java new file mode 100644 index 0000000000..5c85e57904 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Address.java @@ -0,0 +1,135 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 + * + * 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.ibmdev.domain; + +import javax.annotation.Nullable; + +/** + * + * The current state of a Address + * + * @author Adrian Cole + */ +public class Address { + private int state; + private int location; + private String ip; + private long id; + @Nullable + private Long instanceId; + + public Address(int state, int location, String ip, long id, Long instanceId) { + this.state = state; + this.location = location; + this.ip = ip; + this.id = id; + this.instanceId = instanceId; + } + + public Address() { + + } + + public int getState() { + return state; + } + + public void setState(int state) { + this.state = state; + } + + public int getLocation() { + return location; + } + + public void setLocation(int location) { + this.location = location; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Long getInstanceId() { + return instanceId; + } + + public void setInstanceId(Long instanceId) { + this.instanceId = instanceId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); + result = prime * result + ((ip == null) ? 0 : ip.hashCode()); + result = prime * result + location; + result = prime * result + state; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Address other = (Address) obj; + if (id != other.id) + return false; + if (instanceId == null) { + if (other.instanceId != null) + return false; + } else if (!instanceId.equals(other.instanceId)) + return false; + if (ip == null) { + if (other.ip != null) + return false; + } else if (!ip.equals(other.ip)) + return false; + if (location != other.location) + return false; + if (state != other.state) + return false; + return true; + } + + @Override + public String toString() { + return "[id=" + id + ", ip=" + ip + ", location=" + location + ", state=" + state + + ", instanceId=" + instanceId + "]"; + } + +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Key.java b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Key.java new file mode 100644 index 0000000000..a9a3725100 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Key.java @@ -0,0 +1,148 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 + * + * 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.ibmdev.domain; + +import java.util.Date; +import java.util.Set; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; +import com.google.gson.annotations.SerializedName; + +/** + * + * The current state of a public or private key. + * + * @author Adrian Cole + */ +public class Key { + @SerializedName("default") + private boolean isDefault; + private Set instanceIds = Sets.newLinkedHashSet(); + private String keyMaterial; + @SerializedName("keyName") + private String name; + private Date lastModifiedTime; + + public Key(boolean isDefault, Iterable instanceIds, String keyMaterial, String name, + Date lastModifiedTime) { + this.isDefault = isDefault; + Iterables.addAll(this.instanceIds, instanceIds); + this.keyMaterial = keyMaterial; + this.name = name; + this.lastModifiedTime = lastModifiedTime; + } + + public Key() { + + } + + public boolean isDefault() { + return isDefault; + } + + public void setDefault(boolean isDefault) { + this.isDefault = isDefault; + } + + public String getKeyMaterial() { + return keyMaterial; + } + + public void setKeyMaterial(String keyMaterial) { + this.keyMaterial = keyMaterial; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getLastModifiedTime() { + return lastModifiedTime; + } + + public void setLastModifiedTime(Date lastModifiedTime) { + this.lastModifiedTime = lastModifiedTime; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((instanceIds == null) ? 0 : instanceIds.hashCode()); + result = prime * result + (isDefault ? 1231 : 1237); + result = prime * result + ((keyMaterial == null) ? 0 : keyMaterial.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((lastModifiedTime == null) ? 0 : lastModifiedTime.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Key other = (Key) obj; + if (instanceIds == null) { + if (other.instanceIds != null) + return false; + } else if (!instanceIds.equals(other.instanceIds)) + return false; + if (isDefault != other.isDefault) + return false; + if (keyMaterial == null) { + if (other.keyMaterial != null) + return false; + } else if (!keyMaterial.equals(other.keyMaterial)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (lastModifiedTime == null) { + if (other.lastModifiedTime != null) + return false; + } else if (!lastModifiedTime.equals(other.lastModifiedTime)) + return false; + return true; + } + + @Override + public String toString() { + return "Key [isDefault=" + isDefault + ", instanceIds=" + instanceIds + ", name=" + name + + ", keyMaterial=" + keyMaterial + ", lastModifiedTime=" + lastModifiedTime + "]"; + } + + public Set getInstanceIds() { + return instanceIds; + } + + public void setInstanceIds(Set instanceIds) { + this.instanceIds = instanceIds; + } + +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Location.java b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Location.java new file mode 100644 index 0000000000..faa4117b8c --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Location.java @@ -0,0 +1,120 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 + * + * 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.ibmdev.domain; + +import java.util.Map; + +import com.google.common.collect.Maps; + +/** + * + * The current state of a location (datacenter) + * + * @author Adrian Cole + */ +public class Location { + + private final int id; + private final String name; + private final String description; + private final String location; + private final Map> capabilities = Maps.newLinkedHashMap(); + + public Location(int id, String name, String description, String location, + Map> capabilities) { + this.id = id; + this.name = name; + this.description = description; + this.location = location; + this.capabilities.putAll(capabilities); + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public String getLocation() { + return location; + } + + public Map> getCapabilities() { + return capabilities; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((capabilities == null) ? 0 : capabilities.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + id; + result = prime * result + ((location == null) ? 0 : location.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Location other = (Location) obj; + if (capabilities == null) { + if (other.capabilities != null) + return false; + } else if (!capabilities.equals(other.capabilities)) + return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (id != other.id) + return false; + if (location == null) { + if (other.location != null) + return false; + } else if (!location.equals(other.location)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + return "Location [id=" + id + ", name=" + name + ", description=" + description + + ", location=" + location + ", capabilities=" + capabilities + "]"; + } + +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Volume.java b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Volume.java new file mode 100644 index 0000000000..68726e89d1 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/domain/Volume.java @@ -0,0 +1,219 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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 + * + * 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.ibmdev.domain; + +import java.util.Date; +import java.util.Set; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; + +/** + * + * The current state of a Volume + * + * @author Adrian Cole + */ +public class Volume { + + private Long instanceId; + private int state; + private int size; + private String owner; + private Date createdTime; + private int location; + private Set productCodes = Sets.newLinkedHashSet(); + private String format; + private String name; + private long id; + + public Volume(Long instanceId, int state, int size, String owner, Date createdTime, + int location, Iterable productCodes, String format, String name, long id) { + this.instanceId = instanceId; + this.state = state; + this.size = size; + this.owner = owner; + this.createdTime = createdTime; + this.location = location; + Iterables.addAll(this.productCodes, productCodes); + this.format = format; + this.name = name; + this.id = id; + } + + public Volume() { + + } + + public Long getInstanceId() { + return instanceId; + } + + public void setInstanceId(Long instanceId) { + this.instanceId = instanceId; + } + + public int getState() { + return state; + } + + public void setState(int state) { + this.state = state; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Date getCreatedTime() { + return createdTime; + } + + public void setCreatedTime(Date createdTime) { + this.createdTime = createdTime; + } + + public int getLocation() { + return location; + } + + public void setLocation(int location) { + this.location = location; + } + + public Set getProductCodes() { + return productCodes; + } + + public void setProductCodes(Set productCodes) { + this.productCodes = productCodes; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((createdTime == null) ? 0 : createdTime.hashCode()); + result = prime * result + ((format == null) ? 0 : format.hashCode()); + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); + result = prime * result + location; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((owner == null) ? 0 : owner.hashCode()); + result = prime * result + ((productCodes == null) ? 0 : productCodes.hashCode()); + result = prime * result + size; + result = prime * result + state; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Volume other = (Volume) obj; + if (createdTime == null) { + if (other.createdTime != null) + return false; + } else if (!createdTime.equals(other.createdTime)) + return false; + if (format == null) { + if (other.format != null) + return false; + } else if (!format.equals(other.format)) + return false; + if (id != other.id) + return false; + if (instanceId == null) { + if (other.instanceId != null) + return false; + } else if (!instanceId.equals(other.instanceId)) + return false; + if (location != other.location) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (owner == null) { + if (other.owner != null) + return false; + } else if (!owner.equals(other.owner)) + return false; + if (productCodes == null) { + if (other.productCodes != null) + return false; + } else if (!productCodes.equals(other.productCodes)) + return false; + if (size != other.size) + return false; + if (state != other.state) + return false; + return true; + } + + @Override + public String toString() { + return "[id=" + id + ", name=" + name + ", size=" + size + ", state=" + state + + ", instanceId=" + instanceId + ", location=" + location + ", format=" + format + + ", owner=" + owner + ", createdTime=" + createdTime + ", productCodes=" + + productCodes + "]"; + } + +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressFromJson.java new file mode 100644 index 0000000000..908b7d2f0f --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressFromJson.java @@ -0,0 +1,74 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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. + * ==================================================================== + */ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Address; + +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseAddressFromJson extends ParseJson

{ + @Inject + public ParseAddressFromJson(Gson gson) { + super(gson); + } + + @Override + protected Address apply(InputStream stream) { + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), Address.class); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressesFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressesFromJson.java new file mode 100644 index 0000000000..08bfd8d06d --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseAddressesFromJson.java @@ -0,0 +1,80 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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. + * ==================================================================== + */ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Address; + +import com.google.common.collect.Sets; +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseAddressesFromJson extends ParseJson> { + @Inject + public ParseAddressesFromJson(Gson gson) { + super(gson); + } + + private static class AddressListResponse { + Set
addresses = Sets.newLinkedHashSet(); + } + + @Override + protected Set apply(InputStream stream) { + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), AddressListResponse.class).addresses; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJson.java new file mode 100644 index 0000000000..b724e8ddbb --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJson.java @@ -0,0 +1,55 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.Date; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; + +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseExpirationTimeFromJson extends ParseJson { + @Inject + public ParseExpirationTimeFromJson(Gson gson) { + super(gson); + } + + private static class ExpirationTimeResponse { + Date expirationTime; + } + + @Override + protected Date apply(InputStream stream) { + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), ExpirationTimeResponse.class).expirationTime; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeyFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeyFromJson.java new file mode 100644 index 0000000000..7b3fa2ac5c --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeyFromJson.java @@ -0,0 +1,74 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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. + * ==================================================================== + */ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Key; + +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseKeyFromJson extends ParseJson { + @Inject + public ParseKeyFromJson(Gson gson) { + super(gson); + } + + @Override + protected Key apply(InputStream stream) { + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), Key.class); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeysFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeysFromJson.java new file mode 100644 index 0000000000..1220679d7b --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseKeysFromJson.java @@ -0,0 +1,80 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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. + * ==================================================================== + */ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Key; + +import com.google.common.collect.Sets; +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseKeysFromJson extends ParseJson> { + @Inject + public ParseKeysFromJson(Gson gson) { + super(gson); + } + + private static class KeyListResponse { + Set keys = Sets.newLinkedHashSet(); + } + + @Override + protected Set apply(InputStream stream) { + try { + return gson.fromJson(new InputStreamReader(stream, "UTF-8"), KeyListResponse.class).keys; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumeFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumeFromJson.java new file mode 100644 index 0000000000..ca807e811e --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumeFromJson.java @@ -0,0 +1,58 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Volume; + +import com.google.common.collect.ImmutableSet; +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseVolumeFromJson extends ParseJson { + @Inject + public ParseVolumeFromJson(Gson gson) { + super(gson); + } + + private static final Set emptyString = ImmutableSet.of(""); + + @Override + protected Volume apply(InputStream stream) { + try { + Volume returnVal = gson.fromJson(new InputStreamReader(stream, "UTF-8"), Volume.class); + if (emptyString.equals(returnVal.getProductCodes())) + returnVal.getProductCodes().clear(); + return returnVal; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumesFromJson.java b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumesFromJson.java new file mode 100644 index 0000000000..fe1fc238ed --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/functions/ParseVolumesFromJson.java @@ -0,0 +1,65 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.http.functions.ParseJson; +import org.jclouds.ibmdev.domain.Volume; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import com.google.gson.Gson; + +/** + * @author Adrian Cole + */ +@Singleton +public class ParseVolumesFromJson extends ParseJson> { + @Inject + public ParseVolumesFromJson(Gson gson) { + super(gson); + } + + private static class VolumeListResponse { + Set volumes = Sets.newLinkedHashSet(); + } + + private static final Set emptyString = ImmutableSet.of(""); + + @Override + protected Set apply(InputStream stream) { + try { + Set list = gson.fromJson(new InputStreamReader(stream, "UTF-8"), + VolumeListResponse.class).volumes; + for (Volume volume : list) + if (emptyString.equals(volume.getProductCodes())) + volume.getProductCodes().clear(); + return list; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } + } +} \ No newline at end of file diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/handlers/IBMDeveloperCloudErrorHandler.java b/ibmdev/src/main/java/org/jclouds/ibmdev/handlers/IBMDeveloperCloudErrorHandler.java index 794038a9b0..b9a168f10a 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/handlers/IBMDeveloperCloudErrorHandler.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/handlers/IBMDeveloperCloudErrorHandler.java @@ -56,9 +56,12 @@ public class IBMDeveloperCloudErrorHandler implements HttpErrorHandler { .getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 401: + case 402: case 403: exception = new AuthorizationException(message, exception); break; + case 406: + case 409: case 412: exception = new IllegalStateException(message, exception); break; diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/options/CreateInstanceOptions.java b/ibmdev/src/main/java/org/jclouds/ibmdev/options/CreateInstanceOptions.java new file mode 100644 index 0000000000..737d9897c2 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/options/CreateInstanceOptions.java @@ -0,0 +1,125 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.options; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.binders.BindToJsonPayload; + +import com.google.common.collect.Maps; + +/** + * + * + * @author Adrian Cole + * + */ +public class CreateInstanceOptions extends BindToJsonPayload { + Long volumeID; + Long ip; + String publicKey = "DEFAULT"; + Map configurationData = Maps.newLinkedHashMap(); + + @Override + public void bindToRequest(HttpRequest request, Map postParams) { + Map postData = Maps.newLinkedHashMap(); + postData.putAll(postParams); + postData.put("publicKey", publicKey); + if (volumeID != null) + postData.put("volumeID", volumeID); + if (configurationData.size() > 0) + postData.put("configurationData", configurationData); + if (ip != null) + postData.put("ip", ip); + super.bindToRequest(request, postData); + } + + @Override + public void bindToRequest(HttpRequest request, Object toBind) { + throw new IllegalStateException("CreateInstance is a POST operation"); + } + + /** + * + * @param id + * The ID of a storage volume to associate with this instance + * @param mountPoint + * The mount point in which to mount the attached storage volume + */ + public CreateInstanceOptions mountVolume(long id, String mountPoint) { + checkArgument(id > 0, "volume id must be a positive number"); + checkNotNull(mountPoint, "mountPoint"); + this.volumeID = id; + configurationData.put(String.format("oss.storage.id.%d.mnt", id), mountPoint); + return this; + } + + /** + * + * @param publicKeyName + * The public key to use for accessing the created instancee + */ + public CreateInstanceOptions authorizePublicKey(String publicKeyName) { + checkNotNull(publicKeyName, "publicKeyName"); + this.publicKey = publicKeyName; + return this; + } + + /** + * + * @param id + * The ID of a static IP address to associate with this instance + */ + public CreateInstanceOptions attachIp(long id) { + checkArgument(id > 0, "ip id must be a positive number"); + this.ip = id; + return this; + } + + public static class Builder { + + /** + * @see CreateInstanceOptions#mountVolume(long, String ) + */ + public static CreateInstanceOptions mountVolume(long id, String mountPoint) { + CreateInstanceOptions options = new CreateInstanceOptions(); + return options.mountVolume(id, mountPoint); + } + + /** + * @see CreateInstanceOptions#attachIp(long ) + */ + public static CreateInstanceOptions attachIp(long id) { + CreateInstanceOptions options = new CreateInstanceOptions(); + return options.attachIp(id); + } + + /** + * @see CreateInstanceOptions#authorizePublicKey(String ) + */ + public static CreateInstanceOptions authorizePublicKey(String publicKeyName) { + CreateInstanceOptions options = new CreateInstanceOptions(); + return options.authorizePublicKey(publicKeyName); + } + } +} diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/options/RestartInstanceOptions.java b/ibmdev/src/main/java/org/jclouds/ibmdev/options/RestartInstanceOptions.java new file mode 100644 index 0000000000..fdf08c83c3 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/options/RestartInstanceOptions.java @@ -0,0 +1,75 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.options; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.binders.BindToJsonPayload; + +import com.google.common.collect.Maps; + +/** + * + * + * @author Adrian Cole + * + */ +public class RestartInstanceOptions extends BindToJsonPayload { + String keyName; + + @Override + public void bindToRequest(HttpRequest request, Map postParams) { + Map postData = Maps.newLinkedHashMap(); + postData.putAll(postParams); + postData.put("state", "restart"); + if (keyName != null) + postData.put("keyName", keyName); + super.bindToRequest(request, postData); + } + + @Override + public void bindToRequest(HttpRequest request, Object toBind) { + throw new IllegalStateException("RestartInstance is a PUT operation"); + } + + /** + * + * @param keyName + * The name of the SSH Public Key to add to the instance during restart. + */ + public RestartInstanceOptions authorizePublicKey(String keyName) { + checkNotNull(keyName, "keyName"); + this.keyName = keyName; + return this; + } + + public static class Builder { + + /** + * @see RestartInstanceOptions#authorizePublicKey(String ) + */ + public static RestartInstanceOptions authorizePublicKey(String keyName) { + RestartInstanceOptions options = new RestartInstanceOptions(); + return options.authorizePublicKey(keyName); + } + } +} diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/reference/IBMDeveloperCloudConstants.java b/ibmdev/src/main/java/org/jclouds/ibmdev/reference/IBMDeveloperCloudConstants.java index c72bd168e7..8331bc3c93 100644 --- a/ibmdev/src/main/java/org/jclouds/ibmdev/reference/IBMDeveloperCloudConstants.java +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/reference/IBMDeveloperCloudConstants.java @@ -50,4 +50,8 @@ public interface IBMDeveloperCloudConstants { public static final String PROPERTY_IBMDEVELOPERCLOUD_ENDPOINT = "jclouds.ibmdevelopercloud.endpoint"; public static final String PROPERTY_IBMDEVELOPERCLOUD_USER = "jclouds.ibmdevelopercloud.user"; public static final String PROPERTY_IBMDEVELOPERCLOUD_PASSWORD = "jclouds.ibmdevelopercloud.password"; + public static final String CAPABILITY_CAPACITY = "oss.storage.capacity"; + public static final String CAPABILITY_FORMAT = "oss.storage.format"; + public static final String CAPABILITY_I386 = "oss.instance.spec.i386"; + public static final String CAPABILITY_x86_64 = "oss.instance.spec.x86_64"; } diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationHandler.java b/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationHandler.java new file mode 100644 index 0000000000..4abb6b8989 --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationHandler.java @@ -0,0 +1,99 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.xml; + +import java.util.Map; + +import javax.annotation.Resource; + +import org.jclouds.http.functions.ParseSax; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.logging.Logger; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import com.google.common.collect.Maps; + +/** + * @author Adrian Cole + */ +public class LocationHandler extends ParseSax.HandlerWithResult { + private StringBuilder currentText = new StringBuilder(); + + private int id; + private String name; + private String description; + private String location; + private Map> capabilities = Maps.newLinkedHashMap(); + private String capabilityName; + private String capabilityKey; + + @Resource + protected Logger logger = Logger.NULL; + + private Location loc; + + public Location getResult() { + return loc; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + if (qName.equalsIgnoreCase("Capability")) { + capabilityName = attributes.getValue(attributes.getIndex("id")); + capabilities.put(capabilityName, Maps. newLinkedHashMap()); + } else if (qName.equalsIgnoreCase("Entry")) { + capabilityKey = attributes.getValue(attributes.getIndex("key")); + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if (qName.equalsIgnoreCase("ID")) { + id = Integer.parseInt(currentText.toString().trim()); + } else if (qName.equalsIgnoreCase("Name")) { + name = currentText.toString().trim(); + } else if (qName.equalsIgnoreCase("Description")) { + description = currentText.toString().trim(); + if (description.equals("")) + description = null; + } else if (qName.equalsIgnoreCase("Value")) { + capabilities.get(capabilityName).put(capabilityKey, currentText.toString().trim()); + } else if (qName.equalsIgnoreCase("Location")) { + if (currentText.toString().trim().equals("")) { + this.loc = new Location(id, name, description, location, capabilities); + id = 0; + name = null; + description = null; + location = null; + capabilities = Maps.newLinkedHashMap(); + capabilityKey = null; + capabilityName = null; + } else { + location = currentText.toString().trim(); + } + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} diff --git a/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationsHandler.java b/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationsHandler.java new file mode 100644 index 0000000000..dd9f53eb4a --- /dev/null +++ b/ibmdev/src/main/java/org/jclouds/ibmdev/xml/LocationsHandler.java @@ -0,0 +1,69 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.xml; + +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.http.functions.ParseSax; +import org.jclouds.ibmdev.domain.Location; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import com.google.common.collect.Sets; + +/** + * @author Adrian Cole + */ +public class LocationsHandler extends ParseSax.HandlerWithResult> { + private StringBuilder currentText = new StringBuilder(); + + private Set tasks = Sets.newLinkedHashSet(); + private final LocationHandler locationHandler; + + @Inject + public LocationsHandler(LocationHandler locationHandler) { + this.locationHandler = locationHandler; + } + + public Set getResult() { + return tasks; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + locationHandler.startElement(uri, localName, qName, attributes); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + locationHandler.endElement(uri, localName, qName); + if (qName.equals("Location") && currentText.toString().trim().equals("")) { + this.tasks.add(locationHandler.getResult()); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + locationHandler.characters(ch, start, length); + currentText.append(ch, start, length); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClientTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClientTest.java index 30765f1561..7142d367c6 100644 --- a/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClientTest.java +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudAsyncClientTest.java @@ -27,15 +27,28 @@ import static org.testng.Assert.assertEquals; import java.io.IOException; import java.lang.reflect.Method; +import java.util.Date; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.http.functions.CloseContentAndReturn; +import org.jclouds.http.functions.ParseSax; import org.jclouds.ibmdev.config.IBMDeveloperCloudRestClientModule; import org.jclouds.ibmdev.domain.Image; +import org.jclouds.ibmdev.functions.ParseAddressFromJson; +import org.jclouds.ibmdev.functions.ParseAddressesFromJson; +import org.jclouds.ibmdev.functions.ParseExpirationTimeFromJson; import org.jclouds.ibmdev.functions.ParseImageFromJson; import org.jclouds.ibmdev.functions.ParseImagesFromJson; import org.jclouds.ibmdev.functions.ParseInstanceFromJson; import org.jclouds.ibmdev.functions.ParseInstancesFromJson; +import org.jclouds.ibmdev.functions.ParseKeyFromJson; +import org.jclouds.ibmdev.functions.ParseKeysFromJson; +import org.jclouds.ibmdev.functions.ParseVolumeFromJson; +import org.jclouds.ibmdev.functions.ParseVolumesFromJson; +import org.jclouds.ibmdev.options.CreateInstanceOptions; +import org.jclouds.ibmdev.options.RestartInstanceOptions; +import org.jclouds.ibmdev.xml.LocationHandler; +import org.jclouds.ibmdev.xml.LocationsHandler; import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.rest.RestClientTest; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; @@ -161,6 +174,26 @@ public class IBMDeveloperCloudAsyncClientTest extends RestClientTest httpRequest = processor.createRequest( + method, 1l); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/requests/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseInstancesFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + public void testGetInstance() throws SecurityException, NoSuchMethodException, IOException { Method method = IBMDeveloperCloudAsyncClient.class.getMethod("getInstance", long.class); GeneratedHttpRequest httpRequest = processor.createRequest( @@ -179,6 +212,83 @@ public class IBMDeveloperCloudAsyncClientTest extends RestClientTest httpRequest = processor.createRequest( + method, 1, new Date(123215235l)); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 28\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"expirationTime\":123215235}"); + + assertResponseParserClassEquals(method, httpRequest, ParseExpirationTimeFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + + public void testRestartInstance() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("restartInstance", long.class, + RestartInstanceOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 19\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"state\":\"restart\"}"); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + + public void testRestartInstanceNewKey() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("restartInstance", long.class, + RestartInstanceOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1, new RestartInstanceOptions().authorizePublicKey("keyName")); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 39\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"state\":\"restart\",\"keyName\":\"keyName\"}"); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + + public void testSaveInstanceToImage() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("saveInstanceToImage", long.class, + String.class,String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1, "imageName", "imageDescription"); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 68\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"description\":\"imageDescription\",\"name\":\"imageName\",\"state\":\"save\"}"); + + assertResponseParserClassEquals(method, httpRequest, ParseImageFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + } + public void testDeleteInstance() throws SecurityException, NoSuchMethodException, IOException { Method method = IBMDeveloperCloudAsyncClient.class.getMethod("deleteInstance", long.class); GeneratedHttpRequest httpRequest = processor.createRequest( @@ -197,6 +307,358 @@ public class IBMDeveloperCloudAsyncClientTest extends RestClientTest httpRequest = processor + .createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseKeysFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testGetKey() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("getKey", String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "1"); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseKeyFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testGenerateKeyPair() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("generateKeyPair", String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "key"); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 14\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"name\":\"key\"}"); + + assertResponseParserClassEquals(method, httpRequest, ParseKeyFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testAddPublicKey() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("addPublicKey", String.class, + String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "key", "publicbits"); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 39\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"publicKey\":\"publicbits\",\"name\":\"key\"}"); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testUpdatePublicKey() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("updatePublicKey", String.class, + String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "key", "publicbits"); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys/key HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 26\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"publicKey\":\"publicbits\"}"); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testSetDefaultStatusOfKey() throws SecurityException, NoSuchMethodException, + IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("setDefaultStatusOfKey", + String.class, boolean.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "key", true); + + assertRequestLineEquals(httpRequest, + "PUT https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys/key HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 18\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"default\":\"true\"}"); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testDeleteKey() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("deleteKey", String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "1"); + + assertRequestLineEquals(httpRequest, + "DELETE https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/keys/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListVolumes() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("listVolumes"); + GeneratedHttpRequest httpRequest = processor + .createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/storage HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseVolumesFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testGetVolume() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("getVolume", long.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/storage/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseVolumeFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testCreateVolumeInLocation() throws SecurityException, NoSuchMethodException, + IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("createVolumeInLocation", + String.class, String.class, String.class, String.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "location", "name", "format", "size"); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/storage HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 69\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, + "{\"location\":\"location\",\"name\":\"name\",\"format\":\"format\",\"size\":\"size\"}"); + + assertResponseParserClassEquals(method, httpRequest, ParseVolumeFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testCreateInstanceInLocation() throws SecurityException, NoSuchMethodException, + IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("createInstanceInLocation", + String.class, String.class, String.class, String.class, + CreateInstanceOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "location", "name", "imageID", "instanceType"); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 109\nContent-Type: application/json\n"); + assertPayloadEquals( + httpRequest, + "{\"instanceType\":\"instanceType\",\"imageID\":\"imageID\",\"location\":\"location\",\"name\":\"name\",\"publicKey\":\"DEFAULT\"}"); + + assertResponseParserClassEquals(method, httpRequest, ParseInstanceFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testCreateInstanceInLocationWithOptions() throws SecurityException, + NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("createInstanceInLocation", + String.class, String.class, String.class, String.class, + CreateInstanceOptions[].class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, "location", "name", "imageID", "instanceType", new CreateInstanceOptions() + .attachIp(1l).authorizePublicKey("MOO").mountVolume(2l, "/mnt")); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/instances HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 177\nContent-Type: application/json\n"); + assertPayloadEquals( + httpRequest, + "{\"instanceType\":\"instanceType\",\"imageID\":\"imageID\",\"location\":\"location\",\"name\":\"name\",\"publicKey\":\"MOO\",\"volumeID\":2,\"configurationData\":{\"oss.storage.id.2.mnt\":\"/mnt\"},\"ip\":1}"); + + assertResponseParserClassEquals(method, httpRequest, ParseInstanceFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testDeleteVolume() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("deleteVolume", long.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "DELETE https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/storage/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListLocations() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("listLocations"); + GeneratedHttpRequest httpRequest = processor + .createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/locations HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: text/xml\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseSax.class); + assertSaxResponseParserClassEquals(method, LocationsHandler.class); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testGetLocation() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("getLocation", long.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/locations/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: text/xml\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseSax.class); + assertSaxResponseParserClassEquals(method, LocationHandler.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListAddresses() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("listAddresses"); + GeneratedHttpRequest httpRequest = processor + .createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/addresses HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseAddressesFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testAllocateAddressInLocation() throws SecurityException, NoSuchMethodException, + IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("allocateAddressInLocation", + long.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "POST https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/addresses HTTP/1.1"); + assertHeadersEqual(httpRequest, + "Accept: application/json\nContent-Length: 16\nContent-Type: application/json\n"); + assertPayloadEquals(httpRequest, "{\"location\":\"1\"}"); + + assertResponseParserClassEquals(method, httpRequest, ParseAddressFromJson.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + + } + + public void testReleaseAddress() throws SecurityException, NoSuchMethodException, IOException { + Method method = IBMDeveloperCloudAsyncClient.class.getMethod("releaseAddress", long.class); + GeneratedHttpRequest httpRequest = processor.createRequest( + method, 1); + + assertRequestLineEquals(httpRequest, + "DELETE https://www-180.ibm.com/cloud/enterprise/beta/api/rest/20090403/addresses/1 HTTP/1.1"); + assertHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, CloseContentAndReturn.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + @Override protected void checkFilters(GeneratedHttpRequest httpRequest) { assertEquals(httpRequest.getFilters().size(), 1); diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudClientLiveTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudClientLiveTest.java index 2e718811b4..f24eb699db 100644 --- a/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudClientLiveTest.java +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/IBMDeveloperCloudClientLiveTest.java @@ -26,11 +26,16 @@ package org.jclouds.ibmdev; import static com.google.common.base.Preconditions.checkNotNull; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; import java.util.Set; +import org.jclouds.ibmdev.domain.Address; import org.jclouds.ibmdev.domain.Image; import org.jclouds.ibmdev.domain.Instance; +import org.jclouds.ibmdev.domain.Key; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.ibmdev.domain.Volume; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -78,6 +83,12 @@ public class IBMDeveloperCloudClientLiveTest { assertNotNull(response); } + @Test + public void testListInstancesFromRequestReturnsNull() throws Exception { + Set response = connection.listInstancesFromRequest(Long.MAX_VALUE); + assertNull(response); + } + @Test public void testGetInstance() throws Exception { Set response = connection.listInstances(); @@ -87,4 +98,59 @@ public class IBMDeveloperCloudClientLiveTest { assertEquals(connection.getInstance(instance.getId()).getId(), instance.getId()); } } + + @Test + public void testListKeys() throws Exception { + Set response = connection.listKeys(); + assertNotNull(response); + } + + @Test + public void testGetKey() throws Exception { + Set response = connection.listKeys(); + assertNotNull(response); + if (response.size() > 0) { + Key key = Iterables.get(response, 0); + assertEquals(connection.getKey(key.getName()).getName(), key.getName()); + } + } + + @Test + public void testListVolumes() throws Exception { + Set response = connection.listVolumes(); + assertNotNull(response); + } + + @Test + public void testGetVolume() throws Exception { + Set response = connection.listVolumes(); + assertNotNull(response); + if (response.size() > 0) { + Volume image = Iterables.get(response, 0); + assertEquals(connection.getVolume(image.getId()).getId(), image.getId()); + } + } + + @Test + public void testListLocations() throws Exception { + Set response = connection.listLocations(); + assertNotNull(response); + } + + @Test + public void testGetLocation() throws Exception { + Set response = connection.listLocations(); + assertNotNull(response); + if (response.size() > 0) { + Location image = Iterables.get(response, 0); + assertEquals(connection.getLocation(image.getId()).getId(), image.getId()); + } + } + + @Test + public void testListAddresss() throws Exception { + Set response = connection.listAddresses(); + assertNotNull(response); + } + } diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java new file mode 100644 index 0000000000..135a660cd3 --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java @@ -0,0 +1,64 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Address; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseAddressFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseAddressFromJsonTest") +public class ParseAddressFromJsonTest { + + private ParseAddressFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseAddressFromJson.class); + } + + public void test() { + Address address = new Address(2, 1, "129.33.196.243", 1217l, 1l); + + Address compare = handler.apply(new HttpResponse(ParseAddressFromJsonTest.class + .getResourceAsStream("/address.json"))); + assertEquals(compare, address); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java new file mode 100644 index 0000000000..a380f68e9c --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java @@ -0,0 +1,64 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.IOException; +import java.util.Set; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Address; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseAddresssFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseAddressesFromJsonTest") +public class ParseAddressesFromJsonTest { + + private ParseAddressesFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseAddressesFromJson.class); + } + + public void test() { + Address address1 = new Address(2, 1, "129.33.196.243", 1217l, 1l); + Address address2 = new Address(3, 2, "129.33.196.244", 1218l, null); + Set compare = handler.apply(new HttpResponse( + ParseAddressesFromJsonTest.class.getResourceAsStream("/addresses.json"))); + assert (compare.contains(address1)); + assert (compare.contains(address2)); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJsonTest.java new file mode 100644 index 0000000000..c565c0bd63 --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseExpirationTimeFromJsonTest.java @@ -0,0 +1,63 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.util.Date; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.util.Utils; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseExpirationTimeFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseExpirationTimeFromJsonTest") +public class ParseExpirationTimeFromJsonTest { + + private ParseExpirationTimeFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseExpirationTimeFromJson.class); + } + + public void test() { + Date compare = handler.apply(new HttpResponse(Utils + .toInputStream("{ \"expirationTime\":1249876800000 }"))); + assertEquals(compare, new Date(1249876800000l)); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeyFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeyFromJsonTest.java new file mode 100644 index 0000000000..7fdb02a41f --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeyFromJsonTest.java @@ -0,0 +1,67 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.util.Date; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Key; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseKeyFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseKeyFromJsonTest") +public class ParseKeyFromJsonTest { + + private ParseKeyFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseKeyFromJson.class); + } + + public void test() { + Key key = new Key(true, ImmutableSet. of(1l), + "AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+...", "DEFAULT", new Date(1260428507510l)); + + Key compare = handler.apply(new HttpResponse(ParseKeyFromJsonTest.class + .getResourceAsStream("/key.json"))); + assertEquals(compare, key); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeysFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeysFromJsonTest.java new file mode 100644 index 0000000000..c04c031b3d --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseKeysFromJsonTest.java @@ -0,0 +1,69 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.IOException; +import java.util.Date; +import java.util.Set; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Key; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseKeysFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseKeysFromJsonTest") +public class ParseKeysFromJsonTest { + + private ParseKeysFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseKeysFromJson.class); + } + + public void test() { + Key key1 = new Key(true, ImmutableSet. of(1l), + "AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+...", "DEFAULT", new Date(1260428507510l)); + Key key2 = new Key(false, ImmutableSet. of(), "AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+", + "BEAR", new Date(1260428507511l)); + + Set compare = handler.apply(new HttpResponse(ParseKeysFromJsonTest.class + .getResourceAsStream("/keys.json"))); + assert (compare.contains(key1)); + assert (compare.contains(key2)); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumeFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumeFromJsonTest.java new file mode 100644 index 0000000000..3e7c86db0f --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumeFromJsonTest.java @@ -0,0 +1,68 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.util.Date; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Volume; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseVolumeFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseVolumeFromJsonTest") +public class ParseVolumeFromJsonTest { + + private ParseVolumeFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseVolumeFromJson.class); + } + + public void test() { + + Volume volume = new Volume(2l, 5, 50, "aadelucc@us.ibm.com", new Date(1260469075119l), 1, + ImmutableSet. of(), "ext3", "New Storage", 67l); + + Volume compare = handler.apply(new HttpResponse(ParseVolumeFromJsonTest.class + .getResourceAsStream("/volume.json"))); + assertEquals(compare, volume); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumesFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumesFromJsonTest.java new file mode 100644 index 0000000000..0975e03418 --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseVolumesFromJsonTest.java @@ -0,0 +1,70 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.functions; + +import java.io.IOException; +import java.util.Date; +import java.util.Set; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.ParserModule; +import org.jclouds.ibmdev.domain.Volume; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code ParseVolumesFromJson} + * + * @author Adrian Cole + */ +@Test(groups = "unit", sequential = true, testName = "ibmdev.ParseVolumesFromJsonTest") +public class ParseVolumesFromJsonTest { + + private ParseVolumesFromJson handler; + + @BeforeTest + protected void setUpInjector() throws IOException { + Injector injector = Guice.createInjector(new ParserModule() { + @Override + protected void configure() { + bind(DateAdapter.class).to(LongDateAdapter.class); + super.configure(); + } + }); + handler = injector.getInstance(ParseVolumesFromJson.class); + } + + public void test() { + Volume volume1 = new Volume(2l, 5, 50, "aadelucc@us.ibm.com", new Date(1260469075119l), 1, + ImmutableSet. of(), "ext3", "New Storage", 67l); + + Volume volume2 = new Volume(null, 6, 51, "aadelucc@us.ibm.com", new Date(1260469075120l), 2, + ImmutableSet. of("abrad"), "ext3", "New Storage1", 68l); + + Set compare = handler.apply(new HttpResponse(ParseVolumesFromJsonTest.class + .getResourceAsStream("/volumes.json"))); + assert (compare.contains(volume1)); + assert (compare.contains(volume2)); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationHandlerTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationHandlerTest.java new file mode 100644 index 0000000000..ed23a6d6db --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationHandlerTest.java @@ -0,0 +1,60 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Map; + +import org.jclouds.http.functions.BaseHandlerTest; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.ibmdev.reference.IBMDeveloperCloudConstants; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * Tests behavior of {@code LocationHandler} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ibmdev.LocationHandlerTest") +public class LocationHandlerTest extends BaseHandlerTest { + + public void testApplyInputStream() { + InputStream is = getClass().getResourceAsStream("/location.xml"); + + Location result = factory.create(injector.getInstance(LocationHandler.class)).parse(is); + + Map> capabilites = ImmutableMap.> of( + IBMDeveloperCloudConstants.CAPABILITY_CAPACITY, ImmutableMap. of( + "SMALL", "50", "MEDIUM", "100", "LARGE", "200"), + IBMDeveloperCloudConstants.CAPABILITY_FORMAT, ImmutableMap. of( + "EXT3", "ext3"), IBMDeveloperCloudConstants.CAPABILITY_I386, + ImmutableMap. of("SMALL", "SMALL", "MEDIUM", "MEDIUM", "LARGE", + "LARGE"), IBMDeveloperCloudConstants.CAPABILITY_x86_64, ImmutableMap + . of( + + )); + Location expects = new Location(1, "US North East: Poughkeepsie, NY", null, "POK", + capabilites); + assertEquals(result, expects); + } +} diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationsHandlerTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationsHandlerTest.java new file mode 100644 index 0000000000..70004c836e --- /dev/null +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/xml/LocationsHandlerTest.java @@ -0,0 +1,102 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.ibmdev.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.util.Map; +import java.util.Set; + +import org.jclouds.http.functions.BaseHandlerTest; +import org.jclouds.ibmdev.domain.Location; +import org.jclouds.ibmdev.reference.IBMDeveloperCloudConstants; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; + +/** + * Tests behavior of {@code LocationsHandler} + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ibmdev.LocationsHandlerTest") +public class LocationsHandlerTest extends BaseHandlerTest { + public void testLocation() { + InputStream is = getClass().getResourceAsStream("/location.xml"); + + Set result = factory.create(injector.getInstance(LocationsHandler.class)) + .parse(is); + + Map> capabilites = ImmutableMap.> of( + IBMDeveloperCloudConstants.CAPABILITY_CAPACITY, ImmutableMap. of( + "SMALL", "50", "MEDIUM", "100", "LARGE", "200"), + IBMDeveloperCloudConstants.CAPABILITY_FORMAT, ImmutableMap. of( + "EXT3", "ext3"), IBMDeveloperCloudConstants.CAPABILITY_I386, + ImmutableMap. of("SMALL", "SMALL", "MEDIUM", "MEDIUM", "LARGE", + "LARGE"), IBMDeveloperCloudConstants.CAPABILITY_x86_64, ImmutableMap + . of( + + )); + Location location1 = new Location(1, "US North East: Poughkeepsie, NY", null, "POK", + capabilites); + + assertEquals(result, ImmutableSet.of(location1)); + + } + + @Test(enabled = false) + public void testAllLocations() { + InputStream is = getClass().getResourceAsStream("/locations.xml"); + + Set result = factory.create(injector.getInstance(LocationsHandler.class)) + .parse(is); + + Map> capabilites = ImmutableMap.> of( + IBMDeveloperCloudConstants.CAPABILITY_CAPACITY, ImmutableMap. of( + "SMALL", "50", "MEDIUM", "100", "LARGE", "200"), + IBMDeveloperCloudConstants.CAPABILITY_FORMAT, ImmutableMap. of( + "EXT3", "ext3"), IBMDeveloperCloudConstants.CAPABILITY_I386, + ImmutableMap. of("SMALL", "SMALL", "MEDIUM", "MEDIUM", "LARGE", + "LARGE"), IBMDeveloperCloudConstants.CAPABILITY_x86_64, ImmutableMap + . of( + + )); + Location location1 = new Location(1, "US North East: Poughkeepsie, NY", null, "POK", + capabilites); + + capabilites = ImmutableMap.> of( + IBMDeveloperCloudConstants.CAPABILITY_CAPACITY, ImmutableMap. of( + "SMALL", "50", "MEDIUM", "100", "LARGE", "200"), + IBMDeveloperCloudConstants.CAPABILITY_FORMAT, ImmutableMap. of( + "EXT3", "ext3"), IBMDeveloperCloudConstants.CAPABILITY_I386, + ImmutableMap. of("SMALL", "SMALL", "MEDIUM", "MEDIUM", "LARGE", + "LARGE"), IBMDeveloperCloudConstants.CAPABILITY_x86_64, ImmutableMap + . of( + + )); + Location location2 = new Location(1, "US North East: Poughkeepsie, NY", null, "POK", + capabilites); + + assertEquals(result, ImmutableSet.of(location1, location2)); + + } + +} diff --git a/ibmdev/src/test/resources/address.json b/ibmdev/src/test/resources/address.json new file mode 100644 index 0000000000..3cbae65dc9 --- /dev/null +++ b/ibmdev/src/test/resources/address.json @@ -0,0 +1,7 @@ +{ +"state":2, +"location":"1", +"ip": "129.33.196.243", +"id":"1217", +"instanceId":"1" +} \ No newline at end of file diff --git a/ibmdev/src/test/resources/addresses.json b/ibmdev/src/test/resources/addresses.json new file mode 100644 index 0000000000..e23b8ee5cd --- /dev/null +++ b/ibmdev/src/test/resources/addresses.json @@ -0,0 +1,16 @@ +{"addresses": [ +{ +"state":2, +"location":"1", +"ip": "129.33.196.243", +"id":"1217", +"instanceId":"1" +}, +{ +"state":3, +"location":"2", +"ip": "129.33.196.244", +"id":"1218" +} +] +} \ No newline at end of file diff --git a/ibmdev/src/test/resources/key.json b/ibmdev/src/test/resources/key.json new file mode 100644 index 0000000000..e6dfefc3f2 --- /dev/null +++ b/ibmdev/src/test/resources/key.json @@ -0,0 +1,7 @@ +{ +"default":true, +"instanceIds":["1"], +"keyMaterial":"AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+...", +"keyName":"DEFAULT", +"lastModifiedTime":1260428507510 +} \ No newline at end of file diff --git a/ibmdev/src/test/resources/keys.json b/ibmdev/src/test/resources/keys.json new file mode 100644 index 0000000000..bd119d618d --- /dev/null +++ b/ibmdev/src/test/resources/keys.json @@ -0,0 +1,18 @@ +{ +"keys":[ +{ +"default":true, +"instanceIds":["1"], +"keyMaterial":"AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+...", +"keyName":"DEFAULT", +"lastModifiedTime":1260428507510 +}, +{ +"default":false, +"instanceIds":[], +"keyMaterial":"AAAB3NzaC1yc2EAAAADAQABAAABAQCqBw7a+", +"keyName":"BEAR", +"lastModifiedTime":1260428507511 +} +] +} \ No newline at end of file diff --git a/ibmdev/src/test/resources/location.xml b/ibmdev/src/test/resources/location.xml new file mode 100644 index 0000000000..ffc57fb207 --- /dev/null +++ b/ibmdev/src/test/resources/location.xml @@ -0,0 +1,39 @@ + + + 1 + US North East: Poughkeepsie, NY + + POK + + + + 50 + + + 100 + + + 200 + + + + + ext3 + + + + + SMALL + + + MEDIUM + + + LARGE + + + + + + \ No newline at end of file diff --git a/ibmdev/src/test/resources/volume.json b/ibmdev/src/test/resources/volume.json new file mode 100644 index 0000000000..b68b52078b --- /dev/null +++ b/ibmdev/src/test/resources/volume.json @@ -0,0 +1,12 @@ +{ +"instanceId":"2", +"state":5, +"size":"50", +"owner":"aadelucc@us.ibm.com", +"createdTime":1260469075119, +"location":"1", +"productCodes":[], +"format":"ext3", +"name":"New Storage", +"id":"67" +} \ No newline at end of file diff --git a/ibmdev/src/test/resources/volumes.json b/ibmdev/src/test/resources/volumes.json new file mode 100644 index 0000000000..83a40a093e --- /dev/null +++ b/ibmdev/src/test/resources/volumes.json @@ -0,0 +1,26 @@ +{"volumes": [ +{ +"instanceId":"2", +"state":5, +"size":"50", +"owner":"aadelucc@us.ibm.com", +"createdTime":1260469075119, +"location":"1", +"productCodes":[], +"format":"ext3", +"name":"New Storage", +"id":"67" +}, +{ +"state":6, +"size":"51", +"owner":"aadelucc@us.ibm.com", +"createdTime":1260469075120, +"location":"2", +"productCodes":["abrad"], +"format":"ext3", +"name":"New Storage1", +"id":"68" +} + +]} \ No newline at end of file