Search in sources :

Example 1 with ChangeRequest

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

the class ITDnsTest method testCreateChange.

@Test
public void testCreateChange() {
    try {
        DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME));
        ChangeRequest created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1);
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), created.getAdditions());
        assertNotNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE).contains(created.status()));
        assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.getName(), "1"));
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        // with options
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ID));
        assertTrue(created.getAdditions().isEmpty());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
        assertTrue(created.getAdditions().isEmpty());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNotNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME));
        assertTrue(created.getAdditions().isEmpty());
        assertNotNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS));
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), created.getAdditions());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        // finishes with delete otherwise we cannot delete the zone
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        waitForChangeToComplete(created);
        assertEquals(CHANGE_DELETE_ZONE1.getDeletions(), created.getDeletions());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getAdditions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
    } finally {
        clear();
    }
}
Also used : ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 2 with ChangeRequest

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

the class ITDnsTest method waitForChangeToComplete.

private static void waitForChangeToComplete(String zoneName, String changeId) {
    ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId, Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
    waitForChangeToComplete(changeRequest);
}
Also used : ChangeRequest(com.google.cloud.dns.ChangeRequest)

Example 3 with ChangeRequest

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

the class ITDnsTest method testGetChangeBatch.

@Test
public void testGetChangeBatch() {
    try {
        Zone zone = DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME));
        ChangeRequest created = zone.applyChangeRequest(CHANGE_ADD_ZONE1);
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
        DnsBatch batch = DNS.batch();
        DnsBatchResult<ChangeRequest> completeResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId());
        DnsBatchResult<ChangeRequest> idResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.ID));
        DnsBatchResult<ChangeRequest> statusResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
        DnsBatchResult<ChangeRequest> timeResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME));
        DnsBatchResult<ChangeRequest> additionsResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS));
        batch.submit();
        assertEqChangesIgnoreStatus(created, completeResult.get());
        // with options
        ChangeRequest retrieved = idResult.get();
        assertEquals(created.getGeneratedId(), retrieved.getGeneratedId());
        assertEquals(0, retrieved.getAdditions().size());
        assertEquals(0, retrieved.getDeletions().size());
        assertNull(retrieved.getStartTimeMillis());
        assertNull(retrieved.status());
        retrieved = statusResult.get();
        assertEquals(created.getGeneratedId(), retrieved.getGeneratedId());
        assertEquals(0, retrieved.getAdditions().size());
        assertEquals(0, retrieved.getDeletions().size());
        assertNull(retrieved.getStartTimeMillis());
        assertEquals(ChangeRequestInfo.Status.DONE, retrieved.status());
        retrieved = timeResult.get();
        assertEquals(created.getGeneratedId(), retrieved.getGeneratedId());
        assertEquals(0, retrieved.getAdditions().size());
        assertEquals(0, retrieved.getDeletions().size());
        assertEquals(created.getStartTimeMillis(), retrieved.getStartTimeMillis());
        assertNull(retrieved.status());
        retrieved = additionsResult.get();
        assertEquals(created.getGeneratedId(), retrieved.getGeneratedId());
        assertEquals(2, retrieved.getAdditions().size());
        assertTrue(retrieved.getAdditions().contains(A_RECORD_ZONE1));
        assertTrue(retrieved.getAdditions().contains(AAAA_RECORD_ZONE1));
        assertEquals(0, retrieved.getDeletions().size());
        assertNull(retrieved.getStartTimeMillis());
        assertNull(retrieved.status());
        // finishes with delete otherwise we cannot delete the zone
        created = zone.applyChangeRequest(CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        batch = DNS.batch();
        DnsBatchResult<ChangeRequest> deletionsResult = batch.getChangeRequest(zone.getName(), created.getGeneratedId(), Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        batch.submit();
        retrieved = deletionsResult.get();
        assertEquals(created.getGeneratedId(), retrieved.getGeneratedId());
        assertEquals(0, retrieved.getAdditions().size());
        assertEquals(2, retrieved.getDeletions().size());
        assertTrue(retrieved.getDeletions().contains(AAAA_RECORD_ZONE1));
        assertTrue(retrieved.getDeletions().contains(A_RECORD_ZONE1));
        assertNull(retrieved.getStartTimeMillis());
        assertNull(retrieved.status());
        waitForChangeToComplete(zone.getName(), created.getGeneratedId());
    } finally {
        clear();
    }
}
Also used : Zone(com.google.cloud.dns.Zone) DnsBatch(com.google.cloud.dns.DnsBatch) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 4 with ChangeRequest

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

the class ITDnsTest method testCreateChangeBatch.

@Test
public void testCreateChangeBatch() {
    try {
        DNS.create(ZONE1, Dns.ZoneOption.fields(ZoneField.NAME));
        DnsBatch batch = DNS.batch();
        DnsBatchResult<ChangeRequest> result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1);
        batch.submit();
        ChangeRequest created = result.get();
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), created.getAdditions());
        assertNotNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE).contains(created.status()));
        assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.getName(), "1"));
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        // with options
        batch = DNS.batch();
        result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ID));
        batch.submit();
        created = result.get();
        assertTrue(created.getAdditions().isEmpty());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        batch = DNS.batch();
        result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.STATUS));
        batch.submit();
        created = result.get();
        assertTrue(created.getAdditions().isEmpty());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNotNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        batch = DNS.batch();
        result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.START_TIME));
        batch.submit();
        created = result.get();
        assertTrue(created.getAdditions().isEmpty());
        assertNotNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
        created = DNS.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1);
        waitForChangeToComplete(created);
        batch = DNS.batch();
        result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_ADD_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.ADDITIONS));
        batch.submit();
        created = result.get();
        assertEquals(CHANGE_ADD_ZONE1.getAdditions(), created.getAdditions());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getDeletions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        // finishes with delete otherwise we cannot delete the zone
        waitForChangeToComplete(created);
        batch = DNS.batch();
        result = batch.applyChangeRequest(ZONE1.getName(), CHANGE_DELETE_ZONE1, Dns.ChangeRequestOption.fields(ChangeRequestField.DELETIONS));
        batch.submit();
        created = result.get();
        waitForChangeToComplete(created);
        assertEquals(CHANGE_DELETE_ZONE1.getDeletions(), created.getDeletions());
        assertNull(created.getStartTimeMillis());
        assertTrue(created.getAdditions().isEmpty());
        assertNotNull(created.getGeneratedId());
        assertNull(created.status());
        waitForChangeToComplete(created);
    } finally {
        clear();
    }
}
Also used : DnsBatch(com.google.cloud.dns.DnsBatch) ChangeRequest(com.google.cloud.dns.ChangeRequest) Test(org.junit.Test)

Example 5 with ChangeRequest

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

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