Skip to content

Commit 961dc7a

Browse files
committed
Add support for Compute's operations
- Add RegionOperationId, ZoneOperationId, GlobalOperationId classes - Add functional methods for operations to Compute's service and rpc classes - Add Operation class (with functional methods) - Add unit and integration tests
1 parent 4e16b54 commit 961dc7a

File tree

14 files changed

+3173
-18
lines changed

14 files changed

+3173
-18
lines changed

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

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,61 @@ static String selector(LicenseField... fields) {
219219
}
220220
}
221221

222+
/**
223+
* Fields of a Compute Engine Operation resource.
224+
*
225+
* @see <a
226+
* href="https://cloud.google.com/compute/docs/reference/latest/globalOperations#resource">
227+
* GlobalOperation Resource</a>
228+
* @see <a
229+
* href="https://cloud.google.com/compute/docs/reference/latest/regionOperations#resource">
230+
* RegionOperation Resource</a>
231+
* @see <a href="https://cloud.google.com/compute/docs/reference/latest/zoneOperations#resource">
232+
* ZoneOperation Resource</a>
233+
*/
234+
enum OperationField {
235+
CLIENT_OPERATION_ID("clientOperationId"),
236+
CREATION_TIMESTAMP("creationTimestamp"),
237+
DESCRIPTION("description"),
238+
END_TIME("endTime"),
239+
ERROR("error"),
240+
HTTP_ERROR_MESSAGE("httpErrorMessage"),
241+
HTTP_ERROR_STATUS_CODE("httpErrorStatusCode"),
242+
ID("id"),
243+
INSERT_TIME("insertTime"),
244+
NAME("name"),
245+
OPERATION_TYPE("operationType"),
246+
PROGRESS("progress"),
247+
SELF_LINK("selfLink"),
248+
START_TIME("startTime"),
249+
STATUS("status"),
250+
STATUS_MESSAGE("statusMessage"),
251+
REGION("region"),
252+
TARGET_ID("targetId"),
253+
TARGET_LINK("targetLink"),
254+
USER("user"),
255+
WARNINGS("warnings");
256+
257+
private final String selector;
258+
259+
OperationField(String selector) {
260+
this.selector = selector;
261+
}
262+
263+
public String selector() {
264+
return selector;
265+
}
266+
267+
static String selector(OperationField... fields) {
268+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
269+
fieldStrings.add(SELF_LINK.selector());
270+
for (OperationField field : fields) {
271+
fieldStrings.add(field.selector());
272+
}
273+
return Joiner.on(',').join(fieldStrings);
274+
}
275+
}
276+
222277
/**
223278
* Base class for list filters.
224279
*/
@@ -436,6 +491,68 @@ public static ZoneFilter notEquals(ZoneField field, String value) {
436491
}
437492
}
438493

494+
/**
495+
* Class for filtering operation lists.
496+
*/
497+
class OperationFilter extends ListFilter {
498+
499+
private static final long serialVersionUID = -3202249202748346427L;
500+
501+
OperationFilter(OperationField field, ComparisonOperator operator, Object value) {
502+
super(field.selector(), operator, value);
503+
}
504+
505+
/**
506+
* Returns an equality filter for the given field and string value. For string fields,
507+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
508+
* match the entire field.
509+
*
510+
* @see <a href="https://github.com/google/re2">RE2</a>
511+
*/
512+
public static OperationFilter equals(OperationField field, String value) {
513+
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
514+
}
515+
516+
/**
517+
* Returns an equality filter for the given field and string value. For string fields,
518+
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
519+
* match the entire field.
520+
*
521+
* @see <a href="https://github.com/google/re2">RE2</a>
522+
*/
523+
public static OperationFilter notEquals(OperationField field, String value) {
524+
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
525+
}
526+
527+
/**
528+
* Returns an equality filter for the given field and long value.
529+
*/
530+
public static OperationFilter equals(OperationField field, long value) {
531+
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
532+
}
533+
534+
/**
535+
* Returns an inequality filter for the given field and long value.
536+
*/
537+
public static OperationFilter notEquals(OperationField field, long value) {
538+
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
539+
}
540+
541+
/**
542+
* Returns an equality filter for the given field and integer value.
543+
*/
544+
public static OperationFilter equals(OperationField field, int value) {
545+
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
546+
}
547+
548+
/**
549+
* Returns an inequality filter for the given field and integer value.
550+
*/
551+
public static OperationFilter notEquals(OperationField field, int value) {
552+
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
553+
}
554+
}
555+
439556
/**
440557
* Class for specifying disk type get options.
441558
*/
@@ -792,6 +909,73 @@ public static LicenseOption fields(LicenseField... fields) {
792909
}
793910
}
794911

912+
/**
913+
* Class for specifying operation get options.
914+
*/
915+
class OperationOption extends Option {
916+
917+
private static final long serialVersionUID = -4572636917684779912L;
918+
919+
private OperationOption(ComputeRpc.Option option, Object value) {
920+
super(option, value);
921+
}
922+
923+
/**
924+
* Returns an option to specify the operation's fields to be returned by the RPC call. If this
925+
* option is not provided all operation's fields are returned. {@code OperationOption.fields}
926+
* can be used to specify only the fields of interest. {@link Operation#operationId()} is
927+
* always returned, even if not specified.
928+
*/
929+
public static OperationOption fields(OperationField... fields) {
930+
return new OperationOption(ComputeRpc.Option.FIELDS, OperationField.selector(fields));
931+
}
932+
}
933+
934+
/**
935+
* Class for specifying operation list options.
936+
*/
937+
class OperationListOption extends Option {
938+
939+
private static final long serialVersionUID = -1509532420587265823L;
940+
941+
private OperationListOption(ComputeRpc.Option option, Object value) {
942+
super(option, value);
943+
}
944+
945+
/**
946+
* Returns an option to specify a filter to the operations being listed.
947+
*/
948+
public static OperationListOption filter(OperationFilter filter) {
949+
return new OperationListOption(ComputeRpc.Option.FILTER, filter.toPb());
950+
}
951+
952+
/**
953+
* Returns an option to specify the maximum number of operations to be returned.
954+
*/
955+
public static OperationListOption maxResults(long maxResults) {
956+
return new OperationListOption(ComputeRpc.Option.MAX_RESULTS, maxResults);
957+
}
958+
959+
/**
960+
* Returns an option to specify the page token from which to start listing operations.
961+
*/
962+
public static OperationListOption startPageToken(String pageToken) {
963+
return new OperationListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
964+
}
965+
966+
/**
967+
* Returns an option to specify the operation's fields to be returned by the RPC call. If this
968+
* option is not provided all operation's fields are returned.
969+
* {@code OperationListOption.fields} can be used to specify only the fields of interest.
970+
* {@link Operation#operationId()} is always returned, even if not specified.
971+
*/
972+
public static OperationListOption fields(OperationField... fields) {
973+
StringBuilder builder = new StringBuilder();
974+
builder.append("items(").append(OperationField.selector(fields)).append("),nextPageToken");
975+
return new OperationListOption(ComputeRpc.Option.FIELDS, builder.toString());
976+
}
977+
}
978+
795979
/**
796980
* Returns the requested disk type or {@code null} if not found.
797981
*
@@ -889,4 +1073,40 @@ public static LicenseOption fields(LicenseField... fields) {
8891073
* @throws ComputeException upon failure
8901074
*/
8911075
License getLicense(LicenseId license, LicenseOption... options);
1076+
1077+
/**
1078+
* Returns the requested operation or {@code null} if not found.
1079+
*
1080+
* @throws ComputeException upon failure
1081+
*/
1082+
Operation get(OperationId operationId, OperationOption... options);
1083+
1084+
/**
1085+
* Lists the global operations.
1086+
*
1087+
* @throws ComputeException upon failure
1088+
*/
1089+
Page<Operation> listGlobalOperations(OperationListOption... options);
1090+
1091+
/**
1092+
* Lists the operations in the provided region.
1093+
*
1094+
* @throws ComputeException upon failure
1095+
*/
1096+
Page<Operation> listRegionOperations(String region, OperationListOption... options);
1097+
1098+
/**
1099+
* Lists the operations in the provided zone.
1100+
*
1101+
* @throws ComputeException upon failure
1102+
*/
1103+
Page<Operation> listZoneOperations(String zone, OperationListOption... options);
1104+
1105+
/**
1106+
* Deletes the requested operation.
1107+
*
1108+
* @return {@code true} if operation was deleted, {@code false} if it was not found
1109+
* @throws ComputeException upon failure
1110+
*/
1111+
boolean delete(OperationId operation);
8921112
}

0 commit comments

Comments
 (0)