use of com.google.cloud.dns.Dns in project google-cloud-java by GoogleCloudPlatform.
the class CreateZone method main.
public static void main(String... args) {
// Create a service object
// The project ID and credentials will be inferred from the environment.
Dns dns = DnsOptions.getDefaultInstance().getService();
// Create a zone metadata object
// Change this zone name which is unique within your project
String zoneName = "my-unique-zone";
// Change this to a domain which you own
String domainName = "someexampledomain.com.";
String description = "This is a google-cloud-dns sample zone.";
ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description);
// Create zone in Google Cloud DNS
Zone zone = dns.create(zoneInfo);
System.out.printf("Zone was created and assigned ID %s.%n", zone.getGeneratedId());
}
use of com.google.cloud.dns.Dns in project google-cloud-java by GoogleCloudPlatform.
the class DeleteZone method main.
public static void main(String... args) {
// Create a service object.
// The project ID and credentials will be inferred from the environment.
Dns dns = DnsOptions.getDefaultInstance().getService();
// Change this to a zone name that exists within your project and that you want to delete.
String zoneName = "my-unique-zone";
// Get iterable for the existing record sets which have to be deleted before deleting the zone
Iterable<RecordSet> recordIterable = dns.listRecordSets(zoneName).iterateAll();
// Make a change for deleting the records
ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder();
for (RecordSet current : recordIterable) {
// SOA and NS records cannot be deleted
if (!RecordSet.Type.SOA.equals(current.getType()) && !RecordSet.Type.NS.equals(current.getType())) {
changeBuilder.delete(current);
}
}
// Build and apply the change request to our zone if it contains records to delete
ChangeRequestInfo changeRequest = changeBuilder.build();
if (!changeRequest.getDeletions().isEmpty()) {
ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest);
// Wait for the change request to complete
while (!pendingRequest.isDone()) {
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.");
}
}
}
// Delete the zone
boolean result = dns.delete(zoneName);
if (result) {
System.out.println("Zone was deleted.");
} else {
System.out.println("Zone was not deleted because it does not exist.");
}
}
use of com.google.cloud.dns.Dns in project google-cloud-java by GoogleCloudPlatform.
the class ManipulateZonesAndRecordSets method main.
public static void main(String... args) {
Dns dns = DnsOptions.getDefaultInstance().getService();
// Create a zone metadata object
// Change this zone name which is unique within your project
String zoneName = "my-unique-zone";
// Change this to a domain which you own
String domainName = "someexampledomain.com.";
String description = "This is a google-cloud-dns sample zone.";
ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description);
// Create zone in Google Cloud DNS
Zone zone = dns.create(zoneInfo);
System.out.printf("Zone was created and assigned ID %s.%n", zone.getGeneratedId());
// Print assigned name servers
List<String> nameServers = zone.getNameServers();
for (String nameServer : nameServers) {
System.out.println(nameServer);
}
// Prepare a www.someexampledomain.com. type A record with ttl of 24 hours
String ip = "12.13.14.15";
RecordSet toCreate = RecordSet.newBuilder("www.someexampledomain.com.", RecordSet.Type.A).setTtl(24, TimeUnit.HOURS).addRecord(ip).build();
// Make a change
ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder().add(toCreate);
// Verify the type A record does not exist yet.
// If it does exist, we will overwrite it with our prepared record.
Page<RecordSet> recordSetPage = zone.listRecordSets();
for (RecordSet current : recordSetPage.iterateAll()) {
if (toCreate.getName().equals(current.getName()) && toCreate.getType().equals(current.getType())) {
changeBuilder.delete(current);
}
}
// Build and apply the change request to our zone
ChangeRequestInfo changeRequest = changeBuilder.build();
zone.applyChangeRequest(changeRequest);
while (ChangeRequestInfo.Status.PENDING.equals(changeRequest.status())) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
System.err.println("The thread was interrupted while waiting...");
}
changeRequest = dns.getChangeRequest(zone.getName(), changeRequest.getGeneratedId());
}
System.out.println("The change request has been applied.");
// List all your zones
int counter = 1;
for (Zone currentZone : dns.listZones().iterateAll()) {
System.out.printf("#%d.: %s%n%n", counter, currentZone);
counter++;
}
// List the record sets in a particular zone
System.out.println(String.format("Record sets inside %s:", zone.getName()));
for (RecordSet recordSet : recordSetPage.iterateAll()) {
System.out.println(recordSet);
}
// List the change requests applied to a particular zone
System.out.println(String.format("The history of changes in %s:", zone.getName()));
for (ChangeRequest currentChangeRequest : zone.listChangeRequests().iterateAll()) {
System.out.println(currentChangeRequest);
}
// Make a change for deleting the record sets
changeBuilder = ChangeRequestInfo.newBuilder();
for (RecordSet current : recordSetPage.iterateAll()) {
// SOA and NS records cannot be deleted
if (!RecordSet.Type.SOA.equals(current.getType()) && !RecordSet.Type.NS.equals(current.getType())) {
changeBuilder.delete(current);
}
}
// Build and apply the change request to our zone if it contains records to delete
changeRequest = changeBuilder.build();
if (!changeRequest.getDeletions().isEmpty()) {
ChangeRequest pendingRequest = dns.applyChangeRequest(zoneName, changeRequest);
// Wait for the change request to complete
while (!pendingRequest.isDone()) {
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.");
}
}
}
// Delete the zone
boolean result = dns.delete(zoneName);
if (result) {
System.out.println("Zone was deleted.");
} else {
System.out.println("Zone was not deleted because it does not exist.");
}
}
use of com.google.cloud.dns.Dns in project google-cloud-java by GoogleCloudPlatform.
the class DnsExample method main.
public static void main(String... args) throws Exception {
if (args.length < 1) {
System.out.println("Missing required action");
printUsage();
return;
}
String projectId = null;
DnsAction action;
String actionName;
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
actionName = args[1];
projectId = args[0];
args = Arrays.copyOfRange(args, 2, args.length);
} else {
actionName = args[0];
args = Arrays.copyOfRange(args, 1, args.length);
}
action = ACTIONS.get(actionName);
if (action == null) {
System.out.printf("Unrecognized action %s.%n", actionName);
printUsage();
return;
}
boolean valid = false;
try {
valid = action.check(args);
} catch (NumberFormatException ex) {
System.out.printf("Invalid input for action '%s'.%n", actionName);
System.out.println("Ttl must be an integer.");
System.out.printf("Expected: %s%n", action.params());
return;
} catch (Exception ex) {
System.out.println("Failed to parse request.");
System.out.printf("Expected: %s%n", action.params());
ex.printStackTrace();
return;
}
if (valid) {
DnsOptions.Builder optionsBuilder = DnsOptions.newBuilder();
if (projectId != null) {
optionsBuilder.setProjectId(projectId);
}
Dns dns = optionsBuilder.build().getService();
action.run(dns, args);
} else {
System.out.printf("Invalid input for action '%s'%n", actionName);
System.out.printf("Expected: %s%n", action.params());
}
}
use of com.google.cloud.dns.Dns in project google-cloud-java by GoogleCloudPlatform.
the class CreateOrUpdateRecordSets method main.
public static void main(String... args) {
// Create a service object.
// The project ID and credentials will be inferred from the environment.
Dns dns = DnsOptions.getDefaultInstance().getService();
// Change this to a zone name that exists within your project
String zoneName = "my-unique-zone";
// Get zone from the service
Zone zone = dns.getZone(zoneName);
// Prepare a <i>www.<zone-domain>.</i> type A record set with ttl of 24 hours
String ip = "12.13.14.15";
RecordSet toCreate = RecordSet.newBuilder("www." + zone.getDnsName(), RecordSet.Type.A).setTtl(24, TimeUnit.HOURS).addRecord(ip).build();
// Make a change
ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder().add(toCreate);
// If it does exist, we will overwrite it with our prepared record.
for (RecordSet current : zone.listRecordSets().iterateAll()) {
if (toCreate.getName().equals(current.getName()) && toCreate.getType().equals(current.getType())) {
changeBuilder.delete(current);
}
}
// Build and apply the change request to our zone
ChangeRequestInfo changeRequest = changeBuilder.build();
zone.applyChangeRequest(changeRequest);
}
Aggregations