added 2 more methods for managing servers

This commit is contained in:
Alex Yarmula 2010-02-27 15:32:41 -08:00
parent 2b41b83f0c
commit 6abc2110c5
8 changed files with 212 additions and 22 deletions

View File

@ -435,22 +435,27 @@ public class RestAnnotationProcessor<T> {
Multimap<String, String> 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<String, String> 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<Entry<String, String>> sortEntries(
Collection<Map.Entry<String, String>> in, Comparator<Map.Entry<String, String>> sorter) {
SortedSet<Entry<String, String>> entries = Sets.newTreeSet(sorter);

View File

@ -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"));
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;
@ -75,4 +77,13 @@ public interface GridServerAsyncClient {
@Path("/grid/server/list")
ListenableFuture<Set<Server>> getServerList();
@GET
@ResponseParser(ParseServerListFromJsonResponse.class)
@Path("/grid/server/get")
ListenableFuture<Set<Server>> getServersByName(@BinderParam(BindNamesToQueryParams.class) String... names);
@GET
@ResponseParser(ParseServerListFromJsonResponse.class)
@Path("/grid/server/get")
ListenableFuture<Set<Server>> getServersById(@BinderParam(BindIdsToQueryParams.class) Long... ids);
}

View File

@ -44,17 +44,25 @@ public interface GridServerClient {
Set<Server> getServerList();
//Set<Server> 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<Server> 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<Server> getServersById(Long... ids);
Set<Server> getServersById(Long... ids);
}

View File

@ -0,0 +1,7 @@
package org.jclouds.gogrid.util;
/**
* @author Oleksiy Yarmula
*/
public class GoGridUtils {
}

View File

@ -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<GridServerAsyncClient> {
public class GridServerAsyncClientTest extends RestClientTest<GridServerAsyncClient> {
@Test
public void testGetServerList() throws SecurityException, NoSuchMethodException, IOException {
public void testGetServerList() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("getServerList");
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method);
@ -100,6 +101,60 @@ public class GoGridAsyncClientTest extends RestClientTest<GridServerAsyncClient>
assertPayloadEquals(httpRequest, null);
}
@Test
public void testGetServersByName() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("getServersByName", String[].class);
GeneratedHttpRequest<GridServerAsyncClient> 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<GridServerAsyncClient> 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<GridServerAsyncClient> httpMethod) {
assertEquals(httpMethod.getFilters().size(), 1);

View File

@ -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<Server> response = client.getServerClient().getServersByName("PowerServer", "AnotherTestServer");
assert (response.size() > 0);
}
@Test
public void testGetServerById() {
Set<Server> response = client.getServerClient().getServersById(75245L, 75523L);
assert (response.size() > 0);
}
}