Search in sources :

Example 11 with ChangeRequest

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

the class ITDnsTest method testListChangesBatch.

@Test
public void testListChangesBatch() {
    try {
        DnsBatch batch = DNS.batch();
        DnsBatchResult<Page<ChangeRequest>> result = batch.listChangeRequests(ZONE1.getName());
        batch.submit();
        try {
            result.get();
            fail("Zone does not exist yet");
        } catch (DnsException ex) {
            // expected
            assertEquals(404, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        // zone exists but has no changes
        DNS.create(ZONE1);
        batch = DNS.batch();
        result = batch.listChangeRequests(ZONE1.getName());
        batch.submit();
        // default change creating SOA and NS
        assertEquals(1, Iterables.size(result.get().getValues()));
        // 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());
        batch = DNS.batch();
        result = batch.listChangeRequests(ZONE1.getName());
        DnsBatchResult<Page<ChangeRequest>> errorPageSize = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.pageSize(0));
        DnsBatchResult<Page<ChangeRequest>> errorPageNegative = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.pageSize(-1));
        DnsBatchResult<Page<ChangeRequest>> resultAscending = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING));
        DnsBatchResult<Page<ChangeRequest>> resultDescending = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.DESCENDING));
        DnsBatchResult<Page<ChangeRequest>> resultAdditions = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.ADDITIONS));
        DnsBatchResult<Page<ChangeRequest>> resultDeletions = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.DELETIONS));
        DnsBatchResult<Page<ChangeRequest>> resultId = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.ID));
        DnsBatchResult<Page<ChangeRequest>> resultTime = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.START_TIME));
        DnsBatchResult<Page<ChangeRequest>> resultStatus = batch.listChangeRequests(ZONE1.getName(), Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING), Dns.ChangeRequestListOption.fields(ChangeRequestField.STATUS));
        batch.submit();
        assertEquals(3, Iterables.size(result.get().getValues()));
        // error in options
        try {
            errorPageSize.get();
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            errorPageNegative.get();
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        // sorting order
        ImmutableList<ChangeRequest> ascending = ImmutableList.copyOf(resultAscending.get().iterateAll());
        ImmutableList<ChangeRequest> descending = ImmutableList.copyOf(resultDescending.get().iterateAll());
        int size = 3;
        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
        change = Iterables.get(resultAdditions.get().getValues(), 1);
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), change.getAdditions());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        change = Iterables.get(resultDeletions.get().getValues(), 2);
        assertTrue(change.getAdditions().isEmpty());
        assertNotNull(change.getDeletions());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        change = Iterables.get(resultId.get().getValues(), 1);
        assertTrue(change.getAdditions().isEmpty());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNull(change.getStartTimeMillis());
        assertNull(change.status());
        change = Iterables.get(resultTime.get().getValues(), 1);
        assertTrue(change.getAdditions().isEmpty());
        assertTrue(change.getDeletions().isEmpty());
        assertNotNull(change.getGeneratedId());
        assertNotNull(change.getStartTimeMillis());
        assertNull(change.status());
        change = Iterables.get(resultStatus.get().getValues(), 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) Page(com.google.api.gax.paging.Page) DnsBatch(com.google.cloud.dns.DnsBatch) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 12 with ChangeRequest

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

Example 13 with ChangeRequest

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

the class ITDnsTest method testListDnsRecords.

@Test
public void testListDnsRecords() {
    try {
        Zone zone = DNS.create(ZONE1);
        ImmutableList<RecordSet> recordSets = ImmutableList.copyOf(DNS.listRecordSets(zone.getName()).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
        Iterator<RecordSet> recordSetIterator = DNS.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.TTL)).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 = DNS.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.NAME)).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 = DNS.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.DNS_RECORDS)).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);
        recordSetIterator = DNS.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.TYPE), Dns.RecordSetListOption.pageSize(1)).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 = DNS.listRecordSets(zone.getName(), Dns.RecordSetListOption.fields(RecordSetField.TYPE), Dns.RecordSetListOption.pageSize(1));
        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());
        recordSetIterator = DNS.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.getName())).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
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
        recordSetIterator = DNS.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.dnsName(A_RECORD_ZONE1.getName()), Dns.RecordSetListOption.type(A_RECORD_ZONE1.getType())).iterateAll().iterator();
        counter = 0;
        while (recordSetIterator.hasNext()) {
            RecordSet recordSet = recordSetIterator.next();
            assertEquals(A_RECORD_ZONE1, recordSet);
            counter++;
        }
        assertEquals(1, counter);
        change = zone.applyChangeRequest(CHANGE_DELETE_ZONE1);
        // check wrong arguments
        try {
            // name is not set
            DNS.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.type(A_RECORD_ZONE1.getType()));
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            DNS.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.pageSize(0));
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        try {
            DNS.listRecordSets(ZONE1.getName(), Dns.RecordSetListOption.pageSize(-1));
            fail();
        } catch (DnsException ex) {
            // expected
            assertEquals(400, ex.getCode());
            assertFalse(ex.isRetryable());
        }
        waitForChangeToComplete(ZONE1.getName(), change.getGeneratedId());
    } finally {
        clear();
    }
}
Also used : DnsException(com.google.cloud.dns.DnsException) Zone(com.google.cloud.dns.Zone) RecordSet(com.google.cloud.dns.RecordSet) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 14 with ChangeRequest

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

the class ITDnsTest method testGetChange.

@Test
public void testGetChange() {
    try {
        Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME));
        ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1);
        ChangeRequest retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId());
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        zone.applyChangeRequest(CHANGE_DELETE_ZONE1);
        // with options
        created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ID));
        retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.ID));
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        zone.applyChangeRequest(CHANGE_DELETE_ZONE1);
        created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
        retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        zone.applyChangeRequest(CHANGE_DELETE_ZONE1);
        created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME));
        retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME));
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        zone.applyChangeRequest(CHANGE_DELETE_ZONE1);
        created = zone.applyChangeRequest(CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS));
        retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS));
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        // finishes with delete otherwise we cannot delete the zone
        created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        retrieved = DNS.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        assertEqChangesIgnoreStatus(created, retrieved);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
    } finally {
        clear();
    }
}
Also used : Zone(com.google.cloud.dns.Zone) 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