Search in sources :

Example 1 with RecordSet

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

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

use of com.google.cloud.dns.RecordSet 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 4 with RecordSet

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

the class ITDnsTest method testListDnsRecordSetsBatch.

@Test
public void testListDnsRecordSetsBatch() {
    try {
        Zone zone = DNS.create(ZONE1);
        DnsBatch batch = DNS.batch();
        DnsBatchResult<Page<RecordSet>> result = batch.listRecordSets(zone.getName());
        batch.submit();
        ImmutableList<RecordSet> recordSets = ImmutableList.copyOf(result.get().iterateAll());
        assertEquals(2, recordSets.size());
        ImmutableList<RecordSet.Type> defaultRecords = ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA);
        for (RecordSet recordSet : recordSets) {
            assertTrue(defaultRecords.contains(recordSet.getType()));
        }
        // field options
        batch = DNS.batch();
        DnsBatchResult<Page<RecordSet>> ttlResult = batch.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.TTL));
        DnsBatchResult<Page<RecordSet>> nameResult = batch.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.NAME));
        DnsBatchResult<Page<RecordSet>> recordsResult = batch.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.DNS_RECORDS));
        DnsBatchResult<Page<RecordSet>> pageSizeResult = batch.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.TYPE), Dns.RecordSetListOption.pageSize(1));
        batch.submit();
        Iterator<RecordSet> recordSetIterator = ttlResult.get().iterateAll().iterator();
        int counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(recordSets.get(counter).getTtl(), recordSet.getTtl());
            assertEquals(recordSets.get(counter).getName(), recordSet.getName());
            assertEquals(recordSets.get(counter).getType(), recordSet.getType());
            assertTrue(recordSet.getRecords().isEmpty());
            counter++;
        }
        assertEquals(2, counter);
        recordSetIterator = nameResult.get().iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(recordSets.get(counter).getName(), recordSet.getName());
            assertEquals(recordSets.get(counter).getType(), recordSet.getType());
            assertTrue(recordSet.getRecords().isEmpty());
            assertNull(recordSet.getTtl());
            counter++;
        }
        assertEquals(2, counter);
        recordSetIterator = recordsResult.get().iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(recordSets.get(counter).getRecords(), recordSet.getRecords());
            assertEquals(recordSets.get(counter).getName(), recordSet.getName());
            assertEquals(recordSets.get(counter).getType(), recordSet.getType());
            assertNull(recordSet.getTtl());
            counter++;
        }
        assertEquals(2, counter);
        // also test paging
        recordSetIterator = pageSizeResult.get().iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(recordSets.get(counter).getType(), recordSet.getType());
            assertEquals(recordSets.get(counter).getName(), recordSet.getName());
            assertTrue(recordSet.getRecords().isEmpty());
            assertNull(recordSet.getTtl());
            counter++;
        }
        assertEquals(2, counter);
        // test page size
        Page<RecordSet> recordSetPage = pageSizeResult.get();
        assertEquals(1, ImmutableList.copyOf(recordSetPage.getValues().iterator()).size());
        // test name filter
        ChangeRequest change = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1);
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        batch = DNS.batch();
        result = batch.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.getName()));
        batch.submit();
        recordSetIterator = result.get().iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertTrue(ImmutableList.of(A_RECORD_ZONE1.getType(), AAAA_RECORD_ZONE1.getType()).contains(recordSet.getType()));
            counter++;
        }
        assertEquals(2, counter);
        // test type filter
        batch = DNS.batch();
        result = batch.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.getName()), Dns.RecordSetListOption.type(A_RECORD_ZONE1.getType()));
        batch.submit();
        recordSetIterator = result.get().iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(A_RECORD_ZONE1, recordSet);
            counter++;
        }
        assertEquals(1, counter);
        batch = DNS.batch();
        DnsBatchResult<Page<RecordSet>> noNameError = batch.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.type(A_RECORD_ZONE1.getType()));
        DnsBatchResult<Page<RecordSet>> zeroSizeError = batch.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.pageSize(0));
        DnsBatchResult<Page<RecordSet>> negativeSizeError = batch.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.pageSize(-1));
        batch.submit();
        // check wrong arguments
        try {
            // name is not set
            noNameError.get();
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            zeroSizeError.get();
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            negativeSizeError.get();
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
    } finally {
        clear();
    }
}
Also used : Zone(com.google.cloud.dns.Zone) Page(com.google.api.gax.paging.Page) ChangeRequest(com.google.cloud.dns.ChangeRequest) DnsException(com.google.cloud.dns.DnsException) DnsBatch(com.google.cloud.dns.DnsBatch) RecordSet(com.google.cloud.dns.RecordSet) Test(org.junit.Test)

Example 5 with RecordSet

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

the class ITDnsTest method clear.

private static void clear() {
    for (String zoneName : ZONE_NAMES) {
        Zone zone = DNS.getZone(zoneName);
        if (zone != null) {
            /* We wait for all changes to complete before retrieving a list of DNS records to be
        deleted. Waiting is necessary as changes potentially might create more records between
        when the list has been retrieved and executing the subsequent delete operation. */
            Iterator<ChangeRequest> iterator = zone.listChangeRequests().iterateAll().iterator();
            while (iterator.hasNext()) {
                waitForChangeToComplete(zoneName, iterator.next().getGeneratedId());
            }
            Iterator<RecordSet> recordSetIterator = zone.listRecordSets().iterateAll().iterator();
            List<RecordSet> toDelete = new LinkedList<>();
            while (recordSetIterator.hasNext()) {
                RecordSet recordSet = recordSetIterator.next();
                if (!ImmutableList.of(RecordSet.Type.NS, RecordSet.Type.SOA).contains(recordSet.getType())) {
                    toDelete.add(recordSet);
                }
            }
            if (!toDelete.isEmpty()) {
                ChangeRequest deletion = zone.applyChangeRequest(ChangeRequest.newBuilder().setDeletions(toDelete).build());
                waitForChangeToComplete(zone.getName(), deletion.getGeneratedId());
            }
            zone.delete();
        }
    }
}
Also used : Zone(com.google.cloud.dns.Zone) RecordSet(com.google.cloud.dns.RecordSet) ChangeRequest(com.google.cloud.dns.ChangeRequest) LinkedList(java.util.LinkedList)

Aggregations

RecordSet (com.google.cloud.dns.RecordSet)7 ChangeRequest (com.google.cloud.dns.ChangeRequest)6 Zone (com.google.cloud.dns.Zone)6 ChangeRequestInfo (com.google.cloud.dns.ChangeRequestInfo)4 Dns (com.google.cloud.dns.Dns)3 DnsException (com.google.cloud.dns.DnsException)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 Page (com.google.api.gax.paging.Page)1 DnsBatch (com.google.cloud.dns.DnsBatch)1 ZoneInfo (com.google.cloud.dns.ZoneInfo)1