Search in sources :

Example 6 with ChangeRequest

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

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

the class ITDnsTest method testListChanges.

@Test
public void testListChanges() {
    try {
        // no such zone exists
        try {
            DNS.listChangeRequests(ZONE1.getName());
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(404, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        // zone exists but has no changes
        DNS.create(ZONE1);
        ImmutableList<ChangeRequest> changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName()).iterateAll());
        // default change creating SOA and NS
        assertEquals(1, changes.size());
        // zone has changes
        ChangeRequest change = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1);
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        change = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        change = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1);
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        change = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName()).iterateAll());
        assertEquals(5, changes.size());
        // error in options
        try {
            DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.pageSize(0));
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.pageSize(-1));
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        // sorting order
        ImmutableList<ChangeRequest> ascending = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING)).iterateAll());
        ImmutableList<ChangeRequest> descending = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING)).iterateAll());
        int size = 5;
        assertEquals(size, descending.size());
        assertEquals(size, ascending.size());
        for (int i = 0; i < size; i++) {
            assertEquals(descending.get(i), ascending.get(size - i - 1));
        }
        // field options
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.ADDITIONS)).iterateAll());
        change = changes.get(1);
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), change.getAdditions());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.DELETIONS)).iterateAll());
        change = changes.get(2);
        assertTrue(change.getAdditions().isEmpty());
        assertNotNull(change.getDeletions());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.ID)).iterateAll());
        change = changes.get(1);
        assertTrue(change.getAdditions().isEmpty());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.START_TIME)).iterateAll());
        change = changes.get(1);
        assertTrue(change.getAdditions().isEmpty());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNotNull(change.getStartTimeMillis());
        assertNull(change.status());
        changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.STATUS)).iterateAll());
        change = changes.get(1);
        assertTrue(change.getAdditions().isEmpty());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertEquals(ChangeRequest.Status.DONE, change.status());
    } finally {
        clear();
    }
}
Also used : DnsException(com.google.cloud.dns.DnsException) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 8 with ChangeRequest

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

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

Example 10 with ChangeRequest

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

the class ITDnsTest method testBatchCombined.

@Test
public void testBatchCombined() {
    // the results are validated in the other test methods
    try {
        DNS.create(ZONE1);
        DnsBatch batch = DNS.batch();
        DnsBatchResult<Zone> zoneResult = batch.getZone(ZONE_NAME1);
        DnsBatchResult<ChangeRequest> changeRequestResult = batch.getChangeRequest(ZONE_NAME1, "0");
        DnsBatchResult<Page<RecordSet>> pageResult = batch.listRecordSets(ZONE_NAME1);
        DnsBatchResult<ProjectInfo> projectResult = batch.getProject();
        assertFalse(zoneResult.completed());
        try {
            zoneResult.get();
            fail("this should be submitted first");
        } catch (IllegalStateException ex) {
        // expected
        }
        batch.submit();
        assertNotNull(zoneResult.get().getCreationTimeMillis());
        assertEquals(ZONE1.getDnsName(), zoneResult.get().getDnsName());
        assertEquals(ZONE1.getDescription(), zoneResult.get().getDescription());
        assertFalse(zoneResult.get().getNameServers().isEmpty());
        // we did not set it
        assertNull(zoneResult.get().getNameServerSet());
        assertNotNull(zoneResult.get().getGeneratedId());
        assertNotNull(projectResult.get().getQuota());
        assertEquals(2, Iterables.size(pageResult.get().getValues()));
        assertNotNull(changeRequestResult.get());
    } finally {
        DNS.delete(ZONE1.getName());
    }
}
Also used : Zone(com.google.cloud.dns.Zone) ProjectInfo(com.google.cloud.dns.ProjectInfo) Page(com.google.api.gax.paging.Page) DnsBatch(com.google.cloud.dns.DnsBatch) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Aggregations

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