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; package org.jclouds.cloudstack.domain;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import org.omg.CORBA.UNKNOWN;
import java.util.Date; 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 * Represents a host issued by Cloudstack
@ -29,18 +35,120 @@ import java.util.Date;
*/ */
public class Host implements Comparable<Host> { 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() { public static Builder builder() {
return new Builder(); return new Builder();
} }
public static class Builder { public static class Builder {
private long id; private long id;
private String allocationState; private AllocationState allocationState;
private int averageLoad; private int averageLoad;
private String capabilities; private String capabilities;
private long clusterId; private long clusterId;
private String clusterName; private String clusterName;
private String clusterType; private ClusterType clusterType;
private String cpuAllocated; private String cpuAllocated;
private int cpuNumber; private int cpuNumber;
private int cpuSpeed; private int cpuSpeed;
@ -71,8 +179,8 @@ public class Host implements Comparable<Host> {
private long podId; private long podId;
private String podName; private String podName;
private Date removed; private Date removed;
private String state; private State state;
private String type; private Type type;
private String version; private String version;
private long zoneId; private long zoneId;
private String zoneName; private String zoneName;
@ -82,7 +190,7 @@ public class Host implements Comparable<Host> {
return this; return this;
} }
public Builder allocationState(String allocationState) { public Builder allocationState(AllocationState allocationState) {
this.allocationState = allocationState; this.allocationState = allocationState;
return this; return this;
} }
@ -107,7 +215,7 @@ public class Host implements Comparable<Host> {
return this; return this;
} }
public Builder clusterType(String clusterType) { public Builder clusterType(ClusterType clusterType) {
this.clusterType = clusterType; this.clusterType = clusterType;
return this; return this;
} }
@ -262,12 +370,12 @@ public class Host implements Comparable<Host> {
return this; return this;
} }
public Builder state(String state) { public Builder state(State state) {
this.state = state; this.state = state;
return this; return this;
} }
public Builder type(String type) { public Builder type(Type type) {
this.type = type; this.type = type;
return this; return this;
} }
@ -303,7 +411,7 @@ public class Host implements Comparable<Host> {
private long id; private long id;
@SerializedName("allocationstate") @SerializedName("allocationstate")
private String allocationState; private AllocationState allocationState;
@SerializedName("averageload") @SerializedName("averageload")
private int averageLoad; private int averageLoad;
@SerializedName("capabilities") @SerializedName("capabilities")
@ -313,7 +421,7 @@ public class Host implements Comparable<Host> {
@SerializedName("clustername") @SerializedName("clustername")
private String clusterName; private String clusterName;
@SerializedName("clustertype") @SerializedName("clustertype")
private String clusterType; private ClusterType clusterType;
@SerializedName("cpuallocated") @SerializedName("cpuallocated")
private String cpuAllocated; private String cpuAllocated;
@SerializedName("cpunumber") @SerializedName("cpunumber")
@ -368,8 +476,8 @@ public class Host implements Comparable<Host> {
@SerializedName("podname") @SerializedName("podname")
private String podName; private String podName;
private Date removed; private Date removed;
private String state; private State state;
private String type; private Type type;
private String version; private String version;
@SerializedName("zoneid") @SerializedName("zoneid")
private long zoneId; private long zoneId;
@ -380,8 +488,8 @@ public class Host implements Comparable<Host> {
Host() { Host() {
} }
public Host(long id, String allocationState, int averageLoad, String capabilities, public Host(long id, AllocationState allocationState, int averageLoad, String capabilities,
long clusterId, String clusterName, String clusterType, String cpuAllocated, long clusterId, String clusterName, ClusterType clusterType, String cpuAllocated,
int cpuNumber, int cpuSpeed, String cpuUsed, float cpuWithOverProvisioning, int cpuNumber, int cpuSpeed, String cpuUsed, float cpuWithOverProvisioning,
Date created, Date disconnected, long diskSizeAllocated, long diskSizeTotal, Date created, Date disconnected, long diskSizeAllocated, long diskSizeTotal,
String events, boolean hasEnoughCapacity, String hostTags, String hypervisor, 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, Date lastPinged, long managementServerId, long memoryAllocated, long memoryTotal,
long memoryUsed, String name, long networkKbsRead, long networkKbsWrite, long memoryUsed, String name, long networkKbsRead, long networkKbsWrite,
long osCategoryId, long osCategoryName, long podId, String podName, Date removed, 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.id = id;
this.allocationState = allocationState; this.allocationState = allocationState;
this.averageLoad = averageLoad; this.averageLoad = averageLoad;
@ -438,7 +546,7 @@ public class Host implements Comparable<Host> {
return id; return id;
} }
public String getAllocationState() { public AllocationState getAllocationState() {
return allocationState; return allocationState;
} }
@ -458,7 +566,7 @@ public class Host implements Comparable<Host> {
return clusterName; return clusterName;
} }
public String getClusterType() { public ClusterType getClusterType() {
return clusterType; return clusterType;
} }
@ -582,11 +690,11 @@ public class Host implements Comparable<Host> {
return removed; return removed;
} }
public String getState() { public State getState() {
return state; return state;
} }
public String getType() { public Type getType() {
return type; return type;
} }

View File

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

View File

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

View File

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

View File

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