blib/lib/VMware/LabManager.pm

filter: Statementstmt 33.7% 25 / 74 Branchbran 7.1% 1 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 54.5% 6 / 11 Podpod 100% 6 / 6 totaltotal 35.4% 39 / 110 CC 9 SCAR 20.5
CC
9
Cov
30%
deploy_config
20.0
delete_config
12.0
get_machines
6.0
CRAP 7.7
↑ Summary
1 package VMware::LabManager;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Time::HiRes qw(time); #used to generate unique confignames.
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 VMware::LabManager - Perl module to provide basic VMware Lab Manager operations.
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 3 our $VERSION = '0.01';
19  
20 =head1 SYNOPSIS
21  
22 This module provides an encapsulation of VMware Lab Manager SOAP API in
23 convenient API format. Basic functionality like deploying & deleting a library
24 config to & from the workspace are provided.
25  
26 A little code snippet to get you started:
27  
28 use VMware::LabManager;
29 use Data::Dumper;
30
31 #instantiate the LM object
32 my $lm = VMware::LabManager->new('username','passwd','labManager hostname', 'org');
33
34 #or if you want the XML messages dumped (i.e. verbose), pass the debug option:
35 my $lm = VMware::LabManager->new('username','passwd','labManager hostname', 'org', 'debug');
36
37 #deploy_config returns the config ID of the config that is deployed
38 my $configID = $lm->deploy_config('my_library_config_name');
39 print $configID;
40
41 #get all the machines in the deployed config
42 my $machineArray = $lm->get_machines($configID);
43 print Dumper($machineArray);
44
45 #delete the config once you are done.
46 $action = $lm->delete_config($configID);
47 print $action
48  
49 =head1 DESCRIPTION
50  
51 This module does not provide a one-to-one mapping of the Lab Manager SOAP API,
52 but rather it provides an API wrapper, which combines certain SOAP calls as a
53 meaningful single operation. Thus this module is heavily tailored for automation
54 purposes. But, it should also cater to the broader audience as it still does
55 provide overall functionality that might be required.
56  
57 Using this module, you can checkout a config & deploy it in one single method call,
58 similarly undeploy & delete in a single method call. This module also provides a
59 method to get the list of all machines (and their details) that a deployed
60 configuration has. Apart from that the SOAP object & auth header methods are
61 exposed; So if need be, they can be used to access other SOAP calls available
62 through Lab Manager SOAP service.
63  
64 =head1 SUBROUTINES/METHODS
65  
66 =head2 new
67  
68 This function instantiates the LM object. The arguments in the B<required order> are:
69  
70 =over 4
71  
72 =item * Username - Your username for Lab Manager login.
73  
74 =item * Password - Your password for Lab Manager login.
75  
76 =item * Hostname - FQDN of the Lab Manager you are trying to log in.
77  
78 =item * Organization - Organization to which you belong to in Lab Manager.
79  
80 =back
81  
82 This does not authenticate with the Lab Manager server yet. That happens when you
83 use one of the other methods.
84  
85 =cut
86  
87 sub new
88 1 ▶{
Subroutine new: called CC=3 SCAR=11.8
Pod documented
89 1 my $class = shift;
90 1 my $uname = shift; # username for LM
91 1 my $pass = shift; # password for LM
92 1 my $host = shift; # LM hostname
93 1 my $org = shift; # LM organization name
94 1 my $debug = shift; # enable debug, off by default
95  
96 # Set the debug option if passed to the constructor.
97 1 ▶ if ( defined $debug && ( $debug eq 'debug' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug and $debug eq 'debug') { } 0 1 1
Condition33%1/3
defined $debug and $debug eq "debug"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $debug and $debug eq "debug"
defined $debug $debug eq "debug"
Truth table33%1/3
A and B
A= defined $debugB= $debug eq "debug"
ABresult
0X0
100
111
98 {
99 0 eval("use SOAP::Lite +trace => 'debug'");
100 } else
101 {
102 0 ▶ eval("use SOAP::Lite");
Subroutine BEGIN: called CC=1 SCAR=1.2
103 }
104  
105 1 my $self = {
106 Username => $uname,
107 Password => $pass,
108 Hostname => $host,
109 Organization => $org
110 };
111  
112 1 bless( $self, $class );
113 1 return $self;
114 }
115  
116 =head2 get_soap
117  
118 This method is primary used internally, but there are situations where it could
119 be used for other SOAP methods that have not been exposed by this module. This
120 method returns SOAP::Lite object & sets the readable option on. You can use this
121 in conjunction with Authentication header, which you can get from get_auth_header()
122 method.
123  
124 NOTE: Usage of this method outside the scope of this API is not recommended.
125  
126 =cut
127  
128 sub get_soap
129 0 ▶{
Subroutine get_soap: not called CC=1 SCAR=6.9
Pod documented
130 0 my $self = shift;
131 my $soap = SOAP::Lite->on_action(
132 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 return "http://vmware.com/labmanager/" . $_[1];
134 }
135 )->default_ns('http://vmware.com/labmanager')
136 ->proxy(
137 0 'https://' . $self->{Hostname} . '/LabManager/SOAP/LabManager.asmx' );
138 0 $soap->readable(1);
139 0 return $soap;
140 }
141  
142 =head2 get_auth_header
143  
144 This method returns an authentication header wrapper that is needed for each
145 SOAP call that you make to Lab Manager. It uses the options you provided in the
146 new() method to build this header.
147  
148 =cut
149  
150 sub get_auth_header
151 1 ▶{
Subroutine get_auth_header: called CC=1 SCAR=0.4
Pod documented
152 1 my $self = shift;
153 my $auth_header = SOAP::Header->new(
154 name => 'AuthenticationHeader',
155 attr => { xmlns => "http://vmware.com/labmanager" },
156 value => {
157 username => $self->{Username},
158 password => $self->{Password},
159 organizationname => $self->{Organization},
160 },
161 1 );
162
163 0 return $auth_header;
164 }
165  
166 =head2 deploy_config
167  
168 This method is tailored for automation. During automation, one primarily cares about
169 deploying a library image. A single method call to encapsulate the whole set of
170 operations is more deirable.
171  
172 This method requires a config name (make sure it is unique in the system), which
173 it uses to:
174  
175 =over 4
176  
177 =item * Checkout the config to your default workspace with a random name.
178  
179 =item * Deploy the confignation in fenced mode.
180  
181 =item * Return the unique numeric Config ID of the deployed config.
182  
183 =back
184  
185 You have to use this config ID to undeploy & delete it at at later stage.
186  
187 =cut
188  
189 sub deploy_config
190 0 ▶{
Subroutine deploy_config: not called CC=4 SCAR=30.0
Pod documented
191 0 my $self = shift;
192 0 my $config_name = shift; # library config name
193 0 my $soap = $self->get_soap();
194 0 my $auth_header = $self->get_auth_header();
195 0 my $res;
196  
197 # Get Configuration by Name
198 0 $res = $soap->GetConfigurationByName(
199 $auth_header,
200 SOAP::Data->name(
201 'name' => $config_name
202 )->type('s:string')
203 );
204  
205 #check for error ... there's more in the faultdetail hash, could use Dumper to examine it
206 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
207 {
208 return
209 0 join( ': ',
210 'LabManager SOAP error', $res->faultcode,
211 $res->faultstring, $res->faultdetail );
212 }
213  
214 # Get the id of the configuration
215 0 my $configID = undef;
216  
217 # LM API does not provide any direct way of getting the version it is running & the data structure
218 # returned for this method changed in v4.0. I am just lazy, change the statement below if you use older version.
219 # For previous versions of LM: $configID = $res->result->{'Configuration'}->{'id'};
220  
221 0 $configID = $res->result->{'Configuration'}[0]->{'id'};
222  
223 # Check out Configuration to workspace
224 0 my $new_config_name = $config_name . time;
225 0 $res = $soap->ConfigurationCheckout(
226 $auth_header,
227 SOAP::Data->name(
228 'configurationId' => $configID
229 )->type('s:int'),
230 SOAP::Data->name(
231 'workspaceName' => $new_config_name
232 )->type('s:string')
233 );
234  
235 #check for error...
236 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
237 {
238 return
239 0 join( ': ',
240 'LabManager SOAP error', $res->faultcode,
241 $res->faultstring, $res->faultdetail );
242 }
243  
244 # Get the configID of the checked out config
245 0 my $checked_out_config_ID = $res->result;
246  
247 # Deploy that config...
248 0 $res = $soap->ConfigurationDeploy(
249 $auth_header,
250 SOAP::Data->name(
251 'configurationId' => $checked_out_config_ID
252 )->type('s:int'),
253 SOAP::Data->name( 'isCached' => 'false' )
254 ->type('s:boolean'),
255 SOAP::Data->name( 'fenceMode' => 4 )->type('s:int')
256 );
257  
258 #check for error...
259 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
260 {
261 return
262 0 join( ': ',
263 'LabManager SOAP error', $res->faultcode,
264 $res->faultstring, $res->faultdetail )
265 . "\n";
266 }
267  
268 0 return $checked_out_config_ID;
269 }
270  
271 =head2 get_machines
272  
273 This method returns an array of all the Machines in the deployed config. For each
274 machine it includes mac address, name, internal IP, external IP, name & its id.
275  
276 =cut
277  
278 sub get_machines
279 0 ▶{
Subroutine get_machines: not called CC=2 SCAR=17.9
Pod documented
280 0 my $self = shift;
281 0 my $config_ID = shift; # config ID that we got when we deployed
282 0 my $soap = $self->get_soap();
283 0 my $auth_header = $self->get_auth_header();
284 0 my $res;
285  
286 #Get the list of machines in a given config
287 0 $res = $soap->ListMachines(
288 $auth_header,
289 SOAP::Data->name(
290 'configurationId' => $config_ID
291 )->type('s:int')
292 );
293  
294 #check for error...
295 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
296 {
297 return
298 0 join( ': ',
299 'LabManager SOAP error', $res->faultcode,
300 $res->faultstring, $res->faultdetail );
301 }
302  
303 #return an array of all machines information.
304 0 return $res->paramsall;
305 }
306  
307 =head2 delete_config
308  
309 This method performs two actions - 1)undeploy the configuation (if it is not undeployed)
310 and 2) delete the configuation. This method takes the config ID of the configuration
311 you deployed earlier using the deploy_config() method.
312  
313 =cut
314  
315 sub delete_config
316 0 ▶{
Subroutine delete_config: not called CC=3 SCAR=24.8
Pod documented
317 0 my $self = shift;
318 0 my $config_ID = shift; # config ID that we got when we deployed
319 0 my $soap = $self->get_soap();
320 0 my $auth_header = $self->get_auth_header();
321 0 my $res;
322  
323 # Undeploy the configuration
324 0 $res = $soap->ConfigurationUndeploy(
325 $auth_header,
326 SOAP::Data->name(
327 'configurationId' => $config_ID
328 )->type('s:int')
329 );
330  
331 #check for error...
332 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
333 {
334 return
335 0 join( ': ',
336 'LabManager SOAP error', $res->faultcode,
337 $res->faultstring, $res->faultdetail );
338 }
339  
340 # Delete the configuration
341 0 $res = $soap->ConfigurationDelete(
342 $auth_header,
343 SOAP::Data->name(
344 'configurationId' => $config_ID
345 )->type('s:int')
346 );
347  
348 #check for error...
349 0 ▶ if ( $res->fault )
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
350 {
351 return
352 0 join( ': ',
353 'LabManager SOAP error', $res->faultcode,
354 $res->faultstring, $res->faultdetail );
355 }
356  
357 0 return 'Deleted';
358 }
359  
360 =head1 AUTHOR
361  
362 "Aditya Ivaturi", C<< <"ivaturi at gmail.com"> >>
363  
364 =head1 BUGS
365  
366 Please report any bugs or feature requests to C<ivaturi@gmail.com>, or through
367 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=VMware-LabManager>. I will be notified, and then you'll
368 automatically be notified of progress on your bug as I make changes.
369  
370  
371 =head1 SUPPORT
372  
373 You can find documentation for this module with the perldoc command.
374  
375 perldoc VMware::LabManager
376  
377  
378 You can also look for information at:
379  
380 =over 4
381  
382 =item * RT: CPAN's request tracker
383  
384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=VMware-LabManager>
385  
386 =item * AnnoCPAN: Annotated CPAN documentation
387  
388 L<http://annocpan.org/dist/VMware-LabManager>
389  
390 =item * CPAN Ratings
391  
392 L<http://cpanratings.perl.org/d/VMware-LabManager>
393  
394 =item * Search CPAN
395  
396 L<http://search.cpan.org/dist/VMware-LabManager/>
397  
398 =back
399  
400  
401 =head1 LICENSE AND COPYRIGHT
402  
403 Copyright 2010 "Aditya Ivaturi".
404  
405 This program is free software; you can redistribute it and/or modify it
406 under the terms of either: the GNU General Public License as published
407 by the Free Software Foundation; or the Artistic License.
408  
409 See http://dev.perl.org/licenses/ for more information.
410  
411  
412 =cut
413  
414 3 1; # End of VMware::LabManager
↑ Summary