Skip to content

Commit 1922720

Browse files
committed
feat: add support for importing dns records
1 parent bd82f2e commit 1922720

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

phpmd.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<exclude name="StaticAccess" />
1313
</rule>
1414
<rule ref="rulesets/codesize.xml">
15+
<exclude name="ExcessiveClassComplexity" />
1516
<exclude name="TooManyPublicMethods"/>
1617
</rule>
1718
<rule ref="rulesets/controversial.xml" />

src/ApiClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,18 @@ public function getUser(): Collection
678678
return $this->request('get', '/user');
679679
}
680680

681+
/**
682+
* Change the value of the DNS record in the given DNS zone ID or name.
683+
*/
684+
public function importDnsRecord($zoneIdOrName, array $subdomains = [])
685+
{
686+
$zone = $this->getDnsZone($zoneIdOrName);
687+
688+
$this->request('post', "/zones/{$zone['id']}/import-records", [
689+
'subdomains' => array_filter($subdomains),
690+
]);
691+
}
692+
681693
/**
682694
* Invalidate the content delivery network cache for the given project environment.
683695
*/

src/Command/AbstractCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
9999
return $this->perform($input, new ConsoleOutput($input, $output)) ?? 0;
100100
}
101101

102+
/**
103+
* Get the value of an argument that should be an array.
104+
*/
105+
protected function getArrayArgument(InputInterface $input, string $argument, bool $requiredNonInteractive = true): array
106+
{
107+
$value = $input->getArgument($argument);
108+
109+
if (null === $value && $requiredNonInteractive && !$input->isInteractive()) {
110+
throw new InvalidArgumentException(sprintf('You must pass a "%s" argument when running in non-interactive mode', $argument));
111+
} elseif (null !== $value && !is_array($value)) {
112+
throw new InvalidArgumentException(sprintf('The "%s" argument must be an array value', $argument));
113+
}
114+
115+
return (array) $value;
116+
}
117+
102118
/**
103119
* Get the value of a option that should be an array.
104120
*/

src/Command/Dns/CreateDnsZoneCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,15 @@ protected function perform(InputInterface $input, ConsoleOutput $output)
7171
}
7272

7373
$output->info('DNS zone created');
74+
75+
if ($output->confirm('Do you want to import the root DNS records for this domain', false)) {
76+
$this->apiClient->importDnsRecord($zone['id']);
77+
}
78+
79+
if ($output->confirm('Do you want to import DNS records for subdomains of this domain', false)) {
80+
$this->invoke($output, ImportDnsRecordsCommand::NAME, [
81+
'zone' => $zone['domain_name'],
82+
]);
83+
}
7484
}
7585
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Cli\Command\Dns;
15+
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Ymir\Cli\Console\ConsoleOutput;
19+
20+
class ImportDnsRecordsCommand extends AbstractDnsCommand
21+
{
22+
/**
23+
* The name of the command.
24+
*
25+
* @var string
26+
*/
27+
public const NAME = 'dns:zone:import-records';
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function configure()
33+
{
34+
$this
35+
->setName(self::NAME)
36+
->setDescription('Import')
37+
->addArgument('zone', InputArgument::REQUIRED, 'The name of the DNS zone that the DNS record belongs to')
38+
->addArgument('subdomain', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'The subdomains that we want to import');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function perform(InputInterface $input, ConsoleOutput $output)
45+
{
46+
$subdomains = $this->getArrayArgument($input, 'subdomain', false);
47+
48+
if (empty($subdomains)) {
49+
$subdomains = explode(',', (string) $output->ask('Please enter a comma-separated list of subdomains to import DNS records from (leave blank to import the root DNS records)'));
50+
}
51+
52+
$this->apiClient->importDnsRecord($this->getStringArgument($input, 'zone'), $subdomains);
53+
54+
$output->info('DNS records imported');
55+
}
56+
}

0 commit comments

Comments
 (0)