6.0

Table Of Contents
VMware, Inc. 35
Chapter 3 Refining vSphere SDK for Perl Scripts
Foreachsimpleproperty(string,Boolean,numericdatatype),includinginheritedsimpleproperties,
theSDKcreatesanaccessormethod.Theaccessormethodnameisthesameasthepropertyname.
Arraysofpropertiesbecomearraysofpropertiesofthesamename.
Becausemanyoftheserversidemanagedobjectshavealargenumberofproperties,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
tothevSphereWebServicesSDKReferenceforalistofpropertiesforeachserversidemanagedobject.Property
pathscanbefull
paths,andcanincludenestedproperties.Propertiesdonothavetobetoplevelmanaged
objectproperties.
Thefollowingexampleillustratesfilteringbyproperty.
1 Populateavirtualmachineviewwithpowerstateinformationonly,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()workswithfullypopulatedviewsaswell.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.
OnUNIXlikecommandlines,surroundnamesthatcontainspecialcharacterswithsinglequotes,anduse
percent(%) astheescapecharacter.