From fed0eb43fdc366af17851b151e2cb48d3abaf3d9 Mon Sep 17 00:00:00 2001 From: jackdpeterson Date: Tue, 8 Oct 2019 11:19:59 -0700 Subject: [PATCH] Add support to List Domains This change helps facilitate bulks actions like managing ACME from a client-side app. After one calls $powerDns->listZones(); knowing what zone is being returned is extremely helpful. `$zone->getCanonicalName(): string;` --- src/AbstractZone.php | 8 ++++++++ src/Powerdns.php | 13 +++++++++++++ tests/PowerdnsTest.php | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/ZoneTest.php | 7 +++++++ 4 files changed, 70 insertions(+) diff --git a/src/AbstractZone.php b/src/AbstractZone.php index b6ea688..9fb1d40 100644 --- a/src/AbstractZone.php +++ b/src/AbstractZone.php @@ -85,4 +85,12 @@ protected function getZonePath(?string $path = null) : string { return sprintf('zones/%s%s', $this->zone, $path); } + + /** + * @return string + */ + public function getCanonicalName(): string + { + return substr($this->zone, 0, strlen($this->zone) - 1); + } } diff --git a/src/Powerdns.php b/src/Powerdns.php index 3190ca3..18f2deb 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -174,6 +174,19 @@ public function deleteZone(string $canonicalDomain) : bool return true; } + /** + * @return Zone[] + */ + public function listZones() : array + { + return array_map( + function (array $args) { + return new Zone($this->connector, $args['id']); + }, + $this->connector->get('zones') + ); + } + /** * Get a cryptokey instance to work with. * diff --git a/tests/PowerdnsTest.php b/tests/PowerdnsTest.php index 3b5bd88..27e0698 100644 --- a/tests/PowerdnsTest.php +++ b/tests/PowerdnsTest.php @@ -55,4 +55,46 @@ public function testZone() : void $this->assertInstanceOf(Zone::class, $zone); $this->assertTrue($powerDns->deleteZone('test.nl.')); } + + public function testListZones() : void + { + $connector = Mockery::mock(Connector::class); + $connector->shouldReceive('get')->withArgs(['zones'])->once()->andReturn( + [ + [ + 'account' => '', + 'dnssec' => false, + 'id' => 'example.co.uk', + 'kind' => 'Native', + 'last_check' => 0, + 'masters' => [], + 'name' => 'example.', + 'notified_serial' => 0, + 'serial' => 2019100101, + 'url' => '/api/v1/servers/localhost/zones/example.co.uk', + ], + [ + 'account' => '', + 'dnssec' => false, + 'id' => 'example.com', + 'kind' => 'Native', + 'last_check' => 0, + 'masters' => [], + 'name' => 'example.', + 'notified_serial' => 0, + 'serial' => 2019100102, + 'url' => '/api/v1/servers/localhost/zones/example.com', + ], + ] + ); + + $powerDns = new Powerdns(null, null, null, null, $connector); + + $response = $powerDns->listZones(); + $this->assertIsArray($response); + $this->assertCount(2, $response); + foreach ($response as $zone) { + $this->assertInstanceOf(Zone::class, $zone); + } + } } diff --git a/tests/ZoneTest.php b/tests/ZoneTest.php index 9ef500f..a9cfd10 100644 --- a/tests/ZoneTest.php +++ b/tests/ZoneTest.php @@ -112,4 +112,11 @@ public function testFindResourceRecords() : void $this->assertSame(1, $zone->find('record01.test.nl.')->count()); $this->assertSame(0, $zone->find('record01.test.nl.', 'MX')->count()); } + + public function testGetCanonicalName(): void + { + $connector = Mockery::mock(Connector::class); + $zone = new Zone($connector, 'test.nl'); + $this->assertSame('test.nl', $zone->getCanonicalName()); + } }