Commit aba14eb8 authored by Ludwig Ruderstaller's avatar Ludwig Ruderstaller
Browse files

refs #904414897791793 - first zone endpoints

parent 268db58a
......@@ -2,4 +2,5 @@
.idea
.php_cs.cache
vendor
composer.lock
\ No newline at end of file
composer.lock
build
......@@ -61,9 +61,9 @@ class Client
*
* @return mixed
*/
public function call($payload = null, $uri, $hydrationClass = null, $isList = false, $method = 'GET')
public function call($payload = null, $uri, $hydrationClass = null, $isList = false, $method = 'GET', array $queryParams = [])
{
$uri = sprintf('%s/%s', $this->apiUri, $uri);
$uri = sprintf('%s/%s?%s', $this->apiUri, $uri, http_build_query($queryParams));
$uri = rtrim($uri, '/');
$request = new Request($method, $uri, [
......@@ -75,7 +75,13 @@ class Client
$responseBody = $response->getBody()->getContents();
$responseData = json_decode($responseBody);
if ($response->getStatusCode() >= 300) {
//if (getenv('DEBUG')) {
// dump([$uri, $method, isset($responseData->error) ? $responseData->error : [], $response->getStatusCode()]);
//}
if ($response->getStatusCode() >= 300 && isset($responseData->error)) {
throw new \LogicException(sprintf('Error on %s request %s: %s', $method, $uri, $responseData->error));
} elseif ($response->getStatusCode() >= 300) {
$message = isset($responseData->message) ?? 'Unknown';
throw new \Exception(sprintf('Error on request %s: %s', $response->getStatusCode(), $message));
}
......@@ -95,6 +101,8 @@ class Client
$dataObject = [$dataObject];
}
$result = [];
foreach ($dataObject as $data) {
$result[] = $this->serializer->denormalize($data, $hydrationClass, null, [
ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
......@@ -108,11 +116,6 @@ class Client
return current($result);
}
protected function getClient(): HttpClient
{
return $this->client;
}
/**
* @return Serializer
*/
......
......@@ -14,16 +14,38 @@ declare(strict_types=1);
namespace Cwd\PowerDNSClient\Endpoints;
use Cwd\PowerDNSClient\Client;
use Cwd\PowerDNSClient\Validator\ValidationException;
use Symfony\Component\Validator\Validation;
abstract class AbstractEndpoint
{
private $client;
protected $defaultServerId;
protected $validator;
public function __construct(Client $client, $defaultServerId)
{
$this->client = $client;
$this->defaultServerId = $defaultServerId;
$this->validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
}
public function validateEntity($entity)
{
$violations = $this->validator->validate($entity);
if (count($violations) > 0) {
throw new ValidationException(
sprintf('Entity %s does not validate to spezification', get_class($entity)),
0,
null,
$violations
);
}
return true;
}
public function getClient(): Client
......
<?php
/*
* This file is part of the CwdPowerDNS Client
*
* (c) 2018 cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\PowerDNSClient\Endpoints;
use Cwd\PowerDNSClient\Model\Zone;
class ZonesEndpoint extends AbstractEndpoint
{
const ENDPOINT_LIST = 'servers/%s/zones';
const ENDPOINT_ELEMENT = 'servers/%s/zones/%s';
/**
* @param string|Zone $zoneId
*/
public function delete($zoneId)
{
if ($zoneId instanceOf Zone) {
$zoneId = $zoneId->getId();
}
$this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), null, false, 'DELETE');
}
public function create(Zone $zone, $rrsets = true, $hydrationClass = Zone::Class): Zone
{
$rrsets = ($rrsets) ? 'true' : 'false';
$this->validateEntity($zone);
$payload = $this->getClient()->getSerializer()->serialize($zone, 'json');
return $this->getClient()->call($payload, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, false, 'POST', ['rrsets' => $rrsets]);
}
/**
* @param string|Zone $zoneId
* @return Zone
* @throws \Http\Client\Exception
*/
public function get($zoneId, $hydrationClass = Zone::Class): Zone
{
if ($zoneId instanceOf Zone) {
$zoneId = $zoneId->getId();
}
return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), $hydrationClass, false, 'GET');
}
/**
* @return Servers[]
*
* @throws \Http\Client\Exception
*/
public function all($zoneName = null, $hydrationClass = Zone::Class): array
{
$queryParams = [];
if ($zoneName !== null) {
$queryParams['zone'] = $zoneName;
}
return $this->getClient()->call(null, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, true, 'GET', $queryParams);
}
}
......@@ -13,64 +13,86 @@ declare(strict_types=1);
namespace Cwd\PowerDNSClient\Model;
use Symfony\Component\Validator\Constraints as Assert;
use Cwd\PowerDNSClient\Validator\Constraints as DNSAssert;
class Zone
{
/** @var string */
const TYPE = 'Zone';
const KIND_MASTER = 'Master';
const KIND_SLAVE = 'Slave';
const KIND_NATIVE = 'Native';
/** @var string|null */
private $id;
/** @var string */
/**
* @var string
* @Assert\NotBlank()
* @DNSAssert\HasDotPostfix()
*/
private $name;
/** @var string */
private $type;
/** @var string */
/**
* @var string
* @Assert\NotBlank()
* @Assert\Choice(
* choices= {"Zone"}
* )
*/
private $type = self::TYPE;
/** @var string|null */
private $url;
/** @var string */
/**
* @var string|null
* @Assert\Choice(
* choices = {"Master", "Slave", "Native"}
* )
*/
private $kind;
/** @var RRSet[] */
private $rrsets = [];
/** @var int */
/** @var int|null */
private $serial;
/** @var int */
/** @var int|null */
private $notifiedSerial;
/** @var string[] */
private $masters = [];
/** @var bool */
private $dnssec = false;
/** @var string */
/** @var string|null */
private $nsec3param;
/** @var bool */
private $nsec3narrow = false;
/** @var bool */
private $predisgned = false;
/** @var string */
/** @var string|null */
private $soaEdit;
/** @var string */
/** @var string|null */
private $soaEditApi;
/** @var bool */
private $apiRectify = false;
/** @var string */
/** @var string|null */
private $zone;
/** @var string */
/** @var string|null */
private $account;
/** @var string[] */
private $nameservers = [];
/**
* @return string
* @return null|string
*/
public function getId(): string
public function getId(): ?string
{
return $this->id;
}
/**
* @param string $id
*
* @param null|string $id
* @return Zone
*/
public function setId(string $id): Zone
public function setId(?string $id): Zone
{
$this->id = $id;
return $this;
}
......@@ -84,13 +106,11 @@ class Zone
/**
* @param string $name
*
* @return Zone
*/
public function setName(string $name): Zone
{
$this->name = $name;
return $this;
}
......@@ -104,53 +124,47 @@ class Zone
/**
* @param string $type
*
* @return Zone
*/
public function setType(string $type): Zone
{
$this->type = $type;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getUrl(): string
public function getUrl(): ?string
{
return $this->url;
}
/**
* @param string $url
*
* @param null|string $url
* @return Zone
*/
public function setUrl(string $url): Zone
public function setUrl(?string $url): Zone
{
$this->url = $url;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getKind(): string
public function getKind(): ?string
{
return $this->kind;
}
/**
* @param string $kind
*
* @param null|string $kind
* @return Zone
*/
public function setKind(string $kind): Zone
public function setKind(?string $kind): Zone
{
$this->kind = $kind;
return $this;
}
......@@ -164,53 +178,47 @@ class Zone
/**
* @param RRSet[] $rrsets
*
* @return Zone
*/
public function setRrsets(array $rrsets): Zone
{
$this->rrsets = $rrsets;
return $this;
}
/**
* @return int
* @return int|null
*/
public function getSerial(): int
public function getSerial(): ?int
{
return $this->serial;
}
/**
* @param int $serial
*
* @param int|null $serial
* @return Zone
*/
public function setSerial(int $serial): Zone
public function setSerial(?int $serial): Zone
{
$this->serial = $serial;
return $this;
}
/**
* @return int
* @return int|null
*/
public function getNotifiedSerial(): int
public function getNotifiedSerial(): ?int
{
return $this->notifiedSerial;
}
/**
* @param int $notifiedSerial
*
* @param int|null $notifiedSerial
* @return Zone
*/
public function setNotifiedSerial(int $notifiedSerial): Zone
public function setNotifiedSerial(?int $notifiedSerial): Zone
{
$this->notifiedSerial = $notifiedSerial;
return $this;
}
......@@ -224,13 +232,11 @@ class Zone
/**
* @param string[] $masters
*
* @return Zone
*/
public function setMasters(array $masters): Zone
{
$this->masters = $masters;
return $this;
}
......@@ -244,33 +250,29 @@ class Zone
/**
* @param bool $dnssec
*
* @return Zone
*/
public function setDnssec(bool $dnssec): Zone
{
$this->dnssec = $dnssec;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getNsec3param(): string
public function getNsec3param(): ?string
{
return $this->nsec3param;
}
/**
* @param string $nsec3param
*
* @param null|string $nsec3param
* @return Zone
*/
public function setNsec3param(string $nsec3param): Zone
public function setNsec3param(?string $nsec3param): Zone
{
$this->nsec3param = $nsec3param;
return $this;
}
......@@ -284,13 +286,11 @@ class Zone
/**
* @param bool $nsec3narrow
*
* @return Zone
*/
public function setNsec3narrow(bool $nsec3narrow): Zone
{
$this->nsec3narrow = $nsec3narrow;
return $this;
}
......@@ -304,53 +304,47 @@ class Zone
/**
* @param bool $predisgned
*
* @return Zone
*/
public function setPredisgned(bool $predisgned): Zone
{
$this->predisgned = $predisgned;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getSoaEdit(): string
public function getSoaEdit(): ?string
{
return $this->soaEdit;
}
/**
* @param string $soaEdit
*
* @param null|string $soaEdit
* @return Zone
*/
public function setSoaEdit(string $soaEdit): Zone
public function setSoaEdit(?string $soaEdit): Zone
{
$this->soaEdit = $soaEdit;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getSoaEditApi(): string
public function getSoaEditApi(): ?string
{
return $this->soaEditApi;
}
/**
* @param string $soaEditApi
*
* @param null|string $soaEditApi
* @return Zone
*/
public function setSoaEditApi(string $soaEditApi): Zone
public function setSoaEditApi(?string $soaEditApi): Zone
{
$this->soaEditApi = $soaEditApi;
return $this;
}
......@@ -364,53 +358,47 @@ class Zone
/**
* @param bool $apiRectify
*
* @return Zone
*/
public function setApiRectify(bool $apiRectify): Zone
{
$this->apiRectify = $apiRectify;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getZone(): string
public function getZone(): ?string
{
return $this->zone;
}
/**
* @param string $zone
*
* @param null|string $zone
* @return Zone
*/
public function setZone(string $zone): Zone
public function setZone(?string $zone): Zone
{
$this->zone = $zone;
return $this;
}
/**
* @return string
* @return null|string
*/
public function getAccount(): string
public function getAccount(): ?string
{
return $this->account;
}
/**
* @param string $account
*
* @param null|string $account
* @return Zone
*/
public function setAccount(string $account): Zone
public function setAccount(?string $account): Zone
{
$this->account = $account;
return $this;
}
......@@ -424,13 +412,11 @@ class Zone
/**
* @param string[] $nameservers
*
* @return Zone
*/
public function setNameservers(array $nameservers): Zone
{
$this->nameservers = $nameservers;
return $this;
}
}
......@@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Cwd\PowerDNSClient;
use Cwd\PowerDNSClient\Endpoints\ServersEndpoint;
use Cwd\PowerDNSClient\Endpoints\ZonesEndpoint;
class PowerDNSClient
{
......@@ -23,16 +24,15 @@ class PowerDNSClient
/** @var ServersEndpoint */
private $serversEndpoint;
/** @var ZonesEndpoint */
private $zonesEndpoint;
/** @var string */
private $defaultServerId = 'localhost';
public function __construct(?Client $client = null)
public function __construct(Client $client)
{
if (null === $client) {
$this->client = new Client('http://localhost', 'changeme');
} else {
$this->client = $client;
}
$this->client = $client;
}
public function servers(): ServersEndpoint
......@@ -44,6 +44,15 @@ class PowerDNSClient
return $this->serversEndpoint;
}