Ovirt/Storage.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 209 CC 28 SCAR 44.9
CC
28
Cov
0%
get_storage_by_element_id
110.0
get_storage_by_self_id
110.0
list
90.0
CRAP 89.1
1 package Ovirt::Storage;
2  
3 use v5.10;
4 use Carp;
5 use Moo;
6  
7 with 'Ovirt';
8 our $VERSION = '0.01';
9  
10 =head1 NAME
11  
12 Ovirt::Storage - Bindings for oVirt Storage API
13  
14 =head1 VERSION
15  
16 Version 0.01
17  
18 =cut
19  
20 =head1 SYNOPSIS
21  
22 use Ovirt::Storage;
23  
24 my %con = (
25 username => 'admin',
26 password => 'password',
27 manager => 'ovirt-mgr.example.com',
28 storage_output_attrs => 'id,name,type,storage_type', # optional
29 );
30  
31 my $storage = Ovirt::Storage->new(%con);
32  
33 # return xml output
34 print $storage->list_xml;
35
36 # list storage attributes
37 print $storage->list;
38  
39 # the output also available in hash
40 # for example to print all storage name
41 my $hash = $storage->hash_output;
42 for my $array (keys $hash->{storage}) {
43 print $hash->{storage}[$array]->{name};
44 }
45
46 # we can also specify specific storage 'id' when initiating an object
47 # so we can direct access the element for specific storage
48 print $storage->hash_output->{id};
49 print $storage->hash_output->{name};
50  
51 =head1 Attributes
52  
53 Other attributes is also inherited from Ovirt.pm
54 Check 'perldoc Ovirt' for detail
55
56 notes :
57 ro = read only, can be specified only during initialization
58 rw = read write, user can set this attribute
59 rwp = read write protected, for internal class
60
61 storage_url = (ro) store default storage url path
62 storage_output_attrs = (rw) store storage attributes to be returned, default is (id, name, state, description)
63 supported attributes :
64 id name
65 type storage_type
66 used available
67 commited storage_format
68
69 storage_output_delimiter = (rw) specify output delimiter between attribute, default is '||'
70 =cut
71  
72 has 'storage_url' => ( is => 'ro', default => '/api/storagedomains' );
73 has 'storage_output_attrs' => ( is => 'rw', default => 'id,name,type,storage_type',
74 isa => sub {
75 # store all output attribute into array split by ','
76 # $_[0] is the arguments spefied during initialization
77 my @attrs = split ',' => $_[0];
78
79 croak "storage_output_attrs can't be empty"
80 unless @attrs;
81
82 # check if provided attribute is valid / supported
83 my @supported_attr = qw |
84 id name
85 type storage_type
86 used available
87 commited storage_format
88 |;
89 for my $attr (@attrs) {
90 $attr = lc ($attr);
91 $attr = Ovirt->trim($attr);
92 croak "Attribute $attr is not valid / supported"
93 unless grep { /\b$attr\b/ } @supported_attr;
94 }
95 });
96
97 has 'storage_output_delimiter' => ( is => 'rw', default => '||' );
98  
99 =head1 SUBROUTINES/METHODS
100  
101 =head2 BUILD
102  
103 The Constructor, build logging, call pass_log_obj method
104 Built root_url with storage_url
105 set output with get_api_response method from Ovirt.pm
106 =cut
107  
108 sub BUILD {
109 my $self = shift;
110
111 $self->pass_log_obj;
112
113 if ($self->id) {
114 $self->_set_root_url($self->storage_url. '/' . $self->id);
115 }
116 else {
117 $self->_set_root_url($self->storage_url);
118 }
119
120 $self->get_api_response();
121 }
122  
123 =head2 list
124  
125 return storage's attributes text output from hash_output attribute
126 if no argument spesified, it will return all storage attributes (based on storage_output_attrs)
127 argument supported is 'storage id'
128 example :
129 $storage->list('c4738b0f-b73d-4a66-baa8-2ba465d63132');
130 =cut
131  
132 sub list {
133 my $self = shift;
134
135 my $storageid = shift || undef;
136
137 # store the output and return it at the end
138 my $output;
139
140 # store each attribute to array to be looped
141 my @attrs = split ',' => $self->storage_output_attrs;
142
143 # store the last element to escape the storage_output_delimeter
144 my $last_element = pop (@attrs);
145 $self->log->debug("last element = $last_element");
146
147 # if the id is defined during initialization
148 # the rest api output will only contain attributes for this id
149 # so it's not necessary to loop on storage element
150 if ($self->id) {
151 for my $attr (@attrs) {
152 $self->log->debug("requesting attribute $attr");
153
154 my $attr_output = $self->get_storage_by_self_id($attr) || $self->not_available;
155 $output .= $attr_output . $self->storage_output_delimiter;
156 $self->log->debug("output for attribute $attr = " . $attr_output);
157 }
158
159 #handle last element or the only element
160 $self->log->debug("requesting attribute $last_element");
161
162 if (my $last_output = $self->get_storage_by_self_id($last_element) || $self->not_available) {
163 $output .= $last_output;
164 $self->log->debug("output for attribute $last_element = " . $last_output);
165 }
166
167 $output .= "\n";
168 }
169 elsif ($storageid) {
170 #store storageid element
171 my $storageid_element;
172
173 $storageid = $self->trim($storageid);
174
175 for my $element_id ( 0 .. $#{ $self->hash_output->{storage_domain} } ) {
176 next unless $self->hash_output->{storage}[$element_id]->{id} eq $storageid;
177
178 $storageid_element = $element_id;
179 }
180
181 croak "storage id not found" unless $storageid_element >= 0;
182
183 for my $attr (@attrs) {
184 $self->log->debug("requesting attribute $attr for element $storageid_element");
185
186 my $attr_output = $self->get_storage_by_element_id($storageid_element, $attr) || $self->not_available;
187 $output .= $attr_output . $self->storage_output_delimiter;
188 $self->log->debug("output for attribute $attr element $storageid_element = " . $attr_output);
189 }
190
191 #handle last element or the only element
192 $self->log->debug("requesting attribute $last_element for element $storageid_element");
193
194 if (my $last_output = $self->get_storage_by_element_id($storageid_element, $last_element) || $self->not_available) {
195 $output .= $last_output;
196 $self->log->debug("output for attribute $last_element element $storageid_element = " . $last_output);
197 }
198
199 $output .= "\n";
200 }
201 else {
202
203 for my $element_id ( 0 .. $#{ $self->hash_output->{storage_domain} } ) {
204
205 # in case there's no any element left, the last element become the only attribute requested
206 if (@attrs) {
207 for my $attr (@attrs) {
208
209 $self->log->debug("requesting attribute $attr for element $element_id");
210
211 my $attr_output = $self->get_storage_by_element_id($element_id, $attr) || $self->not_available;
212 $output .= $attr_output . $self->storage_output_delimiter;
213 $self->log->debug("output for attribute $attr element $element_id = " . $attr_output);
214 }
215 }
216
217 #handle last element or the only element
218 $self->log->debug("requesting attribute $last_element for element $element_id");
219
220 if (my $last_output = $self->get_storage_by_element_id($element_id, $last_element) || $self->not_available) {
221 $output .= $last_output;
222 $self->log->debug("output for attribute $last_element element $element_id = " . $last_output);
223 }
224
225 $output .= "\n";
226 }
227 }
228
229 return $output;
230 }
231  
232 =head2 get_storage_by_element_id
233
234 This method is used by list method to list all storage attributes requested
235 An array element id and attribute name is required
236 =cut
237  
238 sub get_storage_by_element_id {
239 my $self = shift;
240
241 my ($element_id, $attr) = @_;
242
243 croak "hash output is not defined"
244 unless $self->hash_output;
245
246 $attr = $self->trim($attr);
247 $self->log->debug("element id = $element_id, attribute = $attr");
248
249 if ($attr eq 'id') {
250 return $self->hash_output->{storage_domain}[$element_id]->{id};
251 }
252 elsif ($attr eq 'name') {
253 return $self->hash_output->{storage_domain}[$element_id]->{name};
254 }
255 elsif ($attr eq 'type') {
256 return $self->hash_output->{storage_domain}[$element_id]->{type};
257 }
258 elsif ($attr eq 'storage_type') {
259 return $self->hash_output->{storage_domain}[$element_id]->{storage}->{type};
260 }
261 elsif ($attr eq 'available') {
262 return $self->hash_output->{storage_domain}[$element_id]->{available};
263 }
264 elsif ($attr eq 'storage_format') {
265 return $self->hash_output->{storage_domain}[$element_id]->{storage_format};
266 }
267 elsif ($attr eq 'used') {
268 return $self->hash_output->{storage_domain}[$element_id]->{used};
269 }
270 elsif ($attr eq 'commited') {
271 return $self->hash_output->{storage_domain}[$element_id]->{commited};
272 }
273 }
274  
275 =head2 get_storage_by_self_id
276
277 This method is used by list method if $self->id is defined
278 The id is set during initialization (id => 'storageid')
279 attribute name is required
280 =cut
281  
282 sub get_storage_by_self_id {
283 my $self = shift;
284
285 my $attr = shift;
286
287 croak "hash output is not defined"
288 unless $self->hash_output;
289
290 $attr = $self->trim($attr);
291 $self->log->debug("attribute = $attr");
292
293 if ($attr eq 'id') {
294 return $self->hash_output->{id};
295 }
296 elsif ($attr eq 'name') {
297 return $self->hash_output->{name};
298 }
299 elsif ($attr eq 'type') {
300 return $self->hash_output->{type};
301 }
302 elsif ($attr eq 'storage_type') {
303 return $self->hash_output->{storage}->{type};
304 }
305 elsif ($attr eq 'available') {
306 return $self->hash_output->{available};
307 }
308 elsif ($attr eq 'storage_format') {
309 return $self->hash_output->{storage_format};
310 }
311 elsif ($attr eq 'used') {
312 return $self->hash_output->{used};
313 }
314 elsif ($attr eq 'commited') {
315 return $self->hash_output->{commited};
316 }
317 }
318  
319 =head1 AUTHOR
320  
321 "Heince Kurniawan", C<< <"heince at cpan.org"> >>
322  
323 =head1 BUGS
324  
325 Please report any bugs or feature requests to C<bug-ovirt at rt.cpan.org>, or through
326 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Ovirt::Storage>. I will be notified, and then you'll
327 automatically be notified of progress on your bug as I make changes.
328  
329 =head1 SUPPORT
330  
331 You can find documentation for this module with the perldoc command.
332  
333 perldoc Ovirt::Storage
334  
335 You can also look for information at:
336  
337 =head1 ACKNOWLEDGEMENTS
338  
339  
340 =head1 LICENSE AND COPYRIGHT
341  
342 Copyright 2015 "Heince Kurniawan".
343  
344 This program is free software; you can redistribute it and/or modify it
345 under the terms of the the Artistic License (2.0). You may obtain a
346 copy of the full license at:
347  
348 L<http://www.perlfoundation.org/artistic_license_2_0>
349  
350 Any use, modification, and distribution of the Standard or Modified
351 Versions is governed by this Artistic License. By using, modifying or
352 distributing the Package, you accept this license. Do not use, modify,
353 or distribute the Package, if you do not accept this license.
354  
355 If your Modified Version has been derived from a Modified Version made
356 by someone other than you, you are nevertheless required to ensure that
357 your Modified Version complies with the requirements of this license.
358  
359 This license does not grant you the right to use any trademark, service
360 mark, tradename, or logo of the Copyright Holder.
361  
362 This license includes the non-exclusive, worldwide, free-of-charge
363 patent license to make, have made, use, offer to sell, sell, import and
364 otherwise transfer the Package with respect to any patent claims
365 licensable by the Copyright Holder that are necessarily infringed by the
366 Package. If you institute patent litigation (including a cross-claim or
367 counterclaim) against any party alleging that the Package constitutes
368 direct or contributory patent infringement, then this Artistic License
369 to you shall terminate on the date that such litigation is filed.
370  
371 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
372 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
373 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
374 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
375 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
376 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
377 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
378 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
379  
380  
381 =cut
382  
383 1;