From 723dd61c3396ec2375f3002e674a1e756fc856da Mon Sep 17 00:00:00 2001 From: Alex Heneveld Date: Fri, 2 Dec 2011 03:05:58 +0000 Subject: [PATCH] added some more enums instead of strings, minor tidies (added missing Builder.build methods) --- .../jclouds/cloudstack/domain/Snapshot.java | 37 ++++++++++++--- .../cloudstack/domain/SnapshotPolicy.java | 12 +++++ .../org/jclouds/cloudstack/domain/Volume.java | 47 ++++++++++++++----- .../features/SnapshotClientLiveTest.java | 5 +- 4 files changed, 79 insertions(+), 22 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Snapshot.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Snapshot.java index d2409ce635..c86286225e 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Snapshot.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Snapshot.java @@ -48,7 +48,7 @@ public class Snapshot implements Comparable { 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 { /** * @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 { } } - public static enum Type { + public enum Type { MANUAL, RECURRING, UNRECOGNIZED; @@ -190,7 +195,7 @@ public class Snapshot implements Comparable { } } - public static enum Interval { + public enum Interval { HOURLY, DAILY, WEEKLY, MONTHLY, template, none, UNRECOGNIZED; @@ -224,7 +229,25 @@ public class Snapshot implements Comparable { @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 { /** * @return type of the disk volume */ - public String getVolumeType() { + public Volume.VolumeType getVolumeType() { return volumeType; } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/SnapshotPolicy.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/SnapshotPolicy.java index 56e07cac77..5150b253de 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/SnapshotPolicy.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/SnapshotPolicy.java @@ -86,6 +86,9 @@ public class SnapshotPolicy implements Comparable { 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 { @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 */ diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Volume.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Volume.java index dbfb25578e..80c6cc9872 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Volume.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/Volume.java @@ -61,9 +61,8 @@ public class Volume implements Comparable { 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 { 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 { } } - // for deserialization - Volume() { - - } - private long id; private Date attached; private Date created; @@ -257,7 +251,6 @@ public class Volume implements Comparable { @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 { 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 { 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 { this.zoneName = zoneName; } + // for deserialization + Volume() { + } + public long getId() { return id; } @@ -334,7 +332,6 @@ public class Volume implements Comparable { return attached; } - public Date getCreated() { return created; } @@ -407,7 +404,7 @@ public class Volume implements Comparable { return snapshotId; } - public String getState() { + public State getState() { return state; } @@ -540,6 +537,30 @@ public class Volume implements Comparable { 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), diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java index eb73deb0e5..dbc5ab5513 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/SnapshotClientLiveTest.java @@ -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() { 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()); }