Skip to content

Commit

Permalink
Merge pull request #144 from bennetgallein/fix/save-comments
Browse files Browse the repository at this point in the history
fix: RRSet save comments
  • Loading branch information
robbinjanssen authored Oct 23, 2023
2 parents e6269f4 + 865754b commit 3894d96
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 25 deletions.
19 changes: 17 additions & 2 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Exonet\Powerdns;

use Exonet\Powerdns\Resources\Comment;
use Exonet\Powerdns\Resources\Record;
use Exonet\Powerdns\Resources\ResourceRecord;

Expand All @@ -15,6 +16,7 @@ class Helper
* @param string $type The type of the resource record.
* @param array|string $content The content of the resource record.
* @param int $ttl The TTL.
* @param array $comments The Comment.
*
* @throws Exceptions\InvalidRecordType If the given type is invalid.
*
Expand All @@ -25,14 +27,15 @@ public static function createResourceRecord(
$name,
string $type = '',
$content = '',
int $ttl = 3600
int $ttl = 3600,
array $comments = []
): ResourceRecord {
if (is_array($name)) {
if (isset($name['records'])) {
return (new ResourceRecord())->setApiResponse($name);
}

['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content] = $name;
['name' => $name, 'type' => $type, 'ttl' => $ttl, 'content' => $content, 'comments' => $comments] = $name;
}

$name = str_replace('@', $zoneName, $name);
Expand All @@ -53,6 +56,18 @@ public static function createResourceRecord(
$content = [$content];
}

if (is_array($comments)) {
$commentList = [];
foreach ($comments as $comment) {
// we can use an empty fallback for account and nothing the current time for modified_at
$commentList[] = (new Comment())
->setContent($comment['content'])
->setAccount($comment['account'] ?? '')
->setModifiedAt($comment['modified_at'] ?? time());
}
$resourceRecord->setComments($commentList);
}

$recordList = [];
foreach ($content as $record) {
$recordItem = new Record();
Expand Down
18 changes: 18 additions & 0 deletions src/Transformers/CommentTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Exonet\Powerdns\Transformers;

class CommentTransformer extends Transformer
{
/**
* {@inheritdoc}
*/
public function transform()
{
return (object) [
'content' => $this->data->getContent(),
'account' => $this->data->getAccount(),
'modified_at' => $this->data->getModifiedAt(),
];
}
}
3 changes: 3 additions & 0 deletions src/Transformers/RRSetTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ private function transformResourceRecord(ResourceRecord $resourceRecord)
'ttl' => $resourceRecord->getTtl(),
'changetype' => $resourceRecord->getChangeType(),
'records' => $recordList,
'comments' => array_map(function ($comment) {
return (new CommentTransformer($comment))->transform();
}, $resourceRecord->getComments()),
];
}

Expand Down
30 changes: 16 additions & 14 deletions src/Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ class Zone extends AbstractZone
* resource records will be created in a single call to the PowerDNS server. If $name is a string, a single resource
* record is created.
*
* @param mixed[]|string $name The resource record name.
* @param string $type The type of the resource record.
* @param mixed[]|string $content The content of the resource record. When passing a multidimensional array,
* multiple records are created for this resource record.
* @param int $ttl The TTL.
* @param mixed[]|string $name The resource record name.
* @param string $type The type of the resource record.
* @param mixed[]|string $content The content of the resource record. When passing a multidimensional array,
* multiple records are created for this resource record.
* @param int $ttl The TTL.
* @param array|mixed[] $comments The comment to assign to the record.
*
* @throws Exceptions\InvalidRecordType If the given type is invalid.
*
* @return bool True when created.
*/
public function create($name, string $type = '', $content = '', int $ttl = 3600): bool
public function create($name, string $type = '', $content = '', int $ttl = 3600, array $comments = []): bool
{
if (is_array($name)) {
$resourceRecords = [];
foreach ($name as $item) {
$resourceRecords[] = $this->make($item['name'], $item['type'], $item['content'], $item['ttl'] ?? $ttl);
$resourceRecords[] = $this->make($item['name'], $item['type'], $item['content'], $item['ttl'] ?? $ttl, $item['comments'] ?? []);
}
} else {
$resourceRecords = [$this->make($name, $type, $content, $ttl)];
$resourceRecords = [$this->make($name, $type, $content, $ttl, $comments)];
}

return $this->patch($resourceRecords);
Expand Down Expand Up @@ -138,18 +139,19 @@ public function find(string $resourceRecordName, ?string $recordType = null): Re
/**
* Make (but not insert/POST) a new resource record.
*
* @param string $name The resource record name.
* @param string $type The type of the resource record.
* @param string $content The content of the resource record.
* @param int $ttl The TTL.
* @param string $name The resource record name.
* @param string $type The type of the resource record.
* @param string $content The content of the resource record.
* @param int $ttl The TTL.
* @param array $comments The Comments.
*
* @throws Exceptions\InvalidRecordType If the given type is invalid.
*
* @return ResourceRecord The constructed ResourceRecord.
*/
public function make(string $name, string $type, $content, int $ttl): ResourceRecord
public function make(string $name, string $type, $content, int $ttl, array $comments): ResourceRecord
{
return Helper::createResourceRecord($this->zone, compact('name', 'type', 'content', 'ttl'));
return Helper::createResourceRecord($this->zone, compact('name', 'type', 'content', 'ttl', 'comments'));
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class HelperTest extends TestCase
{
public function testWithArguments(): void
{
$result = Helper::createResourceRecord('unit.test.', 'www', RecordType::A, '127.0.0.1', 1337);
$result = Helper::createResourceRecord('unit.test.', 'www', RecordType::A, '127.0.0.1', 1337, [['content' => 'Hello World', 'account' => 'Tester']]);

self::assertSame('www.unit.test.', $result->getName());
self::assertSame('A', $result->getType());
self::assertSame(1337, $result->getTtl());
self::assertCount(1, $result->getRecords());
self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent());
self::assertSame('Hello World', $result->getComments()[0]->getContent());
self::assertSame('Tester', $result->getComments()[0]->getAccount());
}

public function testWithArray(): void
Expand All @@ -31,6 +33,18 @@ public function testWithArray(): void
'type' => RecordType::A,
'content' => ['127.0.0.1', '127.0.0.2'],
'ttl' => 1337,
'comments' => [
[
'content' => 'Hello',
'account' => 'rooti',
'modified_at' => 999,
],
[
'content' => 'World',
'account' => 'rooti',
'modified_at' => 111,
],
],
]
);

Expand All @@ -40,6 +54,9 @@ public function testWithArray(): void
self::assertCount(2, $result->getRecords());
self::assertSame('127.0.0.1', $result->getRecords()[0]->getContent());
self::assertSame('127.0.0.2', $result->getRecords()[1]->getContent());
self::assertSame(111, $result->getComments()[1]->getModifiedAt());
self::assertSame('World', $result->getComments()[1]->getContent());
self::assertSame('rooti', $result->getComments()[1]->getAccount());
}

public function testWithApiResponse(): void
Expand Down
17 changes: 17 additions & 0 deletions tests/Resources/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Exonet\Powerdns\tests\Resources;

use Exonet\Powerdns\Resources\Comment;
use Exonet\Powerdns\Transformers\CommentTransformer;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -23,4 +24,20 @@ public function testSettersAndGetters(): void
$this->assertSame('test content', $comment->getContent());
$this->assertSame(1234, $comment->getModifiedAt());
}

public function testTransformer(): void
{
$comment = (new Comment())
->setAccount('test account')
->setContent('test content')
->setModifiedAt(1234);

$transformer = new CommentTransformer($comment);

$this->assertEquals((object) [
'modified_at' => 1234,
'account' => 'test account',
'content' => 'test content',
], $transformer->transform());
}
}
18 changes: 10 additions & 8 deletions tests/functional/ZoneRecordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ class ZoneRecordsTest extends FunctionalTestCase
private $canonicalName;

private $dnsRecords = [
['name' => 'www', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60],
['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],
['name' => 'bla', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60],
['name' => '@', 'type' => RecordType::unknownTypePrefix. 65534, 'content' => '\# 4 aabbccdd', '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, 'comments' => []],
['name' => 'www', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'comments' => []],
['name' => 'bla', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60, 'comments' => []],
['name' => '@', 'type' => RecordType::unknownTypePrefix. 65534, 'content' => '\# 4 aabbccdd', 'ttl' => 60, 'comments' => []],
['name' => '@', 'type' => RecordType::AAAA, 'content' => '2a00:1e28:3:1629::1', 'ttl' => 60, 'comments' => []],
[
'name' => '@',
'type' => RecordType::SOA,
'content' => 'ns1.test. hostmaster.test. 0 10800 3605 604800 3600',
'ttl' => 60,
'comments' => [],
],
['name' => '@', 'type' => RecordType::NS, 'content' => 'ns1.powerdns-php.', 'ttl' => 60],
['name' => '@', 'type' => RecordType::NS, 'content' => 'ns2.powerdns-php.', 'ttl' => 60],
['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60],
['name' => '@', 'type' => RecordType::NS, 'content' => 'ns1.powerdns-php.', 'ttl' => 60, 'comments' => []],
['name' => '@', 'type' => RecordType::NS, 'content' => 'ns2.powerdns-php.', 'ttl' => 60, 'comments' => []],
['name' => '@', 'type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'comments' => []],
];

protected function setUp(): void
Expand Down Expand Up @@ -89,6 +90,7 @@ function (ResourceRecord $item) use (&$createdRecords) {
'type' => $item->getType(),
'content' => $content,
'ttl' => $item->getTtl(),
'comments' => [],
];
}
}
Expand Down

0 comments on commit 3894d96

Please sign in to comment.