File Coverage

blib/lib/Ovirt/Cluster.pm
Criterion Covered Total %
statement 8 94 8.5
branch 0 62 0.0
condition 0 20 0.0
subroutine 3 7 42.8
pod n/a
total 11 183 6.0


line stmt bran cond sub pod time code
1             package Ovirt::Cluster;
2              
3 1     1   6826 use v5.10;
  1         4  
4 1     1   3 use Carp;
  1         1  
  1         47  
5 1     1   3 use Moo;
  1         1  
  1         4  
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
126              
127             return cluster's attributes text output from hash_output attribute
128             if no argument specified, it will return all cluster attributes (based on cluster_output_attrs)
129             argument supported is 'cluster id'
130             example :
131             $cluster->list('c4738b0f-b73d-4a66-baa8-2ba465d63132');
132             =cut
133              
134             sub list {
135 0     0     my $self = shift;
136            
137 0   0       my $clusterid = shift || undef;
138            
139             # store the output and return it at the end
140 0           my $output;
141            
142             # store each attribute to array to be looped
143 0           my @attrs = split ',' => $self->cluster_output_attrs;
144            
145             # store the last element to escape the cluster_output_delimeter
146 0           my $last_element = pop (@attrs);
147 0           $self->log->debug("last element = $last_element");
148            
149             # if the id is defined during initialization
150             # the rest api output will only contain attributes for this id
151             # so it's not necessary to loop on cluster element
152 0 0         if ($self->id) {
    0          
153 0           for my $attr (@attrs) {
154 0           $self->log->debug("requesting attribute $attr");
155            
156 0   0       my $attr_output = $self->get_cluster_by_self_id($attr) || $self->not_available;
157 0           $output .= $attr_output . $self->cluster_output_delimiter;
158 0           $self->log->debug("output for attribute $attr = " . $attr_output);
159             }
160            
161             #handle last element or the only element
162 0           $self->log->debug("requesting attribute $last_element");
163            
164 0 0 0       if (my $last_output = $self->get_cluster_by_self_id($last_element) || $self->not_available) {
165 0           $output .= $last_output;
166 0           $self->log->debug("output for attribute $last_element = " . $last_output);
167             }
168            
169 0           $output .= "\n";
170             }
171             elsif ($clusterid) {
172             #store clusterid element
173 0           my $clusterid_element;
174            
175 0           $clusterid = $self->trim($clusterid);
176            
177 0           for my $element_id ( 0 .. $#{ $self->hash_output->{cluster} } ) {
  0            
178 0 0         next unless $self->hash_output->{cluster}[$element_id]->{id} eq $clusterid;
179            
180 0           $clusterid_element = $element_id;
181             }
182            
183 0 0         croak "cluster id not found" unless $clusterid_element >= 0;
184            
185 0           for my $attr (@attrs) {
186 0           $self->log->debug("requesting attribute $attr for element $clusterid_element");
187            
188 0   0       my $attr_output = $self->get_cluster_by_element_id($clusterid_element, $attr) || $self->not_available;
189 0           $output .= $attr_output . $self->cluster_output_delimiter;
190 0           $self->log->debug("output for attribute $attr element $clusterid_element = " . $attr_output);
191             }
192            
193             #handle last element or the only element
194 0           $self->log->debug("requesting attribute $last_element for element $clusterid_element");
195            
196 0 0 0       if (my $last_output = $self->get_cluster_by_element_id($clusterid_element, $last_element) || $self->not_available) {
197 0           $output .= $last_output;
198 0           $self->log->debug("output for attribute $last_element element $clusterid_element = " . $last_output);
199             }
200            
201 0           $output .= "\n";
202             }
203             else {
204            
205 0           for my $element_id ( 0 .. $#{ $self->hash_output->{vm} } ) {
  0            
206            
207             # in case there's no any element left, the last element become the only attribute requested
208 0 0         if (@attrs) {
209 0           for my $attr (@attrs) {
210            
211 0           $self->log->debug("requesting attribute $attr for element $element_id");
212            
213 0   0       my $attr_output = $self->get_cluster_by_element_id($element_id, $attr) || $self->not_available;
214 0           $output .= $attr_output . $self->cluster_output_delimiter;
215 0           $self->log->debug("output for attribute $attr element $element_id = " . $attr_output);
216             }
217             }
218            
219             #handle last element or the only element
220 0           $self->log->debug("requesting attribute $last_element for element $element_id");
221            
222 0 0 0       if (my $last_output = $self->get_cluster_by_element_id($element_id, $last_element) || $self->not_available) {
223 0           $output .= $last_output;
224 0           $self->log->debug("output for attribute $last_element element $element_id = " . $last_output);
225             }
226            
227 0           $output .= "\n";
228             }
229             }
230            
231 0           return $output;
232             }
233              
234             =head2 get_cluster_by_element_id
235            
236             This method is used by list method to list all cluster attributes requested
237             An array element id and attribute name is required
238             =cut
239              
240             sub get_cluster_by_element_id {
241 0     0     my $self = shift;
242            
243 0           my ($element_id, $attr) = @_;
244            
245 0 0         croak "hash output is not defined"
246             unless $self->hash_output;
247            
248 0           $attr = $self->trim($attr);
249 0           $self->log->debug("element id = $element_id, attribute = $attr");
250            
251 0 0         if ($attr eq 'id') {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
252 0           return $self->hash_output->{cluster}[$element_id]->{id};
253             }
254             elsif ($attr eq 'name') {
255 0           return $self->hash_output->{cluster}[$element_id]->{name};
256             }
257             elsif ($attr eq 'description') {
258 0           return $self->hash_output->{cluster}[$element_id]->{description};
259             }
260             elsif ($attr eq 'cpu_arch') {
261 0           return $self->hash_output->{cluster}[$element_id]->{cpu}->{architecture};
262             }
263             elsif ($attr eq 'cpu_id') {
264 0           return $self->hash_output->{cluster}[$element_id]->{cpu}->{id};
265             }
266             elsif ($attr eq 'datacenter_id') {
267 0           return $self->hash_output->{cluster}[$element_id]->{data_center}->{id};
268             }
269             elsif ($attr eq 'sched_name') {
270 0           return $self->hash_output->{cluster}[$element_id]->{scheduling_policy}->{name};
271             }
272             elsif ($attr eq 'sched_policy') {
273 0           return $self->hash_output->{cluster}[$element_id]->{scheduling_policy}->{policy};
274             }
275             elsif ($attr eq 'ver_major') {
276 0           return $self->hash_output->{cluster}[$element_id]->{version}->{major};
277             }
278             elsif ($attr eq 'ver_minor') {
279 0           return $self->hash_output->{cluster}[$element_id]->{version}->{minor};
280             }
281             }
282              
283             =head2 get_cluster_by_self_id
284            
285             This method is used by list method if $self->id is defined
286             The id is set during initialization (id => 'clusterid')
287             attribute name is required
288             =cut
289              
290             sub get_cluster_by_self_id {
291 0     0     my $self = shift;
292            
293 0           my $attr = shift;
294            
295 0 0         croak "hash output is not defined"
296             unless $self->hash_output;
297            
298 0           $attr = $self->trim($attr);
299 0           $self->log->debug("attribute = $attr");
300            
301 0 0         if ($attr eq 'id') {
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
302 0           return $self->hash_output->{id};
303             }
304             elsif ($attr eq 'name') {
305 0           return $self->hash_output->{name};
306             }
307             elsif ($attr eq 'description') {
308 0           return $self->hash_output->{description};
309             }
310             elsif ($attr eq 'cpu_arch') {
311 0           return $self->hash_output->{cpu}->{architecture};
312             }
313             elsif ($attr eq 'cpu_id') {
314 0           return $self->hash_output->{cpu}->{id};
315             }
316             elsif ($attr eq 'datacenter_id') {
317 0           return $self->hash_output->{data_center}->{id};
318             }
319             elsif ($attr eq 'sched_name') {
320 0           return $self->hash_output->{scheduling_policy}->{name};
321             }
322             elsif ($attr eq 'sched_policy') {
323 0           return $self->hash_output->{scheduling_policy}->{policy};
324             }
325             elsif ($attr eq 'ver_major') {
326 0           return $self->hash_output->{version}->{major};
327             }
328             elsif ($attr eq 'ver_minor') {
329 0           return $self->hash_output->{version}->{minor};
330             }
331             }
332              
333             =head1 AUTHOR
334              
335             "Heince Kurniawan", C<< <"heince at cpan.org"> >>
336              
337             =head1 BUGS
338              
339             Please report any bugs or feature requests to C, or through
340             the web interface at L. I will be notified, and then you'll
341             automatically be notified of progress on your bug as I make changes.
342              
343             =head1 SUPPORT
344              
345             You can find documentation for this module with the perldoc command.
346              
347             perldoc Ovirt::Cluster
348              
349             You can also look for information at:
350              
351             =head1 ACKNOWLEDGEMENTS
352              
353              
354             =head1 LICENSE AND COPYRIGHT
355              
356             Copyright 2015 "Heince Kurniawan".
357              
358             This program is free software; you can redistribute it and/or modify it
359             under the terms of the the Artistic License (2.0). You may obtain a
360             copy of the full license at:
361              
362             L
363              
364             Any use, modification, and distribution of the Standard or Modified
365             Versions is governed by this Artistic License. By using, modifying or
366             distributing the Package, you accept this license. Do not use, modify,
367             or distribute the Package, if you do not accept this license.
368              
369             If your Modified Version has been derived from a Modified Version made
370             by someone other than you, you are nevertheless required to ensure that
371             your Modified Version complies with the requirements of this license.
372              
373             This license does not grant you the right to use any trademark, service
374             mark, tradename, or logo of the Copyright Holder.
375              
376             This license includes the non-exclusive, worldwide, free-of-charge
377             patent license to make, have made, use, offer to sell, sell, import and
378             otherwise transfer the Package with respect to any patent claims
379             licensable by the Copyright Holder that are necessarily infringed by the
380             Package. If you institute patent litigation (including a cross-claim or
381             counterclaim) against any party alleging that the Package constitutes
382             direct or contributory patent infringement, then this Artistic License
383             to you shall terminate on the date that such litigation is filed.
384              
385             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
386             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
387             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
388             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
389             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
390             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
391             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
392             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393              
394              
395             =cut
396              
397             1;