Unassign zone, create zone and zone version, copy/remove zone version

This commit is contained in:
Simon Vieille 2015-02-21 15:51:44 +01:00
parent 67eabe7b3a
commit 348c0301bd
7 changed files with 297 additions and 11 deletions

View file

@ -13,26 +13,37 @@ class ZoneHelper extends AbstractHelper
public function showZone(Zone $zone, $key = 0, $withIndent = false)
{
$this->getOutput()->writeln(sprintf(
'%s<info>%s</info>.',
'%s<info>%s</info>',
$withIndent ? self::INDENT : '',
$zone->getName()
));
$this->getOutput()->writeln(sprintf(
'%s<info>%s</info>',
$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<comment> ID | NAME | TYPE | TTL | PRIO | CONTENT</comment>',
$withIndent ? self::INDENT : ''

View file

@ -0,0 +1,48 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\Zone;
class ZoneAddCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->setName('zone:add')
->setDescription('Add a zone')
->addOption('name', null, InputOption::VALUE_REQUIRED, '')
->addOption('description', null, InputOption::VALUE_REQUIRED, '')
->setHelp("The <info>%command.name%</info> ");
}
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('<info>Zone added.</info>');
}
}

View file

@ -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('<error>Zone not found.</error>');
return;

View file

@ -0,0 +1,42 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\Domain;
use Deblan\PowerDNS\Model\DomainQuery;
class ZoneUnassignCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->setName('zone:unassign')
->setDescription('Add a domain')
->addArgument('domain_id', InputArgument::REQUIRED, 'ZONE_ID')
->setHelp("The <info>%command.name%</info> ");
}
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('<error>Domain not found.</error>');
return;
}
$domain->setZone(null)->save();
$this->getOutput()->writeln('<info>Domain zone updated.</info>');
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\ZoneVersionQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Deblan\PowerDNS\Model\ZoneQuery;
use Deblan\PowerDNS\Model\ZoneVersion;
class ZoneVersionAddCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->setName('zone:version:add')
->setDescription('Add a zone version')
->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID')
->setHelp("The <info>%command.name%</info> ");
}
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('<error>Zone not found.</error>');
return;
}
$zoneVersion = (new ZoneVersion())
->setZone($zone)
->setVersion($zone->countZoneVersions() ? ZoneVersionQuery::create()->orderByVersion(Criteria::DESC)->findOne()->getVersion() + 1 : 1)
->setIsActive(false)
->save();
$this->getOutput()->writeln('<info>Zone version added.</info>');
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\ZoneVersionQuery;
use Propel\Runtime\ActiveQuery\Criteria;
class ZoneVersionCopyCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->setName('zone:version:copy')
->setDescription('Copy a zone version')
->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID')
->addArgument('version', InputArgument::REQUIRED, 'VERSION')
->setHelp("The <info>%command.name%</info> ");
}
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('<error>Zone version not found.</error>');
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('<info>Zone version copied.</info>');
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\ZoneVersionQuery;
use Symfony\Component\Console\Input\InputOption;
class ZoneVersionRemoveCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->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 <info>%command.name%</info> ");
}
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('<error>Zone version not found.</error>');
return;
}
if ($zoneVersion->getIsActive()) {
$this->getOutput()->writeln('<error>You can not remove an activated zone version.</error>');
return;
}
$confirm = $this->getInput()->getOption('confirm');
if (false === $confirm) {
$confirm = $this->getHelper('dialog')->askConfirmation($this->getOutput(), '<info>Do you confirm? [<comment>no</comment>] ', false);
}
if ($confirm) {
$zoneVersion->delete();
$this->getOutput()->writeln('<info>Zone version removed.</info>');
} else {
$this->getOutput()->writeln('<info>Aborted.</info>');
}
}
}