From 5be58b3e91cade1d349014122484a29e254ba8f3 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Sun, 7 Feb 2016 14:01:52 -0800 Subject: [PATCH 1/4] Makes Zone subclass of ZoneInfo. Fixes #605. --- .../main/java/com/google/gcloud/dns/Dns.java | 2 +- .../main/java/com/google/gcloud/dns/Zone.java | 121 ++++++++++++++---- .../java/com/google/gcloud/dns/ZoneInfo.java | 98 ++++++++------ .../java/com/google/gcloud/dns/ZoneTest.java | 61 +++++++-- 4 files changed, 209 insertions(+), 73 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 040a97e5b253..3ea505b5e09b 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -439,7 +439,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @see Cloud DNS Managed Zones: * get */ - ZoneInfo getZone(String zoneName, ZoneOption... options); + Zone getZone(String zoneName, ZoneOption... options); /** * Lists the zones inside the project. diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java index 3f4ea8cab3e4..2da67a8e9a01 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java @@ -20,7 +20,8 @@ import com.google.gcloud.Page; -import java.io.Serializable; +import java.util.List; +import java.util.Objects; /** * A Google Cloud DNS Zone object. @@ -33,20 +34,87 @@ * @see Google Cloud DNS managed zone * documentation */ -public class Zone implements Serializable { +public class Zone extends ZoneInfo { - // TODO(mderka) Zone and zoneInfo to be merged. Opened issue #605. + private static final long serialVersionUID = 564454483894599281L; + private transient Dns dns; - private static final long serialVersionUID = 6847890192129375500L; - private final ZoneInfo zoneInfo; - private final Dns dns; + /** + * Builder for {@code Zone}. + */ + public static class Builder extends ZoneInfo.Builder { + private final Dns dns; + private final ZoneInfo.BuilderImpl infoBuilder; + + private Builder(Zone zone) { + this.dns = zone.dns; + this.infoBuilder = new ZoneInfo.BuilderImpl(zone); + } + + @Override + public Builder name(String name) { + infoBuilder.name(name); + return this; + } + + @Override + Builder id(String id) { + infoBuilder.id(id); + return this; + } + + @Override + Builder creationTimeMillis(long creationTimeMillis) { + infoBuilder.creationTimeMillis(creationTimeMillis); + return this; + } + + @Override + public Builder dnsName(String dnsName) { + infoBuilder.dnsName(dnsName); + return this; + } + + @Override + public Builder description(String description) { + infoBuilder.description(description); + return this; + } + + @Override + public Builder nameServerSet(String nameServerSet) { + infoBuilder.nameServerSet(nameServerSet); + return this; + } + + @Override + Builder nameServers(List nameServers) { + infoBuilder.nameServers(nameServers); // infoBuilder makes a copy + return this; + } + + @Override + public Zone build() { + return new Zone(dns, infoBuilder); + } + } + + Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) { + super(infoBuilder); + this.dns = dns; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } /** * Constructs a {@code Zone} object that contains the given {@code zoneInfo}. */ public Zone(Dns dns, ZoneInfo zoneInfo) { - this.zoneInfo = checkNotNull(zoneInfo); - this.dns = checkNotNull(dns); + super(new BuilderImpl(zoneInfo)); + this.dns = dns; } /** @@ -61,8 +129,7 @@ public Zone(Dns dns, ZoneInfo zoneInfo) { public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... options) { checkNotNull(zoneName); checkNotNull(dnsService); - ZoneInfo zoneInfo = dnsService.getZone(zoneName, options); - return zoneInfo == null ? null : new Zone(dnsService, zoneInfo); + return dnsService.getZone(zoneName, options); } /** @@ -73,7 +140,7 @@ public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... option * @throws DnsException upon failure */ public Zone reload(Dns.ZoneOption... options) { - return Zone.get(dns, zoneInfo.name(), options); + return dns.getZone(name(), options); } /** @@ -83,7 +150,7 @@ public Zone reload(Dns.ZoneOption... options) { * @throws DnsException upon failure */ public boolean delete() { - return dns.delete(zoneInfo.name()); + return dns.delete(name()); } /** @@ -94,7 +161,7 @@ public boolean delete() { * @throws DnsException upon failure or if the zone is not found */ public Page listDnsRecords(Dns.DnsRecordListOption... options) { - return dns.listDnsRecords(zoneInfo.name(), options); + return dns.listDnsRecords(name(), options); } /** @@ -108,7 +175,7 @@ public Page listDnsRecords(Dns.DnsRecordListOption... options) { public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, Dns.ChangeRequestOption... options) { checkNotNull(changeRequest); - return dns.applyChangeRequest(zoneInfo.name(), changeRequest, options); + return dns.applyChangeRequest(name(), changeRequest, options); } /** @@ -124,7 +191,7 @@ public ChangeRequest applyChangeRequest(ChangeRequest changeRequest, public ChangeRequest getChangeRequest(String changeRequestId, Dns.ChangeRequestOption... options) { checkNotNull(changeRequestId); - return dns.getChangeRequest(zoneInfo.name(), changeRequestId, options); + return dns.getChangeRequest(name(), changeRequestId, options); } /** @@ -136,14 +203,7 @@ public ChangeRequest getChangeRequest(String changeRequestId, * @throws DnsException upon failure or if the zone is not found */ public Page listChangeRequests(Dns.ChangeRequestListOption... options) { - return dns.listChangeRequests(zoneInfo.name(), options); - } - - /** - * Returns the {@link ZoneInfo} object containing information about this zone. - */ - public ZoneInfo info() { - return zoneInfo; + return dns.listChangeRequests(name(), options); } /** @@ -152,4 +212,19 @@ public ZoneInfo info() { public Dns dns() { return this.dns; } + + @Override + public boolean equals(Object obj) { + return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb()); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) { + ZoneInfo info = ZoneInfo.fromPb(zone); + return new Zone(dns, info); + } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java index a15518ae166f..e397e37a7fbf 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java @@ -49,9 +49,55 @@ public class ZoneInfo implements Serializable { private final List nameServers; /** - * A builder for {@code ZoneInfo}. + * Builder for {@code ZoneInfo}. */ - public static class Builder { + public abstract static class Builder { + /** + * Sets a mandatory user-provided name for the zone. It must be unique within the project. + */ + public abstract Builder name(String name); + + /** + * Sets an id for the zone which is assigned to the zone by the server. + */ + abstract Builder id(String id); + + /** + * Sets the time when this zone was created. + */ + abstract Builder creationTimeMillis(long creationTimeMillis); + + /** + * Sets a mandatory DNS name of this zone, for instance "example.com.". + */ + public abstract Builder dnsName(String dnsName); + + /** + * Sets a mandatory description for this zone. The value is a string of at most 1024 characters + * which has no effect on the zone's function. + */ + public abstract Builder description(String description); + + /** + * Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name + * servers that all host the same zones. Most users will not need to specify this value. + */ + public abstract Builder nameServerSet(String nameServerSet); + // todo(mderka) add more to the doc when questions are answered by the service owner + + /** + * Sets a list of servers that hold the information about the zone. This information is provided + * by Google Cloud DNS and is read only. + */ + abstract Builder nameServers(List nameServers); + + /** + * Builds the instance of {@code ZoneInfo} based on the information set by this builder. + */ + public abstract ZoneInfo build(); + } + + public static class BuilderImpl extends Builder { private String name; private String id; private Long creationTimeMillis; @@ -60,14 +106,14 @@ public static class Builder { private String nameServerSet; private List nameServers = new LinkedList<>(); - private Builder(String name) { + private BuilderImpl(String name) { this.name = checkNotNull(name); } /** * Creates a builder from an existing ZoneInfo object. */ - Builder(ZoneInfo info) { + BuilderImpl(ZoneInfo info) { this.name = info.name; this.id = info.id; this.creationTimeMillis = info.creationTimeMillis; @@ -77,76 +123,56 @@ private Builder(String name) { this.nameServers.addAll(info.nameServers); } - /** - * Sets a mandatory user-provided name for the zone. It must be unique within the project. - */ + @Override public Builder name(String name) { this.name = checkNotNull(name); return this; } - /** - * Sets an id for the zone which is assigned to the zone by the server. - */ + @Override Builder id(String id) { this.id = id; return this; } - /** - * Sets the time when this zone was created. - */ + @Override Builder creationTimeMillis(long creationTimeMillis) { this.creationTimeMillis = creationTimeMillis; return this; } - /** - * Sets a mandatory DNS name of this zone, for instance "example.com.". - */ + @Override public Builder dnsName(String dnsName) { this.dnsName = checkNotNull(dnsName); return this; } - /** - * Sets a mandatory description for this zone. The value is a string of at most 1024 characters - * which has no effect on the zone's function. - */ + @Override public Builder description(String description) { this.description = checkNotNull(description); return this; } - /** - * Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name - * servers that all host the same zones. Most users will not need to specify this value. - */ + @Override public Builder nameServerSet(String nameServerSet) { - // todo(mderka) add more to the doc when questions are answered by the service owner this.nameServerSet = checkNotNull(nameServerSet); return this; } - /** - * Sets a list of servers that hold the information about the zone. This information is provided - * by Google Cloud DNS and is read only. - */ + @Override Builder nameServers(List nameServers) { checkNotNull(nameServers); this.nameServers = Lists.newLinkedList(nameServers); return this; } - /** - * Builds the instance of {@code ZoneInfo} based on the information set by this builder. - */ + @Override public ZoneInfo build() { return new ZoneInfo(this); } } - private ZoneInfo(Builder builder) { + ZoneInfo(BuilderImpl builder) { this.name = builder.name; this.id = builder.id; this.creationTimeMillis = builder.creationTimeMillis; @@ -160,7 +186,7 @@ private ZoneInfo(Builder builder) { * Returns a builder for {@code ZoneInfo} with an assigned {@code name}. */ public static Builder builder(String name) { - return new Builder(name); + return new BuilderImpl(name); } /** @@ -217,7 +243,7 @@ public List nameServers() { * Returns a builder for {@code ZoneInfo} prepopulated with the metadata of this zone. */ public Builder toBuilder() { - return new Builder(this); + return new BuilderImpl(this); } com.google.api.services.dns.model.ManagedZone toPb() { @@ -240,7 +266,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { } static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { - Builder builder = new Builder(pb.getName()); + Builder builder = new BuilderImpl(pb.getName()); if (pb.getDescription() != null) { builder.description(pb.getDescription()); } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java index 59cb642167ed..1b09dca715a4 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java @@ -22,23 +22,27 @@ import static org.easymock.EasyMock.verify; 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.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; import com.google.gcloud.Page; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.math.BigInteger; + public class ZoneTest { private static final String ZONE_NAME = "dns-zone-name"; private static final String ZONE_ID = "123"; - private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME) + private static final ZoneInfo ZONE_INFO = Zone.builder(ZONE_NAME) .id(ZONE_ID) .dnsName("example.com") .creationTimeMillis(123478946464L) @@ -80,20 +84,19 @@ public void tearDown() throws Exception { @Test public void testConstructor() { replay(dns); - assertNotNull(zone.info()); - assertEquals(ZONE_INFO, zone.info()); + assertEquals(ZONE_INFO.toPb(), zone.toPb()); assertNotNull(zone.dns()); assertEquals(dns, zone.dns()); } @Test public void testGetByName() { - expect(dns.getZone(ZONE_NAME)).andReturn(ZONE_INFO); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(ZONE_INFO); // for options + expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); // for options replay(dns); Zone retrieved = Zone.get(dns, ZONE_NAME); assertSame(dns, retrieved.dns()); - assertEquals(ZONE_INFO, retrieved.info()); + assertEquals(zone, retrieved); // test passing options Zone.get(dns, ZONE_NAME, ZONE_FIELD_OPTIONS); try { @@ -188,18 +191,18 @@ public void listDnsRecordsByNameAndNotFound() { @Test public void reloadByNameAndFound() { - expect(dns.getZone(ZONE_NAME)).andReturn(zoneNoId.info()); - expect(dns.getZone(ZONE_NAME)).andReturn(zone.info()); + expect(dns.getZone(ZONE_NAME)).andReturn(zone); + expect(dns.getZone(ZONE_NAME)).andReturn(zone); // again for options - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId.info()); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone.info()); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zoneNoId); + expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); replay(dns); Zone result = zoneNoId.reload(); - assertSame(zoneNoId.dns(), result.dns()); - assertEquals(zoneNoId.info(), result.info()); + assertSame(zone.dns(), result.dns()); + assertEquals(zone, result); result = zone.reload(); assertSame(zone.dns(), result.dns()); - assertEquals(zone.info(), result.info()); + assertEquals(zone, result); zoneNoId.reload(ZONE_FIELD_OPTIONS); // check options zone.reload(ZONE_FIELD_OPTIONS); // check options } @@ -499,4 +502,36 @@ public void listChangeRequestsAndZoneNotFound() { // expected } } + + @Test + public void testFromPb() { + replay(dns); + assertEquals(Zone.fromPb(dns, zone.toPb()), zone); + } + + @Test + public void testEqualsAndToBuilder() { + replay(dns); + assertEquals(zone, zone.toBuilder().build()); + } + + @Test + public void testBuilder() { + replay(dns); + assertNotEquals(zone, zone.toBuilder() + .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) + .build()); + assertNotEquals(zone, zone.toBuilder().dnsName(zone.name() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().nameServerSet(zone.nameServerSet() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().nameServers(ImmutableList.of("nameserverpppp")).build()); + assertNotEquals(zone, zone.toBuilder().dnsName(zone.dnsName() + "aaaa").build()); + assertNotEquals(zone, zone.toBuilder().creationTimeMillis(zone.creationTimeMillis() + 1) + .build()); + Zone.Builder builder = zone.toBuilder(); + builder.id(ZONE_ID) + .dnsName("example.com") + .creationTimeMillis(123478946464L) + .build(); + assertEquals(zone, builder.build()); + } } From d1c45124dffd0a5a3bfdc2a4abe5c6483f32c295 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Sun, 7 Feb 2016 10:32:02 -0800 Subject: [PATCH 2/4] Added implementation of Dns and test. Renamed getProjectInfo. Fixed #619. --- .../main/java/com/google/gcloud/dns/Dns.java | 6 +- .../com/google/gcloud/dns/DnsException.java | 18 + .../java/com/google/gcloud/dns/DnsImpl.java | 335 ++++++++++++++++ .../com/google/gcloud/dns/DnsOptions.java | 2 +- .../com/google/gcloud/dns/DnsImplTest.java | 370 ++++++++++++++++++ 5 files changed, 727 insertions(+), 4 deletions(-) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 3ea505b5e09b..88e0d7a08591 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -36,7 +36,7 @@ public interface Dns extends Service { * The fields of a project. * *

These values can be used to specify the fields to include in a partial response when calling - * {@link Dns#getProjectInfo(ProjectOption...)}. Project ID is always returned, even if not + * {@link Dns#getProject(ProjectOption...)}. Project ID is always returned, even if not * specified. */ enum ProjectField { @@ -429,7 +429,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @see Cloud DNS Managed Zones: * create */ - ZoneInfo create(ZoneInfo zoneInfo, ZoneOption... options); + Zone create(ZoneInfo zoneInfo, ZoneOption... options); /** * Returns the zone by the specified zone name. Returns {@code null} if the zone is not found. The @@ -485,7 +485,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * @throws DnsException upon failure * @see Cloud DNS Projects: get */ - ProjectInfo getProjectInfo(ProjectOption... fields); + ProjectInfo getProject(ProjectOption... fields); /** * Submits a change request for the specified zone. The returned object contains the following diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java index 73c546759260..2092d5909d37 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java @@ -17,6 +17,8 @@ package com.google.gcloud.dns; import com.google.gcloud.BaseServiceException; +import com.google.gcloud.RetryHelper.RetryHelperException; +import com.google.gcloud.RetryHelper.RetryInterruptedException; import java.io.IOException; @@ -31,5 +33,21 @@ public DnsException(IOException exception) { super(exception, true); } + public DnsException(int code, String message) { + super(code, message, null, true); + } + + /** + * Translate RetryHelperException to the DnsException that caused the error. This method will + * always throw an exception. + * + * @throws DnsException when {@code ex} was caused by a {@code DnsException} + * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} + */ + static DnsException translateAndThrow(RetryHelperException ex) { + BaseServiceException.translateAndPropagateIfPossible(ex); + throw new DnsException(UNKNOWN_CODE, ex.getMessage()); + } + //TODO(mderka) Add translation and retry functionality. Created issue #593. } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java new file mode 100644 index 000000000000..b3ae73fd7e93 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -0,0 +1,335 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.gcloud.RetryHelper.RetryHelperException; +import static com.google.gcloud.RetryHelper.runWithRetries; +import static com.google.gcloud.dns.ChangeRequest.fromPb; + +import com.google.api.services.dns.model.Change; +import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.gcloud.BaseService; +import com.google.gcloud.Page; +import com.google.gcloud.PageImpl; +import com.google.gcloud.RetryHelper; +import com.google.gcloud.spi.DnsRpc; + +import java.util.Map; +import java.util.concurrent.Callable; + +/** + * A default implementation of Dns. + */ +final class DnsImpl extends BaseService implements Dns { + + private final DnsRpc dnsRpc; + + private static class ZonePageFetcher implements PageImpl.NextPageFetcher { + + private static final long serialVersionUID = 2158209410430566961L; + private final Map requestOptions; + private final DnsOptions serviceOptions; + + ZonePageFetcher(DnsOptions serviceOptions, String cursor, + Map optionMap) { + this.requestOptions = + PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; + } + + @Override + public Page nextPage() { + return listZones(serviceOptions, requestOptions); + } + } + + private static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher { + + private static final Function PB_TO_CHANGE_REQUEST = + new Function() { + @Override + public ChangeRequest apply(com.google.api.services.dns.model.Change changePb) { + return fromPb(changePb); + } + }; + private static final long serialVersionUID = -8737501076674042014L; + private final String zoneName; + private final Map requestOptions; + private final DnsOptions serviceOptions; + + ChangeRequestPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor, + Map optionMap) { + this.zoneName = zoneName; + this.requestOptions = + PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; + } + + @Override + public Page nextPage() { + return listChangeRequests(zoneName, serviceOptions, requestOptions, PB_TO_CHANGE_REQUEST); + } + } + + private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher { + + private static final Function PB_TO_DNS_RECORD = + new Function() { + @Override + public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { + return DnsRecord.fromPb(pb); + } + }; + private static final long serialVersionUID = 670996349097667660L; + private final Map requestOptions; + private final DnsOptions serviceOptions; + private final String zoneName; + + DnsRecordPageFetcher(String zoneName, DnsOptions serviceOptions, String cursor, + Map optionMap) { + this.zoneName = zoneName; + this.requestOptions = + PageImpl.nextRequestOptions(DnsRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; + } + + @Override + public Page nextPage() { + return listDnsRecords(zoneName, serviceOptions, requestOptions, PB_TO_DNS_RECORD); + } + } + + private static Page listZones(final DnsOptions serviceOptions, + final Map optionsMap) { + // define transformation function + // this differs from the other list operations since zone is functional and requires dns service + Function pbToZoneFunction = new Function() { + @Override + public Zone apply( + com.google.api.services.dns.model.ManagedZone zonePb) { + return new Zone(serviceOptions.service(), ZoneInfo.fromPb(zonePb)); + } + }; + try { + // get a list of managed zones + DnsRpc.ListResult result = + runWithRetries(new Callable>() { + @Override + public DnsRpc.ListResult call() { + return serviceOptions.rpc().listZones(optionsMap); + } + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); + String cursor = result.pageToken(); + // transform that list into zone objects + Iterable zones = result.results() == null + ? ImmutableList.of() : Iterables.transform(result.results(), pbToZoneFunction); + return new PageImpl<>(new ZonePageFetcher(serviceOptions, cursor, optionsMap), + cursor, zones); + } catch (RetryHelperException e) { + throw DnsException.translateAndThrow(e); + } + } + + private static Page listChangeRequests(final String zoneName, + final DnsOptions serviceOptions, final Map optionsMap, + Function TRANSFORM_FUNCTION) { + try { + // get a list of changes + DnsRpc.ListResult result = runWithRetries(new Callable>() { + @Override + public DnsRpc.ListResult call() { + return serviceOptions.rpc().listChangeRequests(zoneName, optionsMap); + } + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); + String cursor = result.pageToken(); + // transform that list into change request objects + Iterable changes = result.results() == null + ? ImmutableList.of() + : Iterables.transform(result.results(), TRANSFORM_FUNCTION); + return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor, + optionsMap), cursor, changes); + } catch (RetryHelperException e) { + throw DnsException.translateAndThrow(e); + } + } + + private static Page listDnsRecords(final String zoneName, + final DnsOptions serviceOptions, final Map optionsMap, + Function TRANSFORM_FUNCTION) { + try { + // get a list of resource record sets + DnsRpc.ListResult result = runWithRetries( + new Callable>() { + @Override + public DnsRpc.ListResult call() { + return serviceOptions.rpc().listDnsRecords(zoneName, optionsMap); + } + }, serviceOptions.retryParams(), EXCEPTION_HANDLER); + String cursor = result.pageToken(); + // transform that list into dns records + Iterable records = result.results() == null + ? ImmutableList.of() + : Iterables.transform(result.results(), TRANSFORM_FUNCTION); + return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap), + cursor, records); + } catch (RetryHelperException e) { + throw DnsException.translateAndThrow(e); + } + } + + DnsImpl(DnsOptions options) { + super(options); + dnsRpc = options.rpc(); + } + + @Override + public Page listZones(ZoneListOption... options) { + return listZones(options(), optionMap(options)); + } + + @Override + public Page listChangeRequests(String zoneName, + ChangeRequestListOption... options) { + return listChangeRequests(zoneName, options(), optionMap(options), + ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST); + } + + @Override + public Page listDnsRecords(String zoneName, DnsRecordListOption... options) { + return listDnsRecords(zoneName, options(), optionMap(options), + DnsRecordPageFetcher.PB_TO_DNS_RECORD); + } + + @Override + public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) { + final Map optionsMap = optionMap(options); + try { + com.google.api.services.dns.model.ManagedZone answer = runWithRetries( + new Callable() { + @Override + public com.google.api.services.dns.model.ManagedZone call() { + return dnsRpc.create(zoneInfo.toPb(), optionsMap); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : Zone.fromPb(this, answer); + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + @Override + public Zone getZone(final String zoneName, Dns.ZoneOption... options) { + final Map optionsMap = optionMap(options); + try { + com.google.api.services.dns.model.ManagedZone answer = runWithRetries( + new Callable() { + @Override + public com.google.api.services.dns.model.ManagedZone call() { + return dnsRpc.getZone(zoneName, optionsMap); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : Zone.fromPb(this, answer); + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + @Override + public boolean delete(final String zoneName) { + try { + return runWithRetries(new Callable() { + @Override + public Boolean call() { + return dnsRpc.deleteZone(zoneName); + } + }, options().retryParams(), EXCEPTION_HANDLER); + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + @Override + public ProjectInfo getProject(Dns.ProjectOption... fields) { + final Map optionsMap = optionMap(fields); + try { + com.google.api.services.dns.model.Project answer = runWithRetries( + new Callable() { + @Override + public com.google.api.services.dns.model.Project call() { + return dnsRpc.getProject(optionsMap); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : ProjectInfo.fromPb(answer); // should never be null + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + @Override + public ChangeRequest applyChangeRequest(final String zoneName, final ChangeRequest changeRequest, + Dns.ChangeRequestOption... options) { + final Map optionsMap = optionMap(options); + try { + com.google.api.services.dns.model.Change answer = + runWithRetries( + new Callable() { + @Override + public com.google.api.services.dns.model.Change call() { + return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : fromPb(answer); // should never be null + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + @Override + public ChangeRequest getChangeRequest(final String zoneName, final String changeRequestId, + Dns.ChangeRequestOption... options) { + final Map optionsMap = optionMap(options); + try { + com.google.api.services.dns.model.Change answer = + runWithRetries( + new Callable() { + @Override + public com.google.api.services.dns.model.Change call() { + return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : fromPb(answer); // should never be null + } catch (RetryHelper.RetryHelperException ex) { + throw DnsException.translateAndThrow(ex); + } + } + + private Map optionMap(AbstractOption... options) { + Map temp = Maps.newEnumMap(DnsRpc.Option.class); + for (AbstractOption option : options) { + Object prev = temp.put(option.rpcOption(), option.value()); + checkArgument(prev == null, "Duplicate option %s", option); + } + return ImmutableMap.copyOf(temp); + } +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index 248fd164a55f..1ce4425366e5 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -37,7 +37,7 @@ public static class DefaultDnsFactory implements DnsFactory { @Override public Dns create(DnsOptions options) { // TODO(mderka) Implement when DnsImpl is available. Created issue #595. - return null; + return new DnsImpl(options); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java new file mode 100644 index 000000000000..8e063c9bb096 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -0,0 +1,370 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.dns; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.api.services.dns.model.Change; +import com.google.api.services.dns.model.ManagedZone; +import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.gcloud.Page; +import com.google.gcloud.RetryParams; +import com.google.gcloud.ServiceOptions; +import com.google.gcloud.spi.DnsRpc; +import com.google.gcloud.spi.DnsRpcFactory; + +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +public class DnsImplTest { + + // Dns entities + private static final String ZONE_NAME = "some zone name"; + private static final String DNS_NAME = "example.com."; + private static final String CHANGE_ID = "some change id"; + private static final DnsRecord DNS_RECORD1 = DnsRecord.builder("Something", DnsRecord.Type.AAAA) + .build(); + private static final DnsRecord DNS_RECORD2 = DnsRecord.builder("Different", DnsRecord.Type.AAAA) + .build(); + private static final Integer MAX_SIZE = 20; + private static final String PAGE_TOKEN = "some token"; + private static final ZoneInfo ZONE_INFO = ZoneInfo.builder(ZONE_NAME).build(); + private static final ProjectInfo PROJECT_INFO = ProjectInfo.builder().build(); + private static final ChangeRequest CHANGE_REQUEST_PARTIAL = ChangeRequest.builder() + .add(DNS_RECORD1) + .build(); + private static final ChangeRequest CHANGE_REQUEST_COMPLETE = ChangeRequest.builder() + .add(DNS_RECORD1) + .startTimeMillis(123L) + .status(ChangeRequest.Status.PENDING) + .id(CHANGE_ID) + .build(); + + // Result lists + private static final DnsRpc.ListResult LIST_RESULT_OF_PB_CHANGES = + DnsRpc.ListResult.of("cursor", ImmutableList.of(CHANGE_REQUEST_COMPLETE.toPb(), + CHANGE_REQUEST_PARTIAL.toPb())); + private static final DnsRpc.ListResult LIST_RESULT_OF_PB_ZONES = + DnsRpc.ListResult.of("cursor", ImmutableList.of(ZONE_INFO.toPb())); + private static final DnsRpc.ListResult LIST_OF_PB_DNS_RECORDS = + DnsRpc.ListResult.of("cursor", ImmutableList.of(DNS_RECORD1.toPb(), DNS_RECORD2.toPb())); + + // Field options + private static final Dns.ZoneOption ZONE_FIELDS = + Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME); + private static final Dns.ProjectOption PROJECT_FIELDS = + Dns.ProjectOption.fields(Dns.ProjectField.QUOTA); + private static final Dns.ChangeRequestOption CHANGE_GET_FIELDS = + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); + + // Listing options + private static final Dns.ZoneListOption[] ZONE_LIST_OPTIONS = + {Dns.ZoneListOption.pageSize(MAX_SIZE), Dns.ZoneListOption.pageToken(PAGE_TOKEN), + Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)}; + private static final Dns.ChangeRequestListOption[] CHANGE_LIST_OPTIONS = + {Dns.ChangeRequestListOption.pageSize(MAX_SIZE), + Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; + private static final Dns.DnsRecordListOption[] DNS_RECORD_LIST_OPTIONS = + {Dns.DnsRecordListOption.pageSize(MAX_SIZE), + Dns.DnsRecordListOption.pageToken(PAGE_TOKEN), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL), + Dns.DnsRecordListOption.dnsName(DNS_NAME), + Dns.DnsRecordListOption.type(DnsRecord.Type.AAAA)}; + + // Other + private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); + private static final ServiceOptions.Clock TIME_SOURCE = new ServiceOptions.Clock() { + @Override + public long millis() { + return 42000L; + } + }; + + private DnsOptions options; + private DnsRpcFactory rpcFactoryMock; + private DnsRpc dnsRpcMock; + private Dns dns; + + @Before + public void setUp() { + rpcFactoryMock = EasyMock.createMock(DnsRpcFactory.class); + dnsRpcMock = EasyMock.createMock(DnsRpc.class); + EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DnsOptions.class))) + .andReturn(dnsRpcMock); + EasyMock.replay(rpcFactoryMock); + options = DnsOptions.builder() + .projectId("projectId") + .clock(TIME_SOURCE) + .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) + .build(); + } + + @After + public void tearDown() throws Exception { + EasyMock.verify(rpcFactoryMock); + } + + @Test + public void testCreateZone() { + EasyMock.expect(dnsRpcMock.create(ZONE_INFO.toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(ZONE_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ZoneInfo zoneInfo = dns.create(ZONE_INFO); + assertEquals(ZONE_INFO, zoneInfo); + } + + @Test + public void testCreateZoneWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.create(EasyMock.eq(ZONE_INFO.toPb()), + EasyMock.capture(capturedOptions))).andReturn(ZONE_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Zone zone = dns.create(ZONE_INFO, ZONE_FIELDS); + String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); + assertEquals(ZONE_INFO, zone); + assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + } + + @Test + public void testGetZone() { + EasyMock.expect(dnsRpcMock.getZone(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) + .andReturn(ZONE_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name()); + assertEquals(ZONE_INFO, zoneInfo); + } + + @Test + public void testGetZoneWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.getZone(EasyMock.eq(ZONE_INFO.name()), + EasyMock.capture(capturedOptions))).andReturn(ZONE_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS); + String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); + assertEquals(ZONE_INFO, zoneInfo); + assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + } + + @Test + public void testDeleteZone() { + EasyMock.expect(dnsRpcMock.deleteZone(ZONE_INFO.name())) + .andReturn(true); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + assertTrue(dns.delete(ZONE_INFO.name())); + } + + @Test + public void testGetProject() { + EasyMock.expect(dnsRpcMock.getProject(EMPTY_RPC_OPTIONS)) + .andReturn(PROJECT_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ProjectInfo projectInfo = dns.getProject(); + assertEquals(PROJECT_INFO, projectInfo); + } + + @Test + public void testProjectGetWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.getProject(EasyMock.capture(capturedOptions))) + .andReturn(PROJECT_INFO.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ProjectInfo projectInfo = dns.getProject(PROJECT_FIELDS); + String selector = (String) capturedOptions.getValue().get(PROJECT_FIELDS.rpcOption()); + assertEquals(PROJECT_INFO, projectInfo); + assertTrue(selector.contains(Dns.ProjectField.QUOTA.selector())); + assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.selector())); + } + + @Test + public void testGetChangeRequest() { + EasyMock.expect(dnsRpcMock.getChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_COMPLETE.id(), + EMPTY_RPC_OPTIONS)).andReturn(CHANGE_REQUEST_COMPLETE.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_COMPLETE.id()); + assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + } + + @Test + public void testGetChangeRequestWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.getChangeRequest(EasyMock.eq(ZONE_INFO.name()), + EasyMock.eq(CHANGE_REQUEST_COMPLETE.id()), EasyMock.capture(capturedOptions))) + .andReturn(CHANGE_REQUEST_COMPLETE.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ChangeRequest changeRequest = dns.getChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_COMPLETE.id(), CHANGE_GET_FIELDS); + String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); + assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + } + + @Test + public void testApplyChangeRequest() { + EasyMock.expect(dnsRpcMock.applyChangeRequest(ZONE_INFO.name(), CHANGE_REQUEST_PARTIAL.toPb(), + EMPTY_RPC_OPTIONS)).andReturn(CHANGE_REQUEST_COMPLETE.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_PARTIAL); + assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + } + + @Test + public void testApplyChangeRequestWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.applyChangeRequest(EasyMock.eq(ZONE_INFO.name()), + EasyMock.eq(CHANGE_REQUEST_PARTIAL.toPb()), EasyMock.capture(capturedOptions))) + .andReturn(CHANGE_REQUEST_COMPLETE.toPb()); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + ChangeRequest changeRequest = dns.applyChangeRequest(ZONE_INFO.name(), + CHANGE_REQUEST_PARTIAL, CHANGE_GET_FIELDS); + String selector = (String) capturedOptions.getValue().get(CHANGE_GET_FIELDS.rpcOption()); + assertEquals(CHANGE_REQUEST_COMPLETE, changeRequest); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + } + + // lists + @Test + public void testListChangeRequests() { + EasyMock.expect(dnsRpcMock.listChangeRequests(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) + .andReturn(LIST_RESULT_OF_PB_CHANGES); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page changeRequestPage = dns.listChangeRequests(ZONE_INFO.name()); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); + } + + @Test + public void testListChangeRequestsWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.listChangeRequests(EasyMock.eq(ZONE_NAME), + EasyMock.capture(capturedOptions))).andReturn(LIST_RESULT_OF_PB_CHANGES); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page changeRequestPage = dns.listChangeRequests(ZONE_NAME, CHANGE_LIST_OPTIONS); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_COMPLETE)); + assertTrue(Lists.newArrayList(changeRequestPage.values()).contains(CHANGE_REQUEST_PARTIAL)); + assertEquals(2, Lists.newArrayList(changeRequestPage.values()).size()); + Integer size = (Integer) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(selector.contains(Dns.ChangeRequestField.ID.selector())); + selector = (String) capturedOptions.getValue().get(CHANGE_LIST_OPTIONS[3].rpcOption()); + assertTrue(selector.contains(Dns.SortingOrder.ASCENDING.selector())); + } + + @Test + public void testListZones() { + EasyMock.expect(dnsRpcMock.listZones(EMPTY_RPC_OPTIONS)) + .andReturn(LIST_RESULT_OF_PB_ZONES); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page zonePage = dns.listZones(); + assertEquals(1, Lists.newArrayList(zonePage.values()).size()); + assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0)); + } + + @Test + public void testListZonesWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.listZones(EasyMock.capture(capturedOptions))) + .andReturn(LIST_RESULT_OF_PB_ZONES); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page zonePage = dns.listZones(ZONE_LIST_OPTIONS); + assertEquals(1, Lists.newArrayList(zonePage.values()).size()); + assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0)); + Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.ZoneField.DESCRIPTION.selector())); + assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); + } + + @Test + public void testListDnsRecords() { + EasyMock.expect(dnsRpcMock.listDnsRecords(ZONE_INFO.name(), EMPTY_RPC_OPTIONS)) + .andReturn(LIST_OF_PB_DNS_RECORDS); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page dnsPage = dns.listDnsRecords(ZONE_INFO.name()); + assertEquals(2, Lists.newArrayList(dnsPage.values()).size()); + assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD1)); + assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD2)); + } + + @Test + public void testListDnsRecordsWithOptions() { + Capture> capturedOptions = Capture.newInstance(); + EasyMock.expect(dnsRpcMock.listDnsRecords(EasyMock.eq(ZONE_NAME), + EasyMock.capture(capturedOptions))).andReturn(LIST_OF_PB_DNS_RECORDS); + EasyMock.replay(dnsRpcMock); + dns = options.service(); // creates DnsImpl + Page dnsPage = dns.listDnsRecords(ZONE_NAME, DNS_RECORD_LIST_OPTIONS); + assertEquals(2, Lists.newArrayList(dnsPage.values()).size()); + assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD1)); + assertTrue(Lists.newArrayList(dnsPage.values()).contains(DNS_RECORD2)); + Integer size = (Integer) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[0].rpcOption()); + assertEquals(MAX_SIZE, size); + String selector = (String) capturedOptions.getValue() + .get(DNS_RECORD_LIST_OPTIONS[1].rpcOption()); + assertEquals(PAGE_TOKEN, selector); + selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[2].rpcOption()); + assertTrue(selector.contains(Dns.DnsRecordField.NAME.selector())); + assertTrue(selector.contains(Dns.DnsRecordField.TTL.selector())); + selector = (String) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[3].rpcOption()); + assertEquals(DNS_RECORD_LIST_OPTIONS[3].value(), selector); + DnsRecord.Type type = (DnsRecord.Type) capturedOptions.getValue().get(DNS_RECORD_LIST_OPTIONS[4] + .rpcOption()); + assertEquals(DNS_RECORD_LIST_OPTIONS[4].value(), type); + } +} From c0c5451d286675248e32fdf3a37c7c9293cd3f4a Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 8 Feb 2016 11:34:41 -0800 Subject: [PATCH 3/4] Included options attribute to Zone. Fixed symmetry of toPb and fromPb. Finished DnsOptions. Fixed #595. --- .../java/com/google/gcloud/dns/DnsImpl.java | 44 +++++++++---------- .../com/google/gcloud/dns/DnsOptions.java | 4 +- .../main/java/com/google/gcloud/dns/Zone.java | 17 +++++-- .../java/com/google/gcloud/dns/ZoneInfo.java | 19 ++++---- .../com/google/gcloud/dns/DnsImplTest.java | 18 ++++---- .../java/com/google/gcloud/dns/ZoneTest.java | 24 +++++++++- 6 files changed, 79 insertions(+), 47 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index b3ae73fd7e93..c93409554dd8 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -120,6 +120,11 @@ public Page nextPage() { } } + @Override + public Page listZones(ZoneListOption... options) { + return listZones(options(), optionMap(options)); + } + private static Page listZones(final DnsOptions serviceOptions, final Map optionsMap) { // define transformation function @@ -151,9 +156,16 @@ public DnsRpc.ListResult call() { } } + @Override + public Page listChangeRequests(String zoneName, + ChangeRequestListOption... options) { + return listChangeRequests(zoneName, options(), optionMap(options), + ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST); + } + private static Page listChangeRequests(final String zoneName, final DnsOptions serviceOptions, final Map optionsMap, - Function TRANSFORM_FUNCTION) { + Function transformFunction) { try { // get a list of changes DnsRpc.ListResult result = runWithRetries(new Callable>() { @@ -166,7 +178,7 @@ public DnsRpc.ListResult call() { // transform that list into change request objects Iterable changes = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), TRANSFORM_FUNCTION); + : Iterables.transform(result.results(), transformFunction); return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, changes); } catch (RetryHelperException e) { @@ -174,9 +186,15 @@ public DnsRpc.ListResult call() { } } + @Override + public Page listDnsRecords(String zoneName, DnsRecordListOption... options) { + return listDnsRecords(zoneName, options(), optionMap(options), + DnsRecordPageFetcher.PB_TO_DNS_RECORD); + } + private static Page listDnsRecords(final String zoneName, final DnsOptions serviceOptions, final Map optionsMap, - Function TRANSFORM_FUNCTION) { + Function transformFunction) { try { // get a list of resource record sets DnsRpc.ListResult result = runWithRetries( @@ -190,7 +208,7 @@ public DnsRpc.ListResult call() { // transform that list into dns records Iterable records = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), TRANSFORM_FUNCTION); + : Iterables.transform(result.results(), transformFunction); return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, records); } catch (RetryHelperException e) { @@ -203,24 +221,6 @@ public DnsRpc.ListResult call() { dnsRpc = options.rpc(); } - @Override - public Page listZones(ZoneListOption... options) { - return listZones(options(), optionMap(options)); - } - - @Override - public Page listChangeRequests(String zoneName, - ChangeRequestListOption... options) { - return listChangeRequests(zoneName, options(), optionMap(options), - ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST); - } - - @Override - public Page listDnsRecords(String zoneName, DnsRecordListOption... options) { - return listDnsRecords(zoneName, options(), optionMap(options), - DnsRecordPageFetcher.PB_TO_DNS_RECORD); - } - @Override public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) { final Map optionsMap = optionMap(options); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index 1ce4425366e5..b47532146b3a 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -24,8 +24,7 @@ import java.util.Set; -public class DnsOptions - extends ServiceOptions { +public class DnsOptions extends ServiceOptions { private static final long serialVersionUID = -519128051411747771L; private static final String GC_DNS_RW = "https://www.googleapis.com/auth/ndev.clouddns.readwrite"; @@ -36,7 +35,6 @@ public static class DefaultDnsFactory implements DnsFactory { @Override public Dns create(DnsOptions options) { - // TODO(mderka) Implement when DnsImpl is available. Created issue #595. return new DnsImpl(options); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java index 2da67a8e9a01..d53909b0f084 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java @@ -20,6 +20,8 @@ import com.google.gcloud.Page; +import java.io.IOException; +import java.io.ObjectInputStream; import java.util.List; import java.util.Objects; @@ -36,7 +38,8 @@ */ public class Zone extends ZoneInfo { - private static final long serialVersionUID = 564454483894599281L; + private static final long serialVersionUID = -5817771337847861598L; + private final DnsOptions options; private transient Dns dns; /** @@ -102,6 +105,7 @@ public Zone build() { Zone(Dns dns, ZoneInfo.BuilderImpl infoBuilder) { super(infoBuilder); this.dns = dns; + this.options = dns.options(); } @Override @@ -115,6 +119,7 @@ public Builder toBuilder() { public Zone(Dns dns, ZoneInfo zoneInfo) { super(new BuilderImpl(zoneInfo)); this.dns = dns; + this.options = dns.options(); } /** @@ -215,12 +220,18 @@ public Dns dns() { @Override public boolean equals(Object obj) { - return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb()); + return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb()) + && Objects.equals(options, ((Zone) obj).options); } @Override public int hashCode() { - return super.hashCode(); + return Objects.hash(super.hashCode(), options); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + this.dns = options.service(); } static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) { diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java index e397e37a7fbf..e26dcde70a83 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java @@ -27,7 +27,6 @@ import java.io.Serializable; import java.math.BigInteger; -import java.util.LinkedList; import java.util.List; import java.util.Objects; @@ -97,14 +96,14 @@ public abstract static class Builder { public abstract ZoneInfo build(); } - public static class BuilderImpl extends Builder { + static class BuilderImpl extends Builder { private String name; private String id; private Long creationTimeMillis; private String dnsName; private String description; private String nameServerSet; - private List nameServers = new LinkedList<>(); + private List nameServers; private BuilderImpl(String name) { this.name = checkNotNull(name); @@ -120,7 +119,9 @@ private BuilderImpl(String name) { this.dnsName = info.dnsName; this.description = info.description; this.nameServerSet = info.nameServerSet; - this.nameServers.addAll(info.nameServers); + if (info.nameServers != null) { + this.nameServers = ImmutableList.copyOf(info.nameServers); + } } @Override @@ -179,7 +180,8 @@ public ZoneInfo build() { this.dnsName = builder.dnsName; this.description = builder.description; this.nameServerSet = builder.nameServerSet; - this.nameServers = ImmutableList.copyOf(builder.nameServers); + this.nameServers = builder.nameServers == null + ? null : ImmutableList.copyOf(builder.nameServers); } /** @@ -236,7 +238,7 @@ public String nameServerSet() { * The nameservers that the zone should be delegated to. This is defined by the Google DNS cloud. */ public List nameServers() { - return nameServers; + return nameServers == null ? ImmutableList.of() : nameServers; } /** @@ -255,7 +257,7 @@ com.google.api.services.dns.model.ManagedZone toPb() { pb.setId(new BigInteger(this.id())); } pb.setName(this.name()); - pb.setNameServers(this.nameServers()); + pb.setNameServers(this.nameServers); // do use real attribute value which may be null pb.setNameServerSet(this.nameServerSet()); if (this.creationTimeMillis() != null) { pb.setCreationTime(ISODateTimeFormat.dateTime() @@ -290,7 +292,8 @@ static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) { @Override public boolean equals(Object obj) { - return obj instanceof ZoneInfo && Objects.equals(toPb(), ((ZoneInfo) obj).toPb()); + return obj != null && obj.getClass().equals(ZoneInfo.class) + && Objects.equals(toPb(), ((ZoneInfo) obj).toPb()); } @Override diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index 8e063c9bb096..62e96b05dda5 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -136,8 +136,8 @@ public void testCreateZone() { .andReturn(ZONE_INFO.toPb()); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - ZoneInfo zoneInfo = dns.create(ZONE_INFO); - assertEquals(ZONE_INFO, zoneInfo); + Zone zone = dns.create(ZONE_INFO); + assertEquals(new Zone(dns, ZONE_INFO), zone); } @Test @@ -149,7 +149,7 @@ public void testCreateZoneWithOptions() { dns = options.service(); // creates DnsImpl Zone zone = dns.create(ZONE_INFO, ZONE_FIELDS); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); - assertEquals(ZONE_INFO, zone); + assertEquals(new Zone(dns, ZONE_INFO), zone); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); } @@ -160,8 +160,8 @@ public void testGetZone() { .andReturn(ZONE_INFO.toPb()); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name()); - assertEquals(ZONE_INFO, zoneInfo); + Zone zone = dns.getZone(ZONE_INFO.name()); + assertEquals(new Zone(dns, ZONE_INFO), zone); } @Test @@ -171,9 +171,9 @@ public void testGetZoneWithOptions() { EasyMock.capture(capturedOptions))).andReturn(ZONE_INFO.toPb()); EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl - ZoneInfo zoneInfo = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS); + Zone zone = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); - assertEquals(ZONE_INFO, zoneInfo); + assertEquals(new Zone(dns, ZONE_INFO), zone); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); } @@ -308,7 +308,7 @@ public void testListZones() { dns = options.service(); // creates DnsImpl Page zonePage = dns.listZones(); assertEquals(1, Lists.newArrayList(zonePage.values()).size()); - assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0)); + assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0)); } @Test @@ -320,7 +320,7 @@ public void testListZonesWithOptions() { dns = options.service(); // creates DnsImpl Page zonePage = dns.listZones(ZONE_LIST_OPTIONS); assertEquals(1, Lists.newArrayList(zonePage.values()).size()); - assertEquals(ZONE_INFO, Lists.newArrayList(zonePage.values()).get(0)); + assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0)); Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); String selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[1].rpcOption()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java index 1b09dca715a4..b28847a4e046 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java @@ -19,6 +19,7 @@ import static org.easymock.EasyMock.createStrictMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -64,16 +65,22 @@ public class ZoneTest { .startTimeMillis(123465L).build(); private static final ChangeRequest CHANGE_REQUEST_NO_ID = ChangeRequest.builder().build(); private static final DnsException EXCEPTION = createStrictMock(DnsException.class); + private static final DnsOptions OPTIONS = createStrictMock(DnsOptions.class); private Dns dns; private Zone zone; private Zone zoneNoId; + @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + replay(dns); zone = new Zone(dns, ZONE_INFO); zoneNoId = new Zone(dns, NO_ID_INFO); + reset(dns); } @After @@ -347,13 +354,13 @@ public void getChangeAndZoneNotFoundByName() { .andThrow(EXCEPTION); replay(dns); try { - ChangeRequest result = zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); + zoneNoId.getChangeRequest(CHANGE_REQUEST.id()); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected } try { - ChangeRequest result = zone.getChangeRequest(CHANGE_REQUEST.id()); + zone.getChangeRequest(CHANGE_REQUEST.id()); fail("Parent container not found, should throw an exception."); } catch (DnsException e) { // expected @@ -505,18 +512,31 @@ public void listChangeRequestsAndZoneNotFound() { @Test public void testFromPb() { + expect(dns.options()).andReturn(OPTIONS); replay(dns); assertEquals(Zone.fromPb(dns, zone.toPb()), zone); } @Test public void testEqualsAndToBuilder() { + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); replay(dns); assertEquals(zone, zone.toBuilder().build()); + assertEquals(zone.hashCode(), zone.toBuilder().build().hashCode()); } @Test public void testBuilder() { + // one for each build() call because it invokes a constructor + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); + expect(dns.options()).andReturn(OPTIONS); replay(dns); assertNotEquals(zone, zone.toBuilder() .id((new BigInteger(zone.id())).add(BigInteger.ONE).toString()) From ee5bb8f1db07dd6a3fb125c16acfd27f8e0c7325 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 8 Feb 2016 16:35:07 -0800 Subject: [PATCH 4/4] Removed zone.get() and optimized retries. --- .../com/google/gcloud/dns/ChangeRequest.java | 27 +++----- .../main/java/com/google/gcloud/dns/Dns.java | 2 +- .../java/com/google/gcloud/dns/DnsImpl.java | 61 +++++++------------ .../java/com/google/gcloud/dns/DnsRecord.java | 18 +++++- .../main/java/com/google/gcloud/dns/Zone.java | 26 +------- .../java/com/google/gcloud/dns/ZoneInfo.java | 2 +- .../com/google/gcloud/dns/DnsImplTest.java | 42 +++++++------ .../java/com/google/gcloud/dns/ZoneTest.java | 28 +-------- 8 files changed, 77 insertions(+), 129 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java index 582dd2b2e05b..76d231b704c4 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ChangeRequest.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.api.services.dns.model.Change; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; @@ -40,21 +40,14 @@ */ public class ChangeRequest implements Serializable { - private static final Function FROM_PB_FUNCTION = - new Function() { + static final Function FROM_PB_FUNCTION = + new Function() { @Override - public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { - return DnsRecord.fromPb(pb); + public ChangeRequest apply(com.google.api.services.dns.model.Change pb) { + return ChangeRequest.fromPb(pb); } }; - private static final Function TO_PB_FUNCTION = - new Function() { - @Override - public com.google.api.services.dns.model.ResourceRecordSet apply(DnsRecord error) { - return error.toPb(); - } - }; - private static final long serialVersionUID = -8703939628990291682L; + private static final long serialVersionUID = -9027378042756366333L; private final List additions; private final List deletions; private final String id; @@ -274,9 +267,9 @@ com.google.api.services.dns.model.Change toPb() { pb.setStatus(status().name().toLowerCase()); } // set a list of additions - pb.setAdditions(Lists.transform(additions(), TO_PB_FUNCTION)); + pb.setAdditions(Lists.transform(additions(), DnsRecord.TO_PB_FUNCTION)); // set a list of deletions - pb.setDeletions(Lists.transform(deletions(), TO_PB_FUNCTION)); + pb.setDeletions(Lists.transform(deletions(), DnsRecord.TO_PB_FUNCTION)); return pb; } @@ -293,10 +286,10 @@ static ChangeRequest fromPb(com.google.api.services.dns.model.Change pb) { builder.status(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase())); } if (pb.getDeletions() != null) { - builder.deletions(Lists.transform(pb.getDeletions(), FROM_PB_FUNCTION)); + builder.deletions(Lists.transform(pb.getDeletions(), DnsRecord.FROM_PB_FUNCTION)); } if (pb.getAdditions() != null) { - builder.additions(Lists.transform(pb.getAdditions(), FROM_PB_FUNCTION)); + builder.additions(Lists.transform(pb.getAdditions(), DnsRecord.FROM_PB_FUNCTION)); } return builder.build(); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 88e0d7a08591..e724e1cbf1ad 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -419,7 +419,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { /** * Creates a new zone. * - *

Returns {@link ZoneInfo} object representing the new zone's information. In addition to the + *

Returns {@link Zone} object representing the new zone's information. In addition to the * name, dns name and description (supplied by the user within the {@code zoneInfo} parameter), * the returned object can include the following read-only fields supplied by the server: creation * time, id, and list of name servers. The returned fields can be optionally restricted by diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java index c93409554dd8..17521c13c625 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsImpl.java @@ -66,14 +66,7 @@ public Page nextPage() { private static class ChangeRequestPageFetcher implements PageImpl.NextPageFetcher { - private static final Function PB_TO_CHANGE_REQUEST = - new Function() { - @Override - public ChangeRequest apply(com.google.api.services.dns.model.Change changePb) { - return fromPb(changePb); - } - }; - private static final long serialVersionUID = -8737501076674042014L; + private static final long serialVersionUID = 4473265130673029139L; private final String zoneName; private final Map requestOptions; private final DnsOptions serviceOptions; @@ -88,20 +81,13 @@ public ChangeRequest apply(com.google.api.services.dns.model.Change changePb) { @Override public Page nextPage() { - return listChangeRequests(zoneName, serviceOptions, requestOptions, PB_TO_CHANGE_REQUEST); + return listChangeRequests(zoneName, serviceOptions, requestOptions); } } private static class DnsRecordPageFetcher implements PageImpl.NextPageFetcher { - private static final Function PB_TO_DNS_RECORD = - new Function() { - @Override - public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { - return DnsRecord.fromPb(pb); - } - }; - private static final long serialVersionUID = 670996349097667660L; + private static final long serialVersionUID = -6039369212511530846L; private final Map requestOptions; private final DnsOptions serviceOptions; private final String zoneName; @@ -116,10 +102,15 @@ public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { @Override public Page nextPage() { - return listDnsRecords(zoneName, serviceOptions, requestOptions, PB_TO_DNS_RECORD); + return listDnsRecords(zoneName, serviceOptions, requestOptions); } } + DnsImpl(DnsOptions options) { + super(options); + dnsRpc = options.rpc(); + } + @Override public Page listZones(ZoneListOption... options) { return listZones(options(), optionMap(options)); @@ -133,16 +124,17 @@ private static Page listZones(final DnsOptions serviceOptions, @Override public Zone apply( com.google.api.services.dns.model.ManagedZone zonePb) { - return new Zone(serviceOptions.service(), ZoneInfo.fromPb(zonePb)); + return Zone.fromPb(serviceOptions.service(), zonePb); } }; try { // get a list of managed zones + final DnsRpc rpc = serviceOptions.rpc(); DnsRpc.ListResult result = runWithRetries(new Callable>() { @Override public DnsRpc.ListResult call() { - return serviceOptions.rpc().listZones(optionsMap); + return rpc.listZones(optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.pageToken(); @@ -159,26 +151,25 @@ public DnsRpc.ListResult call() { @Override public Page listChangeRequests(String zoneName, ChangeRequestListOption... options) { - return listChangeRequests(zoneName, options(), optionMap(options), - ChangeRequestPageFetcher.PB_TO_CHANGE_REQUEST); + return listChangeRequests(zoneName, options(), optionMap(options)); } private static Page listChangeRequests(final String zoneName, - final DnsOptions serviceOptions, final Map optionsMap, - Function transformFunction) { + final DnsOptions serviceOptions, final Map optionsMap) { try { // get a list of changes + final DnsRpc rpc = serviceOptions.rpc(); DnsRpc.ListResult result = runWithRetries(new Callable>() { @Override public DnsRpc.ListResult call() { - return serviceOptions.rpc().listChangeRequests(zoneName, optionsMap); + return rpc.listChangeRequests(zoneName, optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.pageToken(); // transform that list into change request objects Iterable changes = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), transformFunction); + : Iterables.transform(result.results(), ChangeRequest.FROM_PB_FUNCTION); return new PageImpl<>(new ChangeRequestPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, changes); } catch (RetryHelperException e) { @@ -188,27 +179,26 @@ public DnsRpc.ListResult call() { @Override public Page listDnsRecords(String zoneName, DnsRecordListOption... options) { - return listDnsRecords(zoneName, options(), optionMap(options), - DnsRecordPageFetcher.PB_TO_DNS_RECORD); + return listDnsRecords(zoneName, options(), optionMap(options)); } private static Page listDnsRecords(final String zoneName, - final DnsOptions serviceOptions, final Map optionsMap, - Function transformFunction) { + final DnsOptions serviceOptions, final Map optionsMap) { try { // get a list of resource record sets + final DnsRpc rpc = serviceOptions.rpc(); DnsRpc.ListResult result = runWithRetries( new Callable>() { @Override public DnsRpc.ListResult call() { - return serviceOptions.rpc().listDnsRecords(zoneName, optionsMap); + return rpc.listDnsRecords(zoneName, optionsMap); } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.pageToken(); // transform that list into dns records Iterable records = result.results() == null ? ImmutableList.of() - : Iterables.transform(result.results(), transformFunction); + : Iterables.transform(result.results(), DnsRecord.FROM_PB_FUNCTION); return new PageImpl<>(new DnsRecordPageFetcher(zoneName, serviceOptions, cursor, optionsMap), cursor, records); } catch (RetryHelperException e) { @@ -216,11 +206,6 @@ public DnsRpc.ListResult call() { } } - DnsImpl(DnsOptions options) { - super(options); - dnsRpc = options.rpc(); - } - @Override public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) { final Map optionsMap = optionMap(options); @@ -318,7 +303,7 @@ public com.google.api.services.dns.model.Change call() { return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : fromPb(answer); // should never be null + return answer == null ? null : fromPb(answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java index 99ca20386419..c4e710bd0365 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsRecord.java @@ -19,6 +19,8 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.services.dns.model.ResourceRecordSet; +import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -43,7 +45,21 @@ */ public class DnsRecord implements Serializable { - private static final long serialVersionUID = 2016011914302204L; + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public DnsRecord apply(com.google.api.services.dns.model.ResourceRecordSet pb) { + return DnsRecord.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.dns.model.ResourceRecordSet apply(DnsRecord error) { + return error.toPb(); + } + }; + private static final long serialVersionUID = 8148009870800115261L; private final String name; private final List rrdatas; private final Integer ttl; // this is in seconds diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java index d53909b0f084..323b0eb8c439 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Zone.java @@ -113,30 +113,6 @@ public Builder toBuilder() { return new Builder(this); } - /** - * Constructs a {@code Zone} object that contains the given {@code zoneInfo}. - */ - public Zone(Dns dns, ZoneInfo zoneInfo) { - super(new BuilderImpl(zoneInfo)); - this.dns = dns; - this.options = dns.options(); - } - - /** - * Constructs a {@code Zone} object that contains meta information received from the Google Cloud - * DNS service for the provided {@code zoneName}. - * - * @param zoneName name of the zone to be searched for - * @param options optional restriction on what fields should be returned by the service - * @return zone object containing metadata or {@code null} if not not found - * @throws DnsException upon failure - */ - public static Zone get(Dns dnsService, String zoneName, Dns.ZoneOption... options) { - checkNotNull(zoneName); - checkNotNull(dnsService); - return dnsService.getZone(zoneName, options); - } - /** * Retrieves the latest information about the zone. The method retrieves the zone by name. * @@ -236,6 +212,6 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE static Zone fromPb(Dns dns, com.google.api.services.dns.model.ManagedZone zone) { ZoneInfo info = ZoneInfo.fromPb(zone); - return new Zone(dns, info); + return new Zone(dns, new ZoneInfo.BuilderImpl(info)); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java index e26dcde70a83..f02f9f2c5226 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ZoneInfo.java @@ -81,7 +81,7 @@ public abstract static class Builder { * Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name * servers that all host the same zones. Most users will not need to specify this value. */ - public abstract Builder nameServerSet(String nameServerSet); + abstract Builder nameServerSet(String nameServerSet); // todo(mderka) add more to the doc when questions are answered by the service owner /** diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java index 62e96b05dda5..726a455418d8 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsImplTest.java @@ -81,20 +81,20 @@ public class DnsImplTest { Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); // Listing options - private static final Dns.ZoneListOption[] ZONE_LIST_OPTIONS = - {Dns.ZoneListOption.pageSize(MAX_SIZE), Dns.ZoneListOption.pageToken(PAGE_TOKEN), - Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)}; - private static final Dns.ChangeRequestListOption[] CHANGE_LIST_OPTIONS = - {Dns.ChangeRequestListOption.pageSize(MAX_SIZE), - Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), - Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), - Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; - private static final Dns.DnsRecordListOption[] DNS_RECORD_LIST_OPTIONS = - {Dns.DnsRecordListOption.pageSize(MAX_SIZE), - Dns.DnsRecordListOption.pageToken(PAGE_TOKEN), - Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL), - Dns.DnsRecordListOption.dnsName(DNS_NAME), - Dns.DnsRecordListOption.type(DnsRecord.Type.AAAA)}; + private static final Dns.ZoneListOption[] ZONE_LIST_OPTIONS = { + Dns.ZoneListOption.pageSize(MAX_SIZE), Dns.ZoneListOption.pageToken(PAGE_TOKEN), + Dns.ZoneListOption.fields(Dns.ZoneField.DESCRIPTION)}; + private static final Dns.ChangeRequestListOption[] CHANGE_LIST_OPTIONS = { + Dns.ChangeRequestListOption.pageSize(MAX_SIZE), + Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN), + Dns.ChangeRequestListOption.fields(Dns.ChangeRequestField.STATUS), + Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)}; + private static final Dns.DnsRecordListOption[] DNS_RECORD_LIST_OPTIONS = { + Dns.DnsRecordListOption.pageSize(MAX_SIZE), + Dns.DnsRecordListOption.pageToken(PAGE_TOKEN), + Dns.DnsRecordListOption.fields(Dns.DnsRecordField.TTL), + Dns.DnsRecordListOption.dnsName(DNS_NAME), + Dns.DnsRecordListOption.type(DnsRecord.Type.AAAA)}; // Other private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @@ -137,7 +137,7 @@ public void testCreateZone() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Zone zone = dns.create(ZONE_INFO); - assertEquals(new Zone(dns, ZONE_INFO), zone); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), zone); } @Test @@ -149,7 +149,7 @@ public void testCreateZoneWithOptions() { dns = options.service(); // creates DnsImpl Zone zone = dns.create(ZONE_INFO, ZONE_FIELDS); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); - assertEquals(new Zone(dns, ZONE_INFO), zone); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), zone); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); } @@ -161,7 +161,7 @@ public void testGetZone() { EasyMock.replay(dnsRpcMock); dns = options.service(); // creates DnsImpl Zone zone = dns.getZone(ZONE_INFO.name()); - assertEquals(new Zone(dns, ZONE_INFO), zone); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), zone); } @Test @@ -173,7 +173,7 @@ public void testGetZoneWithOptions() { dns = options.service(); // creates DnsImpl Zone zone = dns.getZone(ZONE_INFO.name(), ZONE_FIELDS); String selector = (String) capturedOptions.getValue().get(ZONE_FIELDS.rpcOption()); - assertEquals(new Zone(dns, ZONE_INFO), zone); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), zone); assertTrue(selector.contains(Dns.ZoneField.CREATION_TIME.selector())); assertTrue(selector.contains(Dns.ZoneField.NAME.selector())); } @@ -308,7 +308,8 @@ public void testListZones() { dns = options.service(); // creates DnsImpl Page zonePage = dns.listZones(); assertEquals(1, Lists.newArrayList(zonePage.values()).size()); - assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0)); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), + Lists.newArrayList(zonePage.values()).get(0)); } @Test @@ -320,7 +321,8 @@ public void testListZonesWithOptions() { dns = options.service(); // creates DnsImpl Page zonePage = dns.listZones(ZONE_LIST_OPTIONS); assertEquals(1, Lists.newArrayList(zonePage.values()).size()); - assertEquals(new Zone(dns, ZONE_INFO), Lists.newArrayList(zonePage.values()).get(0)); + assertEquals(new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)), + Lists.newArrayList(zonePage.values()).get(0)); Integer size = (Integer) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[0].rpcOption()); assertEquals(MAX_SIZE, size); String selector = (String) capturedOptions.getValue().get(ZONE_LIST_OPTIONS[1].rpcOption()); diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java index b28847a4e046..5164dfb6001c 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ZoneTest.java @@ -78,8 +78,8 @@ public void setUp() throws Exception { expect(dns.options()).andReturn(OPTIONS); expect(dns.options()).andReturn(OPTIONS); replay(dns); - zone = new Zone(dns, ZONE_INFO); - zoneNoId = new Zone(dns, NO_ID_INFO); + zone = new Zone(dns, new ZoneInfo.BuilderImpl(ZONE_INFO)); + zoneNoId = new Zone(dns, new ZoneInfo.BuilderImpl(NO_ID_INFO)); reset(dns); } @@ -96,30 +96,6 @@ public void testConstructor() { assertEquals(dns, zone.dns()); } - @Test - public void testGetByName() { - expect(dns.getZone(ZONE_NAME)).andReturn(zone); - expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(zone); // for options - replay(dns); - Zone retrieved = Zone.get(dns, ZONE_NAME); - assertSame(dns, retrieved.dns()); - assertEquals(zone, retrieved); - // test passing options - Zone.get(dns, ZONE_NAME, ZONE_FIELD_OPTIONS); - try { - Zone.get(dns, null); - fail("Cannot get by null name."); - } catch (NullPointerException e) { - // expected - } - try { - Zone.get(null, "Not null"); - fail("Cannot get anything from null service."); - } catch (NullPointerException e) { - // expected - } - } - @Test public void deleteByNameAndFound() { expect(dns.delete(ZONE_NAME)).andReturn(true);