vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3 Refining <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Scripts<br />
Alternatively, you can use save_session() with the object‐oriented syntax (see “Using Multiple Sessions” on<br />
page 37):<br />
...<br />
# object-oriented login<br />
my $service_url = "https://$server/sdk/vimService";<br />
my $vim = Vim->new(service_url => $service_url);<br />
$vim->login(user_name => $username, password => $password); ...<br />
# save session $vim in file .mysession<br />
$vim->save_session(session_file => '.mysession');<br />
The session remains active until the program runs a log out or disconnect operation, or until the program times<br />
out. Time out is 30 minutes after the last operation was per<strong>for</strong>med.<br />
Loading Sessions<br />
You can use load_session() to load a saved session into the global session as follows:<br />
Vim::load_session(session_file => '.visession');<br />
Alternatively, you can load a session using the object‐oriented syntax as follows:<br />
my $service_url = "https://$server/sdk/vimService";<br />
my $vim = Vim->new(service_url => $service_url);<br />
$vim = $vim->load_session(session_file => '.visession');<br />
Using Multiple Sessions<br />
In some cases, you might want to create sessions on several <strong>vSphere</strong> servers at once, or create more than one<br />
session on the same server.<br />
Each time an application connects to a server in the <strong>vSphere</strong> environment, a session between the application<br />
and the server is created. The <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> represents the session as a <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> object.<br />
When you use single sessions, one global object is implicit <strong>for</strong> the sessions.<br />
For multiple objects, you cannot use the implicit global <strong>vSphere</strong> object. Instead, you must create and use<br />
<strong>vSphere</strong> objects explicitly, and use the object‐oriented syntax <strong>for</strong> calling <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> methods.<br />
You create an open session in two stages.<br />
1 Create a <strong>vSphere</strong> object using the new() constructor.<br />
2 Log in by calling the object‐oriented login() method. The arguments to the object‐oriented login()<br />
method are the same as <strong>for</strong> the procedural Vim::login() subroutine.<br />
Most procedural Vim:: methods have an object‐oriented counterpart. The procedural methods operate on an<br />
implicitly specified global <strong>vSphere</strong> object. Object‐oriented methods operate on the explicitly supplied <strong>vSphere</strong><br />
object.<br />
The following code fragment from /samples/sessions/multisession.pl illustrates how to use multiple<br />
sessions, using the object‐oriented programming style in <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong>.<br />
Example 3-3. Using Multiple Sessions<br />
use <strong>VMware</strong>::VIRuntime;<br />
...<br />
# create object <strong>for</strong> each host<br />
my @vim_objs;<br />
my $url;<br />
$url = Opts::get_option('url');;<br />
push @vim_objs, Vim->new(service_url => $url);<br />
$url = Opts::get_option('url2');<br />
push @vim_objs, Vim->new(service_url => $url);<br />
# login to all hosts<br />
my $username = Opts::get_option('username');<br />
my $password = Opts::get_option('password');<br />
$vim_objs[0]->login(user_name => $username, password => $password);<br />
<strong>VMware</strong>, Inc. 37