| 1 | package Ceph::RadosGW::Admin; | ||||||||||||||
| 2 | 2 | $Ceph::RadosGW::Admin::VERSION = '0.4'; | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Ceph::RadosGW::Admin::HTTPRequest; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use URI::QueryParam; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use Ceph::RadosGW::Admin::User; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 NAME | ||||||||||||||
| 16 | |||||||||||||||
| 17 | Ceph::RadosGW::Admin - Bindings for the rados gateway admin api. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||
| 20 | |||||||||||||||
| 21 | version 0.4 | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | my $admin = Ceph::RadosGW::Admin->new( | ||||||||||||||
| 26 | access_key => 'not really secret', | ||||||||||||||
| 27 | secret_key => 'actually secret', | ||||||||||||||
| 28 | url => 'https://your.rados.gateway.com/', | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | my $user = $admin->create_user( | ||||||||||||||
| 32 | uid => 'myusername', | ||||||||||||||
| 33 | display_name => 'my user name', | ||||||||||||||
| 34 | ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | # they're really evil, suspending them should be enough | ||||||||||||||
| 37 | $user->suspended(1); | ||||||||||||||
| 38 | $user->save; | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # nah, they're really evil | ||||||||||||||
| 41 | $user->delete; | ||||||||||||||
| 42 | |||||||||||||||
| 43 | my $otheruser = $admin->get_user(uid => 'other'); | ||||||||||||||
| 44 | |||||||||||||||
| 45 | my @keys = $otheruser->keys(); | ||||||||||||||
| 46 | my @keys_plus_one = $otheruser->create_key(); | ||||||||||||||
| 47 | |||||||||||||||
| 48 | $otheruser->delete_key(access_key => $keys[0]->{access_key}); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | my @buckets = $otheruser->get_bucket_info(); | ||||||||||||||
| 51 | |||||||||||||||
| 52 | |||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||
| 54 | |||||||||||||||
| 55 | This module provides an interface to the | ||||||||||||||
| 56 | L<Admin OPs|http://docs.ceph.com/docs/master/radosgw/adminops/> interface of a | ||||||||||||||
| 57 | ceph rados gateway. It is at this time incomplete, with only the parts needed | ||||||||||||||
| 58 | by the authors implemented. Patches for the rest of the functionality are | ||||||||||||||
| 59 | encouraged. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =cut | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 2 | has secret_key => ( is => 'ro', required => 1 ); | |||||||||||||
| 64 | 2 | has access_key => ( is => 'ro', required => 1 ); | |||||||||||||
| 65 | 2 | has url => ( is => 'ro', required => 1 ); | |||||||||||||
| 66 | 2 | has useragent => ( | |||||||||||||
| 67 | is => 'ro', | ||||||||||||||
| 68 | builder => 'build_useragent', | ||||||||||||||
| 69 | ); | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 72 | |||||||||||||||
| 73 | =head1 METHODS | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =head2 get_user | ||||||||||||||
| 76 | |||||||||||||||
| 77 | Returns a L<Ceph::RadosGW::Admin::User> object representing the given C<uid>. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Dies if the user does not exist. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | Example: | ||||||||||||||
| 82 | |||||||||||||||
| 83 | my $user = $admin->get_user(uid => 'someuserhere'); | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =cut | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 22 | ▶ | sub get_user { | ||||||||||||
|
Subroutine
get_user: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 88 | 22 | my ($self, %args) = @_; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 22 | my %user_data = $self->_request(GET => 'user', %args); | |||||||||||||
| 91 | |||||||||||||||
| 92 | 19 | return Ceph::RadosGW::Admin::User->new( | |||||||||||||
| 93 | %user_data, | ||||||||||||||
| 94 | _client => $self | ||||||||||||||
| 95 | ); | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =head2 create_user | ||||||||||||||
| 99 | |||||||||||||||
| 100 | Makes a new user on the rados gateway, and returns a | ||||||||||||||
| 101 | L<Ceph::RadosGW::Admin::User> object representing that user. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | Dies on failure. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | Example: | ||||||||||||||
| 106 | |||||||||||||||
| 107 | my $new_user = $admin->create_user( | ||||||||||||||
| 108 | uid => 'username', | ||||||||||||||
| 109 | display_name => 'Our New User', | ||||||||||||||
| 110 | ); | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =cut | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 9 | ▶ | sub create_user { | ||||||||||||
|
Subroutine
create_user: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 115 | 9 | my ($self, %args) = @_; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 9 | my %user_data = $self->_request(PUT => 'user', %args); | |||||||||||||
| 118 | |||||||||||||||
| 119 | 9 | return Ceph::RadosGW::Admin::User->new( | |||||||||||||
| 120 | %user_data, | ||||||||||||||
| 121 | _client => $self | ||||||||||||||
| 122 | ); | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 15 | ▶ | sub build_useragent { | ||||||||||||
|
Subroutine
build_useragent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 126 | 15 | require LWP::UserAgent; | |||||||||||||
| 127 | 15 | return LWP::UserAgent->new; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 52 | ▶ | sub _debug { | ||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=10.5
| |||||||||||||||
| 131 | 52 | ▶ | if ($ENV{DEBUG_CEPH_CALLS}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | require Data::Dumper; | |||||||||||||
| 133 | 0 | warn Data::Dumper::Dumper(@_); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 52 | ▶ | sub _request { | ||||||||||||
|
Subroutine
_request: called CC=6 SCAR=18.1
| |||||||||||||||
| 138 | 52 | my ($self, $method, $path, %args) = @_; | |||||||||||||
| 139 | |||||||||||||||
| 140 | 52 | my $content = ''; | |||||||||||||
| 141 | |||||||||||||||
| 142 | 52 | my $query_string = _make_query(%args, format => 'json'); | |||||||||||||
| 143 | |||||||||||||||
| 144 | 52 | my $request_builder = Ceph::RadosGW::Admin::HTTPRequest->new( | |||||||||||||
| 145 | method => $method, | ||||||||||||||
| 146 | path => "admin/$path?$query_string", | ||||||||||||||
| 147 | content => '', | ||||||||||||||
| 148 | url => $self->url, | ||||||||||||||
| 149 | access_key => $self->access_key, | ||||||||||||||
| 150 | secret_key => $self->secret_key, | ||||||||||||||
| 151 | ); | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 52 | my $req = $request_builder->http_request(); | |||||||||||||
| 154 | |||||||||||||||
| 155 | 52 | my $res = $self->useragent->request($req); | |||||||||||||
| 156 | |||||||||||||||
| 157 | 52 | _debug($res); | |||||||||||||
| 158 | |||||||||||||||
| 159 | 52 | ▶ | unless ($res->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 3 | die sprintf("%s - %s (%s)", $res->status_line, $res->content, $req->as_string); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 49 | ▶ | if ($res->content) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 32 | my $data = eval { | |||||||||||||
| 165 | 32 | JSON::decode_json($res->content); | |||||||||||||
| 166 | }; | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 32 | ▶ | if (my $e = $@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | die "Could not deserialize server response: $e\nContent: " . $res->content . "\n"; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 32 | ▶ | if (ref($data) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 173 | 30 | return %$data; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | elsif (ref($data) eq 'ARRAY') { | ||||||||||||||
| 176 | 2 | return @$data; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | else { | ||||||||||||||
| 179 | 0 | die "Didn't get an array or hash reference\n"; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | } else { | ||||||||||||||
| 182 | 17 | return; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | 52 | ▶ | sub _make_query { | ||||||||||||
|
Subroutine
_make_query: called CC=3 SCAR=11.0
| |||||||||||||||
| 187 | 52 | my %args = @_; | |||||||||||||
| 188 | |||||||||||||||
| 189 | 52 | my %fixed; | |||||||||||||
| 190 | 52 | ▶ | while (my ($key, $val) = each %args) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | 121 | $key =~ s/_/-/g; | |||||||||||||
| 192 | 121 | $fixed{$key} = $val; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | 52 | my $u = URI->new("", "http"); | |||||||||||||
| 196 | |||||||||||||||
| 197 | 52 | foreach my $key (sort keys %fixed) { | |||||||||||||
| 198 | 121 | $u->query_param($key, $fixed{$key}); | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | |||||||||||||||
| 201 | |||||||||||||||
| 202 | 52 | return $u->query; | |||||||||||||
| 203 | |||||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | |||||||||||||||
| 207 | =head1 TODO | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =over 2 | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =item * | ||||||||||||||
| 212 | |||||||||||||||
| 213 | The docs are pretty middling at the moment. | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =item * | ||||||||||||||
| 216 | |||||||||||||||
| 217 | This module has only been tested against the Dumpling release of ceph. | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =back | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =head1 AUTHORS | ||||||||||||||
| 222 | |||||||||||||||
| 223 | Chris Reinhardt | ||||||||||||||
| 224 | crein@cpan.org | ||||||||||||||
| 225 | |||||||||||||||
| 226 | Mark Ng | ||||||||||||||
| 227 | cpan@markng.co.uk | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =head1 COPYRIGHT | ||||||||||||||
| 230 | |||||||||||||||
| 231 | This program is free software; you can redistribute | ||||||||||||||
| 232 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 233 | |||||||||||||||
| 234 | The full text of the license can be found in the | ||||||||||||||
| 235 | LICENSE file included with this module. | ||||||||||||||
| 236 | |||||||||||||||
| 237 | =head1 SEE ALSO | ||||||||||||||
| 238 | |||||||||||||||
| 239 | perl(1), L<Admin OPs API|http://docs.ceph.com/docs/master/radosgw/adminops/> | ||||||||||||||
| 240 | L<Ceph|http://www.ceph.com/> | ||||||||||||||
| 241 | |||||||||||||||
| 242 | =cut | ||||||||||||||
| 243 | |||||||||||||||
| 244 | |||||||||||||||
| 245 | 2 | 1; | |||||||||||||
| 246 | __END__ | ||||||||||||||