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.

<?php
  ...
   $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(http://aps-standard.org/types/dns/domain/1.0),name=mydomain.com');

   $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(
        "implementing(http://parallels.com/aps/types/pa/dns/record/a/1.0),RRState=active,source=eq=empty()",
        "/aps/2/resources/".$domain->aps->id ."/records"
   );

   // fill in new record properties
   $aRecord = \APS\TypeLibrary::newResourceByTypeId('http://parallels.com/aps/types/pa/dns/record/a/1.0');
   $aRecord->source = "";
   $aRecord->recordId = 0;
   $aRecord->RRState = "active";
   $aRecord->address = "20.13.14.20";
   $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);
   }