Search in sources :

Example 1 with ZoneInfo

use of com.google.cloud.dns.ZoneInfo 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());
}
Also used : Zone(com.google.cloud.dns.Zone) Dns(com.google.cloud.dns.Dns) ZoneInfo(com.google.cloud.dns.ZoneInfo)

Example 2 with ZoneInfo

use of com.google.cloud.dns.ZoneInfo 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.");
    }
}
Also used : Zone(com.google.cloud.dns.Zone) Dns(com.google.cloud.dns.Dns) ChangeRequestInfo(com.google.cloud.dns.ChangeRequestInfo) ChangeRequest(com.google.cloud.dns.ChangeRequest) RecordSet(com.google.cloud.dns.RecordSet) ZoneInfo(com.google.cloud.dns.ZoneInfo)

Aggregations

Dns (com.google.cloud.dns.Dns)2 Zone (com.google.cloud.dns.Zone)2 ZoneInfo (com.google.cloud.dns.ZoneInfo)2 ChangeRequest (com.google.cloud.dns.ChangeRequest)1 ChangeRequestInfo (com.google.cloud.dns.ChangeRequestInfo)1 RecordSet (com.google.cloud.dns.RecordSet)1