Add listPods API call, with supporting Pod domain object and unit tests.

This commit is contained in:
Richard Downer 2012-01-18 11:17:55 +00:00
parent e136c73739
commit dea284fbb3
9 changed files with 673 additions and 2 deletions

View File

@ -0,0 +1,270 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudstack.domain;
import com.google.gson.annotations.SerializedName;
/**
* Represents a Pod in CloudStack.
*
* @author Richard Downer
*/
public class Pod implements Comparable<Pod> {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id;
private String name;
private long zoneId;
private String zoneName;
private String gateway;
private String netmask;
private String startIp;
private String endIp;
private AllocationState allocationState;
private Builder() {}
/**
* @param id the ID of the Pod
*/
public Builder id(long id) {
this.id = id;
return this;
}
/**
* @param name the name of the Pod
*/
public Builder name(String name) {
this.name = name;
return this;
}
/**
* @param zoneId the Zone ID of the Pod
*/
public Builder zoneId(long zoneId) {
this.zoneId = zoneId;
return this;
}
/**
* @param zoneName the Zone name of the Pod
*/
public Builder zoneName(String zoneName) {
this.zoneName = zoneName;
return this;
}
/**
* @param gateway the gateway of the Pod
*/
public Builder gateway(String gateway) {
this.gateway = gateway;
return this;
}
/**
* @param netmask the netmask of the Pod
*/
public Builder netmask(String netmask) {
this.netmask = netmask;
return this;
}
/**
* @param startIp the starting IP for the Pod
*/
public Builder startIp(String startIp) {
this.startIp = startIp;
return this;
}
/**
* @param endIp the ending IP for the Pod
*/
public Builder endIp(String endIp) {
this.endIp = endIp;
return this;
}
/**
* @param allocationState the allocation state of the cluster
*/
public Builder allocationState(AllocationState allocationState) {
this.allocationState = allocationState;
return this;
}
/**
* Build the Pod object
* @return the Pod object
*/
public Pod build() {
return new Pod(id, name, zoneId, zoneName, gateway, netmask, startIp, endIp, allocationState);
}
}
private long id;
private String name;
@SerializedName("zoneid") private long zoneId;
@SerializedName("zonename") private String zoneName;
private String gateway;
private String netmask;
@SerializedName("startip") private String startIp;
@SerializedName("endip") private String endIp;
@SerializedName("allocationstate") private AllocationState allocationState;
/* Just for the serializer */
Pod() {}
public Pod(long id, String name, long zoneId, String zoneName, String gateway, String netmask, String startIp, String endIp, AllocationState allocationState) {
this.id = id;
this.name = name;
this.zoneId = zoneId;
this.zoneName = zoneName;
this.gateway = gateway;
this.netmask = netmask;
this.startIp = startIp;
this.endIp = endIp;
this.allocationState = allocationState;
}
/**
* @return id the ID of the Pod
*/
public long getId() {
return id;
}
/**
* @return name the name of the Pod
*/
public String getName() {
return name;
}
/**
* @return zoneId the Zone ID of the Pod
*/
public long getZoneId() {
return zoneId;
}
/**
* @return zoneName the Zone name of the Pod
*/
public String getZoneName() {
return zoneName;
}
/**
* @return gateway the gateway of the Pod
*/
public String getGateway() {
return gateway;
}
/**
* @return netmask the netmask of the Pod
*/
public String getNetmask() {
return netmask;
}
/**
* @return startIp the starting IP for the Pod
*/
public String getStartIp() {
return startIp;
}
/**
* @return endIp the ending IP for the Pod
*/
public String getEndIp() {
return endIp;
}
/**
* @param allocationState the allocation state of the cluster
*/
public AllocationState getAllocationState() {
return allocationState;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pod pod = (Pod) o;
if (id != pod.id) return false;
if (zoneId != pod.zoneId) return false;
if (allocationState != pod.allocationState) return false;
if (endIp != null ? !endIp.equals(pod.endIp) : pod.endIp != null) return false;
if (gateway != null ? !gateway.equals(pod.gateway) : pod.gateway != null) return false;
if (name != null ? !name.equals(pod.name) : pod.name != null) return false;
if (netmask != null ? !netmask.equals(pod.netmask) : pod.netmask != null) return false;
if (startIp != null ? !startIp.equals(pod.startIp) : pod.startIp != null) return false;
if (zoneName != null ? !zoneName.equals(pod.zoneName) : pod.zoneName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (int) (zoneId ^ (zoneId >>> 32));
result = 31 * result + (zoneName != null ? zoneName.hashCode() : 0);
result = 31 * result + (gateway != null ? gateway.hashCode() : 0);
result = 31 * result + (netmask != null ? netmask.hashCode() : 0);
result = 31 * result + (startIp != null ? startIp.hashCode() : 0);
result = 31 * result + (endIp != null ? endIp.hashCode() : 0);
result = 31 * result + (allocationState != null ? allocationState.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Pod{" +
"id=" + id +
", name='" + name + '\'' +
", zoneId=" + zoneId +
", zoneName='" + zoneName + '\'' +
", gateway='" + gateway + '\'' +
", netmask='" + netmask + '\'' +
", startIp='" + startIp + '\'' +
", endIp='" + endIp + '\'' +
", allocationState=" + allocationState +
'}';
}
@Override
public int compareTo(Pod other) {
return Long.valueOf(this.id).compareTo(other.id);
}
}

View File

@ -18,15 +18,29 @@
*/
package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.Pod;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.ListPodsOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Set;
/**
* Provides asynchronous access to CloudStack Account features available to Global
* Provides asynchronous access to CloudStack Pod features available to Global
* Admin users.
*
* @author Adrian Cole, Andrei Savu
* @author Richard Downer
* @see <a href=
* "http://download.cloud.com/releases/2.2.0/api_2.2.12/TOC_Global_Admin.html"
* />
@ -35,4 +49,25 @@ import org.jclouds.rest.annotations.RequestFilters;
@QueryParams(keys = "response", values = "json")
public interface GlobalPodAsyncClient {
/**
* @see PodClient#listPods
*/
@GET
@QueryParams(keys = "command", values = "listPods")
@SelectJson("pod")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Pod>> listPods(ListPodsOptions... options);
/**
* @see PodClient#getPod
*/
@GET
@QueryParams(keys = "command", values = "listPods")
@SelectJson("pod")
@OnlyElement
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Pod> getPod(@QueryParam("id") long id);
}

View File

@ -18,8 +18,11 @@
*/
package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.Pod;
import org.jclouds.cloudstack.options.ListPodsOptions;
import org.jclouds.concurrent.Timeout;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
@ -34,4 +37,22 @@ import java.util.concurrent.TimeUnit;
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface GlobalPodClient {
/**
* Lists pods
*
* @param options
* if present, how to constrain the list.
* @return pods matching query, or empty set, if no pods are found
*/
Set<Pod> listPods(ListPodsOptions... options);
/**
* get a specific pod by id
*
* @param id
* pod to get
* @return pod or null if not found
*/
Pod getPod(long id);
}

View File

@ -0,0 +1,83 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudstack.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.AllocationState;
import org.jclouds.http.options.BaseHttpRequestOptions;
/**
* Options to the GlobalPodClient.listPods API call.
*
* @author Richard Downer
*/
public class ListPodsOptions extends BaseHttpRequestOptions {
public static final ListPodsOptions NONE = new ListPodsOptions();
public static class Builder {
public static ListPodsOptions allocationState(AllocationState allocationState) {
return new ListPodsOptions().allocationState(allocationState);
}
public static ListPodsOptions id(long id) {
return new ListPodsOptions().id(id);
}
public static ListPodsOptions keyword(String keyword) {
return new ListPodsOptions().keyword(keyword);
}
public static ListPodsOptions name(String name) {
return new ListPodsOptions().name(name);
}
public static ListPodsOptions zoneId(long zoneId) {
return new ListPodsOptions().zoneId(zoneId);
}
}
public ListPodsOptions allocationState(AllocationState allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString()));
return this;
}
public ListPodsOptions id(long id) {
this.queryParameters.replaceValues("id", ImmutableSet.of(id + ""));
return this;
}
public ListPodsOptions keyword(String keyword) {
this.queryParameters.replaceValues("keyword", ImmutableSet.of(keyword));
return this;
}
public ListPodsOptions name(String name) {
this.queryParameters.replaceValues("name", ImmutableSet.of(name));
return this;
}
public ListPodsOptions zoneId(long zoneId) {
this.queryParameters.replaceValues("zoneid", ImmutableSet.of(zoneId + ""));
return this;
}
}

View File

@ -18,9 +18,19 @@
*/
package org.jclouds.cloudstack.features;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.AllocationState;
import org.jclouds.cloudstack.domain.Pod;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Test the CloudStack PodClient
*
@ -29,6 +39,68 @@ import org.testng.annotations.Test;
@Test(groups = "unit", testName = "GlobalPodClientExpectTest")
public class GlobalPodClientExpectTest extends BaseCloudStackRestClientExpectTest<GlobalPodClient> {
public void testListPodsWhenResponseIs2xx() {
GlobalPodClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=listPods&apiKey=identity&signature=asx1px2NQkW4R44%2FDgdozuu9wQg%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/listpodsresponse.json"))
.build());
Pod pod1 = Pod.builder()
.id(1)
.name("Dev Pod 1")
.zoneId(1)
.zoneName("Dev Zone 1")
.gateway("10.26.26.254")
.netmask("255.255.255.0")
.startIp("10.26.26.50")
.endIp("10.26.26.100")
.allocationState(AllocationState.ENABLED)
.build();
Pod pod2 = Pod.builder()
.id(2)
.name("Dev Pod 2")
.zoneId(2)
.zoneName("Dev Zone 2")
.gateway("10.22.22.254")
.netmask("255.255.255.0")
.startIp("10.22.22.25")
.endIp("10.22.22.50")
.allocationState(AllocationState.ENABLED)
.build();
assertEquals(client.listPods(), ImmutableSet.of(pod1, pod2));
}
public void testListPodsWhenResponseIs404() {
GlobalPodClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=listPods&apiKey=identity&signature=asx1px2NQkW4R44%2FDgdozuu9wQg%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(404)
.build());
assertEquals(client.listPods(), ImmutableSet.of());
}
@Override
protected GlobalPodClient clientFrom(CloudStackContext context) {
return context.getGlobalContext().getApi().getPodClient();

View File

@ -18,8 +18,17 @@
*/
package org.jclouds.cloudstack.features;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.jclouds.cloudstack.domain.AllocationState;
import org.jclouds.cloudstack.domain.Pod;
import org.jclouds.cloudstack.options.ListPodsOptions;
import org.testng.annotations.Test;
import java.util.Set;
import static org.testng.Assert.*;
/**
* Tests behavior of {@code GlobalPodClient}
*
@ -28,4 +37,26 @@ import org.testng.annotations.Test;
@Test(groups = "live", singleThreaded = true, testName = "GlobalPodClientLiveTest")
public class GlobalPodClientLiveTest extends BaseCloudStackClientLiveTest {
public void testListPods() throws Exception {
Set<Pod> response = globalAdminClient.getPodClient().listPods();
assert null != response;
long podCount = response.size();
assertTrue(podCount >= 0);
for (Pod pod : response) {
Pod newDetails = Iterables.getOnlyElement(globalAdminClient.getPodClient().listPods(
ListPodsOptions.Builder.id(pod.getId())));
assertEquals(pod, newDetails);
assertEquals(pod, globalAdminClient.getPodClient().getPod(pod.getId()));
assertFalse(pod.getId() <= 0);
assertFalse(Strings.isNullOrEmpty(pod.getName()));
assertFalse(pod.getZoneId() <= 0);
assertFalse(Strings.isNullOrEmpty(pod.getZoneName()));
assertFalse(Strings.isNullOrEmpty(pod.getGateway()));
assertFalse(Strings.isNullOrEmpty(pod.getNetmask()));
assertFalse(Strings.isNullOrEmpty(pod.getStartIp()));
assertFalse(Strings.isNullOrEmpty(pod.getEndIp()));
assertNotEquals(pod.getAllocationState(), AllocationState.UNKNOWN);
}
}
}

View File

@ -0,0 +1,86 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudstack.options;
import com.google.common.collect.ImmutableList;
import org.jclouds.cloudstack.domain.AllocationState;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.options.ListPodsOptions.Builder.*;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ListPodsOptions}
*
* @author Richard Downer
*/
@Test(groups = "unit")
public class ListPodsOptionsTest {
public void testId() {
ListPodsOptions options = new ListPodsOptions().id(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id"));
}
public void testIdStatic() {
ListPodsOptions options = id(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("id"));
}
public void testAllocationState() {
ListPodsOptions options = new ListPodsOptions().allocationState(AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
public void testAllocationStateStatic() {
ListPodsOptions options = allocationState(AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
public void testKeyword() {
ListPodsOptions options = new ListPodsOptions().keyword("fred");
assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("keyword"));
}
public void testKeywordStatic() {
ListPodsOptions options = keyword("fred");
assertEquals(ImmutableList.of("fred"), options.buildQueryParameters().get("keyword"));
}
public void testName() {
ListPodsOptions options = new ListPodsOptions().name("bob");
assertEquals(ImmutableList.of("bob"), options.buildQueryParameters().get("name"));
}
public void testNameStatic() {
ListPodsOptions options = name("bob");
assertEquals(ImmutableList.of("bob"), options.buildQueryParameters().get("name"));
}
public void testZoneId() {
ListPodsOptions options = new ListPodsOptions().zoneId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("zoneid"));
}
public void testZoneIdStatic() {
ListPodsOptions options = zoneId(6);
assertEquals(ImmutableList.of("6"), options.buildQueryParameters().get("zoneid"));
}
}

View File

@ -0,0 +1,72 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds 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.cloudstack.parse;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.AllocationState;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.Pod;
import org.jclouds.cloudstack.domain.Zone;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import java.util.Set;
/**
*
* @author Richard Downer
*/
@Test(groups = "unit")
public class ListPodsResponseTest extends BaseSetParserTest<Pod> {
@Override
public String resource() {
return "/listpodsresponse.json";
}
@Override
@SelectJson("pod")
public Set<Pod> expected() {
Pod pod1 = Pod.builder()
.id(1)
.name("Dev Pod 1")
.zoneId(1)
.zoneName("Dev Zone 1")
.gateway("10.26.26.254")
.netmask("255.255.255.0")
.startIp("10.26.26.50")
.endIp("10.26.26.100")
.allocationState(AllocationState.ENABLED)
.build();
Pod pod2 = Pod.builder()
.id(2)
.name("Dev Pod 2")
.zoneId(2)
.zoneName("Dev Zone 2")
.gateway("10.22.22.254")
.netmask("255.255.255.0")
.startIp("10.22.22.25")
.endIp("10.22.22.50")
.allocationState(AllocationState.ENABLED)
.build();
return ImmutableSet.of(pod1, pod2);
}
}

View File

@ -0,0 +1 @@
{ "listpodsresponse" : { "count":2 ,"pod" : [ {"id":1,"name":"Dev Pod 1","zoneid":1,"zonename":"Dev Zone 1","gateway":"10.26.26.254","netmask":"255.255.255.0","startip":"10.26.26.50","endip":"10.26.26.100","allocationstate":"Enabled"}, {"id":2,"name":"Dev Pod 2","zoneid":2,"zonename":"Dev Zone 2","gateway":"10.22.22.254","netmask":"255.255.255.0","startip":"10.22.22.25","endip":"10.22.22.50","allocationstate":"Enabled"} ] } }