Replacing DNS Records in Customer’s Domains

Some applications need to replace DNS records. For example, email spam filter replaces MX records to its own, in order to filter email and forward back to original MX.

Record replacement is described here.

The following example shows how to replace all ‘A’ records with a custom one. When you add old record in ‘replaces’ collection of the new record, old record is deactivated.

   $apsc = \APS\Request::getController();

   // impersonate as account to get access to account's domains
   $apsc2 = $apsc->impersonate($this);

   // search domain by name
   $domains = $apsc2->getResources('implementing(,');

   $domain = $domains[0];

   // Searching A records in 'records' collection of the domain. Record RRState is 'active' and source - empty.
   // 'source' is a prefix to domain name. Empty 'source' represent records for the domain itself.
   $records = $apsc2->getResources(
        "/aps/2/resources/".$domain->aps->id ."/records"

   // fill in new record properties
   $aRecord = \APS\TypeLibrary::newResourceByTypeId('');
   $aRecord->source = "";
   $aRecord->recordId = 0;
   $aRecord->RRState = "active";
   $aRecord->address = "";
   $aRecord->TTL = 3600;

   // create a link with the domain (zone)
   $aRecord->aps->links['zone'] = new \APS\Link($domain, 'zone', $aRecord);

   // create record
   $aRecord = $apsc2->provisionResource($aRecord);

   // replace all old 'A' records with new one
   foreach ($records as $record) {
       $apsc2->linkResource($aRecord, 'replaces', $record);