6.0
Table Of Contents
- vSphere SDK for Perl Programming Guide
- Contents
- Getting Started with vSphere SDK for Perl
- Writing vSphere SDK for Perl Scripts
- Refining vSphere SDK for Perl Scripts
- Creating and Using Filters
- Filtering Views Selectively Using Properties
- Retrieving the ServiceInstance Object on a vSphere Host
- Saving and Using Sessions
- Using Multiple Sessions
- Learning About Object Structure Using Data::Dumper
- Specifying Untyped Arguments in Scheduled Tasks and Callbacks
- Using Advanced Subroutines
- vSphere SDK for Perl Subroutine Reference
- Web Services for Management Perl Library
- Credential Store Perl Library
VMware, Inc. 37
Chapter 3 Refining vSphere SDK for Perl Scripts
Vim::load_session(session_file => '.visession');
Alternatively,youcanloadasessionusingtheobject‐orientedsyntaxasfollows:
my $service_url = "https://$server/sdk/vimService";
my $vim = Vim->new(service_url => $service_url);
$vim = $vim->load_session(session_file => '.visession');
Using Multiple Sessions
Insomecases,youmightwanttocreatesessionsonseveralvSphereserversatonce,orcreatemorethanone
sessiononthesameserver.
EachtimeanapplicationconnectstoaserverinthevSphereenvironment,asessionbetweentheapplication
andtheserveriscreated.ThevSphereSDKfor
PerlrepresentsthesessionasavSphereSDKforPerlobject.
Whenyouusesinglesessions,oneglobalobjectisimplicitforthesessions.
Formultipleobjects,youcannotusetheimplicitglobalvSphereobject.Instead,youmustcreateanduse
vSphereobjectsexplicitly,andusetheobject‐orientedsyntaxfor
callingvSphereSDKforPerlmethods.
Youcreateanopensessionintwostages.
1 CreateavSphere objectusingthenew()constructor.
2Loginbycallingtheobject‐orientedlogin()method.Theargumentstotheobject‐orientedlogin()
methodarethesameasfortheproceduralVim::login()subroutine.
MostproceduralVim::methods
haveanobject‐orientedcounterpart.Theproceduralmethodsoperateonan
implicitlyspecifiedglobalvSphereobject.Object‐orientedmethodsoperateontheexplicitlysuppliedvSphere
object.
Thefollowingcodefragmentfrom/samples/sessions/multisession.plillustrateshowtousemultiple
sessions,usingtheobject‐orientedprogrammingstyleinvSphereSDKforPerl.
Example 3-3. Using Multiple Sessions
use VMware::VIRuntime;
...
# create object for each host
my @vim_objs;
my $url;
$url = Opts::get_option('url');;
push @vim_objs, Vim->new(service_url => $url);
$url = Opts::get_option('url2');
push @vim_objs, Vim->new(service_url => $url);
# login to all hosts
my $username = Opts::get_option('username');
my $password = Opts::get_option('password');
$vim_objs[0]->login(user_name => $username, password => $password);
if (Opts::option_is_set('username2')) {
$username = Opts::get_option('username2');
}
if (Opts::option_is_set('password2')) {
$password = Opts::get_option('password2');
}
$vim_objs[1]->login(user_name => $username, password => $password);
# list VM's for all hosts
foreach my $vim_obj (@vim_objs) {
print "List of virtual machines:\n";
my $vm_views = $vim_obj->find_entity_views(view_type => 'VirtualMachine');
foreach my $vm (@$vm_views) {
print $vm->name . "\n";
}
print "\n";