Si vous constatez la perte de tickets, de commentaires ou de projets, veuillez consulter la page https://status.framasoft.org/incident/626.

Verified Commit 478ad294 authored by Luc Didry's avatar Luc Didry

— Add Swift storage helpers

parent a2f76558
Pipeline #293211 failed with stage
in 1 minute and 6 seconds
......@@ -53,6 +53,25 @@ Return the native name of a language from its ISO639 code
Return a random string (\[a-zA-Z0-9\_\]) of $length characters
## swift
my $swift = $app->swift;
Return a Net::OpenStack::Swift object, configured from `$app-`config('swift')>
## check\_swift\_container
$app->check_swift_container();
Check if `$app-`config('swift')->{container}> exists. Tries to create it if it doesn’t exist.
## find\_swift\_container
my ($headers, $containers) = $c->swift->get_account(url => $storage_url, token => $token);
my $exists = $app->find_swift_container($containers);
Return a boolean indicating if `$app-`config('swift')->{container}> exists in the containers list returned by Net::OpenStack::Swift->get\_account();
# SEE ALSO
[Mojolicious](https://metacpan.org/pod/Mojolicious), [Mojolicious::Guides](https://metacpan.org/pod/Mojolicious::Guides), [https://mojolicious.org](https://mojolicious.org).
......@@ -7,7 +7,7 @@ use Mojo::Util qw(decode);
use Data::Entropy qw(entropy_source);
use ISO::639_1;
our $VERSION = '0.10';
our $VERSION = '0.11';
sub register {
my ($self, $app) = @_;
......@@ -18,6 +18,13 @@ sub register {
$app->helper(available_langs => \&_available_langs);
$app->helper(iso639_native_name => \&_iso639_native_name);
if ($app->config('swift')) {
use Net::OpenStack::Swift;
$app->helper(swift => \&_swift);
$app->helper(check_swift_container => \&_check_swift_container);
$app->helper(find_swift_container => \&_find_swift_container);
}
# Force language
my $cookie_name = $app->moniker.'_lang';
$app->hook(
......@@ -85,6 +92,37 @@ sub _shortener {
return $result;
}
sub _swift {
my $c = shift;
state $swift = Net::OpenStack::Swift->new($c->app->config('swift'));
return $swift;
}
sub _check_swift_container {
my $c = shift;
my ($storage_url, $token) = $c->swift->get_auth();
my ($headers, $containers) = $c->swift->get_account(url => $storage_url, token => $token);
unless ($c->find_swift_container($containers)) {
$c->swift->put_container(container_name => $c->app->config('swift')->{container});
my ($headers, $containers) = $c->swift->get_account(url => $storage_url, token => $token);
die sprintf("Swift container %s not found, and unable to create it.", $c->app->config('swift')->{container}) unless $c->find_swift_container($containers);
}
}
sub _find_swift_container {
my $c = shift;
my $containers = shift;
my $found_container = 0;
foreach my $container (@{$containers}) {
$found_container = 1 if $container->{name} eq $c->app->config('swift')->{container};
}
return $found_container;
}
1;
__END__
......@@ -145,6 +183,25 @@ Return the native name of a language from its ISO639 code
Return a random string ([a-zA-Z0-9_]) of $length characters
=head2 swift
my $swift = $app->swift;
Return a Net::OpenStack::Swift object, configured from C<$app->config('swift')>
=head2 check_swift_container
$app->check_swift_container();
Check if C<$app->config('swift')->{container}> exists. Tries to create it if it doesn’t exist.
=head2 find_swift_container
my ($headers, $containers) = $c->swift->get_account(url => $storage_url, token => $token);
my $exists = $app->find_swift_container($containers);
Return a boolean indicating if C<$app->config('swift')->{container}> exists in the containers list returned by Net::OpenStack::Swift->get_account();
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment