From 41f8bc3bec2237d98e9acce14ac1aed534fb9864 Mon Sep 17 00:00:00 2001 From: Jiri Semmler Date: Tue, 29 Jul 2025 15:33:58 +0200 Subject: [PATCH 1/2] command to delete backends --- cli.php | 2 + .../Console/Command/DeleteStorageBackend.php | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/Keboola/Console/Command/DeleteStorageBackend.php 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..1a44c59 --- /dev/null +++ b/src/Keboola/Console/Command/DeleteStorageBackend.php @@ -0,0 +1,68 @@ +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) { + // get backend detail and print it + + $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, + ]); + } +} From c97d6348134cf5b96390f447d399f07813c460e5 Mon Sep 17 00:00:00 2001 From: Jiri Semmler Date: Tue, 29 Jul 2025 15:37:29 +0200 Subject: [PATCH 2/2] skipping --- .../Console/Command/DeleteStorageBackend.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Keboola/Console/Command/DeleteStorageBackend.php b/src/Keboola/Console/Command/DeleteStorageBackend.php index 1a44c59..ced074b 100644 --- a/src/Keboola/Console/Command/DeleteStorageBackend.php +++ b/src/Keboola/Console/Command/DeleteStorageBackend.php @@ -19,8 +19,7 @@ protected function configure() ->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') - ; + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Will actually do the work, otherwise it\'s dry run'); } protected function execute(InputInterface $input, OutputInterface $output) @@ -39,25 +38,25 @@ protected function execute(InputInterface $input, OutputInterface $output) $allBackendsAssociative[$backend['id']] = $backend; } foreach (explode(',', $ids) as $id) { - // get backend detail and print it - + 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){ + 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([