diff --git a/cli.php b/cli.php index ba98520..2240c98 100644 --- a/cli.php +++ b/cli.php @@ -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; @@ -63,4 +64,5 @@ $application->add(new DescribeOrganizationWorkspaces()); $application->add(new MassDeleteProjectWorkspaces()); $application->add(new UpdateDataRetention()); +$application->add(new DeleteStorageBackend()); $application->run(); diff --git a/src/Keboola/Console/Command/DeleteStorageBackend.php b/src/Keboola/Console/Command/DeleteStorageBackend.php new file mode 100644 index 0000000..ced074b --- /dev/null +++ b/src/Keboola/Console/Command/DeleteStorageBackend.php @@ -0,0 +1,67 @@ +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, + ]); + } +}