Skip to content

Commit

Permalink
Fixed the snipped and README. Removed possible NPE.
Browse files Browse the repository at this point in the history
Added @throws docs.
  • Loading branch information
mderka committed Apr 4, 2016
1 parent 3becc74 commit 8ad3e89
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
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
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 @@ -162,25 +162,27 @@ public ChangeRequest applyTo(String zoneName, Dns.ChangeRequestOption... options
* {@code options} will be {@code null} regardless of whether they are initialized or not in
* {@code this} instance.
*
* @return an object containing the updated information
* @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 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 returns
* {@code true} without 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
* 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.status() == Status.DONE;

return updated == null || updated.status() == Status.DONE;
}

@Override
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

0 comments on commit 8ad3e89

Please sign in to comment.