Skip to content

Commit

Permalink
Extended example doc, added links. Refactored zone print.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Mar 14, 2016
1 parent 80ffbd0 commit 0173383
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion gcloud-java-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@
* </ol>
*
* <p>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<String, DnsAction> 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);
Expand Down Expand Up @@ -111,14 +112,9 @@ public void run(Dns dns, String... args) {
Iterator<Zone> 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.");
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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<String, DnsAction> entry : ACTIONS.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ 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";

// Get zone from the service
Zone zone = dns.getZone(zoneName);

// Prepare a www.<zone-domain>. type A record with ttl of 24 hours
// Prepare a <i>www.<zone-domain>.</i> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0173383

Please sign in to comment.