Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Keboola\Console\Command\AddFeature;
use Keboola\Console\Command\AllStacksIterator;
use Keboola\Console\Command\DeleteStorageBackend;
use Keboola\Console\Command\DeleteOrganizationOrphanedWorkspaces;
use Keboola\Console\Command\DeleteOrphanedWorkspaces;
use Keboola\Console\Command\DeleteOwnerlessWorkspaces;
Expand Down Expand Up @@ -63,4 +64,5 @@
$application->add(new DescribeOrganizationWorkspaces());
$application->add(new MassDeleteProjectWorkspaces());
$application->add(new UpdateDataRetention());
$application->add(new DeleteStorageBackend());
$application->run();
67 changes: 67 additions & 0 deletions src/Keboola/Console/Command/DeleteStorageBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Keboola\Console\Command;

use Keboola\ManageApi\Client;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class DeleteStorageBackend extends Command
{
protected function configure()
{
$this
->setName('manage:delete-backend')
->setDescription('Set keboola.touch attribute to all tables. This will invalidate async export caches.')
->addArgument('token', InputArgument::REQUIRED, 'storage api token')
->addArgument('ids', InputArgument::REQUIRED, 'list of IDs separated')
->addArgument('url', InputArgument::REQUIRED, 'Stack URL')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Will actually do the work, otherwise it\'s dry run');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$apiToken = $input->getArgument('token');
$apiUrl = $input->getArgument('url');
$ids = $input->getArgument('ids');
$force = (bool) $input->getOption('force');
$output->writeln('DANGER: Using force mode! Backend will be removed.');

$client = $this->createClient($apiUrl, $apiToken);

$allBackends = $client->listStorageBackend();
$allBackendsAssociative = [];
foreach ($allBackends as $backend) {
$allBackendsAssociative[$backend['id']] = $backend;
}
foreach (explode(',', $ids) as $id) {
if (!array_key_exists($id, $allBackendsAssociative)) {
$output->writeln(sprintf('Backend with ID "%s" does not exist, skipping...', $id));
continue;
}
$output->write(sprintf(
'Removing backend "%s" (%s) by "%s" - ',
$id,
$allBackendsAssociative[$id]['host'],
$allBackendsAssociative[$id]['owner']
));
if ($force) {
$output->writeln('really');
$client->removeStorageBackend($id);
} else {
$output->writeln('just kidding - dry mode');
}
}
}

private function createClient(string $host, string $token): Client
{
return new Client([
'url' => $host,
'token' => $token,
]);
}
}