Ovirt/Host.pm untested

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