Skip to content

Commit 3e1ebd9

Browse files
committed
Refactor Disk functional methods
- 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
1 parent f675ee5 commit 3e1ebd9

12 files changed

Lines changed: 194 additions & 231 deletions

File tree

gcloud-java-compute/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>com.google.apis</groupId>
2626
<artifactId>google-api-services-compute</artifactId>
27-
<version>v1-rev97-1.21.0</version>
27+
<version>v1-rev103-1.21.0</version>
2828
<scope>compile</scope>
2929
<exclusions>
3030
<exclusion>

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ enum ZoneField {
161161
CREATION_TIMESTAMP("creationTimestamp"),
162162
DESCRIPTION("description"),
163163
ID("id"),
164-
MAINTENANCE_WINDOWS("maintenanceWindows"),
165164
NAME("name"),
166165
REGION("region"),
167166
SELF_LINK("selfLink"),
@@ -1648,8 +1647,9 @@ private DiskOption(ComputeRpc.Option option, Object value) {
16481647
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
16491648
* is not provided, all disk's fields are returned. {@code DiskOption.fields} can be used to
16501649
* specify only the fields of interest. {@link Disk#diskId()},
1651-
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
1652-
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
1650+
* {@link DiskConfiguration#diskType()} and either
1651+
* {@link SnapshotDiskConfiguration#sourceSnapshot()} or
1652+
* {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
16531653
*/
16541654
public static DiskOption fields(DiskField... fields) {
16551655
return new DiskOption(ComputeRpc.Option.FIELDS, DiskField.selector(fields));
@@ -1693,8 +1693,9 @@ public static DiskListOption pageToken(String pageToken) {
16931693
* Returns an option to specify the disk's fields to be returned by the RPC call. If this option
16941694
* is not provided, all disk's fields are returned. {@code DiskListOption.fields} can be used to
16951695
* specify only the fields of interest. {@link Disk#diskId()},
1696-
* {@link DiskConfiguration#diskType()} and {@link SnapshotDiskConfiguration#sourceSnapshot()}
1697-
* or {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
1696+
* {@link DiskConfiguration#diskType()} and either
1697+
* {@link SnapshotDiskConfiguration#sourceSnapshot()} or
1698+
* {@link ImageDiskConfiguration#sourceImage()} are always returned, even if not specified.
16981699
*/
16991700
public static DiskListOption fields(DiskField... fields) {
17001701
StringBuilder builder = new StringBuilder();
@@ -1922,8 +1923,7 @@ public static DiskAggregatedListOption pageToken(String pageToken) {
19221923
/**
19231924
* Creates a new snapshot.
19241925
*
1925-
* @return a zone operation if the create request was issued correctly, {@code null} if
1926-
* {@code snapshot.sourceDisk} was not found
1926+
* @return a zone operation for snapshot creation
19271927
* @throws ComputeException upon failure
19281928
*/
19291929
Operation create(SnapshotInfo snapshot, OperationOption... options);
@@ -2058,4 +2058,13 @@ Operation deprecate(ImageId image, DeprecationStatus<ImageId> deprecationStatus,
20582058
* @throws ComputeException upon failure
20592059
*/
20602060
Operation delete(DiskId disk, OperationOption... options);
2061+
2062+
/**
2063+
* Resizes the disk to the requested size. The new size must be larger than the previous one.
2064+
*
2065+
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
2066+
* found
2067+
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
2068+
*/
2069+
Operation resize(DiskId disk, long sizeGb, OperationOption... options);
20612070
}

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,23 @@ public com.google.api.services.compute.model.Operation call() {
13191319
}
13201320
}
13211321

1322+
@Override
1323+
public Operation resize(final DiskId disk, final long sizeGb, OperationOption... options) {
1324+
final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options);
1325+
try {
1326+
com.google.api.services.compute.model.Operation answer =
1327+
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
1328+
@Override
1329+
public com.google.api.services.compute.model.Operation call() {
1330+
return computeRpc.resizeDisk(disk.zone(), disk.disk(), sizeGb, optionsMap);
1331+
}
1332+
}, options().retryParams(), EXCEPTION_HANDLER);
1333+
return answer == null ? null : Operation.fromPb(this, answer);
1334+
} catch (RetryHelper.RetryHelperException e) {
1335+
throw ComputeException.translateAndThrow(e);
1336+
}
1337+
}
1338+
13221339
private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
13231340
Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
13241341
for (Option option : options) {

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Disk.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public static class Builder extends DiskInfo.Builder {
5252
Builder(Compute compute, DiskId diskId, DiskConfiguration diskConfiguration) {
5353
this.compute = compute;
5454
this.infoBuilder = new DiskInfo.BuilderImpl(diskId, diskConfiguration);
55-
this.infoBuilder.diskId(diskId);
56-
this.configuration(diskConfiguration);
5755
}
5856

5957
Builder(Disk disk) {
@@ -168,8 +166,7 @@ public Operation delete(OperationOption... options) {
168166
/**
169167
* Creates a snapshot for this disk given the snapshot's name.
170168
*
171-
* @return a zone operation if the snapshot creation was successfully requested. {@code null} if
172-
* the disk was not found
169+
* @return a zone operation for snapshot creation
173170
* @throws ComputeException upon failure
174171
*/
175172
public Operation createSnapshot(String snapshot, OperationOption... options) {
@@ -179,8 +176,7 @@ public Operation createSnapshot(String snapshot, OperationOption... options) {
179176
/**
180177
* Creates a snapshot for this disk given the snapshot's name and description.
181178
*
182-
* @return a zone operation if the snapshot creation was successfully requested. {@code null} if
183-
* this disk was not found
179+
* @return a zone operation for snapshot creation
184180
* @throws ComputeException upon failure
185181
*/
186182
public Operation createSnapshot(String snapshot, String description, OperationOption... options) {
@@ -214,6 +210,17 @@ public Operation createImage(String image, String description, OperationOption..
214210
return compute.create(imageInfo, options);
215211
}
216212

213+
/**
214+
* Resizes this disk to the requested size. The new size must be larger than the previous one.
215+
*
216+
* @return a zone operation if the resize request was issued correctly, {@code null} if this disk
217+
* was not found
218+
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
219+
*/
220+
public Operation resize(long sizeGb, OperationOption... options) {
221+
return compute.resize(diskId(), sizeGb, options);
222+
}
223+
217224
/**
218225
* Returns the disk's {@code Compute} object used to issue requests.
219226
*/

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
package com.google.gcloud.compute;
1818

19-
import com.google.api.services.compute.model.Zone.MaintenanceWindows;
2019
import com.google.common.base.Function;
2120
import com.google.common.base.MoreObjects;
22-
import com.google.common.collect.Lists;
2321

2422
import org.joda.time.format.DateTimeFormatter;
2523
import org.joda.time.format.ISODateTimeFormat;
2624

2725
import java.io.Serializable;
2826
import java.math.BigInteger;
29-
import java.util.List;
3027
import java.util.Objects;
3128

3229
/**
@@ -59,7 +56,6 @@ public com.google.api.services.compute.model.Zone apply(Zone region) {
5956
private final Long creationTimestamp;
6057
private final String description;
6158
private final Status status;
62-
private final List<MaintenanceWindow> maintenanceWindows;
6359
private final RegionId region;
6460
private final DeprecationStatus<ZoneId> deprecationStatus;
6561

@@ -71,113 +67,6 @@ public enum Status {
7167
DOWN
7268
}
7369

74-
/**
75-
* A scheduled maintenance windows for this zone. When a zone is in a maintenance window, all
76-
* resources which reside in the zone will be unavailable.
77-
*
78-
* @see <a href="https://cloud.google.com/compute/docs/robustsystems#maintenance">Maintenance
79-
* Windows</a>
80-
*/
81-
public static final class MaintenanceWindow implements Serializable {
82-
83-
static final Function<MaintenanceWindows, MaintenanceWindow> FROM_PB_FUNCTION =
84-
new Function<MaintenanceWindows, MaintenanceWindow>() {
85-
@Override
86-
public MaintenanceWindow apply(MaintenanceWindows pb) {
87-
return MaintenanceWindow.fromPb(pb);
88-
}
89-
};
90-
static final Function<MaintenanceWindow, MaintenanceWindows> TO_PB_FUNCTION =
91-
new Function<MaintenanceWindow, MaintenanceWindows>() {
92-
@Override
93-
public MaintenanceWindows apply(MaintenanceWindow maintenanceWindow) {
94-
return maintenanceWindow.toPb();
95-
}
96-
};
97-
98-
private static final long serialVersionUID = 2270641266683329963L;
99-
100-
private final String name;
101-
private final String description;
102-
private final Long beginTime;
103-
private final Long endTime;
104-
105-
/**
106-
* Returns a zone maintenance window object.
107-
*/
108-
MaintenanceWindow(String name, String description, Long beginTime, Long endTime) {
109-
this.name = name;
110-
this.description = description;
111-
this.beginTime = beginTime;
112-
this.endTime = endTime;
113-
}
114-
115-
/**
116-
* Returns the name of the maintenance window.
117-
*/
118-
public String name() {
119-
return name;
120-
}
121-
122-
/**
123-
* Returns a textual description of the maintenance window.
124-
*/
125-
public String description() {
126-
return description;
127-
}
128-
129-
/**
130-
* Returns the starting time of the maintenance window in milliseconds since epoch.
131-
*/
132-
public Long beginTime() {
133-
return beginTime;
134-
}
135-
136-
/**
137-
* Returns the ending time of the maintenance window in milliseconds since epoch.
138-
*/
139-
public Long endTime() {
140-
return endTime;
141-
}
142-
143-
@Override
144-
public String toString() {
145-
return MoreObjects.toStringHelper(this)
146-
.add("disk", name)
147-
.add("description", description)
148-
.add("beginTime", beginTime)
149-
.add("endTime", endTime)
150-
.toString();
151-
}
152-
153-
@Override
154-
public int hashCode() {
155-
return Objects.hash(name, description, beginTime, endTime);
156-
}
157-
158-
@Override
159-
public boolean equals(Object obj) {
160-
return obj instanceof MaintenanceWindow
161-
&& Objects.equals(toPb(), ((MaintenanceWindow) obj).toPb());
162-
}
163-
164-
MaintenanceWindows toPb() {
165-
return new MaintenanceWindows()
166-
.setName(name)
167-
.setDescription(description)
168-
.setBeginTime(beginTime != null ? TIMESTAMP_FORMATTER.print(beginTime) : null)
169-
.setEndTime(endTime != null ? TIMESTAMP_FORMATTER.print(endTime) : null);
170-
}
171-
172-
static MaintenanceWindow fromPb(MaintenanceWindows windowPb) {
173-
return new MaintenanceWindow(windowPb.getName(), windowPb.getDescription(),
174-
windowPb.getBeginTime() != null
175-
? TIMESTAMP_FORMATTER.parseMillis(windowPb.getBeginTime()) : null,
176-
windowPb.getEndTime() != null
177-
? TIMESTAMP_FORMATTER.parseMillis(windowPb.getEndTime()) : null);
178-
}
179-
}
180-
18170
static final class Builder {
18271

18372
private ZoneId zoneId;
@@ -186,7 +75,6 @@ static final class Builder {
18675
private String description;
18776

18877
private Status status;
189-
private List<MaintenanceWindow> maintenanceWindows;
19078
private RegionId region;
19179
private DeprecationStatus<ZoneId> deprecationStatus;
19280

@@ -217,11 +105,6 @@ Builder status(Status status) {
217105
return this;
218106
}
219107

220-
Builder maintenanceWindows(List<MaintenanceWindow> maintenanceWindows) {
221-
this.maintenanceWindows = maintenanceWindows;
222-
return this;
223-
}
224-
225108
Builder region(RegionId region) {
226109
this.region = region;
227110
return this;
@@ -243,7 +126,6 @@ private Zone(Builder builder) {
243126
this.creationTimestamp = builder.creationTimestamp;
244127
this.description = builder.description;
245128
this.status = builder.status;
246-
this.maintenanceWindows = builder.maintenanceWindows;
247129
this.region = builder.region;
248130
this.deprecationStatus = builder.deprecationStatus;
249131
}
@@ -283,17 +165,6 @@ public Status status() {
283165
return status;
284166
}
285167

286-
/**
287-
* Returns the scheduled maintenance windows for this zone, if any. When the zone is in a
288-
* maintenance window, all resources which reside in the zone will be unavailable.
289-
*
290-
* @see <a href="https://cloud.google.com/compute/docs/robustsystems#maintenance">Maintenance
291-
* Windows</a>
292-
*/
293-
public List<MaintenanceWindow> maintenanceWindows() {
294-
return maintenanceWindows;
295-
}
296-
297168
/**
298169
* Returns the identity of the region that hosts the zone.
299170
*/
@@ -318,7 +189,6 @@ public String toString() {
318189
.add("creationTimestamp", creationTimestamp)
319190
.add("description", description)
320191
.add("status", status)
321-
.add("maintenanceWindows", maintenanceWindows)
322192
.add("region", region)
323193
.add("deprecationStatus", deprecationStatus)
324194
.toString();
@@ -349,10 +219,6 @@ com.google.api.services.compute.model.Zone toPb() {
349219
if (status != null) {
350220
zonePb.setStatus(status.name());
351221
}
352-
if (maintenanceWindows != null) {
353-
zonePb.setMaintenanceWindows(
354-
Lists.transform(maintenanceWindows, MaintenanceWindow.TO_PB_FUNCTION));
355-
}
356222
if (region != null) {
357223
zonePb.setRegion(region.selfLink());
358224
}
@@ -379,10 +245,6 @@ static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) {
379245
if (zonePb.getStatus() != null) {
380246
builder.status(Status.valueOf(zonePb.getStatus()));
381247
}
382-
if (zonePb.getMaintenanceWindows() != null) {
383-
builder.maintenanceWindows(
384-
Lists.transform(zonePb.getMaintenanceWindows(), MaintenanceWindow.FROM_PB_FUNCTION));
385-
}
386248
if (zonePb.getRegion() != null) {
387249
builder.region(RegionId.fromUrl(zonePb.getRegion()));
388250
}

gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ public Y y() {
306306
/**
307307
* Creates a snapshot for the specified disk.
308308
*
309-
* @return a zone operation if the create request was issued correctly, {@code null} if the disk
310-
* was not found
309+
* @return a zone operation for snapshot creation
311310
* @throws ComputeException upon failure
312311
*/
313312
Operation createSnapshot(String zone, String disk, String snapshot, String description,
@@ -416,4 +415,13 @@ Operation deprecateImage(String project, String image, DeprecationStatus depreca
416415
* @throws ComputeException upon failure
417416
*/
418417
Operation deleteDisk(String zone, String disk, Map<Option, ?> options);
418+
419+
/**
420+
* Resizes the disk to the requested size. The new size must be larger than the previous one.
421+
*
422+
* @return a zone operation if the request was issued correctly, {@code null} if the disk was not
423+
* found
424+
* @throws ComputeException upon failure or if the new disk size is smaller than the previous one
425+
*/
426+
Operation resizeDisk(String zone, String disk, long sizeGb, Map<Option, ?> options);
419427
}

0 commit comments

Comments
 (0)