Search in sources :

Example 1 with ChangeRequestInfo

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

Example 2 with ChangeRequestInfo

use of com.google.cloud.dns.ChangeRequestInfo 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)

Example 3 with ChangeRequestInfo

use of com.google.cloud.dns.ChangeRequestInfo in project google-cloud-java by GoogleCloudPlatform.

the class DnsExample method waitForChangeToFinish.

private static ChangeRequestInfo waitForChangeToFinish(Dns dns, String zoneName, ChangeRequestInfo request) {
    ChangeRequestInfo current = request;
    while (current.status().equals(ChangeRequest.Status.PENDING)) {
        System.out.print(".");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            System.err.println("Thread was interrupted while waiting.");
        }
        current = dns.getChangeRequest(zoneName, current.getGeneratedId());
    }
    return current;
}
Also used : ChangeRequestInfo(com.google.cloud.dns.ChangeRequestInfo)

Example 4 with ChangeRequestInfo

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

Example 5 with ChangeRequestInfo

use of com.google.cloud.dns.ChangeRequestInfo in project google-cloud-java by GoogleCloudPlatform.

the class ITDnsTest method testInvalidChangeRequest.

@Test
public void testInvalidChangeRequest() {
    Zone zone = DNS.create(ZONE1);
    RecordSet validA = RecordSet.newBuilder("subdomain." + zone.getDnsName(), RecordSet.Type.A).setRecords(ImmutableList.of("0.255.1.5")).build();
    boolean recordAdded = false;
    try {
        ChangeRequestInfo validChange = ChangeRequest.newBuilder().add(validA).build();
        zone.applyChangeRequest(validChange);
        recordAdded = true;
        try {
            zone.applyChangeRequest(validChange);
            fail("Created a record set which already exists.");
        } catch (DnsException ex) {
            // expected
            assertFalse(ex.isRetryable());
            assertEquals(409, ex.getCode());
        }
        // delete with field mismatch
        RecordSet mismatch = validA.toBuilder().setTtl(20, TimeUnit.SECONDS).build();
        ChangeRequestInfo deletion = ChangeRequest.newBuilder().delete(mismatch).build();
        try {
            zone.applyChangeRequest(deletion);
            fail("Deleted a record set without a complete match.");
        } catch (DnsException ex) {
            // expected
            assertEquals(412, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        // delete and add SOA
        Iterator<RecordSet> recordSetIterator = zone.listRecordSets().iterateAll().iterator();
        LinkedList<RecordSet> deletions = new LinkedList<>();
        LinkedList<RecordSet> additions = new LinkedList<>();
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            if (recordSet.getType() == RecordSet.Type.SOA) {
                deletions.add(recordSet);
                // the subdomain is necessary to get 400 instead of 412
                RecordSet copy = recordSet.toBuilder().setName("x." + recordSet.getName()).build();
                additions.add(copy);
                break;
            }
        }
        deletion = deletion.toBuilder().setDeletions(deletions).build();
        ChangeRequestInfo addition = ChangeRequest.newBuilder().setAdditions(additions).build();
        try {
            zone.applyChangeRequest(deletion);
            fail("Deleted SOA.");
        } catch (DnsException ex) {
            // expected
            assertFalse(ex.isRetryable());
            assertEquals(400, ex.getCode());
        }
        try {
            zone.applyChangeRequest(addition);
            fail("Added second SOA.");
        } catch (DnsException ex) {
            // expected
            assertFalse(ex.isRetryable());
            assertEquals(400, ex.getCode());
        }
    } finally {
        if (recordAdded) {
            ChangeRequestInfo deletion = ChangeRequest.newBuilder().delete(validA).build();
            ChangeRequest request = zone.applyChangeRequest(deletion);
            waitForChangeToComplete(zone.getName(), request.getGeneratedId());
        }
        zone.delete();
    }
}
Also used : DnsException(com.google.cloud.dns.DnsException) Zone(com.google.cloud.dns.Zone) RecordSet(com.google.cloud.dns.RecordSet) ChangeRequestInfo(com.google.cloud.dns.ChangeRequestInfo) ChangeRequest(com.google.cloud.dns.ChangeRequest) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

ChangeRequestInfo (com.google.cloud.dns.ChangeRequestInfo)5 RecordSet (com.google.cloud.dns.RecordSet)4 ChangeRequest (com.google.cloud.dns.ChangeRequest)3 Dns (com.google.cloud.dns.Dns)3 Zone (com.google.cloud.dns.Zone)3 DnsException (com.google.cloud.dns.DnsException)1 ZoneInfo (com.google.cloud.dns.ZoneInfo)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1