Working with View Object Property Values
vSphere SDK for Perl view objects are Perl objects. You can retrieve a view, access and manipulate its properties, and call its methods using Perl’s object-oriented syntax.
Accessing Property Values
Each property is defined as a specific data type and can be one of the following:
The ManagedEntity managed object has a name property of type string.
A HostSystem managed object contains an array of virtual machines that are hosted by the corresponding physical machine.
A virtual machine’s power state can be a poweredOn, poweredOff, or suspended string value.
AboutInfo, Action, and ServiceContent are all data objects.
Accessing Simple Property Values
To access a simple property from a view, call the property’s accessor on the view object. The accessor has the same name as the property itself, as follows:
$view_name->property_name
As shown in Example: Sample Script (Commented Version), you can access the name property of entity_view by calling its name method, as follows:
my $entity_name = $entity_view->name;
Accessing Enumeration Property Values
To retrieve the value of a property defined as an enumeration, you must dereference its value from within the containing object by qualifying the property with ->val. For example, the power state of a virtual machine (powerState) is a member of the runtime data object.
To retrieve the value of powerState, you must dereference the two containing objects (the view object and the runtime data object) and the value itself (val), as follows:
$vm_view->runtime->powerState->val
Because powerState is an enumeration, you use runtime->powerState->val to retrieve its string value.
foreach my $vm (@$vm_views) {
if ($vm->runtime->powerState->val eq 'poweredOn') {
print "Virtual machine " . $vm->name . " is powered on.\n";
}
else {
print "Virtual machine " . $vm->name . " is not powered on.\n";
}
Modifying Property Values
You can modify a data object’s property value by passing the new value, as follows:
$data_object-> <property> (<new value>);
$data_object is a blessed reference to a Perl object or class name, and property is a method call on the object.
For example, you can change the force property to false, as follows:
$host_connect_spec->force ('false');
To create an enumeration data object, use a string value as the argument to the enumeration type’s constructor.
my $power_state = VirtualMachinePowerState->new('poweredOff');
Typically, enumerations are used as arguments to methods:
$vm->MigrateVM(
host => $target_host,
priority => VirtualMachineMovePriority->new('defaultPriority'),
state => VirtualMachinePowerState->new('poweredOff'),
);
Creating Data Objects with Properties
You create data objects with constructors that have names corresponding to the classes of the data objects in the vSphere API. The constructor syntax follows common Perl conventions. The arguments supplied to the constructor are key-value pairs, where each key is the name of an object property, and the corresponding value is the value with which the property is initialized.
For example, creating a virtual machine requires the creation of a data structure that includes a number of nested data objects. One of those objects is a VirtualMachineFieldInfo data object, which can be constructed as follows:
my $files = VirtualMachineFileInfo->new
(
logDirectory => undef,
snapshotDirectory => undef,
suspendDirectory => undef,
vmPathName => $ds_path
);
The VirtualMachineFileInfo object is then contained within a VirtualMachineConfigSpec object:
my $vm_config_spec = VirtualMachineConfigSpec->new(
name => $args{vmname},
memoryMB => $args{memory},
files => $files,           # <-- here
numCPUs => $args{num_cpus},
guestId => $args{guestid},
deviceChange => \@vm_devices
);
This code is taken from the apps/vm/vmcreate.pl utility application. See the scripts in the apps and samples directories for examples of simple and complex uses of data objects.
To set the value of a property that is defined as an enumeration, you must pass the new value to the data object as follows:
$<ref> = new <enum_type> ('<val>');
For example, you can change the power state as follows:
$power_state = new VirtualMachinePowerState ('poweredOff');