Skip to content

Commit

Permalink
Merge pull request #6 from amanzella/main
Browse files Browse the repository at this point in the history
Fixed PowerDNS NS Error and error handling
  • Loading branch information
getpinga authored Feb 9, 2024
2 parents ea83b34 + 6ba2215 commit c6b14ac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
37 changes: 22 additions & 15 deletions Servicedns/Providers/PowerDNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct($config) {
$api_ip = '127.0.0.1';
}

// Dynamically pull nameserver settings from the configuration
$this->nsRecords = [
'ns1' => $config['ns1'] ?? null,
'ns2' => $config['ns2'] ?? null,
Expand All @@ -33,22 +34,28 @@ public function __construct($config) {
}

public function createDomain($domainName) {
if (empty($domainName)) {
throw new \FOSSBilling\Exception("Domain name cannot be empty");
}

$nsRecords = array_filter($this->nsRecords);
$formattedNsRecords = array_map(function($nsRecord) {
return rtrim($nsRecord, '.') . '.'; // Ensure each record ends with a period
}, $nsRecords);

$response = $this->client->createZone(
$domainName,
$formattedNsRecords
);
if (empty($domainName)) {
throw new \FOSSBilling\Exception("Domain name cannot be empty");
}

return json_decode($domainName, true);
$nsRecords = array_filter($this->nsRecords);
$formattedNsRecords = array_values(array_map(function($nsRecord) {
return rtrim($nsRecord, '.') . '.';
}, $nsRecords));

try {
$this->client->createZone($domainName, $formattedNsRecords);
// On successful creation, simply return true.
return true;
} catch (\Exception $e) {
// Throw an exception to indicate failure, including for conflicts.
if (strpos($e->getMessage(), 'Conflict') !== false) {
throw new \FOSSBilling\Exception("Zone already exists for domain: " . $domainName);
} else {
throw new \FOSSBilling\Exception("Failed to create zone for domain: " . $domainName . ". Error: " . $e->getMessage());
}
}
}

public function listDomains() {
throw new \FOSSBilling\Exception("Not yet implemented");
Expand Down Expand Up @@ -218,4 +225,4 @@ public function deleteBulkRRsets($domainName, $rrsetDataArray) {
throw new \FOSSBilling\Exception("Not yet implemented");
}

}
}
44 changes: 36 additions & 8 deletions Servicedns/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,46 @@ public function uncancel(OODBBean $order, OODBBean $model): bool

public function delete(?OODBBean $order, ?OODBBean $model): void
{
$this->chooseDnsProvider();
if ($this->dnsProvider === null) {
throw new \FOSSBilling\Exception("DNS provider is not set.");
}
if ($order === null) {
throw new \FOSSBilling\Exception("Order is not provided.");
}

$this->dnsProvider->deleteDomain($domainName);
$config = json_decode($order->config, true);
if (!$config) {
throw new \FOSSBilling\Exception("Invalid or missing DNS provider configuration.");
}

$this->chooseDnsProvider($config);
if ($this->dnsProvider === null) {
throw new \FOSSBilling\Exception("DNS provider is not set.");
}

$domainName = $config['domain_name'] ?? null;
if (empty($domainName)) {
throw new \FOSSBilling\Exception("Domain name is not set.");
}

if (is_object($model)) {
$this->di['db']->trash($model);
try {
// Attempt to delete the domain from PowerDNS.
$this->dnsProvider->deleteDomain($domainName);
} catch (\Exception $e) {
// Check if the exception is due to the domain not being found.
if (strpos($e->getMessage(), 'Not Found') !== false) {
// Log the not found error but proceed with deleting the order.
error_log("Domain $domainName not found in PowerDNS, but proceeding with order deletion.");
} else {
// For other exceptions, rethrow them as they indicate actual issues.
throw new \FOSSBilling\Exception("Failed to delete domain $domainName: " . $e->getMessage());
}
}

// Proceed with deleting the order from the database.
if (is_object($model)) {
$this->di['db']->trash($model);
}
}


public function toApiArray(OODBBean $model): array
{
$domain_id = $this->di['db']->findOne('service_dns', 'domain_name = :domain_name', [':domain_name' => $model->domain_name]);
Expand Down Expand Up @@ -388,4 +416,4 @@ private function isActive(OODBBean $model): bool

return $order->status === 'active';
}
}
}

0 comments on commit c6b14ac

Please sign in to comment.