File Coverage

blib/lib/XAS/Lib/WS/Manage.pm
Criterion Covered Total %
statement 3 279 1.0
branch 0 20 0.0
condition n/a
subroutine 1 25 4.0
pod 7 7 100.0
total 11 331 3.3


line stmt bran cond sub pod time code
1             package XAS::Lib::WS::Manage;
2              
3             our $VERSION = '0.01';
4              
5             use XAS::Class
6 1         5 version => $VERSION,
7             base => 'XAS::Lib::WS::Base',
8             utils => ':validation dotid',
9             constants => 'SCALAR HASHREF ARRAYREF',
10 1     1   1322 ;
  1         2  
11              
12             #use Data::Dumper;
13              
14             # ----------------------------------------------------------------------
15             # Public Methods
16             # ----------------------------------------------------------------------
17              
18             sub create {
19 0     0 1   my $self = shift;
20 0           my $p = validate_params(\@_, {
21             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
22             -class => { type => SCALAR },
23             -selector => { type => SCALAR },
24             -xml => { type => SCALAR },
25             });
26              
27 0           my $data = $p->{'xml'};
28 0           my $class = $p->{'class'};
29 0           my $resource = $p->{'resource'};
30 0           my $selector = $p->{'selector'};
31              
32 0           my $object = sprintf('%s/%s', $resource, $class);
33              
34 0           my $uuid = $self->uuid->create_str;
35 0           my $xml = $self->_create_xml($uuid, $object, $selector, $data);
36              
37 0           $self->_make_call($xml);
38              
39 0           return $self->_create_response($uuid);
40              
41             }
42              
43             sub delete {
44 0     0 1   my $self = shift;
45 0           my $p = validate_params(\@_, {
46             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
47             -class => { type => SCALAR },
48             -selector => { type => SCALAR },
49             });
50              
51 0           my $class = $p->{'class'};
52 0           my $resource = $p->{'resource'};
53 0           my $selector = $p->{'selector'};
54              
55 0           my $object = sprintf('%s/%s', $resource, $class);
56              
57 0           my $uuid = $self->uuid->create_str;
58 0           my $xml = $self->_delete_xml($uuid, $object, $selector);
59              
60 0           $self->_make_call($xml);
61              
62 0           return $self->_delete_response($uuid);
63              
64             }
65              
66             sub enumerate {
67 0     0 1   my $self = shift;
68 0           my $p = validate_params(\@_, {
69             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
70             -selector => { optional => 1, type => HASHREF, default => undef },
71             -class => { type => SCALAR },
72             });
73              
74 0           my $class = $p->{'class'};
75 0           my $resource = $p->{'resource'};
76 0           my $selector = $p->{'selector'};
77              
78 0           my $object = sprintf('%s/%s', $resource, $class);
79              
80 0           my $xml;
81 0           my $uuid = $self->uuid->create_str;
82              
83 0 0         if (defined($selector)) {
84              
85 0           $xml = $self->_enumerate_filter_xml($uuid, $object, $selector)
86              
87             } else {
88              
89 0           $xml = $self->_enumerate_xml($uuid, $object)
90              
91             }
92              
93 0           $self->_make_call($xml);
94              
95 0           return $self->_enumerate_response($uuid, $resource, $class);
96              
97             }
98              
99             sub get {
100 0     0 1   my $self = shift;
101 0           my $p = validate_params(\@_, {
102             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
103             -class => { type => SCALAR },
104             -selector => { type => HASHREF }
105             });
106              
107 0           my $class = $p->{'class'};
108 0           my $resource = $p->{'resource'};
109 0           my $selector = $p->{'selector'};
110              
111 0           my $object = sprintf('%s/%s', $resource, $class);
112              
113 0           my $uuid = $self->uuid->create_str;
114 0           my $xml = $self->_get_xml($uuid, $object, $selector);
115              
116 0           $self->_make_call($xml);
117              
118 0           return $self->_get_response($uuid, $class);
119              
120             }
121              
122             sub invoke {
123 0     0 1   my $self = shift;
124 0           my $p = validate_params(\@_, {
125             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
126             -action => { type => SCALAR },
127             -class => { type => SCALAR },
128             -selector => { type => HASHREF },
129             });
130              
131 0           my $class = $p->{'class'};
132 0           my $action = $p->{'action'};
133 0           my $resource = $p->{'resource'};
134 0           my $selector = $p->{'selector'};
135              
136 0           my $object = sprintf('%s/%s', $resource, $class);
137 0           my $oaction = sprintf('%s/%s', $object, $action);
138              
139 0           my $uuid = $self->uuid->create_str;
140 0           my $xml = $self->_invoke_xml($uuid, $object, $oaction, $action, $selector);
141              
142 0           $self->_make_call($xml);
143              
144 0           return $self->_invoke_response($uuid, $class, $action);
145              
146             }
147              
148             sub pull {
149 0     0 1   my $self = shift;
150 0           my $p = validate_params(\@_, {
151             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
152             -class => { type => SCALAR },
153             -context => { type => SCALAR },
154             -items => { type => ARRAYREF },
155             });
156              
157 0           my $items = $p->{'items'};
158 0           my $class = $p->{'class'};
159 0           my $context = $p->{'context'};
160 0           my $resource = $p->{'resource'};
161              
162 0           my $object = sprintf('%s/%s', $resource, $class);
163              
164 0           my $xml;
165             my $uuid;
166 0           my $running;
167              
168 0           do {
169              
170 0           $uuid = $self->uuid->create_str;
171 0           $xml = $self->_pull_xml($uuid, $object, $context);
172              
173 0           $self->_make_call($xml);
174              
175 0           $context = $self->_pull_response($uuid, $class, $items);
176              
177             } while ($context);
178              
179             }
180              
181             sub put {
182 0     0 1   my $self = shift;
183 0           my $p = validate_params(\@_, {
184             -resource => { optional => 1, default => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2' },
185             -class => { type => SCALAR },
186             -key => { type => SCALAR },
187             -value => { type => SCALAR },
188             -data => { type => HASHREF },
189             });
190              
191 0           my $key = $p->{'key'};
192 0           my $data = $p->{'data'};
193 0           my $value = $p->{'value'};
194 0           my $class = $p->{'class'};
195 0           my $resource = $p->{'resource'};
196              
197 0           my $object = sprintf('%s/%s', $resource, $class);
198              
199 0           my $uuid = $self->uuid->create_str;
200 0           my $xml = $self->_put_xml($uuid, $object, $class, $key, $value, $data);
201              
202 0           $self->_make_call($xml);
203              
204 0           return $self->_put_response($uuid, $class);
205              
206             }
207              
208             # ----------------------------------------------------------------------
209             # Private Methods
210             # ----------------------------------------------------------------------
211              
212             sub _create_response {
213 0     0     my $self = shift;
214 0           my $uuid = shift;
215              
216 0           my $stat = 0;
217 0           my $xpath = '//t:ResourceCreated';
218              
219 0           $self->log->debug('entering - _create_response()');
220              
221 0           $self->_check_relates_to($uuid);
222 0           $self->_check_action('CreateResponse');
223              
224 0 0         $stat = 1 if ($self->xml->get_item($xpath));
225              
226 0           return $stat;
227              
228             }
229              
230             sub _delete_response {
231 0     0     my $self = shift;
232 0           my $uuid = shift;
233              
234 0           $self->log->debug('entering - _delete_response()');
235              
236 0           $self->_check_relates_to($uuid);
237 0           $self->_check_action('DeleteResponse');
238              
239 0           return 1;
240              
241             }
242              
243             sub _enumerate_response {
244 0     0     my $self = shift;
245 0           my $uuid = shift;
246 0           my $resource = shift;
247 0           my $class = shift;
248              
249 0           my @items;
250             my $context;
251 0           my $xpath = "//n:EnumerationContext";
252              
253 0           $self->log->debug('entering - _enumerate_response()');
254              
255 0           $self->_check_relates_to($uuid);
256 0           $self->_check_action('EnumerateResponse');
257              
258 0 0         if (my $value = $self->xml->get_item($xpath)) {
259              
260 0           ($context) = $value =~ /uuid:(.*)/;
261              
262 0           $self->pull(
263             -resource => $resource,
264             -class => $class,
265             -context => $context,
266             -items => \@items
267             );
268              
269             }
270              
271 0 0         return wantarray ? @items : \@items;
272              
273             }
274              
275             sub _get_response {
276 0     0     my $self = shift;
277 0           my $uuid = shift;
278 0           my $class = shift;
279              
280 0           my $hash;
281 0           my $xpath = "//p:$class";
282              
283 0           $self->_check_relates_to($uuid);
284 0           $self->_check_action('GetResponse');
285              
286 0 0         if (my $elements = $self->xml->get_items($xpath)) {
287              
288 0           $hash = $self->_get_items($elements);
289              
290             } else {
291              
292 0           $self->throw_msg(
293             dotid($self->class) . '.get_response.noxpath',
294             'ws_noxpath',
295             $xpath
296             );
297              
298             }
299              
300 0           return $hash;
301              
302             }
303              
304             sub _invoke_response {
305 0     0     my $self = shift;
306 0           my $uuid = shift;
307 0           my $class = shift;
308 0           my $action = shift;
309              
310 0           my $rc = -1;
311 0           my $response = sprintf('%sResponse', $action);
312 0           my $xpath = sprintf('//p:%s_OUTPUT', $action);
313              
314 0           $self->_check_relates_to($uuid);
315 0           $self->_check_action($response);
316              
317 0 0         if (my $elements = $self->xml->get_items($xpath)) {
318              
319 0           foreach my $element (@$elements) {
320              
321 0 0         if ($element->localname =~ /ReturnValue/) {
322              
323 0           $rc = $element->textContent;
324              
325             }
326              
327             }
328              
329             } else {
330              
331 0           $self->throw_msg(
332             dotid($self->class) . '.get_response.noxpath',
333             'ws_noxpath',
334             $xpath
335             );
336              
337             }
338              
339 0           return $rc;
340              
341             }
342              
343             sub _pull_response {
344 0     0     my $self = shift;
345 0           my $uuid = shift;
346 0           my $class = shift;
347 0           my $items = shift;
348              
349 0           my $context = undef;
350              
351 0           $self->_check_relates_to($uuid);
352 0           $self->_check_action('PullResponse');
353              
354 0 0         if (my $value = $self->xml->get_item('//n:EnumerationContext')) {
355              
356 0           ($context) = $value =~ /uuid:(.*)/;
357              
358             }
359              
360 0           $self->_get_enum_items($class, $items);
361              
362 0           return $context;
363              
364             }
365              
366             sub _put_response {
367 0     0     my $self = shift;
368 0           my $uuid = shift;
369 0           my $class = shift;
370              
371 0           my $hash;
372 0           my $xpath = "//p:$class";
373              
374 0           $self->_check_relates_to($uuid);
375 0           $self->_check_action('PutResponse');
376              
377 0 0         if (my $elements = $self->xml->get_items($xpath)) {
378              
379 0           $hash = $self->_get_items($elements);
380              
381             } else {
382              
383 0           $self->throw_msg(
384             dotid($self->class) . '.put_response.noxpath',
385             'ws_noxpath',
386             $xpath
387             );
388              
389             }
390              
391 0           return $hash;
392              
393             }
394              
395             sub _get_enum_items {
396 0     0     my $self = shift;
397 0           my $class = shift;
398 0           my $items = shift;
399              
400 0           my $xpath = sprintf('//p:%s', $class);
401              
402 0           $self->log->debug('entering _get_enum_items()');
403              
404 0 0         if (my $elements = $self->xml->get_items($xpath)) {
405              
406 0           my $hash = $self->_get_items($elements);
407              
408 0           push(@$items, $hash);
409              
410             } else {
411              
412 0           $self->throw_msg(
413             dotid($self->class) . '.get_enum_items.noxpath',
414             'ws_noxpath',
415             $xpath
416             );
417              
418             }
419              
420             }
421              
422             sub _get_items {
423 0     0     my $self = shift;
424 0           my $elements = shift;
425              
426 0           my $hash = {};
427              
428 0           foreach my $element (@$elements) {
429              
430 0           my $key = $element->localname;
431 0           my $value = $element->textContent;
432              
433 0           $hash->{$key} = $value;
434              
435             }
436              
437 0           return $hash;
438              
439             }
440              
441             # ----------------------------------------------------------------------
442             # XML boilerplate - we're using heredoc for simplcity
443             #
444             # XML for ws-manage Manage was taken from
445             # https://msdn.microsoft.com/en-us/library/cc251705.aspx
446             # ----------------------------------------------------------------------
447              
448             sub _create_xml {
449 0     0     my $self = shift;
450 0           my $uuid = shift;
451 0           my $resource = shift;
452 0           my $params = shift;
453 0           my $xml_data = shift;
454              
455 0           my $url = $self->url;
456 0           my $timeout = $self->timeout;
457              
458 0           my $xml = <<"XML";
459            
460            
461             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
462             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
463             xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd">
464            
465            
466             $url
467            
468            
469             $resource
470            
471            
472            
473             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
474            
475            
476            
477             http://schemas.xmlsoap.org/ws/2004/09/transfer/Create
478            
479            
480             512000
481            
482            
483             uuid:$uuid
484            
485            
486            
487            
488             __SELECTOR__
489            
490             PT$timeout.000S
491            
492            
493             $xml_data
494            
495            
496             XML
497              
498 0           my $selector = '';
499              
500 0           while (my ($key, $value) = each(%$params)) {
501              
502 0           $selector .= sprintf("%s\n", $key, $value);
503              
504             }
505              
506 0           chomp $selector;
507              
508 0           $xml =~ s/__SELECTOR__/$selector/;
509              
510 0           return $xml;
511              
512             }
513              
514             sub _delete_xml {
515 0     0     my $self = shift;
516 0           my $uuid = shift;
517 0           my $resource = shift;
518 0           my $params = shift;
519              
520 0           my $url = $self->url;
521 0           my $timeout = $self->timeout;
522              
523 0           my $xml = <<"XML";
524            
525            
526             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
527             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
528            
529            
530             $url
531            
532            
533             $resource
534            
535            
536            
537             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
538            
539            
540            
541             http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete
542            
543            
544             512000
545            
546            
547             uuid:$uuid
548            
549            
550            
551             __SELECTOR__
552            
553             PT$timeout.000S
554            
555            
556            
557             XML
558              
559 0           my $selector = '';
560              
561 0           while (my ($key, $value) = each(%$params)) {
562              
563 0           $selector .= sprintf("%s\n", $key, $value);
564              
565             }
566              
567 0           chomp $selector;
568              
569 0           $xml =~ s/__SELECTOR__/$selector/;
570              
571 0           return $xml;
572              
573             }
574              
575             sub _enumerate_xml {
576 0     0     my $self = shift;
577 0           my $uuid = shift;
578 0           my $resource = shift;
579              
580 0           my $url = $self->url;
581 0           my $timeout = $self->timeout;
582              
583 0           my $xml = <<"XML";
584            
585            
586             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
587             xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
588             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
589             xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd">
590            
591            
592             $url
593            
594            
595             $resource
596            
597            
598            
599             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
600            
601            
602            
603             http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate
604            
605            
606             512000
607            
608            
609             uuid:$uuid
610            
611            
612             PT$timeout.000S
613            
614            
615            
616            
617            
618             XML
619              
620 0           return $xml;
621              
622             }
623              
624             sub _enumerate_filter_xml {
625 0     0     my $self = shift;
626 0           my $uuid = shift;
627 0           my $resource = shift;
628 0           my $params = shift;
629              
630 0           my $url = $self->url;
631 0           my $timeout = $self->timeout;
632              
633 0           my $xml = <<"XML";
634            
635            
636             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
637             xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
638             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
639             xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd">
640            
641            
642             $url
643            
644            
645             $resource
646            
647            
648            
649             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
650            
651            
652            
653             http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate
654            
655            
656             512000
657            
658            
659             uuid:$uuid
660            
661            
662             PT$timeout.000S
663            
664            
665            
666            
667            
668             __SELECTOR__
669            
670            
671            
672            
673            
674             XML
675              
676 0           my $selector = '';
677              
678 0           while (my ($key, $value) = each(%$params)) {
679              
680 0           $selector .= sprintf("%s\n", $key, $value);
681              
682             }
683              
684 0           chomp $selector;
685              
686 0           $xml =~ s/__SELECTOR__/$selector/;
687              
688 0           return $xml;
689              
690             }
691              
692             sub _get_xml {
693 0     0     my $self = shift;
694 0           my $uuid = shift;
695 0           my $resource = shift;
696 0           my $params = shift;
697              
698 0           my $url = $self->url;
699 0           my $timeout = $self->timeout;
700              
701 0           my $xml = <<"XML";
702            
703            
704             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
705             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
706            
707            
708             $url
709            
710            
711             $resource
712            
713            
714            
715             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
716            
717            
718            
719             http://schemas.xmlsoap.org/ws/2004/09/transfer/Get
720            
721            
722             512000
723            
724            
725             uuid:$uuid
726            
727            
728            
729             __SELECTOR__
730            
731             PT$timeout.000S
732            
733            
734            
735             XML
736              
737 0           my $selector = '';
738              
739 0           while (my ($key, $value) = each(%$params)) {
740              
741 0           $selector .= sprintf("%s\n", $key, $value);
742              
743             }
744              
745 0           chomp $selector;
746              
747 0           $xml =~ s/__SELECTOR__/$selector/;
748              
749 0           return $xml;
750              
751             }
752              
753             sub _invoke_xml {
754 0     0     my $self = shift;
755 0           my $uuid = shift;
756 0           my $resource = shift;
757 0           my $oaction = shift;
758 0           my $action = shift;
759 0           my $params = shift;
760              
761 0           my $url = $self->url;
762 0           my $timeout = $self->timeout;
763 0           my $input = sprintf('%s_INPUT', $action);
764              
765 0           my $xml = <<"XML";
766            
767            
768             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
769             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
770             xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd">
771            
772            
773             $url
774            
775            
776             $resource
777            
778            
779            
780             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
781            
782            
783            
784             $oaction
785            
786            
787             512000
788            
789            
790             uuid:$uuid
791            
792            
793            
794             __SELECTOR__
795            
796             PT$timeout.000S
797            
798            
799            
800            
801            
802             XML
803              
804 0           my $selector = '';
805              
806 0           while (my ($key, $value) = each(%$params)) {
807              
808 0           $selector .= sprintf("%s\n", $key, $value);
809              
810             }
811              
812 0           chomp $selector;
813              
814 0           $xml =~ s/__INPUT__/$input/;
815 0           $xml =~ s/__SELECTOR__/$selector/;
816              
817 0           return $xml;
818              
819             }
820              
821             sub _pull_xml {
822 0     0     my $self = shift;
823 0           my $uuid = shift;
824 0           my $resource = shift;
825 0           my $context = shift;
826              
827 0           my $url = $self->url;
828 0           my $timeout = $self->timeout;
829              
830 0           my $xml = <<"XML";
831            
832            
833             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
834             xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
835             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
836             xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd">
837            
838            
839             $url
840            
841            
842             $resource
843            
844            
845            
846             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
847            
848            
849            
850             http://schemas.xmlsoap.org/ws/2004/09/enumeration/Pull
851            
852            
853             512000
854            
855            
856             uuid:$uuid
857            
858            
859             PT$timeout.000S
860            
861            
862            
863             uuid:$context
864            
865            
866            
867            
868             XML
869              
870 0           return $xml;
871              
872             }
873              
874             sub _put_xml {
875 0     0     my $self = shift;
876 0           my $uuid = shift;
877 0           my $resource = shift;
878 0           my $class = shift;
879 0           my $key = shift;
880 0           my $value = shift;
881 0           my $params = shift;
882              
883 0           my $url = $self->url;
884 0           my $timeout = $self->timeout;
885              
886 0           my $xml = <<"XML";
887            
888            
889             xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
890             xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
891             xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd">
892            
893            
894             $url
895            
896            
897             $resource
898            
899            
900            
901             http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
902            
903            
904            
905             http://schemas.xmlsoap.org/ws/2004/09/transfer/Put
906            
907            
908             512000
909            
910            
911             uuid:$uuid
912            
913            
914            
915             $value
916            
917             PT$timeout.000S
918            
919            
920            
921             __MODIFY__
922            
923            
924            
925             XML
926              
927 0           my $modify = '';
928              
929 0           while (my ($name, $data) = each(%$params)) {
930              
931 0           $modify .= sprintf("%s\n", $name, $data, $name);
932              
933             }
934              
935 0           chomp $modify;
936              
937 0           $xml =~ s/__MODIFY__/$modify/;
938              
939 0           return $xml;
940              
941             }
942              
943             1;
944              
945             __END__