Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions gcloud-java-dns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,17 @@ 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

This comment was marked as spam.

This comment was marked as spam.

while (!pendingRequest.isDone()) {
System.out.println("Waiting for change to complete. Going to sleep for 500ms...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,41 @@ public Dns dns() {
}

/**
* Applies this change request to the associated zone.
* Applies this change request to the zone identified by {@code zoneName}.
*/
public ChangeRequest applyTo(Dns.ChangeRequestOption... options) {
return dns.applyChangeRequest(zone, this, options);
public ChangeRequest applyTo(String zoneName, Dns.ChangeRequestOption... options) {

This comment was marked as spam.

This comment was marked as spam.

return dns.applyChangeRequest(zoneName, this, options);
}

/**
* Retrieves the up-to-date information about the change request from Google Cloud DNS. Parameter

This comment was marked as spam.

This comment was marked as spam.

* {@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. The function makes an API call
* to Google Cloud DNS in order to request the status update only if the status of the change
* request is {@link Status#PENDING}. If the status is already {@link Status#DONE}, the method

This comment was marked as spam.

This comment was marked as spam.

* returns {@code true} without an API call.
*
* @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;

This comment was marked as spam.

This comment was marked as spam.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,26 @@ 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()
.startTimeMillis(132L)
.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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,9 @@ 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,

This comment was marked as spam.

This comment was marked as spam.

Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS));
while (!changeRequest.isDone()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,21 +60,17 @@ 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);
} catch (InterruptedException e) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,17 @@ 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);
} catch (InterruptedException e) {
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);
}
}

Expand Down