Merge pull request #303 from richardcloudsoft/cs-hosts

Add Hosts API listClusters call; domain objects, options, tests, etc.
This commit is contained in:
Adrian Cole 2012-01-10 09:51:36 -08:00
commit cecff8f681
8 changed files with 619 additions and 0 deletions

View File

@ -0,0 +1,248 @@
/**
* 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;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
/**
* Represents a CloudStack Cluster.
*
* @author Richard Downer
*/
public class Cluster implements Comparable<Cluster> {
public enum ManagedState {
MANAGED,
PREPARE_UNMANAGED,
UNMANAGED,
PREPARE_UNMANAGED_ERROR,
UNRECOGNIZED;
public static ManagedState fromValue(String value) {
try{
return valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, value));
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
@Override
public String toString() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id;
private Host.AllocationState allocationState;
private Host.ClusterType clusterType;
private String hypervisor;
private ManagedState managedState;
private String name;
private long podId;
private String podName;
private long zoneId;
private String zoneName;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder allocationState(Host.AllocationState allocationState) {
this.allocationState = allocationState;
return this;
}
public Builder clusterType(Host.ClusterType clusterType) {
this.clusterType = clusterType;
return this;
}
public Builder hypervisor(String hypervisor) {
this.hypervisor = hypervisor;
return this;
}
public Builder managedState(ManagedState managedState) {
this.managedState = managedState;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder podId(long podId) {
this.podId = podId;
return this;
}
public Builder podName(String podName) {
this.podName = podName;
return this;
}
public Builder zoneId(long zoneId) {
this.zoneId = zoneId;
return this;
}
public Builder zoneName(String zoneName) {
this.zoneName = zoneName;
return this;
}
public Cluster build() {
return new Cluster(id, allocationState, clusterType, hypervisor, managedState, name, podId, podName, zoneId, zoneName);
}
}
private long id;
@SerializedName("allocationstate") private Host.AllocationState allocationState;
@SerializedName("clustertype") private Host.ClusterType clusterType;
@SerializedName("hypervisortype") private String hypervisor;
@SerializedName("managedstate") private ManagedState managedState;
private String name;
@SerializedName("podid") private long podId;
@SerializedName("podname") private String podName;
@SerializedName("zoneid") private long zoneId;
@SerializedName("zonename") private String zoneName;
// Just for the serializer
Cluster() {}
public Cluster(long id, Host.AllocationState allocationState, Host.ClusterType clusterType, String hypervisor, ManagedState managedState, String name, long podId, String podName, long zoneId, String zoneName) {
this.id = id;
this.allocationState = allocationState;
this.clusterType = clusterType;
this.hypervisor = hypervisor;
this.managedState = managedState;
this.name = name;
this.podId = podId;
this.podName = podName;
this.zoneId = zoneId;
this.zoneName = zoneName;
}
public long getId() {
return id;
}
public Host.AllocationState getAllocationState() {
return allocationState;
}
public Host.ClusterType getClusterType() {
return clusterType;
}
public String getHypervisor() {
return hypervisor;
}
public ManagedState getManagedState() {
return managedState;
}
public String getName() {
return name;
}
public long getPodId() {
return podId;
}
public String getPodName() {
return podName;
}
public long getZoneId() {
return zoneId;
}
public String getZoneName() {
return zoneName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cluster cluster = (Cluster) o;
if (id != cluster.id) return false;
if (podId != cluster.podId) return false;
if (zoneId != cluster.zoneId) return false;
if (allocationState != cluster.allocationState) return false;
if (clusterType != cluster.clusterType) return false;
if (hypervisor != null ? !hypervisor.equals(cluster.hypervisor) : cluster.hypervisor != null) return false;
if (managedState != cluster.managedState) return false;
if (name != null ? !name.equals(cluster.name) : cluster.name != null) return false;
if (podName != null ? !podName.equals(cluster.podName) : cluster.podName != null) return false;
if (zoneName != null ? !zoneName.equals(cluster.zoneName) : cluster.zoneName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (allocationState != null ? allocationState.hashCode() : 0);
result = 31 * result + (clusterType != null ? clusterType.hashCode() : 0);
result = 31 * result + (hypervisor != null ? hypervisor.hashCode() : 0);
result = 31 * result + (managedState != null ? managedState.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (int) (podId ^ (podId >>> 32));
result = 31 * result + (podName != null ? podName.hashCode() : 0);
result = 31 * result + (int) (zoneId ^ (zoneId >>> 32));
result = 31 * result + (zoneName != null ? zoneName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Cluster{" +
"id=" + id +
", allocationState=" + allocationState +
", clusterType=" + clusterType +
", hypervisor='" + hypervisor + '\'' +
", managedState=" + managedState +
", name='" + name + '\'' +
", podId=" + podId +
", podName='" + podName + '\'' +
", zoneId=" + zoneId +
", zoneName='" + zoneName + '\'' +
'}';
}
@Override
public int compareTo(Cluster other) {
return Long.valueOf(this.id).compareTo(other.id);
}
}

View File

@ -19,8 +19,10 @@
package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.ListClustersOptions;
import org.jclouds.cloudstack.options.ListHostsOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.QueryParams;
@ -54,4 +56,14 @@ public interface GlobalHostAsyncClient {
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Host>> listHosts(ListHostsOptions... options);
/**
* @see GlobalHostClient#listClusters
*/
@GET
@QueryParams(keys = "command", values = "listClusters")
@SelectJson("cluster")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Cluster>> listClusters(ListClustersOptions... options);
}

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.cloudstack.options.ListClustersOptions;
import org.jclouds.cloudstack.options.ListHostsOptions;
import org.jclouds.concurrent.Timeout;
@ -46,4 +48,11 @@ public interface GlobalHostClient {
*/
Set<Host> listHosts(ListHostsOptions... options);
/**
* Lists clusters
*
* @param options if present, how to constrain the list
* @return clusters matching query, or empty set if no clusters match
*/
Set<Cluster> listClusters(ListClustersOptions... options);
}

View File

@ -0,0 +1,122 @@
/**
* 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.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.http.options.BaseHttpRequestOptions;
/**
* Options used to control what cluster information is returned
*
* @author Richard Downer
* @see <a
* href="http://download.cloud.com/releases/2.2.0/api_2.2.12/global_admin/listClusters.html"
* />
*/
public class ListClustersOptions extends BaseHttpRequestOptions {
public static final ListHostsOptions NONE = new ListHostsOptions();
public ListClustersOptions allocationState(Host.AllocationState allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString()));
return this;
}
public ListClustersOptions clusterType(Host.ClusterType clusterType) {
this.queryParameters.replaceValues("clustertype", ImmutableSet.of(clusterType.toString()));
return this;
}
public ListClustersOptions hypervisor(String hypervisor) {
this.queryParameters.replaceValues("hypervisor", ImmutableSet.of(hypervisor));
return this;
}
public ListClustersOptions id(long id) {
this.queryParameters.replaceValues("id", ImmutableSet.of(id + ""));
return this;
}
public ListClustersOptions keyword(String keyword) {
this.queryParameters.replaceValues("keyword", ImmutableSet.of(keyword));
return this;
}
public ListClustersOptions managedState(Cluster.ManagedState managedState) {
this.queryParameters.replaceValues("managedstate", ImmutableSet.of(managedState.toString()));
return this;
}
public ListClustersOptions name(String name) {
this.queryParameters.replaceValues("name", ImmutableSet.of(name));
return this;
}
public ListClustersOptions podId(long podId) {
this.queryParameters.replaceValues("podid", ImmutableSet.of(podId + ""));
return this;
}
public ListClustersOptions zoneId(long zoneId) {
this.queryParameters.replaceValues("zoneid", ImmutableSet.of(zoneId + ""));
return this;
}
public static class Builder {
public static ListClustersOptions allocationState(Host.AllocationState allocationState) {
return new ListClustersOptions().allocationState(allocationState);
}
public static ListClustersOptions clusterType(Host.ClusterType clusterType) {
return new ListClustersOptions().clusterType(clusterType);
}
public static ListClustersOptions hypervisor(String hypervisor) {
return new ListClustersOptions().hypervisor(hypervisor);
}
public static ListClustersOptions id(long id) {
return new ListClustersOptions().id(id);
}
public static ListClustersOptions keyword(String keyword) {
return new ListClustersOptions().keyword(keyword);
}
public static ListClustersOptions managedState(Cluster.ManagedState managedState) {
return new ListClustersOptions().managedState(managedState);
}
public static ListClustersOptions name(String name) {
return new ListClustersOptions().name(name);
}
public static ListClustersOptions podId(long podId) {
return new ListClustersOptions().podId(podId);
}
public static ListClustersOptions zoneId(long zoneId) {
return new ListClustersOptions().zoneId(zoneId);
}
}
}

View File

@ -18,10 +18,13 @@
*/
package org.jclouds.cloudstack.features;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import com.google.common.base.Strings;
import org.jclouds.cloudstack.domain.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.testng.annotations.Test;
@ -61,4 +64,29 @@ public class GlobalHostClientLiveTest extends BaseCloudStackClientLiveTest {
}
}
@Test(groups = "live", enabled = true)
public void testListClusters() throws Exception {
assertTrue(globalAdminEnabled, "Test cannot run without global admin identity and credentials");
Set<Cluster> clusters = globalAdminClient.getHostClient().listClusters();
assert clusters.size() > 0 : clusters;
for(Cluster cluster : clusters) {
checkCluster(cluster);
}
}
private void checkCluster(Cluster cluster) {
assertTrue(cluster.getId() > 0);
assertFalse(Strings.isNullOrEmpty(cluster.getName()));
assertTrue(cluster.getAllocationState() != Host.AllocationState.UNKNOWN);
assertTrue(cluster.getClusterType() != Host.ClusterType.UNKNOWN);
assertFalse(Strings.isNullOrEmpty(cluster.getHypervisor()));
assertTrue(cluster.getManagedState() != Cluster.ManagedState.UNRECOGNIZED);
assertTrue(cluster.getPodId() > 0);
assertFalse(Strings.isNullOrEmpty(cluster.getPodName()));
assertTrue(cluster.getZoneId() > 0);
assertFalse(Strings.isNullOrEmpty(cluster.getZoneName()));
}
}

View File

@ -0,0 +1,126 @@
/**
* 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.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.options.ListClustersOptions.Builder.*;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ListClustersOptions}
*
* @author Richard Downer
*/
@Test(groups = "unit")
public class ListClustersOptionsTest {
public void testAllocationState() {
ListClustersOptions options = new ListClustersOptions().allocationState(Host.AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
public void testAllocationStateStatic() {
ListClustersOptions options = allocationState(Host.AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
public void testClusterType() {
ListClustersOptions options = new ListClustersOptions().clusterType(Host.ClusterType.CLOUD_MANAGED);
assertEquals(ImmutableList.of("CloudManaged"), options.buildQueryParameters().get("clustertype"));
}
public void testClusterTypeStatic() {
ListClustersOptions options = clusterType(Host.ClusterType.CLOUD_MANAGED);
assertEquals(ImmutableList.of("CloudManaged"), options.buildQueryParameters().get("clustertype"));
}
public void testHypervisor() {
ListClustersOptions options = new ListClustersOptions().hypervisor("XenServer");
assertEquals(ImmutableList.of("XenServer"), options.buildQueryParameters().get("hypervisor"));
}
public void testHypervisorStatic() {
ListClustersOptions options = hypervisor("XenServer");
assertEquals(ImmutableList.of("XenServer"), options.buildQueryParameters().get("hypervisor"));
}
public void testId() {
ListClustersOptions options = new ListClustersOptions().id(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("id"));
}
public void testIdStatic() {
ListClustersOptions options = id(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("id"));
}
public void testKeyword() {
ListClustersOptions options = new ListClustersOptions().keyword("Enabled");
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("keyword"));
}
public void testKeywordStatic() {
ListClustersOptions options = keyword("Enabled");
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("keyword"));
}
public void testManagedState() {
ListClustersOptions options = new ListClustersOptions().managedState(Cluster.ManagedState.PREPARE_UNMANAGED);
assertEquals(ImmutableList.of("PrepareUnmanaged"), options.buildQueryParameters().get("managedstate"));
}
public void testManagedStateStatic() {
ListClustersOptions options = managedState(Cluster.ManagedState.PREPARE_UNMANAGED);
assertEquals(ImmutableList.of("PrepareUnmanaged"), options.buildQueryParameters().get("managedstate"));
}
public void testName() {
ListClustersOptions options = new ListClustersOptions().name("Host Name");
assertEquals(ImmutableList.of("Host Name"), options.buildQueryParameters().get("name"));
}
public void testNameStatic() {
ListClustersOptions options = name("Host Name");
assertEquals(ImmutableList.of("Host Name"), options.buildQueryParameters().get("name"));
}
public void testPodId() {
ListClustersOptions options = new ListClustersOptions().podId(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("podid"));
}
public void testPodIdStatic() {
ListClustersOptions options = podId(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("podid"));
}
public void testZoneId() {
ListClustersOptions options = new ListClustersOptions().zoneId(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("zoneid"));
}
public void testZoneIdStatic() {
ListClustersOptions options = zoneId(42L);
assertEquals(ImmutableList.of("42"), options.buildQueryParameters().get("zoneid"));
}
}

View File

@ -0,0 +1,73 @@
/**
* 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 com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.cloudstack.config.CloudStackParserModule;
import org.jclouds.cloudstack.domain.Cluster;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.json.BaseSetParserTest;
import org.jclouds.json.config.GsonModule;
import org.jclouds.rest.annotations.SelectJson;
import org.testng.annotations.Test;
import java.util.Set;
/**
* @author Richard Downer
*/
@Test(groups = "unit")
public class ListClustersResponseTest extends BaseSetParserTest<Cluster> {
@Override
public String resource() {
return "/listclustersresponse.json";
}
@Override
@SelectJson("cluster")
public Set<Cluster> expected() {
Cluster cluster1 = Cluster.builder()
.id(1)
.name("Xen Clust 1")
.podId(1).podName("Dev Pod 1")
.zoneId(1).zoneName("Dev Zone 1")
.hypervisor("XenServer")
.clusterType(Host.ClusterType.CLOUD_MANAGED)
.allocationState(Host.AllocationState.ENABLED)
.managedState(Cluster.ManagedState.MANAGED)
.build();
Cluster cluster2 = Cluster.builder()
.id(2)
.name("Xen Clust 1")
.podId(2).podName("Dev Pod 2")
.zoneId(2).zoneName("Dev Zone 2")
.hypervisor("XenServer")
.clusterType(Host.ClusterType.CLOUD_MANAGED)
.allocationState(Host.AllocationState.ENABLED)
.managedState(Cluster.ManagedState.MANAGED)
.build();
return ImmutableSet.of(cluster1, cluster2);
}
}

View File

@ -0,0 +1 @@
{ "listclustersresponse" : { "count":2 ,"cluster" : [ {"id":1,"name":"Xen Clust 1","podid":1,"podname":"Dev Pod 1","zoneid":1,"zonename":"Dev Zone 1","hypervisortype":"XenServer","clustertype":"CloudManaged","allocationstate":"Enabled","managedstate":"Managed"}, {"id":2,"name":"Xen Clust 1","podid":2,"podname":"Dev Pod 2","zoneid":2,"zonename":"Dev Zone 2","hypervisortype":"XenServer","clustertype":"CloudManaged","allocationstate":"Enabled","managedstate":"Managed"} ] } }