added get credentials function to gogrid

This commit is contained in:
Adrian Cole 2010-07-13 17:59:22 -05:00
parent 4098c4bd0a
commit 541ccdae51
20 changed files with 804 additions and 356 deletions

View File

@ -21,64 +21,66 @@
* under the License.
* ====================================================================
*/
package org.jclouds.gogrid.domain.internal;
package org.jclouds.gogrid.functions;
import java.util.SortedSet;
/**
* General format of GoGrid's response.
*
* This is the wrapper for most responses, and the actual
* result (or error) will be set to {@link #list}.
* Note that even the single returned item will be set to
* {@link #list} per GoGrid's design.
*
*
* This is the wrapper for most responses, and the actual result (or error) will
* be set to {@link #list}. Note that even the single returned item will be set
* to {@link #list} per GoGrid's design.
*
* This class is not intended to be used by customers directly, it is here to
* assist in deserialization.
*
* @author Oleksiy Yarmula
*/
public class GenericResponseContainer<T> {
class GenericResponseContainer<T> {
private Summary summary;
private String status;
private String method;
private SortedSet<T> list;
private Summary summary;
private String status;
private String method;
private SortedSet<T> list;
public Summary getSummary() {
return summary;
}
public Summary getSummary() {
return summary;
}
public String getStatus() {
return status;
}
public String getStatus() {
return status;
}
public String getMethod() {
return method;
}
public String getMethod() {
return method;
}
public SortedSet<T> getList() {
return list;
}
public SortedSet<T> getList() {
return list;
}
static class Summary {
private int total;
private int start;
private int numPages;
private int returned;
static class Summary {
private int total;
private int start;
private int numPages;
private int returned;
public int getTotal() {
return total;
}
public int getTotal() {
return total;
}
public int getStart() {
return start;
}
public int getStart() {
return start;
}
public int getNumPages() {
return numPages;
}
public int getNumPages() {
return numPages;
}
public int getReturned() {
return returned;
}
}
public int getReturned() {
return returned;
}
}
}

View File

@ -0,0 +1,63 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.gogrid.functions;
import static com.google.common.base.Preconditions.checkState;
import static com.google.inject.internal.Iterables.getOnlyElement;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpResponse;
import com.google.common.base.Function;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ParseCredentialsFromJsonResponse implements
Function<HttpResponse, Credentials> {
private final ParseServerNameToCredentialsMapFromJsonResponse parser;
@Inject
ParseCredentialsFromJsonResponse(
ParseServerNameToCredentialsMapFromJsonResponse parser) {
this.parser = parser;
}
@Override
public Credentials apply(HttpResponse input) {
Map<String, Credentials> returnVal = parser.apply(input);
checkState(!(returnVal.size() > 1),
"expecting only 1 credential in response, but had more: "
+ returnVal.keySet());
return (returnVal.size() > 0) ? getOnlyElement(returnVal.values()) : null;
}
}

View File

@ -32,7 +32,6 @@ import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.internal.ErrorResponse;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
@ -40,18 +39,19 @@ import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* Parses {@link org.jclouds.gogrid.domain.internal.ErrorResponse error response} from a json
* string.
* Parses {@link org.jclouds.gogrid.domain.internal.ErrorResponse error
* response} from a json string.
*
* GoGrid may return multiple error objects, if multiple errors were found.
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseErrorFromJsonResponse extends ParseJson<SortedSet<ErrorResponse>> {
public class ParseErrorFromJsonResponse extends
ParseJson<SortedSet<ErrorResponse>> {
@Inject
public ParseErrorFromJsonResponse(Gson gson) {
ParseErrorFromJsonResponse(Gson gson) {
super(gson);
}
@ -60,7 +60,8 @@ public class ParseErrorFromJsonResponse extends ParseJson<SortedSet<ErrorRespons
}.getType();
GenericResponseContainer<ErrorResponse> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}

View File

@ -18,28 +18,32 @@
*/
package org.jclouds.gogrid.functions;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import java.io.InputStream;
import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.ServerImage;
import org.jclouds.http.functions.ParseJson;
import javax.inject.Inject;
import java.io.InputStream;
import java.util.SortedSet;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.inject.Singleton;
/**
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseImageFromJsonResponse extends ParseJson<ServerImage> {
@Inject
public ParseImageFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseImageFromJsonResponse(Gson gson) {
super(gson);
}
public ServerImage apply(InputStream stream) {
SortedSet<ServerImage> allImages =
new ParseImageListFromJsonResponse(gson).apply(stream);
return Iterables.getOnlyElement(allImages);
}
public ServerImage apply(InputStream stream) {
SortedSet<ServerImage> allImages = new ParseImageListFromJsonResponse(
gson).apply(stream);
return Iterables.getOnlyElement(allImages);
}
}

View File

@ -18,38 +18,43 @@
*/
package org.jclouds.gogrid.functions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jclouds.gogrid.domain.ServerImage;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import javax.inject.Inject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.ServerImage;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* @author Oleksiy Yarmula
*/
public class ParseImageListFromJsonResponse extends ParseJson<SortedSet<ServerImage>> {
@Singleton
public class ParseImageListFromJsonResponse extends
ParseJson<SortedSet<ServerImage>> {
@Inject
public ParseImageListFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseImageListFromJsonResponse(Gson gson) {
super(gson);
}
public SortedSet<ServerImage> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<ServerImage>>() {
}.getType();
GenericResponseContainer<ServerImage> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
public SortedSet<ServerImage> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<ServerImage>>() {
}.getType();
GenericResponseContainer<ServerImage> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
}

View File

@ -18,40 +18,44 @@
*/
package org.jclouds.gogrid.functions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jclouds.gogrid.domain.Ip;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import javax.inject.Inject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.Ip;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* Parses {@link org.jclouds.gogrid.domain.Ip ips} from a json string.
*
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseIpListFromJsonResponse extends ParseJson<SortedSet<Ip>> {
@Inject
public ParseIpListFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseIpListFromJsonResponse(Gson gson) {
super(gson);
}
public SortedSet<Ip> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<Ip>>() {
}.getType();
GenericResponseContainer<Ip> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
public SortedSet<Ip> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<Ip>>() {
}.getType();
GenericResponseContainer<Ip> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
}

View File

@ -18,40 +18,44 @@
*/
package org.jclouds.gogrid.functions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jclouds.gogrid.domain.Job;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import javax.inject.Inject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.Job;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* Parses {@link org.jclouds.gogrid.domain.Job jobs} from a json string.
*
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseJobListFromJsonResponse extends ParseJson<SortedSet<Job>> {
@Inject
public ParseJobListFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseJobListFromJsonResponse(Gson gson) {
super(gson);
}
public SortedSet<Job> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<Job>>() {
}.getType();
GenericResponseContainer<Job> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
public SortedSet<Job> apply(InputStream stream) {
Type setType = new TypeToken<GenericResponseContainer<Job>>() {
}.getType();
GenericResponseContainer<Job> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
return response.getList();
}
}

View File

@ -20,6 +20,8 @@ package org.jclouds.gogrid.functions;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.inject.Singleton;
import org.jclouds.gogrid.domain.LoadBalancer;
import org.jclouds.http.functions.ParseJson;
@ -29,22 +31,24 @@ import java.util.SortedSet;
/**
* Parses the single load balancer out of the response.
*
* This class delegates parsing to {@link ParseLoadBalancerListFromJsonResponse}.
*
*
* This class delegates parsing to {@link ParseLoadBalancerListFromJsonResponse}
* .
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseLoadBalancerFromJsonResponse extends ParseJson<LoadBalancer> {
@Inject
public ParseLoadBalancerFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseLoadBalancerFromJsonResponse(Gson gson) {
super(gson);
}
public LoadBalancer apply(InputStream stream) {
SortedSet<LoadBalancer> allLoadBalancers =
new ParseLoadBalancerListFromJsonResponse(gson).apply(stream);
return Iterables.getOnlyElement(allLoadBalancers);
}
public LoadBalancer apply(InputStream stream) {
SortedSet<LoadBalancer> allLoadBalancers = new ParseLoadBalancerListFromJsonResponse(
gson).apply(stream);
return Iterables.getOnlyElement(allLoadBalancers);
}
}

View File

@ -27,21 +27,24 @@ import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.LoadBalancer;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* Parses {@link org.jclouds.gogrid.domain.LoadBalancer jobs} from a json string.
* Parses {@link org.jclouds.gogrid.domain.LoadBalancer jobs} from a json
* string.
*
* @author Oleksiy Yarmula
*/
public class ParseLoadBalancerListFromJsonResponse extends ParseJson<SortedSet<LoadBalancer>> {
@Singleton
public class ParseLoadBalancerListFromJsonResponse extends
ParseJson<SortedSet<LoadBalancer>> {
@Inject
public ParseLoadBalancerListFromJsonResponse(Gson gson) {
ParseLoadBalancerListFromJsonResponse(Gson gson) {
super(gson);
}
@ -50,7 +53,8 @@ public class ParseLoadBalancerListFromJsonResponse extends ParseJson<SortedSet<L
}.getType();
GenericResponseContainer<LoadBalancer> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}

View File

@ -27,11 +27,11 @@ import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.Option;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
/**
* Parses the list of generic options.
@ -40,10 +40,11 @@ import com.google.gson.reflect.TypeToken;
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseOptionsFromJsonResponse extends ParseJson<SortedSet<Option>> {
@Inject
public ParseOptionsFromJsonResponse(Gson gson) {
ParseOptionsFromJsonResponse(Gson gson) {
super(gson);
}
@ -52,7 +53,8 @@ public class ParseOptionsFromJsonResponse extends ParseJson<SortedSet<Option>> {
}.getType();
GenericResponseContainer<Option> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}

View File

@ -23,31 +23,36 @@
*/
package org.jclouds.gogrid.functions;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.http.functions.ParseJson;
import javax.inject.Inject;
import java.io.InputStream;
import java.util.SortedSet;
import javax.inject.Inject;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.http.functions.ParseJson;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.inject.Singleton;
/**
* Parses a single {@link Server} from a json string.
*
*
* This class delegates parsing to {@link ParseServerListFromJsonResponse}.
*
*
* @author Oleksiy Yarmula
*/
@Singleton
public class ParseServerFromJsonResponse extends ParseJson<Server> {
@Inject
public ParseServerFromJsonResponse(Gson gson) {
super(gson);
}
@Inject
ParseServerFromJsonResponse(Gson gson) {
super(gson);
}
public Server apply(InputStream stream) {
SortedSet<Server> allServers = new ParseServerListFromJsonResponse(gson).apply(stream);
return Iterables.getOnlyElement(allServers);
}
public Server apply(InputStream stream) {
SortedSet<Server> allServers = new ParseServerListFromJsonResponse(gson)
.apply(stream);
return Iterables.getOnlyElement(allServers);
}
}

View File

@ -33,7 +33,6 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import com.google.gson.Gson;
@ -45,10 +44,11 @@ import com.google.gson.reflect.TypeToken;
* @author Adrian Cole
*/
@Singleton
public class ParseServerListFromJsonResponse extends ParseJson<SortedSet<Server>> {
public class ParseServerListFromJsonResponse extends
ParseJson<SortedSet<Server>> {
@Inject
public ParseServerListFromJsonResponse(Gson gson) {
ParseServerListFromJsonResponse(Gson gson) {
super(gson);
}
@ -57,7 +57,8 @@ public class ParseServerListFromJsonResponse extends ParseJson<SortedSet<Server>
}.getType();
GenericResponseContainer<Server> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}

View File

@ -29,7 +29,6 @@ import javax.inject.Singleton;
import org.jclouds.domain.Credentials;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.domain.internal.GenericResponseContainer;
import org.jclouds.http.functions.ParseJson;
import com.google.common.collect.Maps;
@ -42,10 +41,10 @@ import com.google.gson.reflect.TypeToken;
*/
@Singleton
public class ParseServerNameToCredentialsMapFromJsonResponse extends
ParseJson<Map<String, Credentials>> {
ParseJson<Map<String, Credentials>> {
@Inject
public ParseServerNameToCredentialsMapFromJsonResponse(Gson gson) {
ParseServerNameToCredentialsMapFromJsonResponse(Gson gson) {
super(gson);
}
@ -54,19 +53,22 @@ public class ParseServerNameToCredentialsMapFromJsonResponse extends
}.getType();
GenericResponseContainer<Password> response;
try {
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"), setType);
response = gson.fromJson(new InputStreamReader(stream, "UTF-8"),
setType);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("jclouds requires UTF-8 encoding", e);
}
Map<String, Credentials> serverNameToCredentials = Maps.newHashMap();
for (Password password : response.getList()) {
serverNameToCredentials.put(password.getServer().getName(), new Credentials(password
.getUserName(), password.getPassword()));
serverNameToCredentials.put(password.getServer().getName(),
new Credentials(password.getUserName(), password.getPassword()));
}
return serverNameToCredentials;
}
public static class Password implements Comparable<Password> {
// incidental wrapper class to assist in getting the correct data
// deserialized from json
private static class Password implements Comparable<Password> {
@SerializedName("username")
private String userName;
private String password;
@ -93,11 +95,14 @@ public class ParseServerNameToCredentialsMapFromJsonResponse extends
Password password1 = (Password) o;
if (password != null ? !password.equals(password1.password) : password1.password != null)
if (password != null ? !password.equals(password1.password)
: password1.password != null)
return false;
if (server != null ? !server.equals(password1.server) : password1.server != null)
if (server != null ? !server.equals(password1.server)
: password1.server != null)
return false;
if (userName != null ? !userName.equals(password1.userName) : password1.userName != null)
if (userName != null ? !userName.equals(password1.userName)
: password1.userName != null)
return false;
return true;

View File

@ -48,6 +48,7 @@ import org.jclouds.gogrid.domain.Option;
import org.jclouds.gogrid.domain.PowerCommand;
import org.jclouds.gogrid.domain.Server;
import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication;
import org.jclouds.gogrid.functions.ParseCredentialsFromJsonResponse;
import org.jclouds.gogrid.functions.ParseOptionsFromJsonResponse;
import org.jclouds.gogrid.functions.ParseServerFromJsonResponse;
import org.jclouds.gogrid.functions.ParseServerListFromJsonResponse;
@ -112,6 +113,14 @@ public interface GridServerAsyncClient {
@ResponseParser(ParseServerNameToCredentialsMapFromJsonResponse.class)
@Path("/support/password/list")
ListenableFuture<Map<String, Credentials>> getServerCredentialsList();
/**
* @see GridServerClient#getServerCredentials
*/
@GET
@ResponseParser(ParseCredentialsFromJsonResponse.class)
@Path("/support/grid/password/get")
ListenableFuture<Credentials> getServerCredentials(@QueryParam("id") long id);
/**
* @see GridServerClient#addServer(String, String, String, String,
@ -130,8 +139,9 @@ public interface GridServerAsyncClient {
@GET
@ResponseParser(ParseServerFromJsonResponse.class)
@Path("/grid/server/power")
ListenableFuture<Server> power(@QueryParam(SERVER_ID_OR_NAME_KEY) String idOrName,
@QueryParam(POWER_KEY) PowerCommand power);
ListenableFuture<Server> power(
@QueryParam(SERVER_ID_OR_NAME_KEY) String idOrName,
@QueryParam(POWER_KEY) PowerCommand power);
/**
* @see GridServerClient#deleteById(Long)

View File

@ -88,6 +88,12 @@ public interface GridServerClient {
*/
Map<String, Credentials> getServerCredentialsList();
/**
*
* @return the login user and password of a server, or null if none found
*/
Credentials getServerCredentials(long id);
/**
* Adds a server with specified attributes
*
@ -104,7 +110,7 @@ public interface GridServerClient {
* @return created server
*/
Server addServer(String name, String image, String ram, String ip,
AddServerOptions... addServerOptions);
AddServerOptions... addServerOptions);
/**
* Changes the server's state according to {@link PowerCommand}
@ -129,8 +135,8 @@ public interface GridServerClient {
/**
* Deletes the server by name;
*
* NOTE: Using this parameter may generate an error if one or more servers share a non-unique
* name.
* NOTE: Using this parameter may generate an error if one or more servers
* share a non-unique name.
*
* @param name
* name of the server to be deleted
@ -140,20 +146,22 @@ public interface GridServerClient {
Server deleteByName(String name);
/**
* Retrieves the list of supported RAM configurations. The objects will have RAM ID, name and
* description. In most cases, id or name will be used for {@link #addServer}.
* Retrieves the list of supported RAM configurations. The objects will have
* RAM ID, name and description. In most cases, id or name will be used for
* {@link #addServer}.
*
* To see how RAM maps to CPU and disk space (as of March 2010), see
* {@link org.jclouds.gogrid.compute.config.GoGridComputeServiceContextModule#provideSizeToRam}.
* {@link org.jclouds.gogrid.compute.config.GoGridComputeServiceContextModule#provideSizeToRam}
* .
*
* @return supported ram sizes
*/
Set<Option> getRamSizes();
/**
* Retrieves the list of supported Datacenters to launch servers into. The objects will have
* datacenter ID, name and description. In most cases, id or name will be used for
* {@link #addServer}.
* Retrieves the list of supported Datacenters to launch servers into. The
* objects will have datacenter ID, name and description. In most cases, id
* or name will be used for {@link #addServer}.
*
* @return supported datacenters
*/

View File

@ -85,7 +85,8 @@ public class GoGridLiveTest {
private RetryablePredicate<Server> serverLatestJobCompleted;
private RetryablePredicate<LoadBalancer> loadBalancerLatestJobCompleted;
/**
* Keeps track of the servers, created during the tests, to remove them after all tests complete
* Keeps track of the servers, created during the tests, to remove them after
* all tests complete
*/
private List<String> serversToDeleteAfterTheTests = new ArrayList<String>();
private List<String> loadBalancersToDeleteAfterTest = new ArrayList<String>();
@ -94,31 +95,34 @@ public class GoGridLiveTest {
@BeforeGroups(groups = { "live" })
public void setupClient() {
String identity = checkNotNull(System.getProperty("jclouds.test.identity"),
"jclouds.test.identity");
String credential = checkNotNull(System.getProperty("jclouds.test.credential"),
"jclouds.test.credential");
String identity = checkNotNull(System
.getProperty("jclouds.test.identity"), "jclouds.test.identity");
String credential = checkNotNull(System
.getProperty("jclouds.test.credential"), "jclouds.test.credential");
context = new RestContextFactory().createContext("gogrid", identity, credential, ImmutableSet
.<Module> of(new Log4JLoggingModule()));
context = new RestContextFactory().createContext("gogrid", identity,
credential, ImmutableSet.<Module> of(new Log4JLoggingModule()));
client = context.getApi();
serverLatestJobCompleted = new RetryablePredicate<Server>(new ServerLatestJobCompleted(client
.getJobServices()), 800, 20, TimeUnit.SECONDS);
serverLatestJobCompleted = new RetryablePredicate<Server>(
new ServerLatestJobCompleted(client.getJobServices()), 800, 20,
TimeUnit.SECONDS);
loadBalancerLatestJobCompleted = new RetryablePredicate<LoadBalancer>(
new LoadBalancerLatestJobCompleted(client.getJobServices()), 800, 20,
TimeUnit.SECONDS);
new LoadBalancerLatestJobCompleted(client.getJobServices()), 800,
20, TimeUnit.SECONDS);
}
@Test(enabled = false)
@Test(enabled = true)
public void testDescriptionIs500Characters() {
final String nameOfServer = "Description" + String.valueOf(new Date().getTime()).substring(6);
final String nameOfServer = "Description"
+ String.valueOf(new Date().getTime()).substring(6);
serversToDeleteAfterTheTests.add(nameOfServer);
Set<Ip> availableIps = client.getIpServices().getUnassignedPublicIpList();
Ip availableIp = Iterables.getLast(availableIps);
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0).getName();
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0)
.getName();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 1 * 500; i++)
@ -127,60 +131,73 @@ public class GoGridLiveTest {
String description = builder.toString();
Server createdServer = client.getServerServices().addServer(nameOfServer,
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram, availableIp.getIp(),
new AddServerOptions().withDescription(description));
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram,
availableIp.getIp(),
new AddServerOptions().withDescription(description));
assertNotNull(createdServer);
assert serverLatestJobCompleted.apply(createdServer);
assertEquals(Iterables.getLast(client.getServerServices().getServersByName(nameOfServer))
.getDescription(), description);
assertEquals(Iterables.getLast(
client.getServerServices().getServersByName(nameOfServer))
.getDescription(), description);
}
/**
* Tests server start, reboot and deletion. Also verifies IP services and job services.
* Tests server start, reboot and deletion. Also verifies IP services and job
* services.
*/
@Test(enabled = false)
@Test(enabled = true)
public void testServerLifecycle() {
int serverCountBeforeTest = client.getServerServices().getServerList().size();
int serverCountBeforeTest = client.getServerServices().getServerList()
.size();
final String nameOfServer = "Server" + String.valueOf(new Date().getTime()).substring(6);
final String nameOfServer = "Server"
+ String.valueOf(new Date().getTime()).substring(6);
serversToDeleteAfterTheTests.add(nameOfServer);
Set<Ip> availableIps = client.getIpServices().getUnassignedPublicIpList();
Ip availableIp = Iterables.getLast(availableIps);
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0).getName();
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0)
.getName();
Server createdServer = client.getServerServices().addServer(nameOfServer,
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram, availableIp.getIp());
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram,
availableIp.getIp());
assertNotNull(createdServer);
assert serverLatestJobCompleted.apply(createdServer);
// get server by name
Set<Server> response = client.getServerServices().getServersByName(nameOfServer);
Set<Server> response = client.getServerServices().getServersByName(
nameOfServer);
assert (response.size() == 1);
// restart the server
client.getServerServices().power(nameOfServer, PowerCommand.RESTART);
Set<Job> jobs = client.getJobServices().getJobsForObjectName(nameOfServer);
assert ("RestartVirtualServer".equals(Iterables.getLast(jobs).getCommand().getName()));
Set<Job> jobs = client.getJobServices()
.getJobsForObjectName(nameOfServer);
assert ("RestartVirtualServer".equals(Iterables.getLast(jobs)
.getCommand().getName()));
assert serverLatestJobCompleted.apply(createdServer);
int serverCountAfterAddingOneServer = client.getServerServices().getServerList().size();
int serverCountAfterAddingOneServer = client.getServerServices()
.getServerList().size();
assert serverCountAfterAddingOneServer == serverCountBeforeTest + 1 : "There should be +1 increase in the number of servers since the test started";
// delete the server
client.getServerServices().deleteByName(nameOfServer);
jobs = client.getJobServices().getJobsForObjectName(nameOfServer);
assert ("DeleteVirtualServer".equals(Iterables.getLast(jobs).getCommand().getName()));
assert ("DeleteVirtualServer".equals(Iterables.getLast(jobs).getCommand()
.getName()));
assert serverLatestJobCompleted.apply(createdServer);
int serverCountAfterDeletingTheServer = client.getServerServices().getServerList().size();
int serverCountAfterDeletingTheServer = client.getServerServices()
.getServerList().size();
assert serverCountAfterDeletingTheServer == serverCountBeforeTest : "There should be the same # of servers as since the test started";
// make sure that IP is put back to "unassigned"
@ -188,34 +205,37 @@ public class GoGridLiveTest {
}
/**
* Starts a servers, verifies that jobs are created correctly and an be retrieved from the job
* services
* Starts a servers, verifies that jobs are created correctly and an be
* retrieved from the job services
*/
@Test(dependsOnMethods = "testServerLifecycle", enabled = false)
@Test(dependsOnMethods = "testServerLifecycle", enabled = true)
public void testJobs() {
final String nameOfServer = "Server" + String.valueOf(new Date().getTime()).substring(6);
final String nameOfServer = "Server"
+ String.valueOf(new Date().getTime()).substring(6);
serversToDeleteAfterTheTests.add(nameOfServer);
Set<Ip> availableIps = client.getIpServices().getUnassignedPublicIpList();
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0).getName();
String ram = Iterables.get(client.getServerServices().getRamSizes(), 0)
.getName();
Server createdServer = client.getServerServices().addServer(nameOfServer,
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram,
Iterables.getLast(availableIps).getIp());
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", ram,
Iterables.getLast(availableIps).getIp());
assert serverLatestJobCompleted.apply(createdServer);
// restart the server
client.getServerServices().power(nameOfServer, PowerCommand.RESTART);
Set<Job> jobs = client.getJobServices().getJobsForObjectName(nameOfServer);
Set<Job> jobs = client.getJobServices()
.getJobsForObjectName(nameOfServer);
Job latestJob = Iterables.getLast(jobs);
Long latestJobId = latestJob.getId();
Job latestJobFetched = Iterables.getOnlyElement(client.getJobServices().getJobsById(
latestJobId));
Job latestJobFetched = Iterables.getOnlyElement(client.getJobServices()
.getJobsById(latestJobId));
assert latestJob.equals(latestJobFetched) : "Job and its reprentation found by ID don't match";
@ -227,84 +247,96 @@ public class GoGridLiveTest {
Set<Job> jobsFetched = client.getJobServices().getJobsById(idsOfAllJobs);
assert jobsFetched.size() == jobs.size() : format(
"Number of jobs fetched by ids doesn't match the number of jobs "
+ "requested. Requested/expected: %d. Found: %d.", jobs.size(), jobsFetched
.size());
"Number of jobs fetched by ids doesn't match the number of jobs "
+ "requested. Requested/expected: %d. Found: %d.", jobs
.size(), jobsFetched.size());
// delete the server
client.getServerServices().deleteByName(nameOfServer);
}
/**
* Tests common load balancer operations. Also verifies IP services and job services.
* Tests common load balancer operations. Also verifies IP services and job
* services.
*/
@Test(enabled = false)
@Test(enabled = true)
public void testLoadBalancerLifecycle() {
int lbCountBeforeTest = client.getLoadBalancerServices().getLoadBalancerList().size();
int lbCountBeforeTest = client.getLoadBalancerServices()
.getLoadBalancerList().size();
final String nameOfLoadBalancer = "LoadBalancer"
+ String.valueOf(new Date().getTime()).substring(6);
+ String.valueOf(new Date().getTime()).substring(6);
loadBalancersToDeleteAfterTest.add(nameOfLoadBalancer);
Set<Ip> availableIps = client.getIpServices().getUnassignedPublicIpList();
if (availableIps.size() < 4)
throw new SkipException("Not enough available IPs (4 needed) to run the test");
throw new SkipException(
"Not enough available IPs (4 needed) to run the test");
Iterator<Ip> ipIterator = availableIps.iterator();
Ip vip = ipIterator.next();
Ip realIp1 = ipIterator.next();
Ip realIp2 = ipIterator.next();
Ip realIp3 = ipIterator.next();
AddLoadBalancerOptions options = new AddLoadBalancerOptions.Builder().create(
LoadBalancerType.LEAST_CONNECTED, LoadBalancerPersistenceType.SOURCE_ADDRESS);
LoadBalancer createdLoadBalancer = client.getLoadBalancerServices().addLoadBalancer(
nameOfLoadBalancer, new IpPortPair(vip, 80),
Arrays.asList(new IpPortPair(realIp1, 80), new IpPortPair(realIp2, 80)), options);
AddLoadBalancerOptions options = new AddLoadBalancerOptions.Builder()
.create(LoadBalancerType.LEAST_CONNECTED,
LoadBalancerPersistenceType.SOURCE_ADDRESS);
LoadBalancer createdLoadBalancer = client.getLoadBalancerServices()
.addLoadBalancer(
nameOfLoadBalancer,
new IpPortPair(vip, 80),
Arrays.asList(new IpPortPair(realIp1, 80), new IpPortPair(
realIp2, 80)), options);
assertNotNull(createdLoadBalancer);
assert loadBalancerLatestJobCompleted.apply(createdLoadBalancer);
// get load balancer by name
Set<LoadBalancer> response = client.getLoadBalancerServices().getLoadBalancersByName(
nameOfLoadBalancer);
Set<LoadBalancer> response = client.getLoadBalancerServices()
.getLoadBalancersByName(nameOfLoadBalancer);
assert (response.size() == 1);
createdLoadBalancer = Iterables.getOnlyElement(response);
assertNotNull(createdLoadBalancer.getRealIpList());
assertEquals(createdLoadBalancer.getRealIpList().size(), 2);
assertNotNull(createdLoadBalancer.getVirtualIp());
assertEquals(createdLoadBalancer.getVirtualIp().getIp().getIp(), vip.getIp());
assertEquals(createdLoadBalancer.getVirtualIp().getIp().getIp(), vip
.getIp());
LoadBalancer editedLoadBalancer = client.getLoadBalancerServices().editLoadBalancerNamed(
nameOfLoadBalancer, Arrays.asList(new IpPortPair(realIp3, 8181)));
LoadBalancer editedLoadBalancer = client.getLoadBalancerServices()
.editLoadBalancerNamed(nameOfLoadBalancer,
Arrays.asList(new IpPortPair(realIp3, 8181)));
assert loadBalancerLatestJobCompleted.apply(editedLoadBalancer);
assertNotNull(editedLoadBalancer.getRealIpList());
assertEquals(editedLoadBalancer.getRealIpList().size(), 1);
assertEquals(Iterables.getOnlyElement(editedLoadBalancer.getRealIpList()).getIp().getIp(),
realIp3.getIp());
assertEquals(Iterables.getOnlyElement(editedLoadBalancer.getRealIpList())
.getIp().getIp(), realIp3.getIp());
int lbCountAfterAddingOneServer = client.getLoadBalancerServices().getLoadBalancerList()
.size();
int lbCountAfterAddingOneServer = client.getLoadBalancerServices()
.getLoadBalancerList().size();
assert lbCountAfterAddingOneServer == lbCountBeforeTest + 1 : "There should be +1 increase in the number of load balancers since the test started";
// delete the load balancer
client.getLoadBalancerServices().deleteByName(nameOfLoadBalancer);
Set<Job> jobs = client.getJobServices().getJobsForObjectName(nameOfLoadBalancer);
assert ("DeleteLoadBalancer".equals(Iterables.getLast(jobs).getCommand().getName()));
Set<Job> jobs = client.getJobServices().getJobsForObjectName(
nameOfLoadBalancer);
assert ("DeleteLoadBalancer".equals(Iterables.getLast(jobs).getCommand()
.getName()));
assert loadBalancerLatestJobCompleted.apply(createdLoadBalancer);
int lbCountAfterDeletingTheServer = client.getLoadBalancerServices().getLoadBalancerList()
.size();
int lbCountAfterDeletingTheServer = client.getLoadBalancerServices()
.getLoadBalancerList().size();
assert lbCountAfterDeletingTheServer == lbCountBeforeTest : "There should be the same # of load balancers as since the test started";
}
/**
* Tests common server image operations.
*/
@Test(enabled = false)
@Test(enabled = true)
public void testImageLifecycle() {
GetImageListOptions options = new GetImageListOptions.Builder().publicDatabaseServers();
GetImageListOptions options = new GetImageListOptions.Builder()
.publicDatabaseServers();
Set<ServerImage> images = client.getImageServices().getImageList(options);
Predicate<ServerImage> isDatabaseServer = new Predicate<ServerImage>() {
@ -317,14 +349,15 @@ public class GoGridLiveTest {
assert Iterables.all(images, isDatabaseServer) : "All of the images should've been of database type";
ServerImage image = Iterables.getLast(images);
ServerImage imageFromServer = Iterables.getOnlyElement(client.getImageServices()
.getImagesByName(image.getName()));
ServerImage imageFromServer = Iterables.getOnlyElement(client
.getImageServices().getImagesByName(image.getName()));
assertEquals(image, imageFromServer);
try {
client.getImageServices().editImageDescription(image.getName(), "newDescription");
client.getImageServices().editImageDescription(image.getName(),
"newDescription");
throw new TestException(
"An exception hasn't been thrown where expected; expected GoGridResponseException");
"An exception hasn't been thrown where expected; expected GoGridResponseException");
} catch (GoGridResponseException e) {
// expected situation - check and proceed
assertTrue(e.getMessage().contains("GoGridIllegalArgumentException"));
@ -332,50 +365,67 @@ public class GoGridLiveTest {
}
@Test(enabled = false)
@Test(enabled = true)
public void testShellAccess() throws IOException {
final String nameOfServer = "Server" + String.valueOf(new Date().getTime()).substring(6);
final String nameOfServer = "Server"
+ String.valueOf(new Date().getTime()).substring(6);
serversToDeleteAfterTheTests.add(nameOfServer);
Set<Ip> availableIps = client.getIpServices().getUnassignedIpList();
Ip availableIp = Iterables.getLast(availableIps);
Server createdServer = client.getServerServices().addServer(nameOfServer,
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "1", availableIp.getIp());
"GSI-f8979644-e646-4711-ad58-d98a5fa3612c", "1",
availableIp.getIp());
assertNotNull(createdServer);
assert serverLatestJobCompleted.apply(createdServer);
// get server by name
Set<Server> response = client.getServerServices().getServersByName(nameOfServer);
Set<Server> response = client.getServerServices().getServersByName(
nameOfServer);
assert (response.size() == 1);
createdServer = Iterables.getOnlyElement(response);
Map<String, Credentials> credsMap = client.getServerServices().getServerCredentialsList();
Map<String, Credentials> credsMap = client.getServerServices()
.getServerCredentialsList();
Credentials instanceCredentials = credsMap.get(createdServer.getName());
assertNotNull(instanceCredentials);
IPSocket socket = new IPSocket(createdServer.getIp().getIp(), 22);
RetryablePredicate<IPSocket> socketOpen = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 180, 5, TimeUnit.SECONDS);
new InetSocketAddressConnect(), 180, 5, TimeUnit.SECONDS);
socketOpen.apply(socket);
SshClient sshClient = new JschSshClient(new BackoffLimitedRetryHandler(), socket, 60000,
instanceCredentials.identity, instanceCredentials.credential, null);
SshClient sshClient = new JschSshClient(new BackoffLimitedRetryHandler(),
socket, 60000, instanceCredentials.identity,
instanceCredentials.credential, null);
sshClient.connect();
String output = sshClient.exec("df").getOutput();
assertTrue(output.contains("Filesystem"),
"The output should've contained filesystem information, but it didn't. Output: "
+ output);
assertTrue(
output.contains("Filesystem"),
"The output should've contained filesystem information, but it didn't. Output: "
+ output);
sshClient.disconnect();
// check that the get credentials call is the same as this
assertEquals(client.getServerServices().getServerCredentials(
createdServer.getId()), instanceCredentials);
try{
assertEquals(client.getServerServices().getServerCredentials(Long.MAX_VALUE), null);
} catch (AssertionError e){
e.printStackTrace();
}
// delete the server
client.getServerServices().deleteByName(nameOfServer);
}
/**
* In case anything went wrong during the tests, removes the objects created in the tests.
* In case anything went wrong during the tests, removes the objects created
* in the tests.
*/
@AfterTest
public void cleanup() {

View File

@ -0,0 +1,106 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.gogrid.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.UnknownHostException;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.domain.Credentials;
import org.jclouds.gogrid.config.DateSecondsAdapter;
import org.jclouds.gogrid.domain.IpState;
import org.jclouds.gogrid.domain.ServerImageState;
import org.jclouds.gogrid.domain.ServerImageType;
import org.jclouds.gogrid.functions.internal.CustomDeserializers;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.Payloads;
import org.jclouds.http.functions.config.ParserModule;
import org.testng.annotations.Test;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "gogrid.ParseCredentialsFromJsonResponseTest")
public class ParseCredentialsFromJsonResponseTest {
@Test(expectedExceptions = IllegalStateException.class)
public void testFailWhenTooManyPasswords() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream(
"/test_credentials_list.json");
HttpResponse response = new HttpResponse(200, "ok", Payloads
.newInputStreamPayload(is));
ParseCredentialsFromJsonResponse parser = i
.getInstance(ParseCredentialsFromJsonResponse.class);
parser.apply(response);
}
@Test
public void testValid() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream(
"/test_credential.json");
HttpResponse response = new HttpResponse(200, "ok", Payloads
.newInputStreamPayload(is));
ParseCredentialsFromJsonResponse parser = i
.getInstance(ParseCredentialsFromJsonResponse.class);
Credentials creds = parser.apply(response);
assertEquals(creds.identity, "root");
assertEquals(creds.credential, "dig44sos");
}
Injector i = Guice.createInjector(new ParserModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(DateSecondsAdapter.class);
super.configure();
}
@Provides
@Singleton
@SuppressWarnings("unused")
@com.google.inject.name.Named(Constants.PROPERTY_GSON_ADAPTERS)
public Map<Type, Object> provideCustomAdapterBindings() {
Map<Type, Object> bindings = Maps.newHashMap();
bindings.put(IpState.class, new CustomDeserializers.IpStateAdapter());
bindings.put(ServerImageType.class,
new CustomDeserializers.ServerImageTypeAdapter());
bindings.put(ServerImageState.class,
new CustomDeserializers.ServerImageStateAdapter());
bindings.put(ServerImageState.class,
new CustomDeserializers.ServerImageStateAdapter());
return bindings;
}
});
}

View File

@ -30,15 +30,15 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.jclouds.gogrid.functions.ParseErrorFromJsonResponse;
import org.jclouds.gogrid.mock.HttpCommandMock;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.Payloads;
import org.jclouds.http.functions.config.ParserModule;
import org.testng.TestException;
import org.testng.annotations.Test;
import com.google.gson.GsonBuilder;
import com.google.inject.Guice;
/**
* Tests that the GoGridErrorHandler is correctly handling the exceptions.
@ -51,8 +51,8 @@ public class GoGridErrorHandlerTest {
public void testHandler() {
InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
GoGridErrorHandler handler = new GoGridErrorHandler(new ParseErrorFromJsonResponse(
new GsonBuilder().create()));
GoGridErrorHandler handler = Guice.createInjector(new ParserModule()).getInstance(GoGridErrorHandler.class);
HttpCommand command = createHttpCommand();
handler.handleError(command, new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));

View File

@ -45,6 +45,7 @@ import java.io.IOException;
import java.lang.reflect.Method;
import org.jclouds.gogrid.domain.PowerCommand;
import org.jclouds.gogrid.functions.ParseCredentialsFromJsonResponse;
import org.jclouds.gogrid.functions.ParseOptionsFromJsonResponse;
import org.jclouds.gogrid.functions.ParseServerFromJsonResponse;
import org.jclouds.gogrid.functions.ParseServerListFromJsonResponse;
@ -66,148 +67,179 @@ import com.google.inject.TypeLiteral;
* @author Oleksiy Yarmula
*/
@Test(groups = "unit", testName = "gogrid.GoGridAsyncClientTest")
public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridServerAsyncClient> {
public class GridServerAsyncClientTest extends
BaseGoGridAsyncClientTest<GridServerAsyncClient> {
@Test
public void testGetServerListNoOptions() throws NoSuchMethodException, IOException {
public void testGetServerListNoOptions() throws NoSuchMethodException,
IOException {
Method method = GridServerAsyncClient.class.getMethod("getServerList",
GetServerListOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method);
GetServerListOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/list?v=1.5 HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/list?v=1.5 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class);
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/list?"
+ "v=1.5&sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/list?"
+ "v=1.5&sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testGetServerListWithOptions() throws NoSuchMethodException, IOException {
public void testGetServerListWithOptions() throws NoSuchMethodException,
IOException {
Method method = GridServerAsyncClient.class.getMethod("getServerList",
GetServerListOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
new GetServerListOptions.Builder().onlySandboxServers());
GetServerListOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, new GetServerListOptions.Builder()
.onlySandboxServers());
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/list?v=1.5&isSandbox=true HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/list?v=1.5&isSandbox=true HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class);
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/list?"
+ "v=1.5&isSandbox=true&sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertRequestLineEquals(
httpRequest,
"GET https://api.gogrid.com/api/grid/server/list?"
+ "v=1.5&isSandbox=true&sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testGetServersByName() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("getServersByName", String[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
"server1");
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.5&name=server1 HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/get?v=1.5&name=server1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerListFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFound.class);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/server/get?"
+ "v=1.5&name=server1&" + "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/get?"
+ "v=1.5&name=server1&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testGetServersById() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("getServersById", long[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
123L);
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.5&id=123 HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/get?v=1.5&id=123 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerListFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerListFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFound.class);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/server/get?"
+ "v=1.5&id=123&" + "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/get?" + "v=1.5&id=123&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testAddServerNoOptions() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("addServer", String.class,
String.class, String.class, String.class, AddServerOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
"serverName", "img55", "memory", "127.0.0.1");
public void testAddServerNoOptions() throws NoSuchMethodException,
IOException {
Method method = GridServerAsyncClient.class.getMethod("addServer",
String.class, String.class, String.class, String.class,
AddServerOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, "serverName", "img55", "memory", "127.0.0.1");
assertRequestLineEquals(httpRequest, "GET https://api.gogrid.com/api/grid/server/add?v=1.5&"
+ "name=serverName&server.ram=memory&image=img55&ip=127.0.0.1 " + "HTTP/1.1");
assertRequestLineEquals(
httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?v=1.5&"
+ "name=serverName&server.ram=memory&image=img55&ip=127.0.0.1 "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerFromJsonResponse.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/add?"
+ "v=1.5&name=serverName&server.ram=memory&" + "image=img55&ip=127.0.0.1&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?"
+ "v=1.5&name=serverName&server.ram=memory&"
+ "image=img55&ip=127.0.0.1&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testAddServerOptions() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("addServer", String.class,
String.class, String.class, String.class, AddServerOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
"serverName", "img55", "memory", "127.0.0.1", new AddServerOptions().asSandboxType()
Method method = GridServerAsyncClient.class.getMethod("addServer",
String.class, String.class, String.class, String.class,
AddServerOptions[].class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, "serverName", "img55", "memory",
"127.0.0.1", new AddServerOptions().asSandboxType()
.withDescription("fooy"));
assertRequestLineEquals(
httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?v=1.5&name=serverName&server.ram=memory&image=img55&ip=127.0.0.1&isSandbox=true&description=fooy HTTP/1.1");
httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?v=1.5&name=serverName&server.ram=memory&image=img55&ip=127.0.0.1&isSandbox=true&description=fooy HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
@ -215,26 +247,27 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridSer
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(
httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?v=1.5&name=serverName&server.ram=memory&image=img55&ip=127.0.0.1&isSandbox=true&description=fooy&sig=3f446f171455fbb5574aecff4997b273&api_key=foo HTTP/1.1");
httpRequest,
"GET https://api.gogrid.com/api/grid/server/add?v=1.5&name=serverName&server.ram=memory&image=img55&ip=127.0.0.1&isSandbox=true&description=fooy&sig=3f446f171455fbb5574aecff4997b273&api_key=foo HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testPowerServer() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("power", String.class,
PowerCommand.class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
"PowerServer", PowerCommand.RESTART);
Method method = GridServerAsyncClient.class.getMethod("power",
String.class, PowerCommand.class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, "PowerServer", PowerCommand.RESTART);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/power?v=1.5&"
+ "server=PowerServer&power=restart " + "HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/power?v=1.5&"
+ "server=PowerServer&power=restart " + "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
@ -242,26 +275,29 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridSer
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/power?v=1.5&"
+ "server=PowerServer&power=restart&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/power?v=1.5&"
+ "server=PowerServer&power=restart&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testDeleteByName() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("deleteByName", String.class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method,
"PowerServer");
Method method = GridServerAsyncClient.class.getMethod("deleteByName",
String.class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, "PowerServer");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/delete?v=1.5&" + "name=PowerServer "
+ "HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/delete?v=1.5&"
+ "name=PowerServer " + "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseServerFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseServerFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
@ -269,8 +305,10 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridSer
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/server/delete?v=1.5&" + "name=PowerServer&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
"GET https://api.gogrid.com/api/grid/server/delete?v=1.5&"
+ "name=PowerServer&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@ -278,15 +316,17 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridSer
@Test
public void testGetRamSizes() throws NoSuchMethodException, IOException {
Method method = GridServerAsyncClient.class.getMethod("getRamSizes");
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor.createRequest(method);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/common/lookup/list?v=1.5&lookup=server.ram "
+ "HTTP/1.1");
"GET https://api.gogrid.com/api/common/lookup/list?v=1.5&lookup=server.ram "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseOptionsFromJsonResponse.class);
assertResponseParserClassEquals(method, httpRequest,
ParseOptionsFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
@ -294,12 +334,32 @@ public class GridServerAsyncClientTest extends BaseGoGridAsyncClientTest<GridSer
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/common/lookup/list?v=1.5&lookup=server.ram&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo " + "HTTP/1.1");
"GET https://api.gogrid.com/api/common/lookup/list?v=1.5&lookup=server.ram&"
+ "sig=3f446f171455fbb5574aecff4997b273&api_key=foo "
+ "HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
}
@Test
public void testServerCredentials() throws NoSuchMethodException,
IOException {
Method method = GridServerAsyncClient.class.getMethod(
"getServerCredentials", long.class);
GeneratedHttpRequest<GridServerAsyncClient> httpRequest = processor
.createRequest(method, 1);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/support/grid/password/get?v=1.5&id=1 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest,
ParseCredentialsFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<GridServerAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<GridServerAsyncClient>>() {

View File

@ -0,0 +1,110 @@
{
"list": [
{
"password": "dig44sos",
"object": "password",
"username": "root",
"server": {
"object": "server",
"isSandbox": false,
"type": {
"object": "option",
"description": "Web or Application Server",
"name": "Web Server",
"id": 1
},
"os": {
"object": "option",
"description": "CentOS 5.3 (64-bit)",
"name": "CentOS 5.3 (64-bit)",
"id": 17
},
"image": {
"type": {
"object": "option",
"description": "Web or Application Server",
"name": "Web Server",
"id": 1
},
"owner": {
"object": "customer",
"name": "Gear6",
"id": 26443
},
"updatedTime": 1265675466171,
"isActive": true,
"id": 2500,
"createdTime": 1265412834154,
"isPublic": true,
"billingtokens": [
{
"price": 0,
"name": "CentOS 5.3 64bit",
"id": 47
},
{
"price": 60,
"name": "Gear 6 Paid Version",
"id": 76
}
],
"object": "serverimage",
"friendlyName": "gear6-memcache-server-2.3.6.2-x86_64",
"os": {
"object": "option",
"description": "CentOS 5.3 (64-bit)",
"name": "CentOS 5.3 (64-bit)",
"id": 17
},
"price": 60,
"description": "Gear6 Memcache Server 2.3.6.2 (64 bit)",
"state": {
"object": "option",
"description": "Image is available for adds",
"name": "Available",
"id": 2
},
"location": "26443/GSI-7f498260-2b8a-43ef-aa77-5b403f8f739a.img",
"name": "GSI-7f498260-2b8a-43ef-aa77-5b403f8f739a"
},
"state": {
"object": "option",
"description": "Server is in active state.",
"name": "On",
"id": 1
},
"ram": {
"object": "option",
"description": "Server with 512MB RAM",
"name": "512MB",
"id": 1
},
"name": "gogrid-19",
"ip": {
"object": "ip",
"public": true,
"subnet": "204.51.240.176/255.255.255.240",
"state": {
"object": "option",
"description": "IP is reserved or in use",
"name": "Assigned",
"id": 2
},
"ip": "204.51.240.189",
"id": 1313090
},
"id": 77332
},
"id": 82647,
"applicationtype": "os"
}
],
"summary": {
"total": 6,
"start": 0,
"numpages": 0,
"returned": 6
},
"status": "success",
"method": "/support/password/list"
}