3535import java .util .Map ;
3636import java .util .concurrent .TimeUnit ;
3737
38- /**
38+ /*
3939 * An example of using Google Cloud DNS.
4040 *
41- * <p>This example creates, deletes, gets, and lists zones, and creates and deletes DNS records of
42- * type A.
41+ * <p>This example creates, deletes, gets, and lists zones. It also creates and deletes DNS records
42+ * of type A, and lists DNS records .
4343 *
4444 * <p>Steps needed for running the example:
4545 * <ol>
5757 * quota}</li>
5858 * </ol>
5959 *
60- * <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
61- * supplied). The second parameter is a DNS operation (list, delete, create,... ). The remaining
62- * arguments are specific to the operation. See each action's run method for the specific
63- * interaction .
60+ * <p>The first parameter is an optional {@code project_id} (project specified in the Google Cloud
61+ * SDK configuration (see {@code gcloud config list}) will be used if not supplied ). The second
62+ * parameter is a DNS operation (list, delete, create, ...). The remaining arguments are specific to
63+ * the operation. See each action's {@code run} method for the specific arguments .
6464 */
6565public class DnsExample {
6666
@@ -78,7 +78,7 @@ private interface DnsAction {
7878 private static class CreateZoneAction implements DnsAction {
7979
8080 /**
81- * Creates a zone with the provided name, dns name and description (in this order).
81+ * Creates a zone with the provided name, DNS name and description (in this order).
8282 */
8383 @ Override
8484 public void run (Dns dns , String ... args ) {
@@ -113,8 +113,7 @@ public void run(Dns dns, String... args) {
113113 if (zoneIterator .hasNext ()) {
114114 System .out .println ("The project contains the following zones:" );
115115 while (zoneIterator .hasNext ()) {
116- Zone zone = zoneIterator .next ();
117- printZone (zone );
116+ printZone (zoneIterator .next ());
118117 }
119118 } else {
120119 System .out .println ("Project contains no zones." );
@@ -211,18 +210,10 @@ public void run(Dns dns, String... args) {
211210 .delete (record )
212211 .build ();
213212 changeRequest = dns .applyChangeRequest (zoneName , changeRequest );
214- System .out .printf ("The request for deleting A record %s for zone %s was successfully " +
215- "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
213+ System .out .printf ("The request for deleting A record %s for zone %s was successfully "
214+ + "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
216215 System .out .print ("Waiting for deletion to happen..." );
217- while (changeRequest .status ().equals (ChangeRequest .Status .PENDING )) {
218- System .out .print ("." );
219- try {
220- Thread .sleep (500 );
221- } catch (InterruptedException e ) {
222- System .err .println ("Thread was interrupted while waiting." );
223- }
224- changeRequest = dns .getChangeRequest (zoneName , changeRequest .id ());
225- }
216+ waitForChangeToFinish (dns , zoneName , changeRequest );
226217 System .out .printf ("%nThe deletion has been completed.%n" );
227218 }
228219
@@ -262,22 +253,12 @@ public void run(Dns dns, String... args) {
262253 .records (ImmutableList .of (ip ))
263254 .ttl (ttl , TimeUnit .SECONDS )
264255 .build ();
265- ChangeRequest changeRequest = ChangeRequest .builder ()
266- .add (record )
267- .build ();
256+ ChangeRequest changeRequest = ChangeRequest .builder ().add (record ).build ();
268257 changeRequest = dns .applyChangeRequest (zoneName , changeRequest );
269- System .out .printf ("The request for adding A record %s for zone %s was successfully " +
270- "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
258+ System .out .printf ("The request for adding A record %s for zone %s was successfully "
259+ + "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
271260 System .out .print ("Waiting for addition to happen..." );
272- while (changeRequest .status ().equals (ChangeRequest .Status .PENDING )) {
273- System .out .print ("." );
274- try {
275- Thread .sleep (500 );
276- } catch (InterruptedException e ) {
277- System .err .println ("Thread was interrupted while waiting." );
278- }
279- changeRequest = dns .getChangeRequest (zoneName , changeRequest .id ());
280- }
261+ waitForChangeToFinish (dns , zoneName , changeRequest );
281262 System .out .printf ("The addition has been completed.%n" );
282263 }
283264
@@ -333,8 +314,8 @@ public boolean check(String... args) {
333314 private static class ListChangesAction implements DnsAction {
334315
335316 /**
336- * Lists all the changes for a given zone. Optionally, an order, "descending" or "ascending" can
337- * be specified using the last parameter.
317+ * Lists all the changes for a given zone. Optionally, an order ( "descending" or "ascending")
318+ * can be specified using the last parameter.
338319 */
339320 @ Override
340321 public void run (Dns dns , String ... args ) {
@@ -398,7 +379,7 @@ public void run(Dns dns, String... args) {
398379
399380 @ Override
400381 public boolean check (String ... args ) {
401- if (args .length == 0 ) {
382+ if (args .length == 0 || args . length == 1 ) {
402383 return true ;
403384 }
404385 if ("records" .equals (args [1 ])) {
@@ -462,6 +443,21 @@ private static void printZone(Zone zone) {
462443 System .out .printf ("Name servers: %s%n" , Joiner .on (", " ).join (zone .nameServers ()));
463444 }
464445
446+ private static ChangeRequest waitForChangeToFinish (Dns dns , String zoneName ,
447+ ChangeRequest request ) {
448+ ChangeRequest current = request ;
449+ while (current .status ().equals (ChangeRequest .Status .PENDING )) {
450+ System .out .print ("." );
451+ try {
452+ Thread .sleep (500 );
453+ } catch (InterruptedException e ) {
454+ System .err .println ("Thread was interrupted while waiting." );
455+ }
456+ current = dns .getChangeRequest (zoneName , current .id ());
457+ }
458+ return current ;
459+ }
460+
465461 private static void printUsage () {
466462 StringBuilder actionAndParams = new StringBuilder ();
467463 for (Map .Entry <String , DnsAction > entry : ACTIONS .entrySet ()) {
@@ -508,12 +504,13 @@ public static void main(String... args) throws Exception {
508504 return ;
509505 } catch (Exception ex ) {
510506 System .out .println ("Failed to parse request." );
507+ System .out .println ("Expected: " + action .params ());
511508 ex .printStackTrace ();
512509 return ;
513510 }
514511 if (valid ) {
515512 DnsOptions .Builder optionsBuilder = DnsOptions .builder ();
516- if (projectId != null ) {
513+ if (projectId != null ) {
517514 optionsBuilder .projectId (projectId );
518515 }
519516 Dns dns = optionsBuilder .build ().service ();
0 commit comments