Ovirt/Network.pm untested

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