my $vm_list = Vim::find_entity_views(view_type => 'VirtualMachine', filter => \{"config.name" => $vmimagename});
foreach my $vm(@$vm_list) {
$hostobj = $vm->runtime->host;
print $hostobj->name; #summary->HostConfigSummary->name;
}
I am trying to $host to be an object that is the host of the image $vm and then print the name, but it doesn't work. I am getting
"Can't locate object method "name" via package "ManagedObjectReference" at GetHos
tName.pl line 29."
what am I doing wrong?
Message was edited by:
tdfeathe
my $mor_host = $vm->runtime->host;
my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
Those lines fixed it. Can someone explain this all to me? I am new to this API and I am struggling to get the little done that I need it to do.
Well, where do I start.
A MoRef is a pointer to an object on the server. To get a copy of the object (called a view in VI Perl), you have to perform a get_view or find_entity_view operation which creates a local copy (a view).
Once you have the view, then all the properties in that object are available. Of course, if one of those properties is a view, then you need to get it from the server.
So, in your example, you retrieved the property called "$host->runtime->host" which was a MoRef. Before you could use any properties off that object, then you needed to get a copy from the server.
In other words,
$hostobj = $vm->runtime->host.
$host_view = Vim::get_view (moref => $hostobj);
then
$host_view->name would work because you can now grab a property from an object that you retrieved from a server.
Lots of analogies but you just simply need to remember that if the property is a moref then you need to get a copy of it (a view) from the server before you can retrieve its properties.
H
One more thing. In the example that fixed it:
my $mor_host = $vm->runtime->host;
my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
then I would always do this:
my $mor_host = $vm->runtime->host;
my $host_view = Vim::get_view(mo_ref => $mor_host);
my $host = $host_view->name;
That way, you still had a reference to the host view just in case you wanted to get another property in the Host Object. Otherwise, you would do
my $hoststats = Vim::get_view(mo_ref => $mor_host)->runtime->status;
which means that you would do a round trip to the host in order to get one property. It would work but would become awful slow in large configs.
H
hello, I do not know too language Perl, and I will like to find the
name of the host which lodges my virtual machine.
Your program seems
to be exactly what I need, can you me communicate it, please.
Thank you in advance.
Andre