Skip to content

Commit

Permalink
Merge pull request #151 from bennetgallein/feat/tsig-keys
Browse files Browse the repository at this point in the history
feat: implement TSIGKey API
  • Loading branch information
trizz authored Jun 11, 2024
2 parents 03ae449 + 818ffeb commit fd2a74e
Show file tree
Hide file tree
Showing 10 changed files with 714 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Powerdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ public function cryptokeys(string $canonicalDomain): Cryptokey
return new Cryptokey($this->connector, $canonicalDomain);
}

/**
* Get a TSIGKey instance to work with.
*
* @return TSIGKey The TSIGKey instance
*/
public function tsigkeys(): TSIGKey
{
return new TSIGKey($this->connector);
}

/**
* Query PowerDNS internal statistics.
* The ring statistics are disabled by default to speedup the request and reduce the response size.
Expand Down Expand Up @@ -292,7 +302,9 @@ public function search(string $query, int $size = 100, string $type = 'all'): Se
)
);

$searchResults = array_map(static function ($item) { return new SearchResult($item); }, $response);
$searchResults = array_map(static function ($item) {
return new SearchResult($item);
}, $response);

return new SearchResultSet($searchResults);
}
Expand Down
165 changes: 165 additions & 0 deletions src/Resources/TSIGKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

namespace Exonet\Powerdns\Resources;

class TSIGKey
{
/**
* The name of the key.
*
* @var string
*/
private $name;

/**
* The ID for this key, used in the TSIGkey URL endpoint.
*
* @var string
*/
private $id;

/**
* The algorithm of the TSIG key.
*
* @var string
*/
private $algorithm;

/**
* The Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material.
*
* @var string
*/
private $key;

/**
* Set to "TSIGKey".
*
* @var string
*/
private $type = 'TSIGKey';

/**
* Record constructor.
*
* @param string $content Optional content to set.
*/
public function __construct(?array $content = null)
{
if ($content) {
$this->setName($content['name'] ?? '');
$this->setId($content['id'] ?? '');
$this->setAlgorithm($content['algorithm'] ?? '');
$this->setKey($content['key'] ?? '');
$this->setType('TSIGKey');
}
}

/**
* Get set to "TSIGKey".
*
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* Get the Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material.
*
* @return string
*/
public function getKey()
{
return $this->key;
}

/**
* Set the Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material.
*
* @param string $key The Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material
*
* @return self
*/
public function setKey(string $key)
{
$this->key = $key;

return $this;
}

/**
* Get the algorithm of the TSIG key.
*
* @return string
*/
public function getAlgorithm()
{
return $this->algorithm;
}

/**
* Set the algorithm of the TSIG key.
*
* @param string $algorithm The algorithm of the TSIG key
*
* @return self
*/
public function setAlgorithm(string $algorithm)
{
$this->algorithm = $algorithm;

return $this;
}

/**
* Get the ID for this key, used in the TSIGkey URL endpoint.
*
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* Set the ID for this key, used in the TSIGkey URL endpoint.
*
* @param string $id The ID for this key, used in the TSIGkey URL endpoint.
*
* @return self
*/
public function setId(string $id)
{
$this->id = $id;

return $this;
}

/**
* Get the name of the key.
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set the name of the key.
*
* @param string $name The name of the key
*
* @return self
*/
public function setName(string $name)
{
$this->name = $name;

return $this;
}
}
148 changes: 148 additions & 0 deletions src/Resources/TSIGKeySet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace Exonet\Powerdns\Resources;

use ArrayAccess;
use ArrayIterator;
use Closure;
use IteratorAggregate;
use ReturnTypeWillChange;

class TSIGKeySet implements IteratorAggregate, ArrayAccess
{
/**
* @var TSIGKey[] Array containing resources.
*/
private $tsigResources = [];

/**
* TSIGKeySet constructor.
*
* @param array|null $resourceRecords (Optional) The tsigkey resources to add.
*/
public function __construct(?array $resourceRecords = null)
{
if ($resourceRecords) {
$this->tsigResources = $resourceRecords;
}
}

/**
* Add a single tsigkey resource to the existing collection.
*
* @param TSIGKey $metaResource The tsigkey resource to add.
*
* @return TSIGKeySet The current TSIGKeySet instance.
*/
public function addResource(TSIGKey $metaResource): self
{
$this->tsigResources[] = $metaResource;

return $this;
}

/**
* Get the number of tsigkey resources in this collection.
*
* @return int The number of tsigkey resources.
*/
public function count(): int
{
return count($this->tsigResources);
}

/**
* Check if the current collection is not empty.
*
* @return bool True when there are tsigkey resources in this collection.
*/
public function isNotEmpty(): bool
{
return !$this->isEmpty();
}

/**
* Check if the current collection is empty.
*
* @return bool True when there are no tsigkey resources in this collection.
*/
public function isEmpty(): bool
{
return empty($this->tsigResources);
}

/**
* Loop through the collection and call the given closure for each tsigkey resource.
*
* @param Closure $closure The closure to execute for each tsigkey resource.
*
* @return TSIGKeySet The current TSIGKeySet instance.
*/
public function map(Closure $closure): self
{
foreach ($this->tsigResources as $index => $resource) {
$this->tsigResources[$index] = $closure($resource, $index);
}

return $this;
}

/**
* Delete all tsigkey resources that are set in the current collection.
*
* @return bool True when the tsigkey resources are deleted.
*/
public function delete(): bool
{
foreach ($this->tsigResources as $resource) {
$resource->delete();
}

return true;
}

/**
* {@inheritdoc}
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->tsigResources);
}

/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
{
return isset($this->tsigResources[$offset]);
}

/**
* {@inheritdoc}
*
* @return TSIGKey
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->tsigResources[$offset];
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value): void
{
$this->tsigResources[$offset] = $value;
}

/**
* {@inheritdoc}
*/
public function offsetUnset($offset): void
{
unset($this->tsigResources[$offset]);
}
}
Loading

0 comments on commit fd2a74e

Please sign in to comment.