File Coverage

blib/lib/Ovirt/Template.pm
Criterion Covered Total %
statement 9 111 8.1
branch 0 90 0.0
condition 0 20 0.0
subroutine 3 8 37.5
pod n/a
total 12 229 5.2


line stmt bran cond sub pod time code
1             package Ovirt::Template;
2              
3 1     1   9131 use v5.10;
  1         3  
  1         41  
4 1     1   4 use Carp;
  1         1  
  1         58  
5 1     1   5 use Moo;
  1         2  
  1         5  
6              
7             with 'Ovirt';
8             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             has 'template_url' => ( is => 'ro', default => '/api/templates' );
78             has 'template_output_attrs' => ( is => 'rw', default => 'id,name,description',
79             isa => sub {
80             # store all output attribute into array split by ','
81             # $_[0] is the arguments spefied during initialization
82             my @attrs = split ',' => $_[0];
83            
84             croak "template_output_attrs can't be empty"
85             unless @attrs;
86            
87             # check if provided attribute is valid / supported
88             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             for my $attr (@attrs) {
100             $attr = lc ($attr);
101             $attr = Ovirt->trim($attr);
102             croak "Attribute $attr is not valid / supported"
103             unless grep { /\b$attr\b/ } @supported_attr;
104             }
105             });
106            
107             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             sub BUILD {
119 0     0     my $self = shift;
120            
121 0           $self->pass_log_obj;
122            
123 0 0         if ($self->id) {
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_xml
134              
135             return xml output
136             =cut
137              
138             sub list_xml {
139 0     0     my $self = shift;
140            
141 0           return $self->xml_output;
142             }
143              
144             =head2 list
145              
146             return template's attributes text output from hash_output attribute
147             if no argument spesified, it will return all template attributes (based on template_output_attrs)
148             argument supported is 'template id'
149             example :
150             $template->list('c4738b0f-b73d-4a66-baa8-2ba465d63132');
151             =cut
152              
153             sub list {
154 0     0     my $self = shift;
155            
156 0   0       my $templateid = shift || undef;
157            
158             # store the output and return it at the end
159 0           my $output;
160            
161             # store each attribute to array to be looped
162 0           my @attrs = split ',' => $self->template_output_attrs;
163            
164             # store the last element to escape the template_output_delimeter
165 0           my $last_element = pop (@attrs);
166 0           $self->log->debug("last element = $last_element");
167            
168             # if the id is defined during initialization
169             # the rest api output will only contain attributes for this id
170             # so it's not necessary to loop on template element
171 0 0         if ($self->id) {
    0          
172 0           for my $attr (@attrs) {
173 0           $self->log->debug("requesting attribute $attr");
174            
175 0   0       my $attr_output = $self->get_template_by_self_id($attr) || $self->not_available;
176 0           $output .= $attr_output . $self->template_output_delimiter;
177 0           $self->log->debug("output for attribute $attr = " . $attr_output);
178             }
179            
180             #handle last element or the only element
181 0           $self->log->debug("requesting attribute $last_element");
182            
183 0 0 0       if (my $last_output = $self->get_template_by_self_id($last_element) || $self->not_available) {
184 0           $output .= $last_output;
185 0           $self->log->debug("output for attribute $last_element = " . $last_output);
186             }
187            
188 0           $output .= "\n";
189             }
190             elsif ($templateid) {
191             #store templateid element
192 0           my $templateid_element;
193            
194 0           $templateid = $self->trim($templateid);
195            
196 0           for my $element_id ( 0 .. $#{ $self->hash_output->{template} } ) {
  0            
197 0 0         next unless $self->hash_output->{template}[$element_id]->{id} eq $templateid;
198            
199 0           $templateid_element = $element_id;
200             }
201            
202 0 0         croak "template id not found" unless $templateid_element >= 0;
203            
204 0           for my $attr (@attrs) {
205 0           $self->log->debug("requesting attribute $attr for element $templateid_element");
206            
207 0   0       my $attr_output = $self->get_template_by_element_id($templateid_element, $attr) || $self->not_available;
208 0           $output .= $attr_output . $self->template_output_delimiter;
209 0           $self->log->debug("output for attribute $attr element $templateid_element = " . $attr_output);
210             }
211            
212             #handle last element or the only element
213 0           $self->log->debug("requesting attribute $last_element for element $templateid_element");
214            
215 0 0 0       if (my $last_output = $self->get_template_by_element_id($templateid_element, $last_element) || $self->not_available) {
216 0           $output .= $last_output;
217 0           $self->log->debug("output for attribute $last_element element $templateid_element = " . $last_output);
218             }
219            
220 0           $output .= "\n";
221             }
222             else {
223            
224 0           for my $element_id ( 0 .. $#{ $self->hash_output->{template} } ) {
  0            
225            
226             # in case there's no any element left, the last element become the only attribute requested
227 0 0         if (@attrs) {
228 0           for my $attr (@attrs) {
229            
230 0           $self->log->debug("requesting attribute $attr for element $element_id");
231            
232 0   0       my $attr_output = $self->get_template_by_element_id($element_id, $attr) || $self->not_available;
233 0           $output .= $attr_output . $self->template_output_delimiter;
234 0           $self->log->debug("output for attribute $attr element $element_id = " . $attr_output);
235             }
236             }
237            
238             #handle last element or the only element
239 0           $self->log->debug("requesting attribute $last_element for element $element_id");
240            
241 0 0 0       if (my $last_output = $self->get_template_by_element_id($element_id, $last_element) || $self->not_available) {
242 0           $output .= $last_output;
243 0           $self->log->debug("output for attribute $last_element element $element_id = " . $last_output);
244             }
245            
246 0           $output .= "\n";
247             }
248             }
249            
250 0           return $output;
251             }
252              
253             =head2 get_template_by_element_id
254            
255             This method is used by list method to list all template attributes requested
256             An array element id and attribute name is required
257             =cut
258              
259             sub get_template_by_element_id {
260 0     0     my $self = shift;
261            
262 0           my ($element_id, $attr) = @_;
263            
264 0 0         croak "hash output is not defined"
265             unless $self->hash_output;
266            
267 0           $attr = $self->trim($attr);
268 0           $self->log->debug("element id = $element_id, attribute = $attr");
269            
270 0 0         if ($attr eq 'id') {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
271 0           return $self->hash_output->{template}[$element_id]->{id};
272             }
273             elsif ($attr eq 'name') {
274 0           return $self->hash_output->{template}[$element_id]->{name};
275             }
276             elsif ($attr eq 'memory') {
277 0           return $self->hash_output->{template}[$element_id]->{memory};
278             }
279             elsif ($attr eq 'type') {
280 0           return $self->hash_output->{template}[$element_id]->{type};
281             }
282             elsif ($attr eq 'state') {
283 0           return $self->hash_output->{template}[$element_id]->{status}->{state};
284             }
285             elsif ($attr eq 'description') {
286 0           return $self->hash_output->{template}[$element_id]->{description};
287             }
288             elsif ($attr eq 'cpu_cores') {
289 0           return $self->hash_output->{template}[$element_id]->{cpu}->{topology}->{cores};
290             }
291             elsif ($attr eq 'cpu_sockets') {
292 0           return $self->hash_output->{template}[$element_id]->{cpu}->{topology}->{sockets};
293             }
294             elsif ($attr eq 'cpu_arch') {
295 0           return $self->hash_output->{template}[$element_id]->{cpu}->{architecture};
296             }
297             elsif ($attr eq 'cpu_shares') {
298 0           return $self->hash_output->{template}[$element_id]->{cpu_shares};
299             }
300             elsif ($attr eq 'os_type') {
301 0           return $self->hash_output->{template}[$element_id]->{os}->{type};
302             }
303             elsif ($attr eq 'boot_dev') {
304 0           return $self->hash_output->{template}[$element_id]->{os}->{boot}->{dev};
305             }
306             elsif ($attr eq 'ha_enabled') {
307 0           return $self->hash_output->{template}[$element_id]->{high_availability}->{enabled};
308             }
309             elsif ($attr eq 'ha_priority') {
310 0           return $self->hash_output->{template}[$element_id]->{high_availability}->{priority};
311             }
312             elsif ($attr eq 'display_type') {
313 0           return $self->hash_output->{template}[$element_id]->{display}->{type};
314             }
315             elsif ($attr eq 'cluster_id') {
316 0           return $self->hash_output->{template}[$element_id]->{cluster}->{id};
317             }
318             elsif ($attr eq 'creation_time') {
319 0           return $self->hash_output->{template}[$element_id]->{creation_time};
320             }
321             elsif ($attr eq 'usb_enabled') {
322 0           return $self->hash_output->{template}[$element_id]->{usb}->{enabled};
323             }
324             }
325              
326             =head2 get_template_by_self_id
327            
328             This method is used by list method if $self->id is defined
329             The id is set during initialization (id => 'templateid')
330             attribute name is required
331             =cut
332              
333             sub get_template_by_self_id {
334 0     0     my $self = shift;
335            
336 0           my $attr = shift;
337            
338 0 0         croak "hash output is not defined"
339             unless $self->hash_output;
340            
341 0           $attr = $self->trim($attr);
342 0           $self->log->debug("attribute = $attr");
343            
344 0 0         if ($attr eq 'id') {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
345 0           return $self->hash_output->{id};
346             }
347             elsif ($attr eq 'name') {
348 0           return $self->hash_output->{name};
349             }
350             elsif ($attr eq 'state') {
351 0           return $self->hash_output->{status}->{state};
352             }
353             elsif ($attr eq 'description') {
354 0           return $self->hash_output->{description};
355             }
356             elsif ($attr eq 'cpu_cores') {
357 0           return $self->hash_output->{cpu}->{topology}->{cores};
358             }
359             elsif ($attr eq 'cpu_sockets') {
360 0           return $self->hash_output->{cpu}->{topology}->{sockets};
361             }
362             elsif ($attr eq 'cpu_arch') {
363 0           return $self->hash_output->{cpu}->{architecture};
364             }
365             elsif ($attr eq 'cpu_shares') {
366 0           return $self->hash_output->{cpu_shares};
367             }
368             elsif ($attr eq 'os_type') {
369 0           return $self->hash_output->{os}->{type};
370             }
371             elsif ($attr eq 'boot_dev') {
372 0           return $self->hash_output->{os}->{boot}->{dev};
373             }
374             elsif ($attr eq 'ha_enabled') {
375 0           return $self->hash_output->{high_availability}->{enabled};
376             }
377             elsif ($attr eq 'ha_priority') {
378 0           return $self->hash_output->{high_availability}->{priority};
379             }
380             elsif ($attr eq 'display_type') {
381 0           return $self->hash_output->{display}->{type};
382             }
383             elsif ($attr eq 'cluster_id') {
384 0           return $self->hash_output->{cluster}->{id};
385             }
386             elsif ($attr eq 'creation_time') {
387 0           return $self->hash_output->{creation_time};
388             }
389             elsif ($attr eq 'usb_enabled') {
390 0           return $self->hash_output->{usb}->{enabled};
391             }
392             }
393              
394             =head1 AUTHOR
395              
396             "Heince Kurniawan", C<< <"heince at cpan.org"> >>
397              
398             =head1 BUGS
399              
400             Please report any bugs or feature requests to C, or through
401             the web interface at L. I will be notified, and then you'll
402             automatically be notified of progress on your bug as I make changes.
403              
404             =head1 SUPPORT
405              
406             You can find documentation for this module with the perldoc command.
407              
408             perldoc Ovirt::Template
409              
410             You can also look for information at:
411              
412             =head1 ACKNOWLEDGEMENTS
413              
414              
415             =head1 LICENSE AND COPYRIGHT
416              
417             Copyright 2015 "Heince Kurniawan".
418              
419             This program is free software; you can redistribute it and/or modify it
420             under the terms of the the Artistic License (2.0). You may obtain a
421             copy of the full license at:
422              
423             L
424              
425             Any use, modification, and distribution of the Standard or Modified
426             Versions is governed by this Artistic License. By using, modifying or
427             distributing the Package, you accept this license. Do not use, modify,
428             or distribute the Package, if you do not accept this license.
429              
430             If your Modified Version has been derived from a Modified Version made
431             by someone other than you, you are nevertheless required to ensure that
432             your Modified Version complies with the requirements of this license.
433              
434             This license does not grant you the right to use any trademark, service
435             mark, tradename, or logo of the Copyright Holder.
436              
437             This license includes the non-exclusive, worldwide, free-of-charge
438             patent license to make, have made, use, offer to sell, sell, import and
439             otherwise transfer the Package with respect to any patent claims
440             licensable by the Copyright Holder that are necessarily infringed by the
441             Package. If you institute patent litigation (including a cross-claim or
442             counterclaim) against any party alleging that the Package constitutes
443             direct or contributory patent infringement, then this Artistic License
444             to you shall terminate on the date that such litigation is filed.
445              
446             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
447             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
448             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
449             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
450             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
451             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
452             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
453             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
454              
455              
456             =cut
457              
458             1;