Merge pull request #234 from andreisavu/user-enums-for-host

Use enums for the Host domain object
This commit is contained in:
Adrian Cole 2011-12-12 14:51:56 -08:00
commit 2aca11d94b
5 changed files with 155 additions and 45 deletions

View File

@ -18,9 +18,15 @@
*/
package org.jclouds.cloudstack.domain;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName;
import org.omg.CORBA.UNKNOWN;
import java.util.Date;
import java.util.Map;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
/**
* Represents a host issued by Cloudstack
@ -29,18 +35,120 @@ import java.util.Date;
*/
public class Host implements Comparable<Host> {
public static enum AllocationState {
DISABLED,
ENABLED,
UNKNOWN;
public static AllocationState fromValue(String value) {
try{
return valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static enum ClusterType {
CLOUD_MANAGED,
EXTERNAL_MANAGED,
UNKNOWN;
public static ClusterType fromValue(String value) {
try {
return valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, value));
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static enum State {
CONNECTING,
UP,
DOWN,
DISCONNECTED,
UPDATING,
PREPARE_FOR_MAINTENANCE,
ERROR_IN_MAINTENANCE,
MAINTENANCE,
ALERT,
REMOVED,
REBALANCING,
UNKNOWN;
public static State fromValue(String value) {
try {
return valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, value));
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static enum Type {
STORAGE,
ROUTING,
SECONDARY_STORAGE,
SECONDARY_STORAGE_CMD_EXECUTOR,
CONSOLE_PROXY,
EXTERNAL_FIREWALL,
EXTERNAL_LOAD_BALANCER,
PXE_SERVER,
TRAFFIC_MONITOR,
EXTERNAL_DHCP,
SECONDARY_STORAGE_VM,
LOCAL_SECONDARY_STORAGE,
UNKNOWN;
public static Type fromValue(String value) {
try {
if (value.equals("SecondaryStorageVM")) {
return SECONDARY_STORAGE_VM;
}
return valueOf(UPPER_CAMEL.to(UPPER_UNDERSCORE, value));
} catch(IllegalArgumentException e) {
return UNKNOWN;
}
}
@Override
public String toString() {
if (this == SECONDARY_STORAGE_VM) {
return "SecondaryStorageVM"; // note the inconsistency in VM naming
}
return UPPER_UNDERSCORE.to(UPPER_CAMEL, name());
}
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id;
private String allocationState;
private AllocationState allocationState;
private int averageLoad;
private String capabilities;
private long clusterId;
private String clusterName;
private String clusterType;
private ClusterType clusterType;
private String cpuAllocated;
private int cpuNumber;
private int cpuSpeed;
@ -71,8 +179,8 @@ public class Host implements Comparable<Host> {
private long podId;
private String podName;
private Date removed;
private String state;
private String type;
private State state;
private Type type;
private String version;
private long zoneId;
private String zoneName;
@ -82,7 +190,7 @@ public class Host implements Comparable<Host> {
return this;
}
public Builder allocationState(String allocationState) {
public Builder allocationState(AllocationState allocationState) {
this.allocationState = allocationState;
return this;
}
@ -107,7 +215,7 @@ public class Host implements Comparable<Host> {
return this;
}
public Builder clusterType(String clusterType) {
public Builder clusterType(ClusterType clusterType) {
this.clusterType = clusterType;
return this;
}
@ -262,12 +370,12 @@ public class Host implements Comparable<Host> {
return this;
}
public Builder state(String state) {
public Builder state(State state) {
this.state = state;
return this;
}
public Builder type(String type) {
public Builder type(Type type) {
this.type = type;
return this;
}
@ -303,7 +411,7 @@ public class Host implements Comparable<Host> {
private long id;
@SerializedName("allocationstate")
private String allocationState;
private AllocationState allocationState;
@SerializedName("averageload")
private int averageLoad;
@SerializedName("capabilities")
@ -313,7 +421,7 @@ public class Host implements Comparable<Host> {
@SerializedName("clustername")
private String clusterName;
@SerializedName("clustertype")
private String clusterType;
private ClusterType clusterType;
@SerializedName("cpuallocated")
private String cpuAllocated;
@SerializedName("cpunumber")
@ -368,8 +476,8 @@ public class Host implements Comparable<Host> {
@SerializedName("podname")
private String podName;
private Date removed;
private String state;
private String type;
private State state;
private Type type;
private String version;
@SerializedName("zoneid")
private long zoneId;
@ -380,8 +488,8 @@ public class Host implements Comparable<Host> {
Host() {
}
public Host(long id, String allocationState, int averageLoad, String capabilities,
long clusterId, String clusterName, String clusterType, String cpuAllocated,
public Host(long id, AllocationState allocationState, int averageLoad, String capabilities,
long clusterId, String clusterName, ClusterType clusterType, String cpuAllocated,
int cpuNumber, int cpuSpeed, String cpuUsed, float cpuWithOverProvisioning,
Date created, Date disconnected, long diskSizeAllocated, long diskSizeTotal,
String events, boolean hasEnoughCapacity, String hostTags, String hypervisor,
@ -389,7 +497,7 @@ public class Host implements Comparable<Host> {
Date lastPinged, long managementServerId, long memoryAllocated, long memoryTotal,
long memoryUsed, String name, long networkKbsRead, long networkKbsWrite,
long osCategoryId, long osCategoryName, long podId, String podName, Date removed,
String state, String type, String version, long zoneId, String zoneName) {
State state, Type type, String version, long zoneId, String zoneName) {
this.id = id;
this.allocationState = allocationState;
this.averageLoad = averageLoad;
@ -438,7 +546,7 @@ public class Host implements Comparable<Host> {
return id;
}
public String getAllocationState() {
public AllocationState getAllocationState() {
return allocationState;
}
@ -458,7 +566,7 @@ public class Host implements Comparable<Host> {
return clusterName;
}
public String getClusterType() {
public ClusterType getClusterType() {
return clusterType;
}
@ -582,11 +690,11 @@ public class Host implements Comparable<Host> {
return removed;
}
public String getState() {
public State getState() {
return state;
}
public String getType() {
public Type getType() {
return type;
}

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudstack.domain.Host;
import org.jclouds.cloudstack.domain.NetworkType;
import org.jclouds.cloudstack.domain.TrafficType;
@ -45,8 +46,8 @@ public class ListHostsOptions extends AccountInDomainOptions {
/**
* @param allocationState list hosts by allocation state
*/
public ListHostsOptions allocationState(String allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState));
public ListHostsOptions allocationState(Host.AllocationState allocationState) {
this.queryParameters.replaceValues("allocationstate", ImmutableSet.of(allocationState.toString()));
return this;
}
@ -109,8 +110,8 @@ public class ListHostsOptions extends AccountInDomainOptions {
/**
* @param type the type of the host
*/
public ListHostsOptions type(String type) {
this.queryParameters.replaceValues("type", ImmutableSet.of(type));
public ListHostsOptions type(Host.Type type) {
this.queryParameters.replaceValues("type", ImmutableSet.of(type.toString()));
return this;
}
@ -159,7 +160,7 @@ public class ListHostsOptions extends AccountInDomainOptions {
/**
* @see ListHostsOptions#allocationState
*/
public static ListHostsOptions allocationState(String allocationState) {
public static ListHostsOptions allocationState(Host.AllocationState allocationState) {
ListHostsOptions options = new ListHostsOptions();
return options.allocationState(allocationState);
}
@ -223,7 +224,7 @@ public class ListHostsOptions extends AccountInDomainOptions {
/**
* @see ListHostsOptions#type
*/
public static ListHostsOptions type(String type) {
public static ListHostsOptions type(Host.Type type) {
ListHostsOptions options = new ListHostsOptions();
return options.type(type);
}

View File

@ -48,17 +48,17 @@ public class GlobalHostClientLiveTest extends BaseCloudStackClientLiveTest {
}
private void checkHost(Host host) {
if (host.getType().equals("Routing")) {
if (host.getType() == Host.Type.ROUTING) {
assert host.getCpuNumber() > 0;
assert host.getAverageLoad() >= 0;
assert host.getHypervisor() != null;
}
assert host.getAllocationState() != null;
assert host.getEvents() != null;
if (host.getType().equals("SecondaryStorageVM")) {
if (host.getType() == Host.Type.SECONDARY_STORAGE_VM) {
assert host.getName().startsWith("s-");
}
if (host.getType().equals("ConsoleProxy")) {
if (host.getType() == Host.Type.CONSOLE_PROXY) {
assert host.getName().startsWith("v-");
}
}

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.options;
import com.google.common.collect.ImmutableList;
import org.jclouds.cloudstack.domain.Host;
import org.testng.annotations.Test;
import static org.jclouds.cloudstack.options.ListHostsOptions.Builder.allocationState;
@ -54,12 +55,12 @@ public class ListHostsOptionsTest {
}
public void testAllocationState() {
ListHostsOptions options = new ListHostsOptions().allocationState("Enabled");
ListHostsOptions options = new ListHostsOptions().allocationState(Host.AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
public void testAllocationStateStatic() {
ListHostsOptions options = allocationState("Enabled");
ListHostsOptions options = allocationState(Host.AllocationState.ENABLED);
assertEquals(ImmutableList.of("Enabled"), options.buildQueryParameters().get("allocationstate"));
}
@ -134,12 +135,12 @@ public class ListHostsOptionsTest {
}
public void testType() {
ListHostsOptions options = new ListHostsOptions().type("Routing");
ListHostsOptions options = new ListHostsOptions().type(Host.Type.ROUTING);
assertEquals(ImmutableList.of("Routing"), options.buildQueryParameters().get("type"));
}
public void testTypeStatic() {
ListHostsOptions options = type("Routing");
ListHostsOptions options = type(Host.Type.ROUTING);
assertEquals(ImmutableList.of("Routing"), options.buildQueryParameters().get("type"));
}

View File

@ -64,8 +64,8 @@ public class ListHostsResponseTest extends BaseSetParserTest<Host> {
Host.builder()
.id(1L)
.name("cs2-xevsrv.alucloud.local")
.state("Up")
.type("Routing")
.state(Host.State.UP)
.type(Host.Type.ROUTING)
.ipAddress("10.26.26.107")
.zoneId(1)
.zoneName("Dev Zone 1")
@ -88,21 +88,21 @@ public class ListHostsResponseTest extends BaseSetParserTest<Host> {
.managementServerId(223098941760041L)
.clusterId(1)
.clusterName("Xen Clust 1")
.clusterType("CloudManaged")
.clusterType(Host.ClusterType.CLOUD_MANAGED)
.localStorageActive(false)
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-11-26T23:28:36+0200"))
.events("PrepareUnmanaged; HypervisorVersionChanged; ManagementServerDown; PingTimeout; " +
"AgentDisconnected; MaintenanceRequested; HostDown; AgentConnected; StartAgentRebalance; ShutdownRequested; Ping")
.hostTags("")
.hasEnoughCapacity(false)
.allocationState("Enabled").build(),
.allocationState(Host.AllocationState.ENABLED).build(),
Host.builder()
.id(2)
.name("nfs://10.26.26.165/mnt/nfs/cs_sec")
.state("Alert")
.state(Host.State.ALERT)
.disconnected(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-11-26T23:33:38+0200"))
.type("SecondaryStorage")
.type(Host.Type.SECONDARY_STORAGE)
.ipAddress("nfs")
.zoneId(1L)
.zoneName("Dev Zone 1")
@ -113,13 +113,13 @@ public class ListHostsResponseTest extends BaseSetParserTest<Host> {
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-11-26T23:33:38+0200"))
.events("ManagementServerDown; AgentDisconnected; Remove; MaintenanceRequested; AgentConnected; Ping")
.hasEnoughCapacity(false)
.allocationState("Enabled").build(),
.allocationState(Host.AllocationState.ENABLED).build(),
Host.builder()
.id(3)
.name("s-1-VM")
.state("Up")
.type("SecondaryStorageVM")
.state(Host.State.UP)
.type(Host.Type.SECONDARY_STORAGE_VM)
.ipAddress("10.26.26.81")
.zoneId(1)
.zoneName("Dev Zone 1")
@ -133,13 +133,13 @@ public class ListHostsResponseTest extends BaseSetParserTest<Host> {
.events("PrepareUnmanaged; HypervisorVersionChanged; ManagementServerDown; PingTimeout; " +
"AgentDisconnected; MaintenanceRequested; HostDown; AgentConnected; StartAgentRebalance; ShutdownRequested; Ping")
.hasEnoughCapacity(false)
.allocationState("Enabled").build(),
.allocationState(Host.AllocationState.ENABLED).build(),
Host.builder()
.id(4)
.name("v-2-VM")
.state("Up")
.type("ConsoleProxy")
.state(Host.State.UP)
.type(Host.Type.CONSOLE_PROXY)
.ipAddress("10.26.26.96")
.zoneId(1)
.zoneName("Dev Zone 1")
@ -153,7 +153,7 @@ public class ListHostsResponseTest extends BaseSetParserTest<Host> {
.events("PrepareUnmanaged; HypervisorVersionChanged; ManagementServerDown; PingTimeout; " +
"AgentDisconnected; MaintenanceRequested; HostDown; AgentConnected; StartAgentRebalance; ShutdownRequested; Ping")
.hasEnoughCapacity(false)
.allocationState("Enabled").build()
.allocationState(Host.AllocationState.ENABLED).build()
);
}