6.5
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. 35
Chapter 3 Refining vSphere SDK for Perl Scripts
Foreachsimpleproperty(string,Boolean,numericdatatype),includinginheritedsimpleproperties,
theSDKcreatesanaccessormethod.Theaccessormethodnameisthesameasthepropertyname.
Arraysofpropertiesbecomearraysofpropertiesofthesamename.
Becausemanyoftheserver‐sidemanagedobjectshavealargenumberofproperties,accessingonlyasmall
numberofobjectscanpotentiallyresultinnoticeableperformancedegradation.Youuseapropertiesfilterto
populatetheviewobjectonlywith
propertiesyouareinterestedintoavoidthatproblem.
Using View Subroutines with a Properties Argument
Theviewsubroutines—get_view(),get_views(),find_entity_view(),andfind_entity_views()
—canacceptapropertiesargumentthatconsistsofalistofpropertypathsforretrievalfromtheserver.Go
tothevSphereWebServicesSDKReferenceforalistofpropertiesforeachserver‐sidemanagedobject.Property
pathscanbefull
paths,andcanincludenestedproperties.Propertiesdonothavetobetop‐levelmanaged
objectproperties.
Thefollowingexampleillustratesfilteringbyproperty.
1 Populateavirtualmachineviewwithpower‐stateinformationonly,asfollows:
my $vm_view = Vim::find_entity_view(
view_type => 'VirtualMachine',
filter => { 'name' => 'foo' },
properties => [ 'runtime.powerState' ]
);
2Usetheviewobject’sget_property()method.Notethat$vm_viewisanarrayreference,notascalar.
my $state = $vm_view->get_property('runtime.powerState');
3Ifyouareinterestedinsubpropertiesoftheretrievedproperties,youcanretrievethemlikethis:
my $vm_view = Vim::find_entity_view(
view_type => 'VirtualMachine',
filter => { 'name' => 'foo' },
properties => [ 'config.hardware' ]);
my $memsize = $vm_view->get_property('config.hardware.memoryMB');
get_property()workswithfully‐populatedviewsaswell.Thefollowingcodefragmentsuses
get_propertytoretrieveapropertyfromavirtualmachine.
my $vm_view = Vim::find_entity_view(
view_type => 'VirtualMachine',
filter => { 'name' => 'foo' });
my $memsize = $vm_view->get_property('config.hardware.memoryMB');
Thefollowingcodefragment,whichretrievesthesamepropertybytraversingthetree,hasthesameresult.
my $vm_view = Vim::find_entity_view(
view_type => 'VirtualMachine',
filter => { 'name' => 'foo' });
my $memsize = $vm_view->config->hardware->memoryMB;
Whenyouuseafilteredviewandattempttoreadapropertythatwasnotretrievedfromtheserver,theresult
isthesameasifthepropertywereunset.
Using Filters on the Utility Application Command Line
Whenyourunautilityapplicationthattakesargumentsspecifyingnamesforvirtualmachines,hostsystems,
andsoon,youmustsupplytheexactnameonthecommandline.Regularexpressionsarenotaccepted.
Whenyourunautilityapplication,therearesomerestrictionsonspecialcharacters:
Invirtualmachinenames,youmustrepresentthecharacterforwardslash(/)as%2f,backwardslash(\)
as%5c,andpercent(%)as%25whentheyappearinvirtualmachinenames.
OnUNIX‐likecommandlines,surroundnamesthatcontainspecialcharacterswithsingle‐quotes,anduse
percent(%) astheescapecharacter.