Skip to content

Commit

Permalink
Merge pull request #28 from exonet/tsi/EXO-4327
Browse files Browse the repository at this point in the history
Create zone from resource
  • Loading branch information
robbinjanssen authored Apr 1, 2020
2 parents e241fc5 + 1fa8741 commit fd93397
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to `powerdns-php` will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased
[Compare v2.1.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.1.0...develop)
[Compare v2.2.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.2.0...develop)

## [v2.2.0](https://github.com/exonet/powerdns-php/releases/tag/v2.2.0) - 2020-04-01
[Compare v2.1.0 - v2.2.0](https://github.com/exonet/powerdns-php/compare/v2.1.0...v2.2.0)
### Added
- Create a new zone based on a zone resource object.

## [v2.1.0](https://github.com/exonet/powerdns-php/releases/tag/v2.1.0) - 2020-03-11
[Compare v2.0.0 - v2.1.0](https://github.com/exonet/powerdns-php/compare/v2.0.0...v2.1.0)
### Added
Expand Down
44 changes: 44 additions & 0 deletions examples/list_records_for_zone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/CliLogger.php';

/*
* This example will create a new domain and adds some default DNS records.
*/

use Exonet\Powerdns\Powerdns;
use Exonet\Powerdns\Resources\Record;

$domain = 'dns-new-zone-test.nl';

// Update the key to the real PowerDNS API Key.
$powerdns = new Powerdns('127.0.0.1', 'very_secret_secret');

// Uncomment this line to see what happens when executing this example on the command line.
// $powerdns->setLogger(new CliLogger());

echo PHP_EOL;

// Get all the resource records for the specified zone/domain.
$zoneResourceSets = $powerdns->zone($domain)->get();

// Loop through all found resource records.
foreach ($zoneResourceSets as $resourceSet) {
/*
* A resource set can contain multiple records. By mapping them it is possible
* to extract only the content, which is the only necessary information for this example.
*/
$recordContents = array_map(function (Record $record) {
return $record->getContent();
}, $resourceSet->getRecords());

// Show for each resource set in the zone the type, name and values.
echo sprintf(
'| %-4s | %30s --> %-30s%s',
$resourceSet->getType(),
$resourceSet->getName(),
implode(', ', $recordContents),
PHP_EOL
);
}
2 changes: 1 addition & 1 deletion examples/new_domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Exonet\Powerdns\RecordType;

$domain = 'dns-new-zone-test.nl';
$nameServers = ['ns1.example.com.', 'ns2.example.'];
$nameServers = ['ns1.example.com.', 'ns2.example.eu.'];
$dnsRecords = [
['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],
['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],
Expand Down
47 changes: 47 additions & 0 deletions examples/new_domain_from_zone_resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/CliLogger.php';

/*
* This example will create a new domain based on a zone resource object with some more 'advanced'
* settings and adds some default DNS records.
*/

use Exonet\Powerdns\Powerdns;
use Exonet\Powerdns\RecordType;
use Exonet\Powerdns\Resources\Zone as ZoneResource;

$canonicalDomain = 'dns-new-zone-resource-test.nl.';
$nameServers = ['ns1.example.com.', 'ns2.example.eu.'];
$dnsRecords = [
['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],
['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],

['name' => '@', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60],
['name' => 'www', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60],
];

// Update the key to the real PowerDNS API Key.
$powerdns = new Powerdns('127.0.0.1', 'very_secret_secret');

// Uncomment this line to see what happens when executing this example on the command line.
// $powerdns->setLogger(new CliLogger());

// Uncomment this line if you want to run this example multiple times.
// $powerdns->deleteZone($canonicalDomain);

// Create a new zone resource.
$newZone = new ZoneResource();
$newZone->setName($canonicalDomain);
$newZone->setKind('native');
$newZone->setDnssec(true);
$newZone->setSoaEdit('epoch');
$newZone->setSoaEditApi('epoch');
// $newZone->setMasters([...]);
$newZone->setNameservers($nameServers);
$newZone->setApiRectify(false);
$newZone->setNsec3param('1 0 100 1234567890');

// Create a new zone with the defined records and name servers.
$powerdns->createZoneFromResource($newZone)->create($dnsRecords);
14 changes: 14 additions & 0 deletions src/Powerdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ public function createZone(string $canonicalDomain, array $nameservers, bool $us
return $this->zone($canonicalDomain);
}

/**
* Create a new zone based on a zone resource.
*
* @param ZoneResource $zoneResource The zone resource.
*
* @return Zone The created zone.
*/
public function createZoneFromResource(ZoneResource $zoneResource): Zone
{
$this->connector->post('zones', new CreateZoneTransformer($zoneResource));

return $this->zone($zoneResource->getName());
}

/**
* Get a zone instance to work with.
*
Expand Down
1 change: 1 addition & 0 deletions src/Transformers/CreateZoneTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function transform()
'soa_edit_api' => $this->data->getSoaEditApi(),
'masters' => $this->data->getMasters(),
'nameservers' => $this->data->getNameservers(),
'nsec3param' => $this->data->getNsec3param(),
];
}
}
44 changes: 44 additions & 0 deletions tests/functional/AdvancedZoneCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Exonet\Powerdns\tests\functional;

use Exonet\Powerdns\Powerdns;
use Exonet\Powerdns\Resources\Zone as ZoneResource;
use PHPUnit\Framework\TestCase;

class AdvancedZoneCreationTest extends TestCase
{
public function testCreateSoaIncrementZone(): void
{
$powerdns = new Powerdns('127.0.0.1', 'apiKey');
// Create a unique zone/domain name.
$canonicalDomain = 'advanced-zone.'.time().'.test.';

// Create a new zone resource.
$newZone = new ZoneResource();
$newZone->setName($canonicalDomain);
$newZone->setKind('native');
$newZone->setDnssec(true);
$newZone->setSoaEdit('epoch');
$newZone->setSoaEditApi('epoch');
$newZone->setNameservers(['ns1.test.']);
$newZone->setApiRectify(false);
$newZone->setNsec3param('1 0 100 1234567890');

// Create a new zone with the defined records and name servers.
$powerdns->createZoneFromResource($newZone);

// Get the zone and check the results.
$zone = $powerdns->zone($canonicalDomain);
$zoneResource = $zone->resource();
$this->assertSame($canonicalDomain, $zone->getCanonicalName());
$this->assertSame($canonicalDomain, $zoneResource->getName());
$this->assertSame('Native', $zoneResource->getKind());
$this->assertTrue($zoneResource->hasDnssec(), 'DNSSEC not correctly set.');
$this->assertSame('EPOCH', $zoneResource->getSoaEdit());
$this->assertSame('EPOCH', $zoneResource->getSoaEditApi());
$this->assertSame(['ns1.test.'], $zoneResource->getNameservers());
$this->assertFalse($zoneResource->hasAutoRectify(), 'API Rectify not correctly set.');
$this->assertSame('1 0 100 1234567890', $zoneResource->getNsec3param());
}
}
11 changes: 9 additions & 2 deletions tests/functional/ValidateSOAIncrementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@

class ValidateSOAIncrementTest extends TestCase
{
private $canonicalName;

protected function setUp()
{
$this->canonicalName = 'soa-increment.'.time().'.test';
}

public function testCreateSoaIncrementZone(): void
{
$powerdns = new Powerdns('127.0.0.1', 'apiKey');
$zone = $powerdns->createZone('soa-increment.test', ['ns1.powerdns-php.', 'ns2.powerdns-php.']);
$zone = $powerdns->createZone($this->canonicalName, ['ns1.powerdns-php.', 'ns2.powerdns-php.']);
$result = $zone->create('test', RecordType::A, '127.0.0.1', 3600);

$this->assertTrue($result);
Expand All @@ -23,7 +30,7 @@ public function testCreateSoaIncrementZone(): void
public function testSoaIncrement(): void
{
$powerdns = new Powerdns('127.0.0.1', 'apiKey');
$zone = $powerdns->zone('soa-increment.test');
$zone = $powerdns->zone($this->canonicalName);
$soaData = $zone->get(RecordType::SOA);

$expectedSoa = sprintf('ns1.powerdns-php. hostmaster.powerdns-php. %s02 10800 3600 604800 3600', date('Ymd'));
Expand Down

0 comments on commit fd93397

Please sign in to comment.