mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
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.
This commit is contained in:
parent
82ca321715
commit
45e1ce034a
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\AsyncUpload\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class ConfigureOpenstackObjectStorageCommand extends Command
|
||||
{
|
||||
private readonly string $basePath;
|
||||
|
||||
private readonly string $tempUrlKey;
|
||||
|
||||
public function __construct(private readonly HttpClientInterface $client, ParameterBagInterface $parameterBag)
|
||||
{
|
||||
$config = $parameterBag->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;
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AsyncUpload\Command;
|
||||
|
||||
use Chill\DocStoreBundle\AsyncUpload\Command\ConfigureOpenstackObjectStorageCommand;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ConfigureOpenstackObjectStorageCommandTest extends TestCase
|
||||
{
|
||||
public function testRun(): void
|
||||
{
|
||||
$client = new MockHttpClient(function ($method, $url, $options): MockResponse {
|
||||
self::assertSame('POST', $method);
|
||||
self::assertSame($url, 'https://object.store.example/v1/AUTH/container');
|
||||
|
||||
$headers = $options['headers'];
|
||||
|
||||
self::assertContains('X-Auth-Token: abc', $headers);
|
||||
self::assertContains('X-Container-Meta-Temp-URL-Key: 12345679801234567890', $headers);
|
||||
self::assertContains('X-Container-Meta-Access-Control-Allow-Origin: https://chill.domain.social https://chill2.domain.social', $headers);
|
||||
|
||||
return new MockResponse('', ['http_code' => 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user