included image functionality, disabled live tests by default

This commit is contained in:
Alex Yarmula 2010-03-03 14:32:55 -08:00
parent 202dd690f0
commit 4bbff2caf5
9 changed files with 613 additions and 2 deletions

View File

@ -0,0 +1,48 @@
/**
*
* 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.domain;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Oleksiy Yarmula
*/
public enum ServerImageState {
AVAILABLE("Available"),
SAVING("Saving"),
UNKNOWN("Unknown");
String type;
ServerImageState(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
public static ServerImageState fromValue(String type) {
for(ServerImageState serverImageState : values()) {
if(serverImageState.type.equals(checkNotNull(type))) return serverImageState;
}
return UNKNOWN;
}
}

View File

@ -0,0 +1,48 @@
/**
*
* 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.domain;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Oleksiy Yarmula
*/
public enum ServerImageType {
WEB_APPLICATION_SERVER("Web Server"),
DATABASE_SERVER("Database Server"),
UNKNOWN("Unknown");
String type;
ServerImageType(String type) {
this.type = type;
}
@Override
public String toString() {
return type;
}
public static ServerImageType fromValue(String type) {
for(ServerImageType serverImageType : values()) {
if(serverImageType.type.equals(checkNotNull(type))) return serverImageType;
}
return UNKNOWN;
}
}

View File

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

View File

@ -0,0 +1,55 @@
/**
*
* 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 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;
/**
* @author Oleksiy Yarmula
*/
public class ParseImageListFromJsonResponse extends ParseJson<SortedSet<ServerImage>> {
@Inject
public 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();
}
}

View File

@ -0,0 +1,77 @@
/**
*
* 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.options;
import org.jclouds.gogrid.domain.ServerImageState;
import org.jclouds.gogrid.domain.ServerImageType;
import org.jclouds.http.options.BaseHttpRequestOptions;
import static com.google.common.base.Preconditions.checkState;
import static org.jclouds.gogrid.reference.GoGridQueryParams.*;
/**
* @author Oleksiy Yarmula
*/
public class GetImageListOptions extends BaseHttpRequestOptions {
public GetImageListOptions setType(ServerImageType imageType) {
checkState(!queryParameters.containsKey(IMAGE_TYPE_KEY), "Can't have duplicate image type restrictions");
queryParameters.put(IMAGE_TYPE_KEY, imageType.toString());
return this;
}
public GetImageListOptions setState(ServerImageState imageState) {
checkState(!queryParameters.containsKey(IMAGE_STATE_KEY), "Can't have duplicate image state restrictions");
queryParameters.put(IMAGE_STATE_KEY, imageState.toString());
return this;
}
public GetImageListOptions onlyPublic() {
checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), "Can't have duplicate image visibility restrictions");
queryParameters.put(IS_PUBLIC_KEY, "true");
return this;
}
public GetImageListOptions onlyPrivate() {
checkState(!queryParameters.containsKey(IS_PUBLIC_KEY), "Can't have duplicate image visibility restrictions");
queryParameters.put(IS_PUBLIC_KEY, "false");
return this;
}
public GetImageListOptions maxItemsNumber(Integer maxNumber) {
checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), "Can't have duplicate parameter of max returned items");
queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString());
return this;
}
public static class Builder {
public GetImageListOptions publicWebServers() {
return new GetImageListOptions().setState(ServerImageState.AVAILABLE).
setType(ServerImageType.WEB_APPLICATION_SERVER).
onlyPublic();
}
public GetImageListOptions publicDatabaseServers() {
return new GetImageListOptions().setState(ServerImageState.AVAILABLE).
setType(ServerImageType.DATABASE_SERVER).
onlyPublic();
}
}
}

View File

@ -0,0 +1,75 @@
package org.jclouds.gogrid.services;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.gogrid.GoGrid;
import org.jclouds.gogrid.binders.BindIdsToQueryParams;
import org.jclouds.gogrid.binders.BindNamesToQueryParams;
import org.jclouds.gogrid.domain.ServerImage;
import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication;
import org.jclouds.gogrid.functions.ParseImageFromJsonResponse;
import org.jclouds.gogrid.functions.ParseImageListFromJsonResponse;
import org.jclouds.gogrid.options.GetImageListOptions;
import org.jclouds.rest.annotations.*;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import java.util.Set;
import static org.jclouds.gogrid.reference.GoGridHeaders.VERSION;
import static org.jclouds.gogrid.reference.GoGridQueryParams.IMAGE_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.IMAGE_DESCRIPTION_KEY;
import static org.jclouds.gogrid.reference.GoGridQueryParams.IMAGE_FRIENDLY_NAME_KEY;
/**
* @author Oleksiy Yarmula
*/
@Endpoint(GoGrid.class)
@RequestFilters(SharedKeyLiteAuthentication.class)
@QueryParams(keys = VERSION, values = "1.4")
public interface GridImageAsyncClient {
/**
* @see GridImageClient#getImageList
*/
@GET
@ResponseParser(ParseImageListFromJsonResponse.class)
@Path("/grid/image/list")
ListenableFuture<Set<ServerImage>> getImageList(GetImageListOptions... options);
/**
* @see GridImageClient#getImagesById
*/
@GET
@ResponseParser(ParseImageListFromJsonResponse.class)
@Path("/grid/image/get")
ListenableFuture<Set<ServerImage>> getImagesById(@BinderParam(BindIdsToQueryParams.class) Long... ids);
/**
* @see GridImageClient#getImagesByName
*/
@GET
@ResponseParser(ParseImageListFromJsonResponse.class)
@Path("/grid/image/get")
ListenableFuture<Set<ServerImage>> getImagesByName(@BinderParam(BindNamesToQueryParams.class) String... names);
/**
* @see GridImageClient#editImageDescription
*/
@GET
@ResponseParser(ParseImageFromJsonResponse.class)
@Path("/grid/image/edit")
ListenableFuture<ServerImage> editImageDescription(@QueryParam(IMAGE_KEY) String idOrName,
@QueryParam(IMAGE_DESCRIPTION_KEY) String newDescription);
/**
* @see GridImageClient#editImageFriendlyName
*/
@GET
@ResponseParser(ParseImageFromJsonResponse.class)
@Path("/grid/image/edit")
ListenableFuture<ServerImage> editImageFriendlyName(@QueryParam(IMAGE_KEY) String idOrName,
@QueryParam(IMAGE_FRIENDLY_NAME_KEY) String newFriendlyName);
}

View File

@ -0,0 +1,61 @@
package org.jclouds.gogrid.services;
import org.jclouds.concurrent.Timeout;
import org.jclouds.gogrid.binders.BindIdsToQueryParams;
import org.jclouds.gogrid.domain.ServerImage;
import org.jclouds.gogrid.options.GetImageListOptions;
import org.jclouds.rest.annotations.BinderParam;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Manages the server images
*
* @see <a href="http://wiki.gogrid.com/wiki/index.php/API#Server_Image_Methods"/>
* @author Oleksiy Yarmula
*/
@Timeout(duration = 30, timeUnit = TimeUnit.SECONDS)
public interface GridImageClient {
/**
* Returns all server images.
*
* @param options options to narrow the search down
* @return server images found
*/
Set<ServerImage> getImageList(GetImageListOptions... options);
/**
* Returns images, found by specified ids
* @param ids the ids that match existing images
* @return images found
*/
Set<ServerImage> getImagesById(Long... ids);
/**
* Returns images, found by specified names
* @param names the names that march existing images
* @return images found
*/
Set<ServerImage> getImagesByName(String... names);
/**
* Edits an existing image
*
* @param idOrName id or name of the existing image
* @param newDescription description to replace the current one
* @return edited server image
*/
ServerImage editImageDescription(String idOrName, String newDescription);
/**
* Edits an existing image
*
* @param idOrName id or name of the existing image
* @param newFriendlyName friendly name to replace the current one
* @return edited server image
*/
ServerImage editImageFriendlyName(String idOrName, String newFriendlyName);
}

View File

@ -135,7 +135,7 @@ public class GoGridLiveTest {
* 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=false)
public void testJobs() {
final String nameOfServer = "Server" + String.valueOf(new Date().getTime()).substring(6);
serversToDeleteAfterTheTests.add(nameOfServer);
@ -242,7 +242,7 @@ public class GoGridLiveTest {
/**
* Tests common server image operations.
*/
@Test(enabled=true)
@Test(enabled=false)
public void testImageLifecycle() {
GetImageListOptions options = new GetImageListOptions.Builder().publicDatabaseServers();
Set<ServerImage> images = client.getImageServices().getImageList(options);

View File

@ -0,0 +1,202 @@
/**
*
* 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.services;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import org.jclouds.encryption.EncryptionService;
import org.jclouds.gogrid.GoGrid;
import org.jclouds.gogrid.domain.ServerImageState;
import org.jclouds.gogrid.domain.ServerImageType;
import org.jclouds.gogrid.filters.SharedKeyLiteAuthentication;
import org.jclouds.gogrid.functions.ParseImageFromJsonResponse;
import org.jclouds.gogrid.functions.ParseImageListFromJsonResponse;
import org.jclouds.gogrid.options.GetImageListOptions;
import org.jclouds.logging.Logger;
import org.jclouds.rest.RestClientTest;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* @author Oleksiy Yarmula
*/
public class GridImageAsyncClientTest extends RestClientTest<GridImageAsyncClient> {
@Test
public void testGetImageListWithOptions() throws NoSuchMethodException, IOException {
Method method = GridImageAsyncClient.class.getMethod("getImageList", GetImageListOptions[].class);
GeneratedHttpRequest<GridImageAsyncClient> httpRequest = processor.createRequest(method,
new GetImageListOptions().onlyPublic().setState(ServerImageState.AVAILABLE).
setType(ServerImageType.WEB_APPLICATION_SERVER));
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/list?v=1.4&" +
"isPublic=true&image.state=Available&" +
"image.type=Web%20Server HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
assertResponseParserClassEquals(method, httpRequest, ParseImageListFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/list?" +
"v=1.4&isPublic=true&image.state=Available&" +
"image.type=Web%20Server&" +
"sig=3f446f171455fbb5574aecff4997b273&api_key=foo " +
"HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
}
@Test
public void testGetImagesByName() throws NoSuchMethodException, IOException {
Method method = GridImageAsyncClient.class.getMethod("getImagesByName", String[].class);
GeneratedHttpRequest<GridImageAsyncClient> httpRequest = processor.createRequest(method,
"name1", "name2");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/get?v=1.4&" +
"name=name1&name=name2 HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
assertResponseParserClassEquals(method, httpRequest, ParseImageListFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/get?v=1.4&" +
"name=name1&name=name2&" +
"sig=3f446f171455fbb5574aecff4997b273&api_key=foo " +
"HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
}
@Test
public void testEditImageDescription() throws NoSuchMethodException, IOException {
Method method = GridImageAsyncClient.class.getMethod("editImageDescription", String.class, String.class);
GeneratedHttpRequest<GridImageAsyncClient> httpRequest = processor.createRequest(method,
"imageName", "newDesc");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/edit?v=1.4&" +
"image=imageName&description=newDesc HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
assertResponseParserClassEquals(method, httpRequest, ParseImageFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/edit?v=1.4&" +
"image=imageName&description=newDesc&" +
"sig=3f446f171455fbb5574aecff4997b273&api_key=foo " +
"HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
}
@Test
public void testEditImageFriendlyName() throws NoSuchMethodException, IOException {
Method method = GridImageAsyncClient.class.getMethod("editImageFriendlyName", String.class, String.class);
GeneratedHttpRequest<GridImageAsyncClient> httpRequest = processor.createRequest(method,
"imageName", "newFriendlyName");
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/edit?v=1.4&" +
"image=imageName&friendlyName=newFriendlyName HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
assertResponseParserClassEquals(method, httpRequest, ParseImageFromJsonResponse.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(httpRequest);
Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
assertRequestLineEquals(httpRequest,
"GET https://api.gogrid.com/api/grid/image/edit?v=1.4&" +
"image=imageName&friendlyName=newFriendlyName&" +
"sig=3f446f171455fbb5574aecff4997b273&api_key=foo " +
"HTTP/1.1");
assertHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, null);
}
@Override
protected void checkFilters(GeneratedHttpRequest<GridImageAsyncClient> httpMethod) {
assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), SharedKeyLiteAuthentication.class);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<GridImageAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<GridImageAsyncClient>>() {
};
}
@Override
protected Module createModule() {
return new AbstractModule() {
@Override
protected void configure() {
bind(URI.class).annotatedWith(GoGrid.class).toInstance(
URI.create("https://api.gogrid.com/api"));
bind(Logger.LoggerFactory.class).toInstance(new Logger.LoggerFactory() {
public Logger getLogger(String category) {
return Logger.NULL;
}
});
}
@Provides
@Singleton
public SharedKeyLiteAuthentication provideAuthentication(EncryptionService encryptionService)
throws UnsupportedEncodingException {
return new SharedKeyLiteAuthentication("foo", "bar", 1267243795L, encryptionService);
}
};
}
}