Ovirt/Template.pm

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