VMware {code} Community
marck004
Contributor
Contributor
Jump to solution

PowerCLI vs Perl SDK

On the PowerCLI side, I can execute:

set-template -template <name> -tovm

...which will convert a template to a VM.

On the Perl SDK side, I found the MarkAsVirtualMachine call which appears to do something similar.

However, on the Perl SDK side the above call requires a ResourcePool be passed whereas the PowerCLI command does not require a ResourcePool.

In our environment we are not using defined ResourcePools - we do have ClusterComputeResources, but that does not help me.

Can anyone offer any suggestions to getting something on the Perl SDK side that performs similarly to the set-template on the PowerCLI side?

Thanks,

-M

Reply
0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

Yeah, probably have to hop through the host.

my $vm_name = "test_vm_template";

my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => { 'name' => $vm_name });

my $host_ref = $vm->{'runtime'}{'host'};

my $host = Vim::get_view(mo_ref => $host_ref, properties => ['name', 'parent']);

my $compute = Vim::get_view(mo_ref => $host->{'parent'}, properties => ['name', 'resourcePool']);

my $pool = Vim::get_view(mo_ref => $compute->{'resourcePool'}, properties => ['name']);

# Now use $pool as parameter to MarkAsVirtualMachine

I'm doing this completely from the SDK doc, so I'm sure there are typos.

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

Reply
0 Kudos
4 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

So even without any ResourcePools in our vSphere Client UI, there are some default resource pool entities.

You can get it from your virtual machine object with the property 'resourcePool'.  That's most likely what the PowerCLI cmdlet is doing, just using the same resource pool the template is current in (template and virtual machines are the same object, it's just a boolean flag).

So, from Perl, assuming you got the resourcePool property from your get_view or find_entity_view calls:

$pool_moref = $vm->{'resourcePool'};

$pool = Vim::get_view(mo_ref => $pool_moref);

# ...

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
marck004
Contributor
Contributor
Jump to solution

Thanks for the reply, Stumpr.  This makes sense; however, I am still needing some assistance with the proof of concept.

I have this:

my $vm_name = "test_vm_template"

my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => $vm_name});

print "vm name = " . $vm->name . "\n";

$pool_moref = $vm->{'resourcePool'};

$pool = Vim::get_view(mo_ref => $pool_moref);

print "resource name = " . $pool->name . "\n";

The 1st print statement is showing me the vm name but the 2nd is returning an error of (can't call method "type" on an undefined value).

It appears I am attempting to access the resource pool incorrectly. 

Reply
0 Kudos
marck004
Contributor
Contributor
Jump to solution

Upon further testing it appears the error I am receiving is with the 2nd suggested command:

my $vm_name = "test_vm_template"

my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => $vm_name});

print "vm name = " . $vm->name . "\n";

$pool_moref = $vm->{'resourcePool'};

$pool = Vim::get_view(mo_ref => $pool_moref);

print "resource name = " . $pool->name . "\n";

The error is happening on line #5.

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Yeah, probably have to hop through the host.

my $vm_name = "test_vm_template";

my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => { 'name' => $vm_name });

my $host_ref = $vm->{'runtime'}{'host'};

my $host = Vim::get_view(mo_ref => $host_ref, properties => ['name', 'parent']);

my $compute = Vim::get_view(mo_ref => $host->{'parent'}, properties => ['name', 'resourcePool']);

my $pool = Vim::get_view(mo_ref => $compute->{'resourcePool'}, properties => ['name']);

# Now use $pool as parameter to MarkAsVirtualMachine

I'm doing this completely from the SDK doc, so I'm sure there are typos.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos