How to recreate a DNS zone with new records?

I need to be able to replace all of the records in a DNS zone, but I don't want to delete the entire zone and start over. Is there an easy way to do this?

Answers

Answer

This script uses the SoftLayer PHP Soap Client. First the script grabs the current records in the zone, deletes all of the records which are not a "ns" or "soa" record, then submits the new records for creation.

require_once('SoftLayer/SoapClient.class.php');
 
function updateDns() {
    $apiUsername = 'SET ME';
    $apiKey = 'SET ME';
    $domainId = 'SET ME';
    $dnsClient = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain', $domainId, $apiUsername, $apiKey);
    $recordClient = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain_ResourceRecord', null , $apiUsername, $apiKey);
 
    $existingRecords = $dnsClient->getResourceRecords();
 
    // Loop through each record, store everything but ns and soa
    foreach ($existingRecords as $record) {
        // skip ns and soa records
        if ($record->type == 'ns' || $record->type == 'soa') {
            continue;
        }
        $recordsForDeletion[] = $record;
    }
 
    // Delete all records except for soa and ns
    $recordClient->deleteObjects($recordsForDeletion);
 
    // Here you will need to create an array of
    // ResourceRecord template stdObjects 
    // In this example we just have $specificRecord
    $newRecords = array();
    $specificRecord =  array (
        'id' => NULL,
        'data' => '127.0.2.1',
        'domainId' => $domainId,
        'host' => 'test5',
        'ttl' => 900,
        'type' => 'a'
    );
    $newRecords[] = (object)$specificRecord;
    $recordClient->createObjects($newRecords);
}
 
try {
    updateDns();
} catch ( Exception $e) {
    die($e->getMessage() . "\n");
}