Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified methods in ChangeRequest. #849

Merged
merged 5 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 5 additions & 11 deletions gcloud-java-dns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,20 @@ 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).

When the change request is applied, it is registered with the Cloud DNS service for processing. We
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.");
```
Expand Down Expand Up @@ -300,22 +299,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,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) {

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. 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;

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 @@ -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 <a href="https://cloud.google.com/dns/api/v1/changes/create">Cloud DNS Changes: create</a>
*/
ChangeRequest applyChangeRequest(String zoneName, ChangeRequestInfo changeRequest,
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,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,

This comment was marked as spam.

This comment was marked as spam.

Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS));
waitForChangeToComplete(changeRequest);
}

private static void waitForChangeToComplete(ChangeRequest changeRequest) {
while (!changeRequest.isDone()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -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));
Expand All @@ -540,29 +541,29 @@ 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());
assertNull(created.startTimeMillis());
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());
assertNotNull(created.startTimeMillis());
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());
Expand All @@ -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();
}
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