38
38
/**
39
39
* An example of using Google Cloud DNS.
40
40
*
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 .
43
43
*
44
44
* <p>Steps needed for running the example:
45
45
* <ol>
57
57
* quota}</li>
58
58
* </ol>
59
59
*
60
- * <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
61
- * supplied). 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}. The project specified in the Google
61
+ * Cloud SDK configuration (see {@code gcloud config list}) will be used if the project ID is not
62
+ * supplied. The second parameter is a DNS operation (list, delete, create, ...). The remaining
63
+ * arguments are specific to the operation. See each action's {@code run} method for the specific
64
+ * arguments.
64
65
*/
65
66
public class DnsExample {
66
67
67
68
private static final Map <String , DnsAction > ACTIONS = new HashMap <>();
69
+ private static final DateFormat FORMATTER = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss" );
68
70
69
71
private interface DnsAction {
70
72
void run (Dns dns , String ... args );
@@ -77,7 +79,7 @@ private interface DnsAction {
77
79
private static class CreateZoneAction implements DnsAction {
78
80
79
81
/**
80
- * Creates a zone with the provided name, dns name and description (in this order).
82
+ * Creates a zone with the provided name, DNS name and description (in this order).
81
83
*/
82
84
@ Override
83
85
public void run (Dns dns , String ... args ) {
@@ -111,14 +113,8 @@ public void run(Dns dns, String... args) {
111
113
Iterator <Zone > zoneIterator = dns .listZones ().iterateAll ();
112
114
if (zoneIterator .hasNext ()) {
113
115
System .out .println ("The project contains the following zones:" );
114
- DateFormat formatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss" );
115
116
while (zoneIterator .hasNext ()) {
116
- Zone zone = zoneIterator .next ();
117
- System .out .printf ("%nName: %s%n" , zone .name ());
118
- System .out .printf ("ID: %s%n" , zone .id ());
119
- System .out .printf ("Description: %s%n" , zone .description ());
120
- System .out .printf ("Created: %s%n" , formatter .format (new Date (zone .creationTimeMillis ())));
121
- System .out .printf ("Name servers: %s%n" , Joiner .on (", " ).join (zone .nameServers ()));
117
+ printZone (zoneIterator .next ());
122
118
}
123
119
} else {
124
120
System .out .println ("Project contains no zones." );
@@ -148,12 +144,7 @@ public void run(Dns dns, String... args) {
148
144
if (zone == null ) {
149
145
System .out .printf ("No zone with name '%s' exists.%n" , zoneName );
150
146
} else {
151
- System .out .printf ("Name: %s%n" , zone .name ());
152
- System .out .printf ("ID: %s%n" , zone .id ());
153
- System .out .printf ("Description: %s%n" , zone .description ());
154
- DateFormat formatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss" );
155
- System .out .printf ("Created: %s%n" , formatter .format (new Date (zone .creationTimeMillis ())));
156
- System .out .printf ("Name servers: %s%n" , Joiner .on (", " ).join (zone .nameServers ()));
147
+ printZone (zone );
157
148
}
158
149
}
159
150
@@ -210,7 +201,7 @@ public void run(Dns dns, String... args) {
210
201
String ip = args [2 ];
211
202
int ttl = 0 ;
212
203
if (args .length > 3 ) {
213
- ttl = Integer .valueOf (args [3 ]);
204
+ ttl = Integer .parseInt (args [3 ]);
214
205
}
215
206
DnsRecord record = DnsRecord .builder (recordName , DnsRecord .Type .A )
216
207
.records (ImmutableList .of (ip ))
@@ -220,18 +211,10 @@ public void run(Dns dns, String... args) {
220
211
.delete (record )
221
212
.build ();
222
213
changeRequest = dns .applyChangeRequest (zoneName , changeRequest );
223
- System .out .printf ("The request for deleting A record %s for zone %s was successfully " +
224
- "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
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 ());
225
216
System .out .print ("Waiting for deletion to happen..." );
226
- while (changeRequest .status ().equals (ChangeRequest .Status .PENDING )) {
227
- System .out .print ("." );
228
- try {
229
- Thread .sleep (500 );
230
- } catch (InterruptedException e ) {
231
- System .err .println ("Thread was interrupted while waiting." );
232
- }
233
- changeRequest = dns .getChangeRequest (zoneName , changeRequest .id ());
234
- }
217
+ waitForChangeToFinish (dns , zoneName , changeRequest );
235
218
System .out .printf ("%nThe deletion has been completed.%n" );
236
219
}
237
220
@@ -244,7 +227,7 @@ public String params() {
244
227
public boolean check (String ... args ) {
245
228
if (args .length == 4 ) {
246
229
// to check that it can be parsed
247
- Integer .valueOf (args [3 ]);
230
+ Integer .parseInt (args [3 ]);
248
231
return true ;
249
232
} else {
250
233
return args .length == 3 ;
@@ -265,28 +248,18 @@ public void run(Dns dns, String... args) {
265
248
String ip = args [2 ];
266
249
int ttl = 0 ;
267
250
if (args .length > 3 ) {
268
- ttl = Integer .valueOf (args [3 ]);
251
+ ttl = Integer .parseInt (args [3 ]);
269
252
}
270
253
DnsRecord record = DnsRecord .builder (recordName , DnsRecord .Type .A )
271
254
.records (ImmutableList .of (ip ))
272
255
.ttl (ttl , TimeUnit .SECONDS )
273
256
.build ();
274
- ChangeRequest changeRequest = ChangeRequest .builder ()
275
- .add (record )
276
- .build ();
257
+ ChangeRequest changeRequest = ChangeRequest .builder ().add (record ).build ();
277
258
changeRequest = dns .applyChangeRequest (zoneName , changeRequest );
278
- System .out .printf ("The request for adding A record %s for zone %s was successfully " +
279
- "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
280
- System .out .print ("Waiting for deletion to happen..." );
281
- while (changeRequest .status ().equals (ChangeRequest .Status .PENDING )) {
282
- System .out .print ("." );
283
- try {
284
- Thread .sleep (500 );
285
- } catch (InterruptedException e ) {
286
- System .err .println ("Thread was interrupted while waiting." );
287
- }
288
- changeRequest = dns .getChangeRequest (zoneName , changeRequest .id ());
289
- }
259
+ System .out .printf ("The request for adding A record %s for zone %s was successfully "
260
+ + "submitted and assigned ID %s.%n" , recordName , zoneName , changeRequest .id ());
261
+ System .out .print ("Waiting for addition to happen..." );
262
+ waitForChangeToFinish (dns , zoneName , changeRequest );
290
263
System .out .printf ("The addition has been completed.%n" );
291
264
}
292
265
@@ -299,7 +272,7 @@ public String params() {
299
272
public boolean check (String ... args ) {
300
273
if (args .length == 4 ) {
301
274
// to check that it can be parsed
302
- Integer .valueOf (args [3 ]);
275
+ Integer .parseInt (args [3 ]);
303
276
return true ;
304
277
} else {
305
278
return args .length == 3 ;
@@ -342,8 +315,8 @@ public boolean check(String... args) {
342
315
private static class ListChangesAction implements DnsAction {
343
316
344
317
/**
345
- * Lists all the changes for a given zone. Optionally, an order, "descending" or "ascending" can
346
- * be specified using the last parameter.
318
+ * Lists all the changes for a given zone. Optionally, an order ( "descending" or "ascending")
319
+ * can be specified using the last parameter.
347
320
*/
348
321
@ Override
349
322
public void run (Dns dns , String ... args ) {
@@ -358,12 +331,11 @@ public void run(Dns dns, String... args) {
358
331
}
359
332
if (iterator .hasNext ()) {
360
333
System .out .printf ("Change requests for zone %s:%n" , zoneName );
361
- DateFormat formatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss" );
362
334
while (iterator .hasNext ()) {
363
335
ChangeRequest change = iterator .next ();
364
336
System .out .printf ("%nID: %s%n" , change .id ());
365
337
System .out .printf ("Status: %s%n" , change .status ());
366
- System .out .printf ("Started: %s%n" , formatter .format (change .startTimeMillis ()));
338
+ System .out .printf ("Started: %s%n" , FORMATTER .format (change .startTimeMillis ()));
367
339
System .out .printf ("Deletions: %s%n" , Joiner .on (", " ).join (change .deletions ()));
368
340
System .out .printf ("Additions: %s%n" , Joiner .on (", " ).join (change .additions ()));
369
341
}
@@ -408,7 +380,7 @@ public void run(Dns dns, String... args) {
408
380
409
381
@ Override
410
382
public boolean check (String ... args ) {
411
- if (args .length == 0 ) {
383
+ if (args .length == 0 || args . length == 1 ) {
412
384
return true ;
413
385
}
414
386
if ("records" .equals (args [1 ])) {
@@ -464,6 +436,29 @@ public boolean check(String... args) {
464
436
ACTIONS .put ("quota" , new GetProjectAction ());
465
437
}
466
438
439
+ private static void printZone (Zone zone ) {
440
+ System .out .printf ("%nName: %s%n" , zone .name ());
441
+ System .out .printf ("ID: %s%n" , zone .id ());
442
+ System .out .printf ("Description: %s%n" , zone .description ());
443
+ System .out .printf ("Created: %s%n" , FORMATTER .format (new Date (zone .creationTimeMillis ())));
444
+ System .out .printf ("Name servers: %s%n" , Joiner .on (", " ).join (zone .nameServers ()));
445
+ }
446
+
447
+ private static ChangeRequest waitForChangeToFinish (Dns dns , String zoneName ,
448
+ ChangeRequest request ) {
449
+ ChangeRequest current = request ;
450
+ while (current .status ().equals (ChangeRequest .Status .PENDING )) {
451
+ System .out .print ("." );
452
+ try {
453
+ Thread .sleep (500 );
454
+ } catch (InterruptedException e ) {
455
+ System .err .println ("Thread was interrupted while waiting." );
456
+ }
457
+ current = dns .getChangeRequest (zoneName , current .id ());
458
+ }
459
+ return current ;
460
+ }
461
+
467
462
private static void printUsage () {
468
463
StringBuilder actionAndParams = new StringBuilder ();
469
464
for (Map .Entry <String , DnsAction > entry : ACTIONS .entrySet ()) {
@@ -510,12 +505,13 @@ public static void main(String... args) throws Exception {
510
505
return ;
511
506
} catch (Exception ex ) {
512
507
System .out .println ("Failed to parse request." );
508
+ System .out .println ("Expected: " + action .params ());
513
509
ex .printStackTrace ();
514
510
return ;
515
511
}
516
512
if (valid ) {
517
513
DnsOptions .Builder optionsBuilder = DnsOptions .builder ();
518
- if (projectId != null ) {
514
+ if (projectId != null ) {
519
515
optionsBuilder .projectId (projectId );
520
516
}
521
517
Dns dns = optionsBuilder .build ().service ();
0 commit comments