File Coverage

blib/lib/Ovirt/Cluster.pm
Criterion Covered Total %
statement 9 97 9.2
branch 0 62 0.0
condition 0 20 0.0
subroutine 3 8 37.5
pod n/a
total 12 187 6.4


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