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