vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> <strong>Programming</strong> <strong>Guide</strong><br />
Table 2-1. Basic <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Script (simpleclient.pl) (Continued)<br />
Code element Discussed in<br />
my %opts = (<br />
entity => {<br />
type => "=s",<br />
variable => "VI_ENTITY",<br />
help => "ManagedEntity type: HostSystem, etc",<br />
required => 1,<br />
},<br />
);<br />
Opts::add_options(%opts);<br />
Opts::parse();<br />
Opts::validate();<br />
Step 1: Import the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Modules<br />
All <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> scripts must use the <strong>VMware</strong>::VIRuntime module:<br />
use <strong>VMware</strong>::VIRuntime;<br />
This module handles all client‐side infrastructure details. For example, it transparently maps data types and<br />
provides local <strong>Perl</strong> interfaces to server‐side objects. The module also loads subroutines that you can use to<br />
connect to a vCenter Server or ESX/ESXi system and to retrieve views. Views are the client‐side <strong>Perl</strong> objects<br />
that encapsulate the properties and operations of server‐side managed objects. The subroutines are organized<br />
into different packages:<br />
The Opts package subroutines handle built‐in options and creating custom options.<br />
The Util package subroutines facilitate routine tasks, such as setting up and closing connections to the<br />
server.<br />
The Vim package subroutines access server‐side managed objects, instantiate local proxy objects (views),<br />
update properties, and run local methods that result in operations on remote servers.<br />
See “<strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Subroutine Reference” on page 41.<br />
“Step 2: (Optional) Define Script‐Specific<br />
Command‐Line Options” on page 20.<br />
Util::connect(); “Step 3: Connect to the Server” on page 22.<br />
# Obtain all inventory objects of the specified type<br />
my $entity_type = Opts::get_option('entity');<br />
my $entity_views = Vim::find_entity_views(<br />
view_type => $entity_type);<br />
# Process the findings and output to the console<br />
<strong>for</strong>each my $entity_view (@$entity_views) {<br />
my $entity_name = $entity_view->name;<br />
Util::trace(0, "Found $entity_type:<br />
$entity_name\n");<br />
}<br />
# Disconnect from the server<br />
Util::disconnect();<br />
“Step 4: Obtain View Objects of Server‐Side<br />
Managed Objects” on page 22.<br />
“Step 5: Process Views and Report Results”<br />
on page 22.<br />
“Step 6: Close the Server Connection” on<br />
page 23.<br />
Step 2: (Optional) Define Script-Specific Command-Line Options<br />
When you run a script from the command line, you usually specify connection in<strong>for</strong>mation and might also<br />
specify other in<strong>for</strong>mation such as a virtual machine that you want to power off or a host <strong>for</strong> which you need<br />
status in<strong>for</strong>mation. <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> lets you specify these options in a variety of ways. See “Specifying<br />
Options” on page 13.<br />
A number of common command‐line options, most of them connection options, are already defined <strong>for</strong> all<br />
utility applications (see Table 1‐3, “Options Available <strong>for</strong> all <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Scripts,” on page 15). In<br />
addition, most applications have application‐specific options you pass to the script at execution time.<br />
20 <strong>VMware</strong>, Inc.