Search in sources :

Example 16 with Zone

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

the class ITDnsTest method testCreateValidZoneBatch.

@Test
public void testCreateValidZoneBatch() {
    try {
        DnsBatch batch = DNS.batch();
        DnsBatchResult<Zone> completeZoneResult = batch.createZone(ZONE1);
        DnsBatchResult<Zone> partialZoneResult = batch.createZone(ZONE_EMPTY_DESCRIPTION);
        batch.submit();
        Zone created = completeZoneResult.get();
        assertEquals(ZONE1.getDescription(), created.getDescription());
        assertEquals(ZONE1.getDnsName(), created.getDnsName());
        assertEquals(ZONE1.getName(), created.getName());
        assertNotNull(created.getCreationTimeMillis());
        assertNotNull(created.getNameServers());
        assertNull(created.getNameServerSet());
        assertNotNull(created.getGeneratedId());
        Zone retrieved = DNS.getZone(ZONE1.getName());
        assertEquals(created, retrieved);
        created = partialZoneResult.get();
        assertEquals(ZONE_EMPTY_DESCRIPTION.getDescription(), created.getDescription());
        assertEquals(ZONE_EMPTY_DESCRIPTION.getDnsName(), created.getDnsName());
        assertEquals(ZONE_EMPTY_DESCRIPTION.getName(), created.getName());
        assertNotNull(created.getCreationTimeMillis());
        assertNotNull(created.getNameServers());
        assertNull(created.getNameServerSet());
        assertNotNull(created.getGeneratedId());
        retrieved = DNS.getZone(ZONE_EMPTY_DESCRIPTION.getName());
        assertEquals(created, retrieved);
    } finally {
        DNS.delete(ZONE1.getName());
        DNS.delete(ZONE_EMPTY_DESCRIPTION.getName());
    }
}
Also used : Zone(com.google.cloud.dns.Zone) DnsBatch(com.google.cloud.dns.DnsBatch) Test(org.junit.Test)

Example 17 with Zone

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

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

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

the class ITDnsTest method testGetZone.

@Test
public void testGetZone() {
    try {
        DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME));
        Zone created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.CREATION_TIME));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNotNull(created.getCreationTimeMillis());
        assertNull(created.getDescription());
        assertNull(created.getDnsName());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.DESCRIPTION));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertEquals(ZONE1.getDescription(), created.getDescription());
        assertNull(created.getDnsName());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.DNS_NAME));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertEquals(ZONE1.getDnsName(), created.getDnsName());
        assertNull(created.getDescription());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.NAME));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertNull(created.getDnsName());
        assertNull(created.getDescription());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.NAME_SERVER_SET));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertNull(created.getDnsName());
        assertNull(created.getDescription());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        // we did not set it
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.NAME_SERVERS));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertNull(created.getDnsName());
        assertNull(created.getDescription());
        assertFalse(created.getNameServers().isEmpty());
        assertNull(created.getNameServerSet());
        assertNull(created.getGeneratedId());
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.ZONE_ID));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertNull(created.getDnsName());
        assertNull(created.getDescription());
        assertNotNull(created.getNameServers());
        // never returns null
        assertTrue(created.getNameServers().isEmpty());
        assertNotNull(created.getGeneratedId());
        // combination of multiple things
        created = DNS.getZone(ZONE1.getName(), Dns.ZoneOption.fields(ZoneField.ZONE_ID, ZoneField.NAME_SERVERS, ZoneField.NAME_SERVER_SET, ZoneField.DESCRIPTION));
        // always returned
        assertEquals(ZONE1.getName(), created.getName());
        assertNull(created.getCreationTimeMillis());
        assertNull(created.getDnsName());
        assertEquals(ZONE1.getDescription(), created.getDescription());
        assertFalse(created.getNameServers().isEmpty());
        // we did not set it
        assertNull(created.getNameServerSet());
        assertNotNull(created.getGeneratedId());
    } finally {
        DNS.delete(ZONE1.getName());
    }
}
Also used : Zone(com.google.cloud.dns.Zone) Test(org.junit.Test)

Example 20 with Zone

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

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