diff --git a/gcloud-java-dns/README.md b/gcloud-java-dns/README.md index ae5e52e9291f..0bb9a47635a2 100644 --- a/gcloud-java-dns/README.md +++ b/gcloud-java-dns/README.md @@ -212,7 +212,7 @@ while (recordSetIterator.hasNext()) { // Build and apply the change request to our zone ChangeRequestInfo changeRequest = changeBuilder.build(); -zone.applyChangeRequest(changeRequest); +ChangeRequest pendingRequest = zone.applyChangeRequest(changeRequest); ``` You can find more information about changes in the [Cloud DNS documentation] (https://cloud.google.com/dns/what-is-cloud-dns#cloud_dns_api_concepts). @@ -220,13 +220,12 @@ When the change request is applied, it is registered with the Cloud DNS service can wait for its completion as follows: ```java -while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { +while (!pendingRequest.isDone()) { try { Thread.sleep(500L); } catch (InterruptedException e) { System.err.println("The thread was interrupted while waiting..."); } - changeRequest = dns.getChangeRequest(zone.name(), changeRequest.generatedId()); } System.out.println("The change request has been applied."); ``` @@ -300,12 +299,10 @@ while (recordIterator.hasNext()) { // Build and apply the change request to our zone if it contains records to delete ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -313,9 +310,6 @@ if (!changeRequest.deletions().isEmpty()) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } 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 60b053faab6d..66e841c20a15 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 @@ -148,10 +148,42 @@ public Dns dns() { } /** - * Applies this change request to the associated zone. + * Applies this change request to the zone identified by {@code zoneName}. + * + * @throws DnsException upon failure or if zone is not found */ - public ChangeRequest applyTo(Dns.ChangeRequestOption... options) { - return dns.applyChangeRequest(zone, this, options); + public ChangeRequest applyTo(String zoneName, Dns.ChangeRequestOption... options) { + return dns.applyChangeRequest(zoneName, this, options); + } + + /** + * Retrieves the up-to-date information about the change request from Google Cloud DNS. Parameter + * {@code options} can be used to restrict the fields to be included in the updated object the + * same way as in {@link Dns#getChangeRequest(String, String, Dns.ChangeRequestOption...)}. If + * {@code options} are provided, any field other than generatedId which is not included in the + * {@code options} will be {@code null} regardless of whether they are initialized or not in + * {@code this} instance. + * + * @return an object with the updated information or {@code null} if it does not exist + * @throws DnsException upon failure of the API call or if the associated zone was not found + */ + public ChangeRequest reload(Dns.ChangeRequestOption... options) { + return dns.getChangeRequest(zone, generatedId(), options); + } + + /** + * Returns {@code true} if the change request has been completed. If the status is not {@link + * Status#DONE} already, the method makes an API call to Google Cloud DNS to update the change + * request first. + * + * @throws DnsException upon failure of the API call or if the associated zone was not found + */ + public boolean isDone() { + if (status() == Status.DONE) { + return true; + } + ChangeRequest updated = reload(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + return updated == null || updated.status() == Status.DONE; } @Override 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 d95d11a97c19..a78eaef7c4e7 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 @@ -475,7 +475,7 @@ public static ChangeRequestListOption sortOrder(SortingOrder order) { * servers. The fields to be returned can be selected by {@link ChangeRequestOption}s. * * @return the new {@link ChangeRequest} - * @throws DnsException upon failure if zone is not found + * @throws DnsException upon failure or if zone is not found * @see Cloud DNS Changes: create */ ChangeRequest applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest, diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java index 4d4b2c01823c..2c3be337f033 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/ChangeRequestTest.java @@ -43,12 +43,13 @@ public class ChangeRequestTest { private Dns dns; private ChangeRequest changeRequest; + private ChangeRequest changeRequestPending; private ChangeRequest changeRequestPartial; @Before public void setUp() throws Exception { dns = createStrictMock(Dns.class); - expect(dns.options()).andReturn(OPTIONS).times(2); + expect(dns.options()).andReturn(OPTIONS).times(3); replay(dns); changeRequest = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( CHANGE_REQUEST_INFO.toBuilder() @@ -56,6 +57,12 @@ public void setUp() throws Exception { .generatedId("12") .status(ChangeRequest.Status.DONE) .build())); + changeRequestPending = new ChangeRequest(dns, ZONE_NAME, new ChangeRequestInfo.BuilderImpl( + CHANGE_REQUEST_INFO.toBuilder() + .startTimeMillis(132L) + .generatedId("12") + .status(ChangeRequest.Status.PENDING) + .build())); changeRequestPartial = new ChangeRequest(dns, ZONE_NAME, new ChangeRequest.BuilderImpl(CHANGE_REQUEST_INFO)); reset(dns); @@ -133,8 +140,34 @@ public void testApplyTo() { Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) .andReturn(changeRequest); replay(dns); - assertSame(changeRequest, changeRequest.applyTo()); - assertSame(changeRequest, - changeRequest.applyTo(Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + assertSame(changeRequest, changeRequest.applyTo(ZONE_NAME)); + assertSame(changeRequest, changeRequest.applyTo(ZONE_NAME, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + } + + @Test + public void testReload() { + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId())).andReturn(changeRequest); + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))) + .andReturn(changeRequest); + replay(dns); + assertSame(changeRequest, changeRequest.reload()); + assertSame(changeRequest, changeRequest.reload( + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME))); + } + + @Test + public void testIsDone() { + replay(dns); + assertTrue(changeRequest.isDone()); + verify(dns); + reset(dns); + expect(dns.getChangeRequest(ZONE_NAME, changeRequest.generatedId(), + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS))) + .andReturn(changeRequest); + replay(dns); + assertTrue(changeRequestPending.isDone()); + verify(dns); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java index aa6c78461b8b..f25bd537cd72 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/it/ITDnsTest.java @@ -147,12 +147,13 @@ private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRe } private static void waitForChangeToComplete(String zoneName, String changeId) { - while (true) { - ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); - if (ChangeRequest.Status.DONE.equals(changeRequest.status())) { - return; - } + ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, + Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); + waitForChangeToComplete(changeRequest); + } + + private static void waitForChangeToComplete(ChangeRequest changeRequest) { + while (!changeRequest.isDone()) { try { Thread.sleep(500); } catch (InterruptedException e) { @@ -529,9 +530,9 @@ public void testCreateChange() { assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE) .contains(created.status())); assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1")); - waitForChangeToComplete(ZONE1.name(), "1"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "2"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); // with options created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID)); @@ -540,9 +541,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("3", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "3"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "4"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS)); assertTrue(created.additions().isEmpty()); @@ -550,9 +551,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("5", created.generatedId()); assertNotNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "5"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "6"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME)); assertTrue(created.additions().isEmpty()); @@ -560,9 +561,9 @@ public void testCreateChange() { assertTrue(created.deletions().isEmpty()); assertEquals("7", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "7"); - DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); - waitForChangeToComplete(ZONE1.name(), "8"); + waitForChangeToComplete(created); + created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS)); assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions()); @@ -571,16 +572,16 @@ public void testCreateChange() { assertEquals("9", created.generatedId()); assertNull(created.status()); // finishes with delete otherwise we cannot delete the zone - waitForChangeToComplete(ZONE1.name(), "9"); + waitForChangeToComplete(created); created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS)); - waitForChangeToComplete(ZONE1.name(), "10"); + waitForChangeToComplete(created); assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions()); assertNull(created.startTimeMillis()); assertTrue(created.additions().isEmpty()); assertEquals("10", created.generatedId()); assertNull(created.status()); - waitForChangeToComplete(ZONE1.name(), "10"); + waitForChangeToComplete(created); } finally { clear(); } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java index aaf663920173..c791ea8c0992 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java @@ -22,6 +22,7 @@ package com.google.gcloud.examples.dns.snippets; +import com.google.gcloud.dns.ChangeRequest; import com.google.gcloud.dns.ChangeRequestInfo; import com.google.gcloud.dns.Dns; import com.google.gcloud.dns.DnsOptions; @@ -59,12 +60,10 @@ public static void main(String... args) { // Build and apply the change request to our zone if it contains records to delete ChangeRequestInfo changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -72,8 +71,6 @@ public static void main(String... args) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java index 50f3a1a5c951..c96c93191c10 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/ManipulateZonesAndRecordSets.java @@ -128,12 +128,10 @@ public static void main(String... args) { // Build and apply the change request to our zone if it contains records to delete changeRequest = changeBuilder.build(); if (!changeRequest.deletions().isEmpty()) { - changeRequest = dns.applyChangeRequest(zoneName, changeRequest); + ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest); - // Wait for change to finish, but save data traffic by transferring only ID and status - Dns.ChangeRequestOption option = - Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS); - while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) { + // Wait for the change request to complete + while (!pendingRequest.isDone()) { System.out.println("Waiting for change to complete. Going to sleep for 500ms..."); try { Thread.sleep(500); @@ -141,9 +139,6 @@ public static void main(String... args) { System.err.println("The thread was interrupted while waiting for change request to be " + "processed."); } - - // Update the change, but fetch only change ID and status - changeRequest = dns.getChangeRequest(zoneName, changeRequest.generatedId(), option); } }