Skip to content

Commit

Permalink
Refactor Disk functional methods
Browse files Browse the repository at this point in the history
- Update compute dependency
- Add resize method for disks
- Udpate zone and remove MaintenanceWindow
- make create(Snapshot) throw if disk is not found
- Minor fixes to javadoc and tests
  • Loading branch information
mziccard committed Apr 11, 2016
1 parent f675ee5 commit 3e1ebd9
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 231 deletions.
2 changes: 1 addition & 1 deletion gcloud-java-compute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-compute</artifactId>
<version>v1-rev97-1.21.0</version>
<version>v1-rev103-1.21.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ enum ZoneField {
CREATION_TIMESTAMP("creationTimestamp"),
DESCRIPTION("description"),
ID("id"),
MAINTENANCE_WINDOWS("maintenanceWindows"),
NAME("name"),
REGION("region"),
SELF_LINK("selfLink"),
Expand Down Expand Up @@ -1648,8 +1647,9 @@ private DiskOption(ComputeRpc.Option option, Object value) {
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
* is not provided, all disk's fields are returned. {@code DiskOption.fields} can be used to
* specify only the fields of interest. {@link Disk#diskId()},
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
* {@link DiskConfiguration#diskType()} and either
* {@link SnapshotDiskConfiguration#sourceSnapshot()} or
* {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
*/
public static DiskOption fields(DiskField... fields) {
return new DiskOption(ComputeRpc.Option.FIELDS, DiskField.selector(fields));
Expand Down Expand Up @@ -1693,8 +1693,9 @@ public static DiskListOption pageToken(String pageToken) {
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
* is not provided, all disk's fields are returned. {@code DiskListOption.fields} can be used to
* specify only the fields of interest. {@link Disk#diskId()},
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
* {@link DiskConfiguration#diskType()} and either
* {@link SnapshotDiskConfiguration#sourceSnapshot()} or
* {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
*/
public static DiskListOption fields(DiskField... fields) {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -1922,8 +1923,7 @@ public static DiskAggregatedListOption pageToken(String pageToken) {
/**
* Creates a new snapshot.
*
* @return a zone operation if the create request was issued correctly, {@code null} if
* {@code snapshot.sourceDisk} was not found
* @return a zone operation for snapshot creation
* @throws ComputeException upon failure
*/
Operation create(SnapshotInfo snapshot, OperationOption... options);
Expand Down Expand Up @@ -2058,4 +2058,13 @@ Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
* @throws ComputeException upon failure
*/
Operation delete(DiskId disk, OperationOption... options);

/**
* Resizes the disk to the requested size. The new size must be larger than the previous one.
*
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
* found
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
*/
Operation resize(DiskId disk, long sizeGb, OperationOption... options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,23 @@ public com.google.api.services.compute.model.Operation call() {
}
}

@Override
public Operation resize(final DiskId disk, final long sizeGb, OperationOption... options) {
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
try {
com.google.api.services.compute.model.Operation answer =
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
@Override
public com.google.api.services.compute.model.Operation call() {
return computeRpc.resizeDisk(disk.zone(), disk.disk(), sizeGb, optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : Operation.fromPb(this, answer);
} catch (RetryHelper.RetryHelperException e) {
throw ComputeException.translateAndThrow(e);
}
}

private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
for (Option option : options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public static class Builder extends DiskInfo.Builder {
Builder(Compute compute, DiskId diskId, DiskConfiguration diskConfiguration) {
this.compute = compute;
this.infoBuilder = new DiskInfo.BuilderImpl(diskId, diskConfiguration);
this.infoBuilder.diskId(diskId);
this.configuration(diskConfiguration);
}

Builder(Disk disk) {
Expand Down Expand Up @@ -168,8 +166,7 @@ public Operation delete(OperationOption... options) {
/**
* Creates a snapshot for this disk given the snapshot's name.
*
* @return a zone operation if the snapshot creation was successfully requested. {@code null} if
* the disk was not found
* @return a zone operation for snapshot creation
* @throws ComputeException upon failure
*/
public Operation createSnapshot(String snapshot, OperationOption... options) {
Expand All @@ -179,8 +176,7 @@ public Operation createSnapshot(String snapshot, OperationOption... options) {
/**
* Creates a snapshot for this disk given the snapshot's name and description.
*
* @return a zone operation if the snapshot creation was successfully requested. {@code null} if
* this disk was not found
* @return a zone operation for snapshot creation
* @throws ComputeException upon failure
*/
public Operation createSnapshot(String snapshot, String description, OperationOption... options) {
Expand Down Expand Up @@ -214,6 +210,17 @@ public Operation createImage(String image, String description, OperationOption..
return compute.create(imageInfo, options);
}

/**
* Resizes this disk to the requested size. The new size must be larger than the previous one.
*
* @return a zone operation if the resize request was issued correctly, {@code null} if this disk
* was not found
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
*/
public Operation resize(long sizeGb, OperationOption... options) {
return compute.resize(diskId(), sizeGb, options);
}

/**
* Returns the disk's {@code Compute} object used to issue requests.
*/
Expand Down
138 changes: 0 additions & 138 deletions gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

package com.google.gcloud.compute;

import com.google.api.services.compute.model.Zone.MaintenanceWindows;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -59,7 +56,6 @@ public com.google.api.services.compute.model.Zone apply(Zone region) {
private final Long creationTimestamp;
private final String description;
private final Status status;
private final List<MaintenanceWindow> maintenanceWindows;
private final RegionId region;
private final DeprecationStatus<ZoneId> deprecationStatus;

Expand All @@ -71,113 +67,6 @@ public enum Status {
DOWN
}

/**
* A scheduled maintenance windows for this zone. When a zone is in a maintenance window, all
* resources which reside in the zone will be unavailable.
*
* @see <a href="https://cloud.google.com/compute/docs/robustsystems#maintenance">Maintenance
* Windows</a>
*/
public static final class MaintenanceWindow implements Serializable {

static final Function<MaintenanceWindows, MaintenanceWindow> FROM_PB_FUNCTION =
new Function<MaintenanceWindows, MaintenanceWindow>() {
@Override
public MaintenanceWindow apply(MaintenanceWindows pb) {
return MaintenanceWindow.fromPb(pb);
}
};
static final Function<MaintenanceWindow, MaintenanceWindows> TO_PB_FUNCTION =
new Function<MaintenanceWindow, MaintenanceWindows>() {
@Override
public MaintenanceWindows apply(MaintenanceWindow maintenanceWindow) {
return maintenanceWindow.toPb();
}
};

private static final long serialVersionUID = 2270641266683329963L;

private final String name;
private final String description;
private final Long beginTime;
private final Long endTime;

/**
* Returns a zone maintenance window object.
*/
MaintenanceWindow(String name, String description, Long beginTime, Long endTime) {
this.name = name;
this.description = description;
this.beginTime = beginTime;
this.endTime = endTime;
}

/**
* Returns the name of the maintenance window.
*/
public String name() {
return name;
}

/**
* Returns a textual description of the maintenance window.
*/
public String description() {
return description;
}

/**
* Returns the starting time of the maintenance window in milliseconds since epoch.
*/
public Long beginTime() {
return beginTime;
}

/**
* Returns the ending time of the maintenance window in milliseconds since epoch.
*/
public Long endTime() {
return endTime;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("disk", name)
.add("description", description)
.add("beginTime", beginTime)
.add("endTime", endTime)
.toString();
}

@Override
public int hashCode() {
return Objects.hash(name, description, beginTime, endTime);
}

@Override
public boolean equals(Object obj) {
return obj instanceof MaintenanceWindow
&& Objects.equals(toPb(), ((MaintenanceWindow) obj).toPb());
}

MaintenanceWindows toPb() {
return new MaintenanceWindows()
.setName(name)
.setDescription(description)
.setBeginTime(beginTime != null ? TIMESTAMP_FORMATTER.print(beginTime) : null)
.setEndTime(endTime != null ? TIMESTAMP_FORMATTER.print(endTime) : null);
}

static MaintenanceWindow fromPb(MaintenanceWindows windowPb) {
return new MaintenanceWindow(windowPb.getName(), windowPb.getDescription(),
windowPb.getBeginTime() != null
? TIMESTAMP_FORMATTER.parseMillis(windowPb.getBeginTime()) : null,
windowPb.getEndTime() != null
? TIMESTAMP_FORMATTER.parseMillis(windowPb.getEndTime()) : null);
}
}

static final class Builder {

private ZoneId zoneId;
Expand All @@ -186,7 +75,6 @@ static final class Builder {
private String description;

private Status status;
private List<MaintenanceWindow> maintenanceWindows;
private RegionId region;
private DeprecationStatus<ZoneId> deprecationStatus;

Expand Down Expand Up @@ -217,11 +105,6 @@ Builder status(Status status) {
return this;
}

Builder maintenanceWindows(List<MaintenanceWindow> maintenanceWindows) {
this.maintenanceWindows = maintenanceWindows;
return this;
}

Builder region(RegionId region) {
this.region = region;
return this;
Expand All @@ -243,7 +126,6 @@ private Zone(Builder builder) {
this.creationTimestamp = builder.creationTimestamp;
this.description = builder.description;
this.status = builder.status;
this.maintenanceWindows = builder.maintenanceWindows;
this.region = builder.region;
this.deprecationStatus = builder.deprecationStatus;
}
Expand Down Expand Up @@ -283,17 +165,6 @@ public Status status() {
return status;
}

/**
* Returns the scheduled maintenance windows for this zone, if any. When the zone is in a
* maintenance window, all resources which reside in the zone will be unavailable.
*
* @see <a href="https://cloud.google.com/compute/docs/robustsystems#maintenance">Maintenance
* Windows</a>
*/
public List<MaintenanceWindow> maintenanceWindows() {
return maintenanceWindows;
}

/**
* Returns the identity of the region that hosts the zone.
*/
Expand All @@ -318,7 +189,6 @@ public String toString() {
.add("creationTimestamp", creationTimestamp)
.add("description", description)
.add("status", status)
.add("maintenanceWindows", maintenanceWindows)
.add("region", region)
.add("deprecationStatus", deprecationStatus)
.toString();
Expand Down Expand Up @@ -349,10 +219,6 @@ com.google.api.services.compute.model.Zone toPb() {
if (status != null) {
zonePb.setStatus(status.name());
}
if (maintenanceWindows != null) {
zonePb.setMaintenanceWindows(
Lists.transform(maintenanceWindows, MaintenanceWindow.TO_PB_FUNCTION));
}
if (region != null) {
zonePb.setRegion(region.selfLink());
}
Expand All @@ -379,10 +245,6 @@ static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) {
if (zonePb.getStatus() != null) {
builder.status(Status.valueOf(zonePb.getStatus()));
}
if (zonePb.getMaintenanceWindows() != null) {
builder.maintenanceWindows(
Lists.transform(zonePb.getMaintenanceWindows(), MaintenanceWindow.FROM_PB_FUNCTION));
}
if (zonePb.getRegion() != null) {
builder.region(RegionId.fromUrl(zonePb.getRegion()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ public Y y() {
/**
* Creates a snapshot for the specified disk.
*
* @return a zone operation if the create request was issued correctly, {@code null} if the disk
* was not found
* @return a zone operation for snapshot creation
* @throws ComputeException upon failure
*/
Operation createSnapshot(String zone, String disk, String snapshot, String description,
Expand Down Expand Up @@ -416,4 +415,13 @@ Operation deprecateImage(String project, String image, DeprecationStatus depreca
* @throws ComputeException upon failure
*/
Operation deleteDisk(String zone, String disk, Map<Option, ?> options);

/**
* Resizes the disk to the requested size. The new size must be larger than the previous one.
*
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
* found
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
*/
Operation resizeDisk(String zone, String disk, long sizeGb, Map<Option, ?> options);
}

0 comments on commit 3e1ebd9

Please sign in to comment.