6.0

Table Of Contents
VMware, Inc. 31
Chapter 2 Writing vSphere SDK for Perl Scripts
Examples of Operations
ThefollowingtablelistssomeoftheoperationsavailableforaVirtualMachinemanagedobject.
SeethevSphereAPIReferenceGuideforlistsofalloperationsforeachmanagedobject.
Calling Methods
Afteryouhaveretrievedtheviewobjectthatcorrespondstoamanagedobject,youcanrunmethodsonthat
viewtomakeuseofthemanagedobjectsservices.Yourunamethodbyspecifyingthemethod’sname
parameter,forexample:
$vm->MigrateVM (name => 'productionVM');
ThetypeofparameterrequiredbythemethoddependsontheoperationdefinedinthevSphereAPI.Itmight
beasimpletype,dataobject,ormanagedobjectreference.Forinformationaboutspecificparametersanddata
types,seethevSphereAPIReferenceGuide.
Blockingoperationsarerunasmethodson
aviewobject.Forexample,tosuspendavirtualmachine,call:
$vm_view->SuspendVM();
Youcanexecuteanyoperationthatisdefinedforamanagedobjectasamethodonacorrespondingview
object.BecausethevSphereSDKforPerlcreatesanaccessorandamutatormethod(getterandsettermethod)
foreachpropertydefinedinthemanagedobject,youcanreferencethenameof
anypropertyasamethodcall
oftheview,forexample:
my $network_name = $network_view->name
ThevSphereSDKforPerlallowsyoutopassaviewobjecttoamethodthatrequiresa
ManagedObjectReference.Forexample,ifyouhavetheviewthatrepresentsahost($host),youcanpassthe
viewtothepowerOn()methodasfollows:
my $host = Vim::find_entity_view (view_type => 'HostSystem', name => 'my host');
my $vm = Vim::find_entity_view (view_type => 'VirtualMachine', name => 'my virtual machine');
$vm->powerOn (host => $host)
Omitting Optional Arguments in Method Calls
WhenyoucallavSphereAPImethodusingvSphereSDKforPerl,andwanttoomitanoptionalargument,
youcandooneoftwothings:
Youcanomittheargument:
$vm->PowerOnVM(host => $host); # with the optional host argument
$vm->PowerOnVM(); # without the optional host argument
Youcansupplyundefasthevalueoftheoptionalargument:
$vm->PowerOnVM(host => undef);
Supplyingundefasthevalueoftheoptionalargumentisusefulwhenthevalueofanargument,whichmight
ormightnotbeundef,iscontainedinavariable,asinthefollowingexample:
my $host = Vim::find_entity_view(
Table 2-4. Examples for Asynchronous and Synchronous Methods
vSphere API
Non-blocking (asynchronous)
vSphere SDK for Perl
Non-blocking (asynchronous)
vSphere SDK for Perl Only
Blocking (synchronous)
PowerOnVM_Task() PowerOnVM_Task() PowerOnVM()
CloneVM_Task() CloneVM_Task() CloneVM()
SuspendVM_Task() SuspendVM_Task() SuspendVM()
NOTE“SpecifyingUntypedArgumentsinScheduledTasksandCallbacks”onpage 39discussesusingthe
vSphereSDKforPerlPrimTypestructureinsomecalls.