From 462dfcbceb31d8713749e207bf8fb4e35063a082 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 21 Feb 2015 20:32:40 +0100 Subject: [PATCH] Add zone record command --- .../PowerDNS/Command/ZoneRecordAddCommand.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/Deblan/PowerDNS/Command/ZoneRecordAddCommand.php diff --git a/src/Deblan/PowerDNS/Command/ZoneRecordAddCommand.php b/src/Deblan/PowerDNS/Command/ZoneRecordAddCommand.php new file mode 100644 index 0000000..8167cd3 --- /dev/null +++ b/src/Deblan/PowerDNS/Command/ZoneRecordAddCommand.php @@ -0,0 +1,105 @@ +setName('zone:record:add') + ->setDescription('Add a zone record') + ->addArgument('zone_id', InputArgument::REQUIRED, 'ZONE_ID') + ->addArgument('version', InputArgument::REQUIRED, 'VERSION') + ->addOption('name', null, InputOption::VALUE_REQUIRED, '') + ->addOption('type', null, InputOption::VALUE_REQUIRED, '') + ->addOption('content', null, InputOption::VALUE_REQUIRED, '') + ->addOption('ttl', null, InputOption::VALUE_REQUIRED, '') + ->addOption('prio', null, InputOption::VALUE_REQUIRED, '') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $name = $this->getInput()->getOption('name'); + $type = $this->getInput()->getOption('type'); + $content = $this->getInput()->getOption('content'); + $ttl = $this->getInput()->getOption('ttl'); + $prio = $this->getInput()->getOption('prio'); + + $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 add zone record of an activated zone version.'); + + return; + } + + while (null === $name || trim($name) === '') { + $name = $this->getHelper('dialog')->ask($this->getOutput(), 'Name: ', null); + } + + while (null === $content || trim($content) === '') { + $content = $this->getHelper('dialog')->ask($this->getOutput(), 'Content: ', null); + } + + while (!$this->getHelper('validator')->isRecordType($type)) { + $this->getOutput()->writeln(''); + $this->getOutput()->writeln(sprintf( + 'Possible choices: %s', + implode(' ', ZoneRecordTableMap::getValueSet(ZoneRecordTableMap::COL_TYPE)) + )); + + $type = $this->getHelper('dialog')->ask($this->getOutput(), 'Type: ', null); + } + + while (!is_numeric($ttl)) { + $ttl = $this->getHelper('dialog')->ask($this->getOutput(), 'TTL: ', null); + } + + if ($prio === 'null') { + $prio = null; + } elseif (null === $prio || trim($prio) === '') { + $response = $this->getHelper('dialog')->ask($this->getOutput(), 'Prio [null]: ', null); + $prio = empty($response) ? null : (int) $response; + } + + $zoneRecord = (new ZoneRecord()) + ->setZoneVersion($zoneVersion) + ->setName($name) + ->setType($type) + ->setContent($content) + ->setPrio($prio) + ->setTtl($ttl); + + $zoneRecord->save(); + + $this->getOutput()->writeln('Zone record added.'); + } +}