18.08.2013 Views

vSphere SDK for Perl Programming Guide - Documentation - VMware

vSphere SDK for Perl Programming Guide - Documentation - VMware

vSphere SDK for Perl Programming Guide - Documentation - VMware

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> <strong>Programming</strong> <strong>Guide</strong><br />

The Vim::find_entity_views() subroutine uses the value the user passes in later in the script. The value<br />

must be one of the managed‐entity types listed as view_type parameter supported by<br />

Vim::find_entity_views().<br />

NOTE Your script must call Opts::parse() and Opts::validate() to process the options available <strong>for</strong> all<br />

scripts, even if you do not define script‐specific command‐line options.<br />

When you attempt to run a script and do not supply all necessary options, the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> displays<br />

usage help <strong>for</strong> the script, as in the following example:<br />

perl simpleclient.pl<br />

Required command option 'entity' not specified<br />

Common VI options:<br />

. . .<br />

Command-specific options:<br />

--entity (required)<br />

ManagedEntity type: ClusterComputeResource, ComputeResource, Datacenter,<br />

Folder, HostSystem, ResourcePool, VirtualMachine...<br />

Step 3: Connect to the Server<br />

The <strong>vSphere</strong> API is hosted as a secure Web service on a vCenter Server and ESX/ESXi system. By default, the<br />

Web service is available over HTTPS. Clients must provide valid credentials to connect to the service.<br />

Depending on the specifics of your server, you might have to enter only a user name and password. You might<br />

need other in<strong>for</strong>mation. 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.<br />

A call to Util::connect() connects to the server.<br />

When a script reaches the call to Util::connect(), the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> runtime checks the environment<br />

variables, configuration file contents, and command‐line entries (in this order) <strong>for</strong> connection options. If the<br />

options are not defined, the runtime uses the defaults (localhost and no user name and password) to set up<br />

the connection.<br />

Step 4: Obtain View Objects of Server-Side Managed Objects<br />

When you call the subroutines in the Vim package to retrieve entities from the host, the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong><br />

runtime creates the corresponding <strong>Perl</strong> objects (view objects) locally.<br />

Example 2‐1 uses the Opts::get_option() subroutine to assign to $entity_type the string value of the<br />

parameter that the user passes in when executing the script. Example 2‐1 then uses $entity_type as the<br />

view_type parameter in the subsequent call to Vim::find_entity_views().<br />

# get all inventory objects of the specified type<br />

my $entity_type = Opts::get_option('entity');<br />

my $entity_views = Vim::find_entity_views(view_type => $entity_type);<br />

The Vim::find_entity_views() subroutine creates a local <strong>Perl</strong> object (an array of references) from the<br />

server‐side managed object of the specified entity type.<br />

IMPORTANT This object is static and must be explicitly updated when the corresponding server‐side object<br />

changes.<br />

Step 5: Process Views and Report Results<br />

The last part of the script processes the views. For this step, you must know the view objects’ properties and<br />

methods, so you must understand the server‐side objects. See “Understanding Server‐Side Objects” on page 24<br />

<strong>for</strong> an introduction. For in‐depth in<strong>for</strong>mation about server‐side objects, see the <strong>vSphere</strong> API Reference <strong>Guide</strong><br />

which is included on the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> documentation page.<br />

Because views are <strong>Perl</strong> objects, you use <strong>Perl</strong> object‐oriented syntax to process the views. Example 2‐1 loops<br />

through the array of entities returned (@$entity_views) and accesses the name property of each entity by<br />

calling $entity_view->name. The example then prints the name of each entity to the console.<br />

22 <strong>VMware</strong>, Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!