From c1637ba7409737d6d949fbb5ce5da334ed38e992 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 4 Mar 2011 15:45:59 -0500 Subject: [PATCH] added guest os to cloudstack --- .../org/jclouds/cloudstack/domain/OSType.java | 137 ++++++++++++++++++ .../features/GuestOSAsyncClient.java | 72 +++++++++ .../cloudstack/features/GuestOSClient.java | 56 +++++++ .../options/ListOSTypesOptions.java | 71 +++++++++ .../features/GuestOSAsyncClientTest.java | 101 +++++++++++++ .../features/GuestOSClientLiveTest.java | 60 ++++++++ .../options/ListOSTypesOptionsTest.java | 57 ++++++++ 7 files changed, 554 insertions(+) create mode 100644 sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/OSType.java create mode 100644 sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSAsyncClient.java create mode 100644 sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSClient.java create mode 100644 sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListOSTypesOptions.java create mode 100644 sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSAsyncClientTest.java create mode 100644 sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSClientLiveTest.java create mode 100644 sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListOSTypesOptionsTest.java diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/OSType.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/OSType.java new file mode 100644 index 0000000000..ea41c7c670 --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/OSType.java @@ -0,0 +1,137 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.domain; + +import com.google.gson.annotations.SerializedName; + +/** + * + * @author Adrian Cole + */ +public class OSType implements Comparable { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long OSCategoryId; + private String description; + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder OSCategoryId(long OSCategoryId) { + this.OSCategoryId = OSCategoryId; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public OSType build() { + return new OSType(id, OSCategoryId, description); + } + } + + // for deserialization + OSType() { + + } + + private long id; + @SerializedName("oscategoryid") + private long OSCategoryId; + private String description; + + public OSType(long id, long OSCategoryId, String description) { + this.id = id; + this.OSCategoryId = OSCategoryId; + this.description = description; + } + + /** + * @return the ID of the OS type + */ + public long getId() { + return id; + } + + /** + * @return the ID of the OS category + */ + public long getOSCategoryId() { + return OSCategoryId; + } + + /** + * @return the name/description of the OS type + */ + public String getDescription() { + return description; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (OSCategoryId ^ (OSCategoryId >>> 32)); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + (int) (id ^ (id >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OSType other = (OSType) obj; + if (OSCategoryId != other.OSCategoryId) + return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (id != other.id) + return false; + return true; + } + + @Override + public String toString() { + return "[id=" + id + ", OSCategoryId=" + OSCategoryId + ", description=" + description + "]"; + } + + @Override + public int compareTo(OSType arg0) { + return new Long(id).compareTo(arg0.getId()); + } + +} diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSAsyncClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSAsyncClient.java new file mode 100644 index 0000000000..40a9ec84f8 --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSAsyncClient.java @@ -0,0 +1,72 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.features; + +import java.util.Set; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.jclouds.cloudstack.domain.OSType; +import org.jclouds.cloudstack.filters.QuerySigner; +import org.jclouds.cloudstack.options.ListOSTypesOptions; +import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.QueryParams; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.Unwrap; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * Provides asynchronous access to cloudstack via their REST API. + *

+ * + * @see AsyncJobClient + * @see + * @author Adrian Cole + */ +@RequestFilters(QuerySigner.class) +@QueryParams(keys = "response", values = "json") +public interface GuestOSAsyncClient { + + /** + * @see GuestOSClient#listOSTypes + */ + @GET + @QueryParams(keys = "command", values = "listOsTypes") + @Unwrap(depth = 2) + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) + ListenableFuture> listOSTypes(ListOSTypesOptions... options); + + /** + * @see OSTypeClient#getOSType + */ + @GET + @QueryParams(keys = "command", values = "listOsTypes") + @Unwrap(depth = 3, edgeCollection = Set.class) + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getOSType(@QueryParam("id") long id); +} diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSClient.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSClient.java new file mode 100644 index 0000000000..80c4d76a3e --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/features/GuestOSClient.java @@ -0,0 +1,56 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.features; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.jclouds.cloudstack.domain.OSType; +import org.jclouds.cloudstack.options.ListOSTypesOptions; +import org.jclouds.concurrent.Timeout; + +/** + * Provides synchronous access to CloudStack Operating System features. + *

+ * + * @see GuestOSAsyncClient + * @see + * @author Adrian Cole + */ +@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) +public interface GuestOSClient { + /** + * Lists all supported OS types for this cloud. + * + * @param options + * if present, how to constrain the list + * @return os types matching query, or empty set, if no types are found + */ + Set listOSTypes(ListOSTypesOptions... options); + + /** + * get a specific os type by id + * + * @param id + * os type to get + * @return os type or null if not found + */ + OSType getOSType(long id); +} diff --git a/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListOSTypesOptions.java b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListOSTypesOptions.java new file mode 100644 index 0000000000..a7138b2a66 --- /dev/null +++ b/sandbox-apis/cloudstack/src/main/java/org/jclouds/cloudstack/options/ListOSTypesOptions.java @@ -0,0 +1,71 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +import com.google.common.collect.ImmutableSet; + +/** + * Options used to control what OSType information is returned + * + * @see + * @author Adrian Cole + */ +public class ListOSTypesOptions extends BaseHttpRequestOptions { + + public static final ListOSTypesOptions NONE = new ListOSTypesOptions(); + /** + * @param id list by Os type Id + */ + public ListOSTypesOptions id(long id) { + this.queryParameters.replaceValues("id", ImmutableSet.of(id + "")); + return this; + } + /** + * @param OSCategoryId + * list by Os Category id + */ + public ListOSTypesOptions OSCategoryId(long OSCategoryId) { + this.queryParameters.replaceValues("oscategoryid", ImmutableSet.of(OSCategoryId + "")); + return this; + } + + + public static class Builder { + /** + * @see ListOSTypesOptions#id + */ + public static ListOSTypesOptions id(long id) { + ListOSTypesOptions options = new ListOSTypesOptions(); + return options.id(id); + } + + + /** + * @see ListOSTypesOptions#OSCategoryId + */ + public static ListOSTypesOptions OSCategoryId(long id) { + ListOSTypesOptions options = new ListOSTypesOptions(); + return options.OSCategoryId(id); + } + + } +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSAsyncClientTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSAsyncClientTest.java new file mode 100644 index 0000000000..bfd1fbb96c --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSAsyncClientTest.java @@ -0,0 +1,101 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.features; + +import java.io.IOException; +import java.lang.reflect.Method; + +import org.jclouds.cloudstack.options.ListOSTypesOptions; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.functions.UnwrapOnlyNestedJsonValue; +import org.jclouds.http.functions.UnwrapOnlyNestedJsonValueInSet; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.testng.annotations.Test; + +import com.google.inject.TypeLiteral; + +/** + * Tests behavior of {@code GuestOSAsyncClient} + * + * @author Adrian Cole + */ +// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire +@Test(groups = "unit", testName = "GuestOSAsyncClientTest") +public class GuestOSAsyncClientTest extends BaseCloudStackAsyncClientTest { + + public void testGetOSType() throws SecurityException, NoSuchMethodException, IOException { + Method method = GuestOSAsyncClient.class.getMethod("getOSType", long.class); + HttpRequest httpRequest = processor.createRequest(method, 11l); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listOsTypes&id=11 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValueInSet.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListOSTypes() throws SecurityException, NoSuchMethodException, IOException { + Method method = GuestOSAsyncClient.class.getMethod("listOSTypes", ListOSTypesOptions[].class); + HttpRequest httpRequest = processor.createRequest(method); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listOsTypes HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + public void testListOSTypesOptions() throws SecurityException, NoSuchMethodException, IOException { + Method method = GuestOSAsyncClient.class.getMethod("listOSTypes", ListOSTypesOptions[].class); + HttpRequest httpRequest = processor.createRequest(method, ListOSTypesOptions.Builder.OSCategoryId(11)); + + assertRequestLineEquals(httpRequest, + "GET http://localhost:8080/client/api?response=json&command=listOsTypes&oscategoryid=11 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, UnwrapOnlyNestedJsonValue.class); + assertSaxResponseParserClassEquals(method, null); + assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class); + + checkFilters(httpRequest); + + } + + @Override + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { + }; + } +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSClientLiveTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSClientLiveTest.java new file mode 100644 index 0000000000..4e20f8678f --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/GuestOSClientLiveTest.java @@ -0,0 +1,60 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.features; + +import static com.google.common.collect.Iterables.getOnlyElement; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.Set; + +import org.jclouds.cloudstack.domain.OSType; +import org.jclouds.cloudstack.options.ListOSTypesOptions; +import org.testng.annotations.Test; + +/** + * Tests behavior of {@code GuestOSClientLiveTest} + * + * @author Adrian Cole + */ +@Test(groups = "live", sequential = true, testName = "GuestOSClientLiveTest") +public class GuestOSClientLiveTest extends BaseCloudStackClientLiveTest { + + public void testListOSTypes() throws Exception { + Set response = client.getGuestOSClient().listOSTypes(); + assert null != response; + assertTrue(response.size() >= 0); + for (OSType type : response) { + OSType newDetails = getOnlyElement(client.getGuestOSClient().listOSTypes( + ListOSTypesOptions.Builder.id(type.getId()))); + assertEquals(type.getId(), newDetails.getId()); + checkIP(type); + } + } + + protected void checkIP(OSType type) { + assertEquals(type.getId(), client.getGuestOSClient().getOSType(type.getId()).getId()); + assert type.getId() > 0 : type; + assert type.getOSCategoryId() > 0 : type; + assert type.getDescription() != null : type; + + } + +} diff --git a/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListOSTypesOptionsTest.java b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListOSTypesOptionsTest.java new file mode 100644 index 0000000000..e87b835584 --- /dev/null +++ b/sandbox-apis/cloudstack/src/test/java/org/jclouds/cloudstack/options/ListOSTypesOptionsTest.java @@ -0,0 +1,57 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.cloudstack.options; + +import static org.jclouds.cloudstack.options.ListOSTypesOptions.Builder.OSCategoryId; +import static org.jclouds.cloudstack.options.ListOSTypesOptions.Builder.id; +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +/** + * Tests behavior of {@code ListOSTypesOptions} + * + * @author Adrian Cole + */ +@Test(groups = "unit") +public class ListOSTypesOptionsTest { + + public void testId() { + ListOSTypesOptions options = new ListOSTypesOptions().id(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id")); + } + + public void testIdStatic() { + ListOSTypesOptions options = id(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id")); + } + + public void testOSCategoryId() { + ListOSTypesOptions options = new ListOSTypesOptions().OSCategoryId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("oscategoryid")); + } + + public void testOSCategoryIdStatic() { + ListOSTypesOptions options = OSCategoryId(6); + assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("oscategoryid")); + } +}