From 29f35f74067beece06892542d37a9b8cb1d3154c Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 4 Mar 2016 09:54:37 +0100 Subject: [PATCH] Refactor compute service - Add missing resource fields and remove non-needed ones - Add selector classes and selector options - Add dedicated option classes for aggregated options - Remove unnecessary "throws" from method signature - Add static function to handle 404 exceptions --- .../com/google/gcloud/compute/Compute.java | 375 ++++++++++++++++-- .../google/gcloud/compute/ComputeImpl.java | 39 +- .../com/google/gcloud/spi/ComputeRpc.java | 40 +- .../google/gcloud/spi/DefaultComputeRpc.java | 87 ++-- .../gcloud/compute/ComputeImplTest.java | 64 ++- .../gcloud/compute/it/ITComputeTest.java | 106 ++++- 6 files changed, 555 insertions(+), 156 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java index 148e0bb88aad..eb96df0eaba9 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java @@ -16,12 +16,17 @@ package com.google.gcloud.compute; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.common.base.Joiner; +import com.google.common.base.MoreObjects; import com.google.common.collect.Sets; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.spi.ComputeRpc; +import java.io.Serializable; +import java.util.Objects; import java.util.Set; /** @@ -45,7 +50,8 @@ enum DiskTypeField { NAME("name"), SELF_LINK("selfLink"), VALID_DISK_SIZE("validDiskSize"), - ZONE("zone"); + ZONE("zone"), + DEPRECATED("deprecated"); private final String selector; @@ -76,7 +82,7 @@ static String selector(DiskTypeField... fields) { enum MachineTypeField { CREATION_TIMESTAMP("creationTimestamp"), DESCRIPTION("description"), - GUEST_CPUS("cpus"), + GUEST_CPUS("guestCpus"), ID("id"), IMAGE_SPACE_GB("imageSpaceGb"), MAXIMUM_PERSISTENT_DISKS("maximumPersistentDisks"), @@ -84,9 +90,9 @@ enum MachineTypeField { MEMORY_MB("memoryMb"), NAME("name"), SCRATCH_DISKS("scratchDisks"), - DISK_GB("diskGb"), SELF_LINK("selfLink"), - ZONE("zone"); + ZONE("zone"), + DEPRECATED("deprecated"); private final String selector; @@ -122,7 +128,8 @@ enum RegionField { QUOTAS("quotas"), SELF_LINK("selfLink"), STATUS("status"), - ZONES("zones"); + ZONES("zones"), + DEPRECATED("deprecated"); private final String selector; @@ -158,7 +165,8 @@ enum ZoneField { NAME("name"), REGION("region"), SELF_LINK("selfLink"), - STATUS("status"); + STATUS("status"), + DEPRECATED("deprecated"); private final String selector; @@ -211,6 +219,223 @@ static String selector(LicenseField... fields) { } } + /** + * Base class for list filters. + */ + abstract class ListFilter implements Serializable { + + private static final long serialVersionUID = -238638392811165127L; + + private final String field; + private final ComparisonOperator operator; + private final Object value; + + enum ComparisonOperator { + /** + * Defines an equality filter. + */ + EQ, + + /** + * Defines an inequality filter. + */ + NE + } + + ListFilter(String field, ComparisonOperator operator, Object value) { + this.field = field; + this.operator = operator; + this.value = value; + } + + @Override + public int hashCode() { + return Objects.hash(field, operator, value); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ListFilter && toPb().equals(((ListFilter) obj).toPb()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("field", field) + .add("operator", operator) + .add("value", value) + .toString(); + } + + String toPb() { + return field + ' ' + operator.name().toLowerCase() + ' ' + value.toString(); + } + } + + /** + * Class for filtering disk type lists. + */ + class DiskTypeFilter extends ListFilter { + + private static final long serialVersionUID = 4847837203592234453L; + + DiskTypeFilter(DiskTypeField field, ComparisonOperator operator, Object value) { + super(field.selector(), operator, value); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static DiskTypeFilter equals(DiskTypeField field, String value) { + return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value)); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static DiskTypeFilter notEquals(DiskTypeField field, String value) { + return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value)); + } + + /** + * Returns an equality filter for the given field and long value. + */ + public static DiskTypeFilter equals(DiskTypeField field, long value) { + return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value); + } + + /** + * Returns an inequality filter for the given field and long value. + */ + public static DiskTypeFilter notEquals(DiskTypeField field, long value) { + return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.NE, value); + } + } + + /** + * Class for filtering machine type lists. + */ + class MachineTypeFilter extends ListFilter { + + private static final long serialVersionUID = 7346062041571853235L; + + MachineTypeFilter(MachineTypeField field, ComparisonOperator operator, Object value) { + super(field.selector(), operator, value); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static MachineTypeFilter equals(MachineTypeField field, String value) { + return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value)); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static MachineTypeFilter notEquals(MachineTypeField field, String value) { + return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value)); + } + + /** + * Returns an equality filter for the given field and long value. + */ + public static MachineTypeFilter equals(MachineTypeField field, long value) { + return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value); + } + + /** + * Returns an inequality filter for the given field and long value. + */ + public static MachineTypeFilter notEquals(MachineTypeField field, long value) { + return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.NE, value); + } + } + + /** + * Class for filtering region lists. + */ + class RegionFilter extends ListFilter { + + private static final long serialVersionUID = 4464892812442567172L; + + RegionFilter(RegionField field, ComparisonOperator operator, Object value) { + super(field.selector(), operator, value); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static RegionFilter equals(RegionField field, String value) { + return new RegionFilter(checkNotNull(field), ComparisonOperator.EQ, value); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static RegionFilter notEquals(RegionField field, String value) { + return new RegionFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value)); + } + } + + /** + * Class for filtering zone lists. + */ + class ZoneFilter extends ListFilter { + + private static final long serialVersionUID = -3927428278548808737L; + + ZoneFilter(ZoneField field, ComparisonOperator operator, Object value) { + super(field.selector(), operator, value); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static ZoneFilter equals(ZoneField field, String value) { + return new ZoneFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value)); + } + + /** + * Returns an equality filter for the given field and string value. For string fields, + * {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must + * match the entire field. + * + * @see RE2 + */ + public static ZoneFilter notEquals(ZoneField field, String value) { + return new ZoneFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value)); + } + } + /** * Class for specifying disk type get options. */ @@ -244,6 +469,13 @@ private DiskTypeListOption(ComputeRpc.Option option, Object value) { super(option, value); } + /** + * Returns an option to specify a filter to the disk types being listed. + */ + public static DiskTypeListOption filter(DiskTypeFilter filter) { + return new DiskTypeListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + /** * Returns an option to specify the maximum number of disk types to be returned. */ @@ -271,6 +503,39 @@ public static DiskTypeListOption fields(DiskTypeField... fields) { } } + /** + * Class for specifying disk type aggregated list options. + */ + class DiskTypeAggregatedListOption extends Option { + + private static final long serialVersionUID = 7611137483018305170L; + + private DiskTypeAggregatedListOption(ComputeRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify a filter to the disk types being listed. + */ + public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) { + return new DiskTypeAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + + /** + * Returns an option to specify the maximum number of disk types to be returned. + */ + public static DiskTypeAggregatedListOption maxResults(long maxResults) { + return new DiskTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, maxResults); + } + + /** + * Returns an option to specify the page token from which to start listing disk types. + */ + public static DiskTypeAggregatedListOption startPageToken(String pageToken) { + return new DiskTypeAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken); + } + } + /** * Class for specifying machine type get options. */ @@ -304,6 +569,13 @@ private MachineTypeListOption(ComputeRpc.Option option, Object value) { super(option, value); } + /** + * Returns an option to specify a filter to the machine types being listed. + */ + public static MachineTypeListOption filter(MachineTypeFilter filter) { + return new MachineTypeListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + /** * Returns an option to specify the maximum number of machine types to be returned. */ @@ -331,6 +603,39 @@ public static MachineTypeListOption fields(MachineTypeField... fields) { } } + /** + * Class for specifying machine type aggregated list options. + */ + class MachineTypeAggregatedListOption extends Option { + + private static final long serialVersionUID = 8492257475500296057L; + + private MachineTypeAggregatedListOption(ComputeRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify a filter to the machine types being listed. + */ + public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) { + return new MachineTypeAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + + /** + * Returns an option to specify the maximum number of machine types to be returned. + */ + public static MachineTypeAggregatedListOption maxResults(long maxResults) { + return new MachineTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, maxResults); + } + + /** + * Returns an option to specify the page token from which to start listing machine types. + */ + public static MachineTypeAggregatedListOption startPageToken(String pageToken) { + return new MachineTypeAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken); + } + } + /** * Class for specifying region get options. */ @@ -364,6 +669,13 @@ private RegionListOption(ComputeRpc.Option option, Object value) { super(option, value); } + /** + * Returns an option to specify a filter to the regions being listed. + */ + public static RegionListOption filter(RegionFilter filter) { + return new RegionListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + /** * Returns an option to specify the maximum number of regions to be returned. */ @@ -424,6 +736,13 @@ private ZoneListOption(ComputeRpc.Option option, Object value) { super(option, value); } + /** + * Returns an option to specify a filter to the zones being listed. + */ + public static ZoneListOption filter(ZoneFilter filter) { + return new ZoneListOption(ComputeRpc.Option.FILTER, filter.toPb()); + } + /** * Returns an option to specify the maximum number of zones to be returned. */ @@ -478,100 +797,96 @@ public static LicenseOption fields(LicenseField... fields) { * * @throws ComputeException upon failure */ - DiskType getDiskType(DiskTypeId diskTypeId, DiskTypeOption... options) throws ComputeException; + DiskType getDiskType(DiskTypeId diskTypeId, DiskTypeOption... options); /** * Returns the requested disk type or {@code null} if not found. * * @throws ComputeException upon failure */ - DiskType getDiskType(String zone, String diskType, DiskTypeOption... options) - throws ComputeException; + DiskType getDiskType(String zone, String diskType, DiskTypeOption... options); /** - * Lists the disk types in the provided zone available to the current project. + * Lists the disk types in the provided zone. * * @throws ComputeException upon failure */ - Page listDiskTypes(String zone, DiskTypeListOption... options) throws ComputeException; + Page listDiskTypes(String zone, DiskTypeListOption... options); /** - * Lists all disk types available to the current project. + * Lists all disk types. * * @throws ComputeException upon failure */ - Page listDiskTypes(DiskTypeListOption... options) throws ComputeException; + Page listDiskTypes(DiskTypeAggregatedListOption... options); /** * Returns the requested machine type or {@code null} if not found. * * @throws ComputeException upon failure */ - MachineType getMachineType(MachineTypeId machineTypeId, MachineTypeOption... options) - throws ComputeException; + MachineType getMachineType(MachineTypeId machineTypeId, MachineTypeOption... options); /** * Returns the requested machine type or {@code null} if not found. * * @throws ComputeException upon failure */ - MachineType getMachineType(String zone, String machineType, MachineTypeOption... options) - throws ComputeException; + MachineType getMachineType(String zone, String machineType, MachineTypeOption... options); /** - * Lists the machine types in the provided zone available to the current project. + * Lists the machine types in the provided zone. * * @throws ComputeException upon failure */ - Page listMachineTypes(String zone, MachineTypeListOption... options) - throws ComputeException; + Page listMachineTypes(String zone, MachineTypeListOption... options); /** - * Lists all machine types available to the current project. + * Lists all machine types. * * @throws ComputeException upon failure */ - Page listMachineTypes(MachineTypeListOption... options) throws ComputeException; + Page listMachineTypes(MachineTypeAggregatedListOption... options); /** * Returns the requested region or {@code null} if not found. * * @throws ComputeException upon failure */ - Region getRegion(String region, RegionOption... options) throws ComputeException; + Region getRegion(String region, RegionOption... options); /** - * Lists the regions available to the current project. + * Lists the regions. * * @throws ComputeException upon failure */ - Page listRegions(RegionListOption... options) throws ComputeException; + Page listRegions(RegionListOption... options); /** * Returns the requested zone or {@code null} if not found. * * @throws ComputeException upon failure */ - Zone getZone(String zone, ZoneOption... options) throws ComputeException; + Zone getZone(String zone, ZoneOption... options); /** - * Lists the zones available to the current project. + * Lists the zones. * * @throws ComputeException upon failure */ - Page listZones(ZoneListOption... options) throws ComputeException; + Page listZones(ZoneListOption... options); /** * Returns the requested license or {@code null} if not found. * * @throws ComputeException upon failure */ - License getLicense(String license, LicenseOption... options) throws ComputeException; + License getLicense(String license, LicenseOption... options); /** * Returns the requested license or {@code null} if not found. * * @throws ComputeException upon failure */ - License getLicense(LicenseId license, LicenseOption... options) throws ComputeException; + License getLicense(LicenseId license, LicenseOption... options); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java index 2aa54d55e90e..2087e570a349 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java @@ -161,8 +161,7 @@ public Page nextPage() { } @Override - public DiskType getDiskType(final DiskTypeId diskTypeId, DiskTypeOption... options) - throws ComputeException { + public DiskType getDiskType(final DiskTypeId diskTypeId, DiskTypeOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.compute.model.DiskType answer = @@ -179,14 +178,12 @@ public com.google.api.services.compute.model.DiskType call() { } @Override - public DiskType getDiskType(String zone, String diskType, DiskTypeOption... options) - throws ComputeException { + public DiskType getDiskType(String zone, String diskType, DiskTypeOption... options) { return getDiskType(DiskTypeId.of(zone, diskType), options); } @Override - public Page listDiskTypes(String zone, DiskTypeListOption... options) - throws ComputeException { + public Page listDiskTypes(String zone, DiskTypeListOption... options) { return listDiskTypes(zone, options(), optionMap(options)); } @@ -220,7 +217,7 @@ public DiskType apply(com.google.api.services.compute.model.DiskType diskType) { } @Override - public Page listDiskTypes(DiskTypeListOption... options) throws ComputeException { + public Page listDiskTypes(DiskTypeAggregatedListOption... options) { return listDiskTypes(options(), optionMap(options)); } @@ -252,15 +249,14 @@ public DiskType apply(com.google.api.services.compute.model.DiskType diskType) { } @Override - public MachineType getMachineType(final MachineTypeId machineTypeId, MachineTypeOption... options) - throws ComputeException { + public MachineType getMachineType(final MachineTypeId machineType, MachineTypeOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.compute.model.MachineType answer = runWithRetries(new Callable() { @Override public com.google.api.services.compute.model.MachineType call() { - return computeRpc.getMachineType(machineTypeId.zone(), machineTypeId.machineType(), + return computeRpc.getMachineType(machineType.zone(), machineType.machineType(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); @@ -271,14 +267,12 @@ public com.google.api.services.compute.model.MachineType call() { } @Override - public MachineType getMachineType(String zone, String machineType, MachineTypeOption... options) - throws ComputeException { + public MachineType getMachineType(String zone, String machineType, MachineTypeOption... options) { return getMachineType(MachineTypeId.of(zone, machineType), options); } @Override - public Page listMachineTypes(String zone, MachineTypeListOption... options) - throws ComputeException { + public Page listMachineTypes(String zone, MachineTypeListOption... options) { return listMachineTypes(zone, options(), optionMap(options)); } @@ -313,8 +307,7 @@ public MachineType apply( } @Override - public Page listMachineTypes(MachineTypeListOption... options) - throws ComputeException { + public Page listMachineTypes(MachineTypeAggregatedListOption... options) { return listMachineTypes(options(), optionMap(options)); } @@ -348,7 +341,7 @@ public MachineType apply( } @Override - public Region getRegion(final String region, RegionOption... options) throws ComputeException { + public Region getRegion(final String region, RegionOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.compute.model.Region answer = @@ -365,7 +358,7 @@ public com.google.api.services.compute.model.Region call() { } @Override - public Page listRegions(RegionListOption... options) throws ComputeException { + public Page listRegions(RegionListOption... options) { return listRegions(options(), optionMap(options)); } @@ -399,7 +392,7 @@ public Region apply(com.google.api.services.compute.model.Region region) { } @Override - public Zone getZone(final String zone, ZoneOption... options) throws ComputeException { + public Zone getZone(final String zone, ZoneOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.compute.model.Zone answer = @@ -416,7 +409,7 @@ public com.google.api.services.compute.model.Zone call() { } @Override - public Page listZones(ZoneListOption... options) throws ComputeException { + public Page listZones(ZoneListOption... options) { return listZones(options(), optionMap(options)); } @@ -449,14 +442,12 @@ public Zone apply(com.google.api.services.compute.model.Zone zone) { } @Override - public License getLicense(String license, LicenseOption... options) - throws ComputeException { + public License getLicense(String license, LicenseOption... options) { return getLicense(LicenseId.of(license), options); } @Override - public License getLicense(LicenseId license, LicenseOption... options) - throws ComputeException { + public License getLicense(LicenseId license, LicenseOption... options) { final LicenseId completeId = license.setProjectId(options().projectId()); final Map optionsMap = optionMap(options); try { diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java index 4668756e04cb..35524e0c116d 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java @@ -90,81 +90,75 @@ public Y y() { * * @throws ComputeException upon failure */ - DiskType getDiskType(String zone, String diskType, Map options) - throws ComputeException; + DiskType getDiskType(String zone, String diskType, Map options); /** - * Lists the disk types in the provided zone available to the current project. + * Lists the disk types in the provided zone. * * @throws ComputeException upon failure */ - Tuple> listDiskTypes(String zone, Map options) - throws ComputeException; + Tuple> listDiskTypes(String zone, Map options); /** - * Lists all disk types available to the current project. + * Lists all disk types. * * @throws ComputeException upon failure */ - Tuple> listDiskTypes(Map options) throws ComputeException; + Tuple> listDiskTypes(Map options); /** * Returns the requested machine type or {@code null} if not found. * * @throws ComputeException upon failure */ - MachineType getMachineType(String zone, String diskType, Map options) - throws ComputeException; + MachineType getMachineType(String zone, String diskType, Map options); /** - * Lists the machine types in the provided zone available to the current project. + * Lists the machine types in the provided zone. * * @throws ComputeException upon failure */ - Tuple> listMachineTypes(String zone, Map options) - throws ComputeException; + Tuple> listMachineTypes(String zone, Map options); /** - * Lists all machine types available to the current project. + * Lists all machine types. * * @throws ComputeException upon failure */ - Tuple> listMachineTypes(Map options) - throws ComputeException; + Tuple> listMachineTypes(Map options); /** * Returns the requested region or {@code null} if not found. * * @throws ComputeException upon failure */ - Region getRegion(String region, Map options) throws ComputeException; + Region getRegion(String region, Map options); /** - * Lists the regions available to the current project. + * Lists the regions. * * @throws ComputeException upon failure */ - Tuple> listRegions(Map options) throws ComputeException; + Tuple> listRegions(Map options); /** * Returns the requested zone or {@code null} if not found. * * @throws ComputeException upon failure */ - Zone getZone(String zone, Map options) throws ComputeException; + Zone getZone(String zone, Map options); /** - * Lists the zones available to the current project. + * Lists the zones. * * @throws ComputeException upon failure */ - Tuple> listZones(Map options) throws ComputeException; + Tuple> listZones(Map options); /** * Returns the requested license or {@code null} if not found. * * @throws ComputeException upon failure */ - License getLicense(String project, String license, Map options) - throws ComputeException; + License getLicense(String project, String license, Map options); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java index 16e8464843fb..fe714acab502 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java @@ -66,25 +66,19 @@ private static ComputeException translate(IOException exception) { } @Override - public DiskType getDiskType(String zone, String diskType, Map options) - throws ComputeException { + public DiskType getDiskType(String zone, String diskType, Map options) { try { return compute.diskTypes() .get(this.options.projectId(), zone, diskType) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { - ComputeException serviceException = translate(ex); - if (serviceException.code() == HTTP_NOT_FOUND) { - return null; - } - throw serviceException; + return nullForNotFound(ex); } } @Override - public Tuple> listDiskTypes(String zone, Map options) - throws ComputeException { + public Tuple> listDiskTypes(String zone, Map options) { try { DiskTypeList diskTypesList = compute.diskTypes() .list(this.options.projectId(), zone) @@ -101,21 +95,20 @@ public Tuple> listDiskTypes(String zone, Map> listDiskTypes(Map options) - throws ComputeException { + public Tuple> listDiskTypes(Map options) { try { DiskTypeAggregatedList aggregatedList = compute.diskTypes() .aggregatedList(this.options.projectId()) .setFilter(FILTER.getString(options)) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) - .setFields(FIELDS.getString(options)) + // todo(mziccard): uncomment or remove once #711 is closed + // .setFields(FIELDS.getString(options)) .execute(); ImmutableList.Builder builder = ImmutableList.builder(); Map scopedList = aggregatedList.getItems(); if (scopedList != null) { - for (String key : scopedList.keySet()) { - DiskTypesScopedList diskTypesScopedList = scopedList.get(key); + for (DiskTypesScopedList diskTypesScopedList : scopedList.values()) { if (diskTypesScopedList.getDiskTypes() != null) { builder.addAll(diskTypesScopedList.getDiskTypes()); } @@ -129,25 +122,20 @@ public Tuple> listDiskTypes(Map options) } @Override - public MachineType getMachineType(String zone, String machineType, Map options) - throws ComputeException { + public MachineType getMachineType(String zone, String machineType, Map options) { try { return compute.machineTypes() .get(this.options.projectId(), zone, machineType) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { - ComputeException serviceException = translate(ex); - if (serviceException.code() == HTTP_NOT_FOUND) { - return null; - } - throw serviceException; + return nullForNotFound(ex); } } @Override - public Tuple> listMachineTypes(String zone, Map options) - throws ComputeException { + public Tuple> listMachineTypes(String zone, + Map options) { try { MachineTypeList machineTypesList = compute.machineTypes() .list(this.options.projectId(), zone) @@ -164,21 +152,20 @@ public Tuple> listMachineTypes(String zone, Map> listMachineTypes(Map options) - throws ComputeException { + public Tuple> listMachineTypes(Map options) { try { MachineTypeAggregatedList aggregatedList = compute.machineTypes() .aggregatedList(this.options.projectId()) .setFilter(FILTER.getString(options)) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) - .setFields(FIELDS.getString(options)) + // todo(mziccard): uncomment or remove once #711 is closed + // .setFields(FIELDS.getString(options)) .execute(); ImmutableList.Builder builder = ImmutableList.builder(); Map scopedList = aggregatedList.getItems(); if (scopedList != null) { - for (String key : scopedList.keySet()) { - MachineTypesScopedList machineTypesScopedList = scopedList.get(key); + for (MachineTypesScopedList machineTypesScopedList : scopedList.values()) { if (machineTypesScopedList.getMachineTypes() != null) { builder.addAll(machineTypesScopedList.getMachineTypes()); } @@ -192,24 +179,19 @@ public Tuple> listMachineTypes(Map opti } @Override - public Region getRegion(String region, Map options) throws ComputeException { + public Region getRegion(String region, Map options) { try { return compute.regions() .get(this.options.projectId(), region) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { - ComputeException serviceException = translate(ex); - if (serviceException.code() == HTTP_NOT_FOUND) { - return null; - } - throw serviceException; + return nullForNotFound(ex); } } @Override - public Tuple> listRegions(Map options) - throws ComputeException { + public Tuple> listRegions(Map options) { try { RegionList regionsList = compute.regions() .list(this.options.projectId()) @@ -226,23 +208,19 @@ public Tuple> listRegions(Map options) } @Override - public Zone getZone(String zone, Map options) throws ComputeException { + public Zone getZone(String zone, Map options) { try { return compute.zones() .get(this.options.projectId(), zone) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { - ComputeException serviceException = translate(ex); - if (serviceException.code() == HTTP_NOT_FOUND) { - return null; - } - throw serviceException; + return nullForNotFound(ex); } } @Override - public Tuple> listZones(Map options) throws ComputeException { + public Tuple> listZones(Map options) { try { ZoneList zonesList = compute.zones() .list(this.options.projectId()) @@ -259,19 +237,28 @@ public Tuple> listZones(Map options) throws Co } @Override - public License getLicense(String project, String license, Map options) - throws ComputeException { + public License getLicense(String project, String license, Map options) { try { return compute.licenses() .get(project, license) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { - ComputeException serviceException = translate(ex); - if (serviceException.code() == HTTP_NOT_FOUND) { - return null; - } - throw serviceException; + return nullForNotFound(ex); + } + } + + /** + * This method returns {@code null} if the error code of {@code exception} was 404, re-throws the + * exception otherwise. + * + * @throws ComputeException if the error code of {@code exception} was not 404. + */ + private static T nullForNotFound(IOException exception) { + ComputeException serviceException = translate(exception); + if (serviceException.code() == HTTP_NOT_FOUND) { + return (T) null; } + throw serviceException; } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ComputeImplTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ComputeImplTest.java index c1b44a26b51f..5ef9b04ed446 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ComputeImplTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ComputeImplTest.java @@ -126,13 +126,26 @@ public class ComputeImplTest { Compute.DiskTypeOption.fields(Compute.DiskTypeField.ID, Compute.DiskTypeField.DESCRIPTION); // DiskType list options + private static final Compute.DiskTypeFilter DISK_TYPE_FILTER = + Compute.DiskTypeFilter.equals(Compute.DiskTypeField.DESCRIPTION, "someDescription"); private static final Compute.DiskTypeListOption DISK_TYPE_LIST_PAGE_TOKEN = Compute.DiskTypeListOption.startPageToken("cursor"); private static final Compute.DiskTypeListOption DISK_TYPE_LIST_MAX_RESULTS = Compute.DiskTypeListOption.maxResults(42L); + private static final Compute.DiskTypeListOption DISK_TYPE_LIST_FILTER = + Compute.DiskTypeListOption.filter(DISK_TYPE_FILTER); private static final Map DISK_TYPE_LIST_OPTIONS = ImmutableMap.of( ComputeRpc.Option.PAGE_TOKEN, "cursor", - ComputeRpc.Option.MAX_RESULTS, 42L); + ComputeRpc.Option.MAX_RESULTS, 42L, + ComputeRpc.Option.FILTER, "description eq someDescription"); + + // DiskType aggregated list options + private static final Compute.DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN = + Compute.DiskTypeAggregatedListOption.startPageToken("cursor"); + private static final Compute.DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_MAX_RESULTS = + Compute.DiskTypeAggregatedListOption.maxResults(42L); + private static final Compute.DiskTypeAggregatedListOption DISK_TYPE_AGGREGATED_LIST_FILTER = + Compute.DiskTypeAggregatedListOption.filter(DISK_TYPE_FILTER); // MachineType options private static final Compute.MachineTypeOption MACHINE_TYPE_OPTION_FIELDS = @@ -140,39 +153,64 @@ public class ComputeImplTest { Compute.MachineTypeField.DESCRIPTION); // MachineType list options + private static final Compute.MachineTypeFilter MACHINE_TYPE_FILTER = + Compute.MachineTypeFilter.notEquals(Compute.MachineTypeField.MAXIMUM_PERSISTENT_DISKS, 42L); private static final Compute.MachineTypeListOption MACHINE_TYPE_LIST_PAGE_TOKEN = Compute.MachineTypeListOption.startPageToken("cursor"); private static final Compute.MachineTypeListOption MACHINE_TYPE_LIST_MAX_RESULTS = Compute.MachineTypeListOption.maxResults(42L); + private static final Compute.MachineTypeListOption MACHINE_TYPE_LIST_FILTER = + Compute.MachineTypeListOption.filter(MACHINE_TYPE_FILTER); private static final Map MACHINE_TYPE_LIST_OPTIONS = ImmutableMap.of( ComputeRpc.Option.PAGE_TOKEN, "cursor", - ComputeRpc.Option.MAX_RESULTS, 42L); + ComputeRpc.Option.MAX_RESULTS, 42L, + ComputeRpc.Option.FILTER, "maximumPersistentDisks ne 42"); + + // MachineType aggregated list options + private static final Compute.MachineTypeAggregatedListOption + MACHINE_TYPE_AGGREGATED_LIST_PAGE_TOKEN = + Compute.MachineTypeAggregatedListOption.startPageToken("cursor"); + private static final Compute.MachineTypeAggregatedListOption + MACHINE_TYPE_AGGREGATED_LIST_MAX_RESULTS = + Compute.MachineTypeAggregatedListOption.maxResults(42L); + private static final Compute.MachineTypeAggregatedListOption MACHINE_TYPE_AGGREGATED_LIST_FILTER = + Compute.MachineTypeAggregatedListOption.filter(MACHINE_TYPE_FILTER); // Region options private static final Compute.RegionOption REGION_OPTION_FIELDS = Compute.RegionOption.fields(Compute.RegionField.ID, Compute.RegionField.DESCRIPTION); // Region list options + private static final Compute.RegionFilter REGION_FILTER = + Compute.RegionFilter.equals(Compute.RegionField.ID, "someId"); private static final Compute.RegionListOption REGION_LIST_PAGE_TOKEN = Compute.RegionListOption.startPageToken("cursor"); private static final Compute.RegionListOption REGION_LIST_MAX_RESULTS = Compute.RegionListOption.maxResults(42L); + private static final Compute.RegionListOption REGION_LIST_FILTER = + Compute.RegionListOption.filter(REGION_FILTER); private static final Map REGION_LIST_OPTIONS = ImmutableMap.of( ComputeRpc.Option.PAGE_TOKEN, "cursor", - ComputeRpc.Option.MAX_RESULTS, 42L); + ComputeRpc.Option.MAX_RESULTS, 42L, + ComputeRpc.Option.FILTER, "id eq someId"); // Zone options private static final Compute.ZoneOption ZONE_OPTION_FIELDS = Compute.ZoneOption.fields(Compute.ZoneField.ID, Compute.ZoneField.DESCRIPTION); // Zone list options + private static final Compute.ZoneFilter ZONE_FILTER = + Compute.ZoneFilter.notEquals(Compute.ZoneField.NAME, "someName"); private static final Compute.ZoneListOption ZONE_LIST_PAGE_TOKEN = Compute.ZoneListOption.startPageToken("cursor"); private static final Compute.ZoneListOption ZONE_LIST_MAX_RESULTS = Compute.ZoneListOption.maxResults(42L); + private static final Compute.ZoneListOption ZONE_LIST_FILTER = + Compute.ZoneListOption.filter(ZONE_FILTER); private static final Map ZONE_LIST_OPTIONS = ImmutableMap.of( ComputeRpc.Option.PAGE_TOKEN, "cursor", - ComputeRpc.Option.MAX_RESULTS, 42L); + ComputeRpc.Option.MAX_RESULTS, 42L, + ComputeRpc.Option.FILTER, "name ne someName"); // License options private static final Compute.LicenseOption LICENSE_OPTION_FIELDS = @@ -293,7 +331,7 @@ public void testListDiskTypesWithOptions() { .andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listDiskTypes(DISK_TYPE_ID.zone(), DISK_TYPE_LIST_MAX_RESULTS, - DISK_TYPE_LIST_PAGE_TOKEN); + DISK_TYPE_LIST_PAGE_TOKEN, DISK_TYPE_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class)); } @@ -334,8 +372,8 @@ public void testAggregatedListDiskTypesWithOptions() { Tuple.of(cursor, Iterables.transform(diskTypeList, DiskType.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); - Page page = - compute.listDiskTypes(DISK_TYPE_LIST_MAX_RESULTS, DISK_TYPE_LIST_PAGE_TOKEN); + Page page = compute.listDiskTypes(DISK_TYPE_AGGREGATED_LIST_MAX_RESULTS, + DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN, DISK_TYPE_AGGREGATED_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.values(), DiskType.class)); } @@ -427,7 +465,7 @@ public void testListMachineTypesWithOptions() { .andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listMachineTypes(MACHINE_TYPE_ID.zone(), - MACHINE_TYPE_LIST_MAX_RESULTS, MACHINE_TYPE_LIST_PAGE_TOKEN); + MACHINE_TYPE_LIST_MAX_RESULTS, MACHINE_TYPE_LIST_PAGE_TOKEN, MACHINE_TYPE_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(machineTypeList.toArray(), Iterables.toArray(page.values(), MachineType.class)); @@ -473,8 +511,8 @@ public void testAggregatedListMachineTypesWithOptions() { EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_LIST_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); - Page page = - compute.listMachineTypes(MACHINE_TYPE_LIST_MAX_RESULTS, MACHINE_TYPE_LIST_PAGE_TOKEN); + Page page = compute.listMachineTypes(MACHINE_TYPE_AGGREGATED_LIST_MAX_RESULTS, + MACHINE_TYPE_AGGREGATED_LIST_PAGE_TOKEN, MACHINE_TYPE_AGGREGATED_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(machineTypeList.toArray(), Iterables.toArray(page.values(), MachineType.class)); @@ -543,7 +581,8 @@ public void testListRegionsWithOptions() { Tuple.of(cursor, Iterables.transform(regionList, Region.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listRegions(REGION_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); - Page page = compute.listRegions(REGION_LIST_MAX_RESULTS, REGION_LIST_PAGE_TOKEN); + Page page = compute.listRegions(REGION_LIST_MAX_RESULTS, REGION_LIST_PAGE_TOKEN, + REGION_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(regionList.toArray(), Iterables.toArray(page.values(), Region.class)); } @@ -610,7 +649,8 @@ public void testListZonesWithOptions() { Tuple.of(cursor, Iterables.transform(zoneList, Zone.TO_PB_FUNCTION)); EasyMock.expect(computeRpcMock.listZones(ZONE_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); - Page page = compute.listZones(ZONE_LIST_MAX_RESULTS, ZONE_LIST_PAGE_TOKEN); + Page page = + compute.listZones(ZONE_LIST_MAX_RESULTS, ZONE_LIST_PAGE_TOKEN, ZONE_LIST_FILTER); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(zoneList.toArray(), Iterables.toArray(page.values(), Zone.class)); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java index 15f3e1c05420..5bc2589e6244 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java @@ -17,8 +17,11 @@ package com.google.gcloud.compute.it; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import com.google.gcloud.Page; import com.google.gcloud.compute.Compute; @@ -87,6 +90,7 @@ public void testGetDiskTypeWithSelectedFields() { public void testListDiskTypes() { Page diskPage = compute.listDiskTypes(ZONE); Iterator diskTypeIterator = diskPage.iterateAll(); + assertTrue(diskTypeIterator.hasNext()); while(diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); // todo(mziccard): uncomment or remove once #695 is closed @@ -105,6 +109,7 @@ public void testListDiskTypesWithSelectedFields() { Page diskPage = compute.listDiskTypes(ZONE, Compute.DiskTypeListOption.fields(Compute.DiskTypeField.CREATION_TIMESTAMP)); Iterator diskTypeIterator = diskPage.iterateAll(); + assertTrue(diskTypeIterator.hasNext()); while(diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); assertNull(diskType.id()); @@ -117,10 +122,30 @@ public void testListDiskTypesWithSelectedFields() { } } + @Test + public void testListDiskTypesWithFilter() { + Page diskPage = compute.listDiskTypes(ZONE, Compute.DiskTypeListOption.filter( + Compute.DiskTypeFilter.equals(Compute.DiskTypeField.DEFAULT_DISK_SIZE_GB, 375))); + Iterator diskTypeIterator = diskPage.iterateAll(); + assertTrue(diskTypeIterator.hasNext()); + while(diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); + assertNotNull(diskType.diskTypeId()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertNotNull(diskType.creationTimestamp()); + assertNotNull(diskType.description()); + assertNotNull(diskType.validDiskSize()); + assertEquals(375, (long) diskType.defaultDiskSizeGb()); + } + } + @Test public void testAggregatedListDiskTypes() { Page diskPage = compute.listDiskTypes(); Iterator diskTypeIterator = diskPage.iterateAll(); + assertTrue(diskTypeIterator.hasNext()); while(diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); // todo(mziccard): uncomment or remove once #695 is closed @@ -134,19 +159,20 @@ public void testAggregatedListDiskTypes() { } @Test - public void testAggregatedListDiskTypesWithSelectedFields() { - Page diskPage = compute.listDiskTypes( - Compute.DiskTypeListOption.fields(Compute.DiskTypeField.CREATION_TIMESTAMP)); + public void testAggregatedListDiskTypesWithFilter() { + Page diskPage = compute.listDiskTypes(Compute.DiskTypeAggregatedListOption.filter( + Compute.DiskTypeFilter.notEquals(Compute.DiskTypeField.DEFAULT_DISK_SIZE_GB, 375))); Iterator diskTypeIterator = diskPage.iterateAll(); + assertTrue(diskTypeIterator.hasNext()); while(diskTypeIterator.hasNext()) { DiskType diskType = diskTypeIterator.next(); - assertNull(diskType.id()); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); assertNotNull(diskType.diskTypeId()); - assertEquals(ZONE, diskType.diskTypeId().zone()); assertNotNull(diskType.creationTimestamp()); - assertNull(diskType.description()); - assertNull(diskType.validDiskSize()); - assertNull(diskType.defaultDiskSizeGb()); + assertNotNull(diskType.description()); + assertNotNull(diskType.validDiskSize()); + assertNotEquals(375, (long) diskType.defaultDiskSizeGb()); } } @@ -183,6 +209,7 @@ public void testGetMachineTypeWithSelectedFields() { public void testListMachineTypes() { Page machinePage = compute.listMachineTypes(ZONE); Iterator machineTypeIterator = machinePage.iterateAll(); + assertTrue(machineTypeIterator.hasNext()); while(machineTypeIterator.hasNext()) { MachineType machineType = machineTypeIterator.next(); assertNotNull(machineType.machineTypeId()); @@ -202,6 +229,7 @@ public void testListMachineTypesWithSelectedFields() { Page machinePage = compute.listMachineTypes(ZONE, Compute.MachineTypeListOption.fields(Compute.MachineTypeField.CREATION_TIMESTAMP)); Iterator machineTypeIterator = machinePage.iterateAll(); + assertTrue(machineTypeIterator.hasNext()); while(machineTypeIterator.hasNext()) { MachineType machineType = machineTypeIterator.next(); assertNotNull(machineType.machineTypeId()); @@ -216,10 +244,33 @@ public void testListMachineTypesWithSelectedFields() { } } + @Test + public void testListMachineTypesWithFilter() { + Page machinePage = compute.listMachineTypes(ZONE, + Compute.MachineTypeListOption.filter( + Compute.MachineTypeFilter.equals(Compute.MachineTypeField.GUEST_CPUS, 2))); + Iterator machineTypeIterator = machinePage.iterateAll(); + assertTrue(machineTypeIterator.hasNext()); + while(machineTypeIterator.hasNext()) { + MachineType machineType = machineTypeIterator.next(); + assertNotNull(machineType.machineTypeId()); + assertEquals(ZONE, machineType.machineTypeId().zone()); + assertNotNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNotNull(machineType.description()); + assertNotNull(machineType.cpus()); + assertEquals(2, (long) machineType.cpus()); + assertNotNull(machineType.memoryMb()); + assertNotNull(machineType.maximumPersistentDisks()); + assertNotNull(machineType.maximumPersistentDisksSizeGb()); + } + } + @Test public void testAggregatedListMachineTypes() { Page machinePage = compute.listMachineTypes(); Iterator machineTypeIterator = machinePage.iterateAll(); + assertTrue(machineTypeIterator.hasNext()); while(machineTypeIterator.hasNext()) { MachineType machineType = machineTypeIterator.next(); assertNotNull(machineType.machineTypeId()); @@ -234,20 +285,23 @@ public void testAggregatedListMachineTypes() { } @Test - public void testAggregatedListMachineTypesWithSelectedFields() { - Page machinePage = compute.listMachineTypes( - Compute.MachineTypeListOption.fields(Compute.MachineTypeField.CREATION_TIMESTAMP)); + public void testAggregatedListMachineTypesWithFilter() { + Page machinePage = + compute.listMachineTypes(Compute.MachineTypeAggregatedListOption.filter( + Compute.MachineTypeFilter.notEquals(Compute.MachineTypeField.GUEST_CPUS, 2))); Iterator machineTypeIterator = machinePage.iterateAll(); + assertTrue(machineTypeIterator.hasNext()); while(machineTypeIterator.hasNext()) { MachineType machineType = machineTypeIterator.next(); assertNotNull(machineType.machineTypeId()); - assertNull(machineType.id()); + assertNotNull(machineType.id()); assertNotNull(machineType.creationTimestamp()); - assertNull(machineType.description()); - assertNull(machineType.cpus()); - assertNull(machineType.memoryMb()); - assertNull(machineType.maximumPersistentDisks()); - assertNull(machineType.maximumPersistentDisksSizeGb()); + assertNotNull(machineType.description()); + assertNotNull(machineType.cpus()); + assertNotEquals(2, (long) machineType.cpus()); + assertNotNull(machineType.memoryMb()); + assertNotNull(machineType.maximumPersistentDisks()); + assertNotNull(machineType.maximumPersistentDisksSizeGb()); } } @@ -322,6 +376,15 @@ public void testListRegionsWithSelectedFields() { } } + @Test + public void testListRegionsWithFilter() { + Page regionPage = compute.listRegions(Compute.RegionListOption.filter( + Compute.RegionFilter.equals(Compute.RegionField.NAME, REGION))); + Iterator regionIterator = regionPage.iterateAll(); + assertEquals(REGION, regionIterator.next().regionId().region()); + assertFalse(regionIterator.hasNext()); + } + @Test public void testGetZone() { Zone zone = compute.getZone(ZONE); @@ -375,4 +438,13 @@ public void testListZonesWithSelectedFields() { assertNull(zone.region()); } } + + @Test + public void testListZonesWithFilter() { + Page zonePage = compute.listZones( + Compute.ZoneListOption.filter(Compute.ZoneFilter.equals(Compute.ZoneField.NAME, ZONE))); + Iterator zoneIterator = zonePage.iterateAll(); + assertEquals(ZONE, zoneIterator.next().zoneId().zone()); + assertFalse(zoneIterator.hasNext()); + } }