| 1 | package Ovirt::DataCenter; | ||
| 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::DataCenter - Bindings for oVirt DataCenter API | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | Version 0.01 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Ovirt::DataCenter; | ||
| 23 | |||
| 24 | my %con = ( | ||
| 25 | username => 'admin', | ||
| 26 | password => 'password', | ||
| 27 | manager => 'ovirt-mgr.example.com', | ||
| 28 | datacenter_output_attrs => 'id,name,description', # optional | ||
| 29 | ); | ||
| 30 | |||
| 31 | my $datacenter = Ovirt::DataCenter->new(%con); | ||
| 32 | |||
| 33 | # return xml output | ||
| 34 | print $datacenter->list_xml; | ||
| 35 | |||
| 36 | # list datacenter attributes | ||
| 37 | print $datacenter->list; | ||
| 38 | |||
| 39 | # the output also available in hash | ||
| 40 | # for example to print all datacenter name | ||
| 41 | my $hash = $datacenter->hash_output; | ||
| 42 | for my $array (keys $hash->{datacenter}) { | ||
| 43 | print $hash->{datacenter}[$array]->{name}; | ||
| 44 | } | ||
| 45 | |||
| 46 | # we can also specify specific datacenter 'id' when initiating an object | ||
| 47 | # so we can direct access the element for specific datacenter | ||
| 48 | print $datacenter->hash_output->{id}; | ||
| 49 | print $datacenter->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 | datacenter_url = (ro) store default datacenter url path | ||
| 62 | datacenter_output_attrs = (rw) store datacenter attributes to be returned, default is (id, name, state, description) | ||
| 63 | supported attributes : | ||
| 64 | id name | ||
| 65 | state description | ||
| 66 | local storage_format | ||
| 67 | major_version minor_version | ||
| 68 | |||
| 69 | datacenter_output_delimiter = (rw) specify output delimiter between attribute, default is '||' | ||
| 70 | =cut | ||
| 71 | |||
| 72 | has 'datacenter_url' => ( is => 'ro', default => '/api/datacenters' ); | ||
| 73 | has 'datacenter_output_attrs' => ( is => 'rw', default => 'id,name,state,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 "datacenter_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 | state description | ||
| 86 | local storage_format | ||
| 87 | major_version minor_version | ||
| 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 'datacenter_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 datacenter_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->datacenter_url. '/' . $self->id); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $self->_set_root_url($self->datacenter_url); | ||
| 118 | } | ||
| 119 | |||
| 120 | $self->get_api_response(); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 list | ||
| 124 | |||
| 125 | return datacenter's attributes text output from hash_output attribute | ||
| 126 | if no argument spesified, it will return all datacenter attributes (based on datacenter_output_attrs) | ||
| 127 | argument supported is 'datacenter id' | ||
| 128 | example : | ||
| 129 | $datacenter->list('c4738b0f-b73d-4a66-baa8-2ba465d63132'); | ||
| 130 | =cut | ||
| 131 | |||
| 132 | sub list { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $datacenterid = 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->datacenter_output_attrs; | ||
| 142 | |||
| 143 | # store the last element to escape the datacenter_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 datacenter element | ||
| 150 | if ($self->id) { | ||
| 151 | for my $attr (@attrs) { | ||
| 152 | $self->log->debug("requesting attribute $attr"); | ||
| 153 | |||
| 154 | my $attr_output = $self->get_datacenter_by_self_id($attr) || $self->not_available; | ||
| 155 | $output .= $attr_output . $self->datacenter_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_datacenter_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 ($datacenterid) { | ||
| 170 | #store datacenterid element | ||
| 171 | my $datacenterid_element; | ||
| 172 | |||
| 173 | $datacenterid = $self->trim($datacenterid); | ||
| 174 | |||
| 175 | for my $element_id ( 0 .. $#{ $self->hash_output->{data_center} } ) { | ||
| 176 | next unless $self->hash_output->{datacenter}[$element_id]->{id} eq $datacenterid; | ||
| 177 | |||
| 178 | $datacenterid_element = $element_id; | ||
| 179 | } | ||
| 180 | |||
| 181 | croak "datacenter id not found" unless $datacenterid_element >= 0; | ||
| 182 | |||
| 183 | for my $attr (@attrs) { | ||
| 184 | $self->log->debug("requesting attribute $attr for element $datacenterid_element"); | ||
| 185 | |||
| 186 | my $attr_output = $self->get_datacenter_by_element_id($datacenterid_element, $attr) || $self->not_available; | ||
| 187 | $output .= $attr_output . $self->datacenter_output_delimiter; | ||
| 188 | $self->log->debug("output for attribute $attr element $datacenterid_element = " . $attr_output); | ||
| 189 | } | ||
| 190 | |||
| 191 | #handle last element or the only element | ||
| 192 | $self->log->debug("requesting attribute $last_element for element $datacenterid_element"); | ||
| 193 | |||
| 194 | if (my $last_output = $self->get_datacenter_by_element_id($datacenterid_element, $last_element) || $self->not_available) { | ||
| 195 | $output .= $last_output; | ||
| 196 | $self->log->debug("output for attribute $last_element element $datacenterid_element = " . $last_output); | ||
| 197 | } | ||
| 198 | |||
| 199 | $output .= "\n"; | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | |||
| 203 | for my $element_id ( 0 .. $#{ $self->hash_output->{data_center} } ) { | ||
| 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_datacenter_by_element_id($element_id, $attr) || $self->not_available; | ||
| 212 | $output .= $attr_output . $self->datacenter_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_datacenter_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_datacenter_by_element_id | ||
| 233 | |||
| 234 | This method is used by list method to list all datacenter attributes requested | ||
| 235 | An array element id and attribute name is required | ||
| 236 | =cut | ||
| 237 | |||
| 238 | sub get_datacenter_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->{data_center}[$element_id]->{id}; | ||
| 251 | } | ||
| 252 | elsif ($attr eq 'name') { | ||
| 253 | return $self->hash_output->{data_center}[$element_id]->{name}; | ||
| 254 | } | ||
| 255 | elsif ($attr eq 'state') { | ||
| 256 | return $self->hash_output->{data_center}[$element_id]->{status}->{state}; | ||
| 257 | } | ||
| 258 | elsif ($attr eq 'description') { | ||
| 259 | return $self->hash_output->{data_center}[$element_id]->{description}; | ||
| 260 | } | ||
| 261 | elsif ($attr eq 'local') { | ||
| 262 | return $self->hash_output->{data_center}[$element_id]->{local}; | ||
| 263 | } | ||
| 264 | elsif ($attr eq 'storage_format') { | ||
| 265 | return $self->hash_output->{data_center}[$element_id]->{storage_format}; | ||
| 266 | } | ||
| 267 | elsif ($attr eq 'major_version') { | ||
| 268 | return $self->hash_output->{data_center}[$element_id]->{version}->{major}; | ||
| 269 | } | ||
| 270 | elsif ($attr eq 'minor_version') { | ||
| 271 | return $self->hash_output->{data_center}[$element_id]->{version}->{minor}; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | =head2 get_datacenter_by_self_id | ||
| 276 | |||
| 277 | This method is used by list method if $self->id is defined | ||
| 278 | The id is set during initialization (id => 'datacenterid') | ||
| 279 | attribute name is required | ||
| 280 | =cut | ||
| 281 | |||
| 282 | sub get_datacenter_by_self_id { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | my $attr = shift; | ||
| 286 | |||
| 287 | croak "hash output is not defined" | ||
| 288 | unless $self->hash_output; | ||
| 289 | |||
| 290 | $attr = $self->trim($attr); | ||
| 291 | $self->log->debug("attribute = $attr"); | ||
| 292 | |||
| 293 | if ($attr eq 'id') { | ||
| 294 | return $self->hash_output->{id}; | ||
| 295 | } | ||
| 296 | elsif ($attr eq 'name') { | ||
| 297 | return $self->hash_output->{name}; | ||
| 298 | } | ||
| 299 | elsif ($attr eq 'state') { | ||
| 300 | return $self->hash_output->{status}->{state}; | ||
| 301 | } | ||
| 302 | elsif ($attr eq 'description') { | ||
| 303 | return $self->hash_output->{description}; | ||
| 304 | } | ||
| 305 | elsif ($attr eq 'local') { | ||
| 306 | return $self->hash_output->{local}; | ||
| 307 | } | ||
| 308 | elsif ($attr eq 'storage_format') { | ||
| 309 | return $self->hash_output->{storage_format}; | ||
| 310 | } | ||
| 311 | elsif ($attr eq 'major_version') { | ||
| 312 | return $self->hash_output->{version}->{major}; | ||
| 313 | } | ||
| 314 | elsif ($attr eq 'minor_version') { | ||
| 315 | return $self->hash_output->{version}->{minor}; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | =head1 AUTHOR | ||
| 320 | |||
| 321 | "Heince Kurniawan", C<< <"heince at cpan.org"> >> | ||
| 322 | |||
| 323 | =head1 BUGS | ||
| 324 | |||
| 325 | Please report any bugs or feature requests to C<bug-ovirt at rt.cpan.org>, or through | ||
| 326 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Ovirt::DataCenter>. I will be notified, and then you'll | ||
| 327 | automatically be notified of progress on your bug as I make changes. | ||
| 328 | |||
| 329 | =head1 SUPPORT | ||
| 330 | |||
| 331 | You can find documentation for this module with the perldoc command. | ||
| 332 | |||
| 333 | perldoc Ovirt::DataCenter | ||
| 334 | |||
| 335 | You can also look for information at: | ||
| 336 | |||
| 337 | =head1 ACKNOWLEDGEMENTS | ||
| 338 | |||
| 339 | |||
| 340 | =head1 LICENSE AND COPYRIGHT | ||
| 341 | |||
| 342 | Copyright 2015 "Heince Kurniawan". | ||
| 343 | |||
| 344 | This program is free software; you can redistribute it and/or modify it | ||
| 345 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 346 | copy of the full license at: | ||
| 347 | |||
| 348 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 349 | |||
| 350 | Any use, modification, and distribution of the Standard or Modified | ||
| 351 | Versions is governed by this Artistic License. By using, modifying or | ||
| 352 | distributing the Package, you accept this license. Do not use, modify, | ||
| 353 | or distribute the Package, if you do not accept this license. | ||
| 354 | |||
| 355 | If your Modified Version has been derived from a Modified Version made | ||
| 356 | by someone other than you, you are nevertheless required to ensure that | ||
| 357 | your Modified Version complies with the requirements of this license. | ||
| 358 | |||
| 359 | This license does not grant you the right to use any trademark, service | ||
| 360 | mark, tradename, or logo of the Copyright Holder. | ||
| 361 | |||
| 362 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 363 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 364 | otherwise transfer the Package with respect to any patent claims | ||
| 365 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 366 | Package. If you institute patent litigation (including a cross-claim or | ||
| 367 | counterclaim) against any party alleging that the Package constitutes | ||
| 368 | direct or contributory patent infringement, then this Artistic License | ||
| 369 | to you shall terminate on the date that such litigation is filed. | ||
| 370 | |||
| 371 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 372 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 373 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 374 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 375 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 376 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 377 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 378 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 379 | |||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | 1; |