From 348c0301bd636f03bb8ba58877fb9abfdfe85ff8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 21 Feb 2015 15:51:44 +0100 Subject: [PATCH] Unassign zone, create zone and zone version, copy/remove zone version --- .../PowerDNS/Command/Helper/ZoneHelper.php | 38 ++++++++--- .../PowerDNS/Command/ZoneAddCommand.php | 48 ++++++++++++++ .../PowerDNS/Command/ZoneAssignCommand.php | 2 +- .../PowerDNS/Command/ZoneUnassignCommand.php | 42 ++++++++++++ .../Command/ZoneVersionAddCommand.php | 50 ++++++++++++++ .../Command/ZoneVersionCopyCommand.php | 62 +++++++++++++++++ .../Command/ZoneVersionRemoveCommand.php | 66 +++++++++++++++++++ 7 files changed, 297 insertions(+), 11 deletions(-) create mode 100644 src/Deblan/PowerDNS/Command/ZoneAddCommand.php create mode 100644 src/Deblan/PowerDNS/Command/ZoneUnassignCommand.php create mode 100644 src/Deblan/PowerDNS/Command/ZoneVersionAddCommand.php create mode 100644 src/Deblan/PowerDNS/Command/ZoneVersionCopyCommand.php create mode 100644 src/Deblan/PowerDNS/Command/ZoneVersionRemoveCommand.php diff --git a/src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php b/src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php index df05e08..35f7f3f 100644 --- a/src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php +++ b/src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php @@ -13,26 +13,37 @@ class ZoneHelper extends AbstractHelper public function showZone(Zone $zone, $key = 0, $withIndent = false) { $this->getOutput()->writeln(sprintf( - '%s%s.', + '%s%s', $withIndent ? self::INDENT : '', $zone->getName() )); + $this->getOutput()->writeln(sprintf( + '%s%s', + $withIndent ? self::INDENT : '', + str_repeat('-', strlen($zone->getName())) + )); + if ($zone->getDescription()) { $this->getOutput()->writeln(($withIndent ? self::INDENT : '').$zone->getDescription()); } - if ($zone->getDescription()) { - $this->getOutput()->writeln(sprintf( - '%sID: %d', - $withIndent ? self::INDENT : '', - $zone->getId() - )); + $this->getOutput()->writeln(sprintf( + '%sID: %d', + $withIndent ? self::INDENT : '', + $zone->getId() + )); + + if (!$zone->countZoneVersions()) { + $this->getOutput()->writeln(($withIndent ? self::INDENT : '')); + $this->getOutput()->writeln(($withIndent ? self::INDENT : '').'No version found.'); + } else { + foreach ($zone->getZoneVersions() as $key => $zoneVersion) { + $this->showZoneVersion($zoneVersion, $key, $withIndent); + } } - foreach ($zone->getZoneVersions() as $key => $zoneVersion) { - $this->showZoneVersion($zoneVersion, $key, $withIndent); - } + $this->getOutput()->writeln(($withIndent ? self::INDENT : '')); } public function showZoneVersion(ZoneVersion $zoneVersion, $key = 0, $withIndent = false) @@ -59,6 +70,13 @@ class ZoneHelper extends AbstractHelper public function showZoneVersionRecords(ZoneVersion $zoneVersion, $withIndent = false) { $this->getOutput()->writeln($withIndent ? self::INDENT : ''); + + if (!$zoneVersion->countZoneRecords()) { + $this->getOutput()->writeln(($withIndent ? self::INDENT : '').'No record found.'); + + return; + } + $this->getOutput()->writeln(sprintf( '%s ID | NAME | TYPE | TTL | PRIO | CONTENT', $withIndent ? self::INDENT : '' diff --git a/src/Deblan/PowerDNS/Command/ZoneAddCommand.php b/src/Deblan/PowerDNS/Command/ZoneAddCommand.php new file mode 100644 index 0000000..66dbbd2 --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneAddCommand.php @@ -0,0 +1,48 @@ +setName('zone:add') + ->setDescription('Add a zone') + ->addOption('name', null, InputOption::VALUE_REQUIRED, '') + ->addOption('description', null, InputOption::VALUE_REQUIRED, '') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $name = $this->getInput()->getOption('name'); + $description = $this->getInput()->getOption('description'); + + while (null === $name || trim($name) === '') { + $name = $this->getHelper('dialog')->ask($this->getOutput(), 'Name: ', null); + } + + if (null === $description || trim($description) === '') { + $description = $this->getHelper('dialog')->ask($this->getOutput(), 'Description: ', null); + } + + $zone = (new Zone()) + ->setName($name) + ->setDescription($description) + ->save(); + + $this->getOutput()->writeln('Zone added.'); + } +} diff --git a/src/Deblan/PowerDNS/Command/ZoneAssignCommand.php b/src/Deblan/PowerDNS/Command/ZoneAssignCommand.php index e93616f..2602546 100644 --- a/src/Deblan/PowerDNS/Command/ZoneAssignCommand.php +++ b/src/Deblan/PowerDNS/Command/ZoneAssignCommand.php @@ -33,7 +33,7 @@ class ZoneAssignCommand extends AbstractCommand $zone = ZoneQuery::create()->findOneById($zoneId); $domain = DomainQuery::create()->findOneById((int) $this->getInput()->getArgument('domain_id')); - if ($zoneId !== 0 && null === $zone) { + if ($null === $zone) { $this->getOutput()->writeln('Zone not found.'); return; diff --git a/src/Deblan/PowerDNS/Command/ZoneUnassignCommand.php b/src/Deblan/PowerDNS/Command/ZoneUnassignCommand.php new file mode 100644 index 0000000..a04412f --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneUnassignCommand.php @@ -0,0 +1,42 @@ +setName('zone:unassign') + ->setDescription('Add a domain') + ->addArgument('domain_id', InputArgument::REQUIRED, 'ZONE_ID') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $domain = DomainQuery::create()->findOneById((int) $this->getInput()->getArgument('domain_id')); + + if (null === $domain) { + $this->getOutput()->writeln('Domain not found.'); + + return; + } + + $domain->setZone(null)->save(); + + $this->getOutput()->writeln('Domain zone updated.'); + } +} diff --git a/src/Deblan/PowerDNS/Command/ZoneVersionAddCommand.php b/src/Deblan/PowerDNS/Command/ZoneVersionAddCommand.php new file mode 100644 index 0000000..8e5e427 --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneVersionAddCommand.php @@ -0,0 +1,50 @@ +setName('zone:version:add') + ->setDescription('Add a zone version') + ->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $zoneId = (int) $this->getInput()->getArgument('zone_id'); + + $zone = ZoneQuery::create()->findOneById($zoneId); + + if (null === $zone) { + $this->getOutput()->writeln('Zone not found.'); + + return; + } + + $zoneVersion = (new ZoneVersion()) + ->setZone($zone) + ->setVersion($zone->countZoneVersions() ? ZoneVersionQuery::create()->orderByVersion(Criteria::DESC)->findOne()->getVersion() + 1 : 1) + ->setIsActive(false) + ->save(); + + $this->getOutput()->writeln('Zone version added.'); + } +} diff --git a/src/Deblan/PowerDNS/Command/ZoneVersionCopyCommand.php b/src/Deblan/PowerDNS/Command/ZoneVersionCopyCommand.php new file mode 100644 index 0000000..a75e978 --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneVersionCopyCommand.php @@ -0,0 +1,62 @@ +setName('zone:version:copy') + ->setDescription('Copy a zone version') + ->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID') + ->addArgument('version', InputArgument::REQUIRED, 'VERSION') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $zoneId = (int) $this->getInput()->getArgument('zone_id'); + $version = (int) $this->getInput()->getArgument('version'); + + $zoneVersion = ZoneVersionQuery::create() + ->filterByZoneId($zoneId) + ->filterByVersion($version) + ->findOne(); + + if (null === $zoneVersion) { + $this->getOutput()->writeln('Zone version not found.'); + + return; + } + + $zoneVersionCopy = $zoneVersion->copy(); + + $zoneVersionCopy + ->setVersion(ZoneVersionQuery::create()->orderByVersion(Criteria::DESC)->findOne()->getVersion() + 1) + ->setIsActive(false); + + foreach ($zoneVersion->getZoneRecords() as $record) { + $recordCopy = $record->copy(); + $recordCopy->save(); + + $zoneVersionCopy->addZoneRecord($recordCopy); + } + + $zoneVersionCopy->save(); + + $this->getOutput()->writeln('Zone version copied.'); + } +} diff --git a/src/Deblan/PowerDNS/Command/ZoneVersionRemoveCommand.php b/src/Deblan/PowerDNS/Command/ZoneVersionRemoveCommand.php new file mode 100644 index 0000000..5cb3cda --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneVersionRemoveCommand.php @@ -0,0 +1,66 @@ +setName('zone:version:remove') + ->setDescription('Remove an unactivated zone version') + ->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID') + ->addArgument('version', InputArgument::REQUIRED, 'VERSION') + ->addOption('confirm', null, InputOption::VALUE_NONE, '') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $zoneId = (int) $this->getInput()->getArgument('zone_id'); + $version = (int) $this->getInput()->getArgument('version'); + + $zoneVersion = ZoneVersionQuery::create() + ->filterByZoneId($zoneId) + ->filterByVersion($version) + ->findOne(); + + if (null === $zoneVersion) { + $this->getOutput()->writeln('Zone version not found.'); + + return; + } + + if ($zoneVersion->getIsActive()) { + $this->getOutput()->writeln('You can not remove an activated zone version.'); + + return; + } + + $confirm = $this->getInput()->getOption('confirm'); + + if (false === $confirm) { + $confirm = $this->getHelper('dialog')->askConfirmation($this->getOutput(), 'Do you confirm? [no] ', false); + } + + if ($confirm) { + $zoneVersion->delete(); + + $this->getOutput()->writeln('Zone version removed.'); + } else { + $this->getOutput()->writeln('Aborted.'); + } + } +}