diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index d9317546cea0..31c0db2dd983 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -96,6 +96,14 @@ public static Builder builder() { return new Builder(); } + /** + * Creates a default instance of {@code DnsOptions} with the project ID and credentials inferred + * from the environment. + */ + public static DnsOptions defaultInstance() { + return builder().build(); + } + @Override public boolean equals(Object obj) { return obj instanceof DnsOptions && baseEquals((DnsOptions) obj); diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 73d613c94e27..5e11fd2b0cb7 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -74,7 +74,7 @@ To run examples from your command line: * Here's an example run of `DnsExample`. Note that you have to enable the Google Cloud DNS API on the [Google Developers Console][developers-console] before running the following commands. - You will need to replace the domain name `elaborateexample.com` with your own domain name with verified ownership. + You will need to replace the domain name `elaborateexample.com` with your own domain name with [verified ownership] (https://www.google.com/webmasters/verification/home). Also, note that the example creates and deletes DNS records of type A only. Operations with other record types are not implemented in the example. ``` mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.dns.DnsExample" -Dexec.args="create some-sample-zone elaborateexample.com. description" diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java index 2061f4931cab..1d8581bafeec 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java @@ -58,13 +58,14 @@ * * *

The first parameter is an optional {@code project_id} (logged-in project will be used if not - * supplied). Second parameter is a DNS operation (list, delete, create,...). The remaining + * 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. */ public class DnsExample { private static final Map ACTIONS = new HashMap<>(); + private static final DateFormat FORMATTER = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); private interface DnsAction { void run(Dns dns, String... args); @@ -111,14 +112,9 @@ public void run(Dns dns, String... args) { Iterator zoneIterator = dns.listZones().iterateAll(); if (zoneIterator.hasNext()) { System.out.println("The project contains the following zones:"); - DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); while (zoneIterator.hasNext()) { Zone zone = zoneIterator.next(); - System.out.printf("%nName: %s%n", zone.name()); - System.out.printf("ID: %s%n", zone.id()); - System.out.printf("Description: %s%n", zone.description()); - System.out.printf("Created: %s%n", formatter.format(new Date(zone.creationTimeMillis()))); - System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); + printZone(zone); } } else { System.out.println("Project contains no zones."); @@ -148,12 +144,7 @@ public void run(Dns dns, String... args) { if (zone == null) { System.out.printf("No zone with name '%s' exists.%n", zoneName); } else { - System.out.printf("Name: %s%n", zone.name()); - System.out.printf("ID: %s%n", zone.id()); - System.out.printf("Description: %s%n", zone.description()); - DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); - System.out.printf("Created: %s%n", formatter.format(new Date(zone.creationTimeMillis()))); - System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); + printZone(zone); } } @@ -210,7 +201,7 @@ public void run(Dns dns, String... args) { String ip = args[2]; int ttl = 0; if (args.length > 3) { - ttl = Integer.valueOf(args[3]); + ttl = Integer.parseInt(args[3]); } DnsRecord record = DnsRecord.builder(recordName, DnsRecord.Type.A) .records(ImmutableList.of(ip)) @@ -244,7 +235,7 @@ public String params() { public boolean check(String... args) { if (args.length == 4) { // to check that it can be parsed - Integer.valueOf(args[3]); + Integer.parseInt(args[3]); return true; } else { return args.length == 3; @@ -265,7 +256,7 @@ public void run(Dns dns, String... args) { String ip = args[2]; int ttl = 0; if (args.length > 3) { - ttl = Integer.valueOf(args[3]); + ttl = Integer.parseInt(args[3]); } DnsRecord record = DnsRecord.builder(recordName, DnsRecord.Type.A) .records(ImmutableList.of(ip)) @@ -277,7 +268,7 @@ public void run(Dns dns, String... args) { 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.print("Waiting for deletion to happen..."); + System.out.print("Waiting for addition to happen..."); while (changeRequest.status().equals(ChangeRequest.Status.PENDING)) { System.out.print("."); try { @@ -299,7 +290,7 @@ public String params() { public boolean check(String... args) { if (args.length == 4) { // to check that it can be parsed - Integer.valueOf(args[3]); + Integer.parseInt(args[3]); return true; } else { return args.length == 3; @@ -358,12 +349,11 @@ public void run(Dns dns, String... args) { } if (iterator.hasNext()) { System.out.printf("Change requests for zone %s:%n", zoneName); - DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); while (iterator.hasNext()) { ChangeRequest change = iterator.next(); System.out.printf("%nID: %s%n", change.id()); System.out.printf("Status: %s%n", change.status()); - System.out.printf("Started: %s%n", formatter.format(change.startTimeMillis())); + System.out.printf("Started: %s%n", FORMATTER.format(change.startTimeMillis())); System.out.printf("Deletions: %s%n", Joiner.on(", ").join(change.deletions())); System.out.printf("Additions: %s%n", Joiner.on(", ").join(change.additions())); } @@ -464,6 +454,14 @@ public boolean check(String... args) { ACTIONS.put("quota", new GetProjectAction()); } + private static void printZone(Zone zone) { + System.out.printf("%nName: %s%n", zone.name()); + System.out.printf("ID: %s%n", zone.id()); + System.out.printf("Description: %s%n", zone.description()); + System.out.printf("Created: %s%n", FORMATTER.format(new Date(zone.creationTimeMillis()))); + System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers())); + } + private static void printUsage() { StringBuilder actionAndParams = new StringBuilder(); for (Map.Entry entry : ACTIONS.entrySet()) { diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java index 052847d6a062..7a29fcf2f2cf 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java @@ -38,8 +38,9 @@ public class CreateAndListDnsRecords { public static void main(String... args) { - // Create a service object. The project ID will be extracted from the environment. - Dns dns = DnsOptions.builder().build().service(); + // Create a service object. + // The project ID and credentials will be inferred from the environment. + Dns dns = DnsOptions.defaultInstance().service(); // Change this to a zone name that exists within your project String zoneName = "some-sample-zone"; @@ -47,7 +48,7 @@ public static void main(String... args) { // Get zone from the service Zone zone = dns.getZone(zoneName); - // Prepare a www.. type A record with ttl of 24 hours + // Prepare a www.. type A record with ttl of 24 hours String ip = "12.13.14.15"; DnsRecord toCreate = DnsRecord.builder("www." + zone.dnsName(), DnsRecord.Type.A) .ttl(24, TimeUnit.HOURS) diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java index e163949c7ceb..ce4bcfa2fd66 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java @@ -31,17 +31,17 @@ /** * A snippet for Google Cloud DNS showing how to create a zone and list all zones in the project. - * You will need to change the {@code domainName} to a domain name the ownership of which you - * verified with Google. + * You will need to change the {@code domainName} to a domain name, the ownership of which you + * should verify with Google. */ public class CreateAndListZones { public static void main(String... args) { // Create a service object - // Credentials are inferred from the environment. - Dns dns = DnsOptions.builder().build().service(); + // The project ID and credentials will be inferred from the environment. + Dns dns = DnsOptions.defaultInstance().service(); - // Create a zone metadata + // Create a zone metadata object String zoneName = "my_unique_zone"; // Change this zone name which is unique within your project String domainName = "someexampledomain.com."; // Change this to a domain which you own String description = "This is a gcloud-java-dns sample zone."; 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 cc975d283f27..507b80774cbd 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 @@ -37,10 +37,11 @@ public class DeleteZone { public static void main(String... args) { - // Create a service object. The project ID will be extracted from the environment. + // Create a service object. + // The project ID and credentials will be inferred from the environment. Dns dns = DnsOptions.builder().build().service(); - // Change this to a zone name that exists within your project + // Change this to a zone name that exists within your project and that you want to delete. String zoneName = "some-sample-zone"; // Get iterator for the existing records which have to be deleted before deleting the zone