From 45e1ce034a3063b3da35994efb80f787e9900dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 12 Dec 2023 11:35:44 +0100 Subject: [PATCH] Add ConfigureOpenstackObjectStorageCommand to ChillDocStoreBundle Added new command file "ConfigureOpenstackObjectStorageCommand.php" under ChillDocStoreBundle that helps in setting up OpenStack container for document storage. Along with the command, added corresponding test file "ConfigureOpenstackObjectStorageCommandTest.php" to ensure the functionality is working as expected. --- ...ConfigureOpenstackObjectStorageCommand.php | 90 +++++++++++++++++++ ...igureOpenstackObjectStorageCommandTest.php | 65 ++++++++++++++ .../ChillDocStoreBundle/config/services.yaml | 3 + 3 files changed, 158 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommand.php create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommandTest.php diff --git a/src/Bundle/ChillDocStoreBundle/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommand.php b/src/Bundle/ChillDocStoreBundle/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommand.php new file mode 100644 index 000000000..de6690faf --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommand.php @@ -0,0 +1,90 @@ +get('chill_doc_store')['openstack']['temp_url']; + + $this->tempUrlKey = $config['temp_url_key']; + $this->basePath = $config['temp_url_base_path']; + + parent::__construct(); + } + + protected function configure() + { + $this->setDescription('Configure openstack container to store documents') + ->setName('chill:doc-store:configure-openstack') + ->addOption('os_token', 'o', InputOption::VALUE_REQUIRED, 'Openstack token') + ->addOption('domain', 'd', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Domain name') + ; + } + + protected function interact(InputInterface $input, OutputInterface $output) + { + if (!$input->hasOption('os_token')) { + $output->writeln('The option os_token is required'); + + throw new \RuntimeException('The option os_token is required'); + } + + if (0 === count($input->getOption('domain'))) { + $output->writeln('At least one occurence of option domain is required'); + + throw new \RuntimeException('At least one occurence of option domain is required'); + } + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $domains = trim(implode(' ', $input->getOption('domain'))); + + if ($output->isVerbose()) { + $output->writeln(['Domains configured will be', $domains]); + } + + try { + $response = $this->client->request('POST', $this->basePath, [ + 'headers' => [ + 'X-Auth-Token' => $input->getOption('os_token'), + 'X-Container-Meta-Access-Control-Allow-Origin' => $domains, + 'X-Container-Meta-Temp-URL-Key' => $this->tempUrlKey, + ], + ]); + $response->getContent(); + } catch (HttpExceptionInterface $e) { + $output->writeln('Error'); + $output->writeln($e->getMessage()); + + if ($output->isVerbose()) { + $output->writeln($e->getTraceAsString()); + } + } + + return 0; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommandTest.php b/src/Bundle/ChillDocStoreBundle/Tests/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommandTest.php new file mode 100644 index 000000000..e0318313c --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/AsyncUpload/Command/ConfigureOpenstackObjectStorageCommandTest.php @@ -0,0 +1,65 @@ + 204]); + }); + + $parameters = new ParameterBag([ + 'chill_doc_store' => [ + 'openstack' => [ + 'temp_url' => [ + 'temp_url_key' => '12345679801234567890', + 'temp_url_base_path' => 'https://object.store.example/v1/AUTH/container', + ], + ], + ], + ]); + + $command = new ConfigureOpenstackObjectStorageCommand($client, $parameters); + + $tester = new CommandTester($command); + + $status = $tester->execute([ + '--os_token' => 'abc', + '--domain' => ['https://chill.domain.social', 'https://chill2.domain.social'], + ]); + + self::assertSame(0, $status); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/config/services.yaml b/src/Bundle/ChillDocStoreBundle/config/services.yaml index 27c04a60e..55163abb3 100644 --- a/src/Bundle/ChillDocStoreBundle/config/services.yaml +++ b/src/Bundle/ChillDocStoreBundle/config/services.yaml @@ -51,5 +51,8 @@ services: Chill\DocStoreBundle\AsyncUpload\Templating\: resource: '../AsyncUpload/Templating/' + Chill\DocStoreBundle\AsyncUpload\Command\: + resource: '../AsyncUpload/Command/' + Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface: alias: Chill\DocStoreBundle\AsyncUpload\Driver\OpenstackObjectStore\TempUrlOpenstackGenerator