Skip to content

Commit

Permalink
Add cache:purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed May 20, 2022
1 parent 1000779 commit 9aa367a
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/Cloudflare/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function updateDnsRecord(
);
}

public function purgeCache(string $zoneId): void
{
$this->getZonesClient()->cachePurgeEverything($zoneId);
}

private function getZonesClient(): Zones
{
if (!$this->zonesClient) {
Expand Down
14 changes: 12 additions & 2 deletions src/Cloudflare/Cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ public function findDnsRecord(
return !empty($dnsRecords) ? reset($dnsRecords) : null;
}

public function saveDnsRecord(DnsRecord $dnsRecord): void
public function persistDnsRecord(DnsRecord $dnsRecord): void
{
$this->manager->saveDnsRecord($dnsRecord);
$this->manager->persistDnsRecord($dnsRecord);
}

public function flush(): void
{
$this->manager->flush();
}

public function purgeCache(Zone $zone): void
{
$this->manager->purgeCache($zone);
}
}
2 changes: 2 additions & 0 deletions src/Cloudflare/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ public function updateDnsRecord(
int $ttl,
bool $proxied
): void;

public function purgeCache(string $zoneId): void;
}
6 changes: 5 additions & 1 deletion src/Cloudflare/Contracts/Cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public function findDnsRecord(
string $content = null
): ?DnsRecord;

public function saveDnsRecord(DnsRecord $dnsRecord): void;
public function persistDnsRecord(DnsRecord $dnsRecord): void;

public function flush(): void;

public function purgeCache(Zone $zone): void;
}
6 changes: 5 additions & 1 deletion src/Cloudflare/Contracts/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public function getZones(): array;
/** @return DnsRecord[]|array */
public function getDnsRecords(Zone $zone): array;

public function saveDnsRecord(DnsRecord $dnsRecord): void;
public function persistDnsRecord(DnsRecord $dnsRecord): void;

public function flush(): void;

public function purgeCache(Zone $zone): void;
}
4 changes: 4 additions & 0 deletions src/Cloudflare/DnsRecord/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function prepare(Zone $zone, string $type, string $name, string $content,
if (!$dnsRecord) {
$dnsRecord = $this->createDnsRecord($zone, $type, $name, $content, $ttl, $proxied);

$this->cloudflare->persistDnsRecord($dnsRecord);

$this->addAction($dnsRecord, self::ACTION_CREATE);

return;
Expand All @@ -46,6 +48,8 @@ public function prepare(Zone $zone, string $type, string $name, string $content,
if ($dnsRecord->needsUpdate($content)) {
$dnsRecord->update($content);

$this->cloudflare->persistDnsRecord($dnsRecord);

$this->addAction($dnsRecord, self::ACTION_UPDATE);

return;
Expand Down
34 changes: 28 additions & 6 deletions src/Cloudflare/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Manager implements ManagerInterface
/** @var array|DnsRecord[] */
private array $cachedDnsRecords = [];

private array $dnsRecordsToCreate = [];
private array $dnsRecordsToUpdate = [];

public function __construct(Client $client)
{
$this->client = $client;
Expand All @@ -39,15 +42,35 @@ public function getDnsRecords(Zone $zone): array
return $this->cachedDnsRecords[$zone->getName()];
}

public function saveDnsRecord(DnsRecord $dnsRecord): void
public function persistDnsRecord(DnsRecord $dnsRecord): void
{
if (!$dnsRecord->getId()) {
$this->addDnsRecord($dnsRecord);
if ($dnsRecord->getId()) {
if (!in_array($dnsRecord, $this->dnsRecordsToUpdate)) {
$this->dnsRecordsToUpdate[] = $dnsRecord;
}

return;
}

$this->updateDnsRecord($dnsRecord);
$this->cachedDnsRecords[$dnsRecord->getZone()->getName()][] = $dnsRecord;

$this->dnsRecordsToCreate[] = $dnsRecord;
}

public function flush(): void
{
foreach ($this->dnsRecordsToCreate as $dnsRecord) {
$this->addDnsRecord($dnsRecord);
}

foreach ($this->dnsRecordsToUpdate as $dnsRecord) {
$this->updateDnsRecord($dnsRecord);
}
}

public function purgeCache(Zone $zone): void
{
$this->client->purgeCache($zone->getId());
}

private function addDnsRecord(DnsRecord $dnsRecord): void
Expand All @@ -62,12 +85,11 @@ private function addDnsRecord(DnsRecord $dnsRecord): void
);

$dnsRecord->setId($dnsRecordId);

$this->cachedDnsRecords[$dnsRecord->getZone()->getName()][] = $dnsRecord;
}

private function updateDnsRecord(DnsRecord $dnsRecord): void
{
$this->dnsRecordsToUpdate[] = $dnsRecord;
$this->client->updateDnsRecord(
$dnsRecord->getZone()->getId(),
$dnsRecord->getId(),
Expand Down
64 changes: 64 additions & 0 deletions src/Command/Cloudflare/CachePurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Command\Cloudflare;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CachePurgeCommand extends AbstractCommand
{
protected static $defaultName = 'cloudflare:cache:purge';

protected function configure()
{
$this
->setDescription('Purge cache for a given zone')
->addArgument('zone', InputArgument::REQUIRED, 'Zone name for DNS lsiting')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command purges the cache for a given zone:
<info>%command.full_name%</info>
You must specify the <comment>zone</comment> as an argument to bypass the zone selection prompt:
<info>%command.full_name% domain.net</info>
Only zones granted by the <comment>CLOUDFLARE_API_TOKEN</comment> environment variable will be allowed.
EOF
)
;
}

protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('zone')) {
$input->setArgument('zone', $this->askZoneQuestion());
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io->title('Cache purge');

$zoneName = $input->getArgument('zone');

if (!$zone = $this->cloudflare->findZone($zoneName)) {
$this->io->error("No zone found with name \"$zoneName\".");

return self::INVALID;
}

$this->io->section("Purging cache for zone <info>$zone</info>:");

if (!$this->io->confirm('Do you want to continue ?')) {
return self::FAILURE;
}

$this->cloudflare->purgeCache($zone);

$this->io->success('Cache purged successfully!');

return self::SUCCESS;
}
}
8 changes: 2 additions & 6 deletions src/Command/Cloudflare/DnsImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::FAILURE;
}

$this->io->text("Creating <comment>$createCount</comment> DNS record(s)...");
$this->io->text('Flushing DNS record(s)...');

$this->saveDnsRecords($dnsRecordsToCreate);

$this->io->text("Updating <comment>$updateCount</comment> DNS record(s)...");

$this->saveDnsRecords($dnsRecordsToUpdate);
$this->cloudflare->flush();

$this->io->success("Successfully processed $total DNS records.");

Expand Down
4 changes: 4 additions & 0 deletions src/Command/Cloudflare/DnsListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::INVALID;
}

$this->io->text('Zone details:');

$this->displayZones([$zone]);

$this->io->section("Listing DNS records for zone <info>$zone</info>:");

$filters = [];
Expand Down
16 changes: 0 additions & 16 deletions src/Command/CsvCommandTrait.php

This file was deleted.

89 changes: 78 additions & 11 deletions tests/Command/Cloudflare/DnsImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,92 @@ public function getCommandName(): string
return 'cloudflare:dns:import';
}

public function testSuccessCommand(): void
{
/**
* @dataProvider successCommandDataProvider
*/
public function testSuccessCommand(
string $csvAsString,
int $expectedCreated,
int $expectedUpdated,
int $expectedUnchanged
): void {
$this->setAvailableZones([['id' => 'abc123', 'name' => 'foo.test', 'status' => 'active']]);
$this->createInputFile(<<<'CSV'
type,name,content
A,foo.test,157.230.77.126
A,bar.test,141.94.219.130
A,foo.test,157.230.77.126
A,bar.test,141.94.219.131
CSV
);
$this->setAvailableDnsRecords([['id' => 'abc123', 'type' => 'A', 'name' => 'foo.test', 'content' => '127.0.0.1']]);

$this->createInputFile($csvAsString);

$commandTester = $this->executeCommand([
'zone' => 'foo.test',
'file' => 'dns.csv',
]);

$display = $commandTester->getDisplay();

self::assertSame(Command::SUCCESS, $commandTester->getStatusCode());
self::assertStringContainsString('Successfully processed 4 DNS records.', $commandTester->getDisplay());
self::assertStringContainsString("$expectedCreated DNS record(s) will be created", $display);
self::assertStringContainsString("$expectedUpdated DNS record(s) will be updated", $display);
self::assertStringContainsString("$expectedUnchanged DNS record(s) will remain unchanged", $display);
}

public function successCommandDataProvider(): iterable
{
yield [
<<<'CSV'
type,name,content
A,bar.test,127.0.0.1
CSV,
1,
0,
0,
];

yield [
<<<'CSV'
type,name,content
A,foo.test,127.0.0.2
A,bar.test,127.0.0.1
CSV,
1,
1,
0,
];

yield [
<<<'CSV'
type,name,content
A,foo.test,127.0.0.1
A,bar.test,127.0.0.1
A,foo.test,127.0.0.2
CSV,
1,
1,
1,
];

yield [
<<<'CSV'
type,name,content
A,foo.test,127.0.0.2
A,bar.test,127.0.0.1
A,foo.test,127.0.0.1
CSV,
1,
2,
0,
];

yield [
<<<'CSV'
type,name,content
A,foo.test,127.0.0.2
A,bar.test,127.0.0.1
A,foo.test,127.0.0.1
A,bar.test,127.0.0.1
CSV,
1,
2,
1,
];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Test/Cloudflare/DummyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public function setDnsRecords(array $dnsRecords): void
$this->dnsRecords[] = (object) $dnsRecord;
}
}

public function purgeCache(string $zoneId): void
{
}
}

0 comments on commit 9aa367a

Please sign in to comment.