added some more enums instead of strings, minor tidies (added missing Builder.build methods)

This commit is contained in:
Alex Heneveld 2011-12-02 03:05:58 +00:00
parent fc72c945f7
commit 723dd61c33
4 changed files with 79 additions and 22 deletions

View File

@ -48,7 +48,7 @@ public class Snapshot implements Comparable<Snapshot> {
private State state;
private long volumeId;
private String volumeName;
private String volumeType;
private Volume.VolumeType volumeType;
/**
* @param id ID of the snapshot
@ -157,14 +157,19 @@ public class Snapshot implements Comparable<Snapshot> {
/**
* @param volumeType type of the disk volume
*/
public Builder volumeType(String volumeType) {
public Builder volumeType(Volume.VolumeType volumeType) {
this.volumeType = volumeType;
return this;
}
public Snapshot build() {
return new Snapshot(id, account, created, domain, domainId, interval, jobId,
jobStatus, name, snapshotType, state, volumeId, volumeName, volumeType);
}
}
public static enum State {
public enum State {
BackedUp, Creating, BackingUp, UNRECOGNIZED;
@ -177,7 +182,7 @@ public class Snapshot implements Comparable<Snapshot> {
}
}
public static enum Type {
public enum Type {
MANUAL, RECURRING, UNRECOGNIZED;
@ -190,7 +195,7 @@ public class Snapshot implements Comparable<Snapshot> {
}
}
public static enum Interval {
public enum Interval {
HOURLY, DAILY, WEEKLY, MONTHLY, template, none, UNRECOGNIZED;
@ -224,7 +229,25 @@ public class Snapshot implements Comparable<Snapshot> {
@SerializedName("volumename")
private String volumeName;
@SerializedName("volumetype")
private String volumeType; // FIXME: replace this with a proper enumerated type (blocked until volume API implemented)
private Volume.VolumeType volumeType;
public Snapshot(long id, String account, Date created, String domain, long domainId, Interval interval, long jobId,
String jobStatus, String name, Type snapshotType, State state, long volumeId, String volumeName, Volume.VolumeType volumeType) {
this.id = id;
this.account = account;
this.created = created;
this.domain = domain;
this.domainId = domainId;
this.interval = interval;
this.jobId = jobId;
this.jobStatus = jobStatus;
this.name = name;
this.snapshotType = snapshotType;
this.state = state;
this.volumeId = volumeId;
this.volumeName = volumeName;
this.volumeType = volumeType;
}
/**
* present only for serializer
@ -326,7 +349,7 @@ public class Snapshot implements Comparable<Snapshot> {
/**
* @return type of the disk volume
*/
public String getVolumeType() {
public Volume.VolumeType getVolumeType() {
return volumeType;
}

View File

@ -86,6 +86,9 @@ public class SnapshotPolicy implements Comparable<SnapshotPolicy> {
return this;
}
public SnapshotPolicy build() {
return new SnapshotPolicy(id, interval, numberToRetain, schedule, timezone, volumeId);
}
}
private long id;
@ -98,6 +101,15 @@ public class SnapshotPolicy implements Comparable<SnapshotPolicy> {
@SerializedName("volumeid")
private long volumeId;
public SnapshotPolicy(long id, Snapshot.Interval interval, long numberToRetain, String schedule, String timezone, long volumeId) {
this.id = id;
this.interval = interval;
this.numberToRetain = numberToRetain;
this.schedule = schedule;
this.timezone = timezone;
this.volumeId = volumeId;
}
/**
* present only for serializer
*/

View File

@ -61,9 +61,8 @@ public class Volume implements Comparable<Volume> {
private String serviceOfferingName;
private long size;
private long snapshotId;
private String state;
private State state;
private String storage;
// TODO enum
private String storageType;
private VolumeType type;
private long virtualMachineId;
@ -173,7 +172,7 @@ public class Volume implements Comparable<Volume> {
return this;
}
public Builder state(String state) {
public Builder state(State state) {
this.state = state;
return this;
}
@ -231,11 +230,6 @@ public class Volume implements Comparable<Volume> {
}
}
// for deserialization
Volume() {
}
private long id;
private Date attached;
private Date created;
@ -257,7 +251,6 @@ public class Volume implements Comparable<Volume> {
@SerializedName("jobid")
private long jobId;
@SerializedName("jobstatus")
//TODO Change to enum
private String jobStatus;
private String name;
@SerializedName("serviceofferingdisplaytext")
@ -269,9 +262,10 @@ public class Volume implements Comparable<Volume> {
private long size;
@SerializedName("snapshotid")
private long snapshotId;
private String state;
private State state;
private String storage;
@SerializedName("storagetype")
// MAYDO: this should perhaps be an enum; only value I have seen is "shared"
private String storageType;
private VolumeType type;
@SerializedName("virtualmachineid")
@ -291,7 +285,7 @@ public class Volume implements Comparable<Volume> {
String diskOfferingDisplayText, long diskOfferingId, String diskOfferingName,
String domain, long domainId, String hypervisor, boolean extractable, long jobId,
String jobStatus, String name, String serviceOfferingDisplayText, long serviceOfferingId,
String serviceOfferingName, long size, long snapshotId, String state, String storage,
String serviceOfferingName, long size, long snapshotId, State state, String storage,
String storageType, VolumeType type, long virtualMachineId, String vmDisplayName, String vmName,
VirtualMachine.State vmState, long zoneId, String zoneName) {
this.id = id;
@ -326,6 +320,10 @@ public class Volume implements Comparable<Volume> {
this.zoneName = zoneName;
}
// for deserialization
Volume() {
}
public long getId() {
return id;
}
@ -334,7 +332,6 @@ public class Volume implements Comparable<Volume> {
return attached;
}
public Date getCreated() {
return created;
}
@ -407,7 +404,7 @@ public class Volume implements Comparable<Volume> {
return snapshotId;
}
public String getState() {
public State getState() {
return state;
}
@ -540,6 +537,30 @@ public class Volume implements Comparable<Volume> {
return getClass().getCanonicalName()+"["+id+"; "+name+"; "+vmState+"]";
}
public enum State {
/** indicates that the volume record is created in the DB, but not on the backend */
Allocated,
/** the volume is being created on the backend */
Creating,
/** the volume is ready to be used */
Ready,
/** the volume is destroyed (either as a result of deleteVolume command for DataDisk or as a part of destroyVm) */
Destroyed,
/** the volume has failed somehow, e.g. during creation (in cloudstack development) */
Failed,
UNRECOGNIZED;
public static State fromValue(String type) {
try {
return valueOf(checkNotNull(type, "type"));
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
}
public enum VolumeType {
ROOT(0),
DATADISK(1),

View File

@ -112,7 +112,7 @@ public class SnapshotClientLiveTest extends BaseCloudStackClientLiveTest {
protected Volume getPreferredVolume() {
for (Volume candidate : client.getVolumeClient().listVolumes()) {
if ("Ready".equals(candidate.getState()))
if (candidate.getState() == Volume.State.Ready)
return candidate;
}
throw new AssertionError("No suitable Volume found.");
@ -123,6 +123,7 @@ public class SnapshotClientLiveTest extends BaseCloudStackClientLiveTest {
Snapshot snapshot = Retryables.retryGettingResultOrFailing(new PredicateCallable<Snapshot>() {
public Snapshot call() {
logger.info("creating snapshot from volume %s", volume);
AsyncCreateResponse job = client.getSnapshotClient().createSnapshot(volume.getId());
assertTrue(jobComplete.apply(job.getJobId()));
return findSnapshotWithId(job.getId());
@ -131,7 +132,7 @@ public class SnapshotClientLiveTest extends BaseCloudStackClientLiveTest {
logger.info("failed creating snapshot (retrying): %s", getLastFailure());
}
}, null, 60*1000, "failed to create snapshot");
logger.info("created snapshot %s from volume %s", snapshot, volume);
checkSnapshot(snapshot);
client.getSnapshotClient().deleteSnapshot(snapshot.getId());
}