From 6abc2110c5cbcedfcc16ec31c379fb0eb69ba951 Mon Sep 17 00:00:00 2001 From: Alex Yarmula Date: Sat, 27 Feb 2010 15:32:41 -0800 Subject: [PATCH] added 2 more methods for managing servers --- .../internal/RestAnnotationProcessor.java | 23 ++++--- .../gogrid/binders/BindIdsToQueryParams.java | 44 +++++++++++++ .../binders/BindNamesToQueryParams.java | 46 ++++++++++++++ .../services/GridServerAsyncClient.java | 19 ++++-- .../gogrid/services/GridServerClient.java | 16 +++-- .../org/jclouds/gogrid/util/GoGridUtils.java | 7 +++ .../GridServerAsyncClientTest.java} | 61 ++++++++++++++++++- .../GridServerClientLiveTest.java} | 18 +++++- 8 files changed, 212 insertions(+), 22 deletions(-) create mode 100644 gogrid/src/main/java/org/jclouds/gogrid/binders/BindIdsToQueryParams.java create mode 100644 gogrid/src/main/java/org/jclouds/gogrid/binders/BindNamesToQueryParams.java create mode 100644 gogrid/src/main/java/org/jclouds/gogrid/util/GoGridUtils.java rename gogrid/src/test/java/org/jclouds/gogrid/{GoGridAsyncClientTest.java => services/GridServerAsyncClientTest.java} (66%) rename gogrid/src/test/java/org/jclouds/gogrid/{GoGridClientLiveTest.java => services/GridServerClientLiveTest.java} (78%) diff --git a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java index ac423026a5..cd2fa3ea75 100755 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -435,22 +435,27 @@ public class RestAnnotationProcessor { Multimap map = LinkedListMultimap.create(); if (in == null) { } else if (in.indexOf('&') == -1) { - map.put(in, null); + if(in.contains("=")) parseKeyValueFromStringToMap(in, map); + else map.put(in, null); } else { String[] parts = HttpUtils.urlDecode(in).split("&"); - for (int partIndex = 0; partIndex < parts.length; partIndex++) { - // note that '=' can be a valid part of the value - int indexOfFirstEquals = parts[partIndex].indexOf('='); - String key = indexOfFirstEquals == -1 ? parts[partIndex] : parts[partIndex].substring( - 0, indexOfFirstEquals); - String value = indexOfFirstEquals == -1 ? null : parts[partIndex] - .substring(indexOfFirstEquals + 1); - map.put(key, value); + for(String part : parts) { + parseKeyValueFromStringToMap(part, map); } } return map; } + public static void parseKeyValueFromStringToMap(String stringToParse, Multimap map) { + // note that '=' can be a valid part of the value + int indexOfFirstEquals = stringToParse.indexOf('='); + String key = indexOfFirstEquals == -1 ? stringToParse : stringToParse.substring( + 0, indexOfFirstEquals); + String value = indexOfFirstEquals == -1 ? null : stringToParse + .substring(indexOfFirstEquals + 1); + map.put(key, value); + } + public static SortedSet> sortEntries( Collection> in, Comparator> sorter) { SortedSet> entries = Sets.newTreeSet(sorter); diff --git a/gogrid/src/main/java/org/jclouds/gogrid/binders/BindIdsToQueryParams.java b/gogrid/src/main/java/org/jclouds/gogrid/binders/BindIdsToQueryParams.java new file mode 100644 index 0000000000..cc86e9ede5 --- /dev/null +++ b/gogrid/src/main/java/org/jclouds/gogrid/binders/BindIdsToQueryParams.java @@ -0,0 +1,44 @@ +package org.jclouds.gogrid.binders; + +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; +import org.jclouds.rest.internal.GeneratedHttpRequest; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Oleksiy Yarmula + */ +public class BindIdsToQueryParams implements Binder { + + /** + * Binds the ids to query parameters. The pattern, as + * specified by GoGrid's specification, is: + * + * https://api.gogrid.com/api/grid/server/get + * ?id=5153 + * &id=3232 + * + * @param request + * request where the query params will be set + * @param input array of String params + */ + @Override + public void bindToRequest(HttpRequest request, Object input) { + checkArgument(checkNotNull(request, "request is null") instanceof GeneratedHttpRequest, + "this binder is only valid for GeneratedHttpRequests!"); + checkArgument(checkNotNull(input, "input is null") instanceof Long[], + "this binder is only valid for Long[] arguments"); + + Long[] names = (Long[]) input; + GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; + + for(Long id : names) { + generatedRequest.addQueryParam("id", checkNotNull(id.toString(), + /*or throw*/ "id must have a non-null value")); + } + + } + +} diff --git a/gogrid/src/main/java/org/jclouds/gogrid/binders/BindNamesToQueryParams.java b/gogrid/src/main/java/org/jclouds/gogrid/binders/BindNamesToQueryParams.java new file mode 100644 index 0000000000..bf619c08c1 --- /dev/null +++ b/gogrid/src/main/java/org/jclouds/gogrid/binders/BindNamesToQueryParams.java @@ -0,0 +1,46 @@ +package org.jclouds.gogrid.binders; + +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; +import org.jclouds.rest.internal.GeneratedHttpRequest; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Oleksiy Yarmula + */ +public class BindNamesToQueryParams implements Binder { + + /** + * Binds the names to query parameters. The pattern, as + * specified by GoGrid's specification, is: + * + * https://api.gogrid.com/api/grid/server/get + * ?name=My+Server + * &name=My+Server+2 + * &name=My+Server+3 + * &name=My+Server+4 + * @param request + * request where the query params will be set + * @param input array of String params + */ + @Override + public void bindToRequest(HttpRequest request, Object input) { + checkArgument(checkNotNull(request, "request is null") instanceof GeneratedHttpRequest, + "this binder is only valid for GeneratedHttpRequests!"); + checkArgument(checkNotNull(input, "input is null") instanceof String[], + "this binder is only valid for String[] arguments"); + + String[] names = (String[]) input; + GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; + + for(String name : names) { + generatedRequest.addQueryParam("name", name); + } + + } + + + +} diff --git a/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java b/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java index b84414d667..88a6d03aa3 100644 --- a/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java +++ b/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerAsyncClient.java @@ -47,6 +47,8 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import org.jclouds.gogrid.GoGrid; +import org.jclouds.gogrid.binders.BindIdsToQueryParams; +import org.jclouds.gogrid.binders.BindNamesToQueryParams; import org.jclouds.gogrid.domain.Server; import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication; import org.jclouds.gogrid.functions.ParseServerListFromJsonResponse; @@ -70,9 +72,18 @@ import static org.jclouds.gogrid.reference.GoGridHeaders.VERSION; @QueryParams(keys = VERSION, values = "1.3") public interface GridServerAsyncClient { - @GET - @ResponseParser(ParseServerListFromJsonResponse.class) - @Path("/grid/server/list") - ListenableFuture> getServerList(); + @GET + @ResponseParser(ParseServerListFromJsonResponse.class) + @Path("/grid/server/list") + ListenableFuture> getServerList(); + @GET + @ResponseParser(ParseServerListFromJsonResponse.class) + @Path("/grid/server/get") + ListenableFuture> getServersByName(@BinderParam(BindNamesToQueryParams.class) String... names); + + @GET + @ResponseParser(ParseServerListFromJsonResponse.class) + @Path("/grid/server/get") + ListenableFuture> getServersById(@BinderParam(BindIdsToQueryParams.class) Long... ids); } diff --git a/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerClient.java b/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerClient.java index 4f7fda0768..8510665daa 100644 --- a/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerClient.java +++ b/gogrid/src/main/java/org/jclouds/gogrid/services/GridServerClient.java @@ -44,17 +44,25 @@ public interface GridServerClient { Set getServerList(); - //Set getServersByName(String... names); + /** + * Retrieves the server(s) by unique name(s). + * + * Given a name or a set of names, finds one or + * multiple servers. + * @param names to get the servers + * @return server(s) matching the name(s) + */ + Set getServersByName(String... names); /** * Retrieves the server(s) by unique id(s). * * Given an id or a set of ids, finds one or * multiple servers. - * @param id - * @return + * @param ids to get the servers + * @return server(s) matching the ids */ - // Set getServersById(Long... ids); + Set getServersById(Long... ids); } diff --git a/gogrid/src/main/java/org/jclouds/gogrid/util/GoGridUtils.java b/gogrid/src/main/java/org/jclouds/gogrid/util/GoGridUtils.java new file mode 100644 index 0000000000..055151a6e5 --- /dev/null +++ b/gogrid/src/main/java/org/jclouds/gogrid/util/GoGridUtils.java @@ -0,0 +1,7 @@ +package org.jclouds.gogrid.util; + +/** + * @author Oleksiy Yarmula + */ +public class GoGridUtils { +} diff --git a/gogrid/src/test/java/org/jclouds/gogrid/GoGridAsyncClientTest.java b/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java similarity index 66% rename from gogrid/src/test/java/org/jclouds/gogrid/GoGridAsyncClientTest.java rename to gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java index a90dae8ccf..83ef8dc1d2 100644 --- a/gogrid/src/test/java/org/jclouds/gogrid/GoGridAsyncClientTest.java +++ b/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerAsyncClientTest.java @@ -39,7 +39,7 @@ * under the License. * ==================================================================== */ -package org.jclouds.gogrid; +package org.jclouds.gogrid.services; import static org.testng.Assert.assertEquals; @@ -51,6 +51,7 @@ import java.net.URI; import javax.inject.Singleton; import com.google.common.collect.Iterables; +import org.jclouds.gogrid.GoGrid; import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication; import org.jclouds.gogrid.services.GridServerAsyncClient; import org.jclouds.logging.Logger; @@ -74,10 +75,10 @@ import com.google.inject.TypeLiteral; * @author Oleksiy Yarmula */ @Test(groups = "unit", testName = "gogrid.GoGridAsyncClientTest") -public class GoGridAsyncClientTest extends RestClientTest { +public class GridServerAsyncClientTest extends RestClientTest { @Test - public void testGetServerList() throws SecurityException, NoSuchMethodException, IOException { + public void testGetServerList() throws NoSuchMethodException, IOException { Method method = GridServerAsyncClient.class.getMethod("getServerList"); GeneratedHttpRequest httpRequest = processor.createRequest(method); @@ -100,6 +101,60 @@ public class GoGridAsyncClientTest extends RestClientTest assertPayloadEquals(httpRequest, null); } + + @Test + public void testGetServersByName() throws NoSuchMethodException, IOException { + Method method = GridServerAsyncClient.class.getMethod("getServersByName", String[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, "server1"); + + assertRequestLineEquals(httpRequest, + "GET https://api.gogrid.com/api/grid/server/get?v=1.3&name=server1 HTTP/1.1"); + assertHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); + + assertRequestLineEquals(httpRequest, + "GET https://api.gogrid.com/api/grid/server/get?" + + "v=1.3&name=server1&" + + "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + + "HTTP/1.1"); + assertHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, null); + } + + + @Test + public void testGetServersById() throws NoSuchMethodException, IOException { + Method method = GridServerAsyncClient.class.getMethod("getServersById", Long[].class); + GeneratedHttpRequest httpRequest = processor.createRequest(method, 123L); + + assertRequestLineEquals(httpRequest, + "GET https://api.gogrid.com/api/grid/server/get?v=1.3&id=123 HTTP/1.1"); + assertHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, null); + + assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, null); + + checkFilters(httpRequest); + Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest); + + assertRequestLineEquals(httpRequest, + "GET https://api.gogrid.com/api/grid/server/get?" + + "v=1.3&id=123&" + + "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + + "HTTP/1.1"); + assertHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, null); + } + @Override protected void checkFilters(GeneratedHttpRequest httpMethod) { assertEquals(httpMethod.getFilters().size(), 1); diff --git a/gogrid/src/test/java/org/jclouds/gogrid/GoGridClientLiveTest.java b/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerClientLiveTest.java similarity index 78% rename from gogrid/src/test/java/org/jclouds/gogrid/GoGridClientLiveTest.java rename to gogrid/src/test/java/org/jclouds/gogrid/services/GridServerClientLiveTest.java index 5731038357..f4ac5017dc 100644 --- a/gogrid/src/test/java/org/jclouds/gogrid/GoGridClientLiveTest.java +++ b/gogrid/src/test/java/org/jclouds/gogrid/services/GridServerClientLiveTest.java @@ -21,10 +21,12 @@ * under the License. * ==================================================================== */ -package org.jclouds.gogrid; +package org.jclouds.gogrid.services; import static com.google.common.base.Preconditions.checkNotNull; +import org.jclouds.gogrid.GoGridClient; +import org.jclouds.gogrid.GoGridContextFactory; import org.jclouds.gogrid.domain.Server; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.testng.annotations.BeforeGroups; @@ -38,7 +40,7 @@ import java.util.Set; * @author Adrian Cole */ @Test(groups = "live", testName = "gogrid.GoGridClientLiveTest") -public class GoGridClientLiveTest { +public class GridServerClientLiveTest { private GoGridClient client; @@ -57,4 +59,16 @@ public class GoGridClientLiveTest { assert (response.size() > 0); } + @Test + public void testGetServerByName() { + Set response = client.getServerClient().getServersByName("PowerServer", "AnotherTestServer"); + assert (response.size() > 0); + } + + @Test + public void testGetServerById() { + Set response = client.getServerClient().getServersById(75245L, 75523L); + assert (response.size() > 0); + } + }