Skip to content

Commit

Permalink
Removed possible ArrayIndexOutOfBoundsException.
Browse files Browse the repository at this point in the history
Fixed code formatting and comments.
  • Loading branch information
mderka committed Mar 14, 2016
1 parent 0173383 commit a0cd84f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
/*
* An example of using Google Cloud DNS.
*
* <p>This example creates, deletes, gets, and lists zones, and creates and deletes DNS records of
* type A.
* <p>This example creates, deletes, gets, and lists zones. It also creates and deletes DNS records
* of type A, and lists DNS records.
*
* <p>Steps needed for running the example:
* <ol>
Expand All @@ -57,10 +57,10 @@
* quota}</li>
* </ol>
*
* <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
* supplied). The second parameter is a DNS operation (list, delete, create,...). The remaining
* arguments are specific to the operation. See each action's run method for the specific
* interaction.
* <p>The first parameter is an optional {@code project_id} (project specified in the Google Cloud
* SDK configuration (see {@code gcloud config list}) will be used if not supplied). The second
* parameter is a DNS operation (list, delete, create, ...). The remaining arguments are specific to
* the operation. See each action's {@code run} method for the specific arguments.
*/
public class DnsExample {

Expand All @@ -78,7 +78,7 @@ private interface DnsAction {
private static class CreateZoneAction implements DnsAction {

/**
* Creates a zone with the provided name, dns name and description (in this order).
* Creates a zone with the provided name, DNS name and description (in this order).
*/
@Override
public void run(Dns dns, String... args) {
Expand Down Expand Up @@ -113,8 +113,7 @@ public void run(Dns dns, String... args) {
if (zoneIterator.hasNext()) {
System.out.println("The project contains the following zones:");
while (zoneIterator.hasNext()) {
Zone zone = zoneIterator.next();
printZone(zone);
printZone(zoneIterator.next());
}
} else {
System.out.println("Project contains no zones.");
Expand Down Expand Up @@ -211,18 +210,10 @@ public void run(Dns dns, String... args) {
.delete(record)
.build();
changeRequest = dns.applyChangeRequest(zoneName, changeRequest);
System.out.printf("The request for deleting A record %s for zone %s was successfully " +
"submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
System.out.printf("The request for deleting A record %s for zone %s was successfully "
+ "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
System.out.print("Waiting for deletion to happen...");
while (changeRequest.status().equals(ChangeRequest.Status.PENDING)) {
System.out.print(".");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted while waiting.");
}
changeRequest = dns.getChangeRequest(zoneName, changeRequest.id());
}
waitForChangeToFinish(dns, zoneName, changeRequest);
System.out.printf("%nThe deletion has been completed.%n");
}

Expand Down Expand Up @@ -262,22 +253,12 @@ public void run(Dns dns, String... args) {
.records(ImmutableList.of(ip))
.ttl(ttl, TimeUnit.SECONDS)
.build();
ChangeRequest changeRequest = ChangeRequest.builder()
.add(record)
.build();
ChangeRequest changeRequest = ChangeRequest.builder().add(record).build();
changeRequest = dns.applyChangeRequest(zoneName, changeRequest);
System.out.printf("The request for adding A record %s for zone %s was successfully " +
"submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
System.out.printf("The request for adding A record %s for zone %s was successfully "
+ "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
System.out.print("Waiting for addition to happen...");
while (changeRequest.status().equals(ChangeRequest.Status.PENDING)) {
System.out.print(".");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted while waiting.");
}
changeRequest = dns.getChangeRequest(zoneName, changeRequest.id());
}
waitForChangeToFinish(dns, zoneName, changeRequest);
System.out.printf("The addition has been completed.%n");
}

Expand Down Expand Up @@ -333,8 +314,8 @@ public boolean check(String... args) {
private static class ListChangesAction implements DnsAction {

/**
* Lists all the changes for a given zone. Optionally, an order, "descending" or "ascending" can
* be specified using the last parameter.
* Lists all the changes for a given zone. Optionally, an order ("descending" or "ascending")
* can be specified using the last parameter.
*/
@Override
public void run(Dns dns, String... args) {
Expand Down Expand Up @@ -398,7 +379,7 @@ public void run(Dns dns, String... args) {

@Override
public boolean check(String... args) {
if (args.length == 0) {
if (args.length == 0 || args.length == 1) {
return true;
}
if ("records".equals(args[1])) {
Expand Down Expand Up @@ -462,6 +443,21 @@ private static void printZone(Zone zone) {
System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers()));
}

private static ChangeRequest waitForChangeToFinish(Dns dns, String zoneName,
ChangeRequest request) {
ChangeRequest current = request;
while (current.status().equals(ChangeRequest.Status.PENDING)) {
System.out.print(".");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted while waiting.");
}
current = dns.getChangeRequest(zoneName, current.id());
}
return current;
}

private static void printUsage() {
StringBuilder actionAndParams = new StringBuilder();
for (Map.Entry<String, DnsAction> entry : ACTIONS.entrySet()) {
Expand Down Expand Up @@ -508,12 +504,13 @@ public static void main(String... args) throws Exception {
return;
} catch (Exception ex) {
System.out.println("Failed to parse request.");
System.out.println("Expected: " + action.params());
ex.printStackTrace();
return;
}
if (valid) {
DnsOptions.Builder optionsBuilder = DnsOptions.builder();
if(projectId != null) {
if (projectId != null) {
optionsBuilder.projectId(projectId);
}
Dns dns = optionsBuilder.build().service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public class CreateAndListDnsRecords {

public static void main(String... args) {

// Create a service object.
// The project ID and credentials will be inferred from the environment.
Dns dns = DnsOptions.defaultInstance().service();
Expand All @@ -63,7 +62,7 @@ public static void main(String... args) {
Iterator<DnsRecord> recordIterator = zone.listDnsRecords().iterateAll();
while (recordIterator.hasNext()) {
DnsRecord current = recordIterator.next();
if(toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) {
if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) {
changeBuilder.delete(current);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static void main(String... args) {
Zone createdZone = dns.create(zoneInfo);
System.out.printf("Zone was created and assigned ID %s.%n", createdZone.id());

// Now list all the zones within this project and print them using the default toString
// Now list all the zones within this project
Iterator<Zone> zoneIterator = dns.listZones().iterateAll();
int counter = 1;
while (zoneIterator.hasNext()) {
System.out.printf("#%d.: %s", counter, zoneIterator.next().toString());
System.out.printf("#%d.: %s%n%n", counter, zoneIterator.next().toString());
counter++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
public class DeleteZone {

public static void main(String... args) {

// Create a service object.
// The project ID and credentials will be inferred from the environment.
Dns dns = DnsOptions.builder().build().service();
Dns dns = DnsOptions.defaultInstance().service();

// Change this to a zone name that exists within your project and that you want to delete.
String zoneName = "some-sample-zone";
Expand All @@ -63,14 +62,15 @@ public static void main(String... args) {
changeRequest = 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);
Dns.ChangeRequestOption option =
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS);
while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) {
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.");
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.id(), option);
Expand Down

0 comments on commit a0cd84f

Please sign in to comment.