How do I retrieve VM data within a manageiq post provisioning method call?


#1

Specifically I’m trying to lookup the uid_ems value for a VM during a post1 call on CatalogItemInitialization.

I can look the uid_ems value up after the fact by browsing to My Services, looking up the VM id and passing that as the param for a rest call to /api/vms/#{vmid} but I need to do it automatically within my method. My first though was to try $evm.root[‘vm’] but it seems to be a nilClass. What is the variable where the uid_ems is stored?


#2

You could try something like this since the vm object doesnt exist until after the task ‘miq_provision’ is complete.

case $evm.root[‘vmdb_object_type’]

when 'miq_provision’
prov = $evm.root[‘miq_provision’]
log(:info, “Provision:<#{prov.id}> Request:<#{prov.miq_provision_request.id}> Type:<#{prov.type}>”)

# get vm object from miq_provision. This assumes that the vm container on the management system is present
vm = prov.vm

do something

end


#3

The ‘miq_provision’ object wasn’t available in the $evm.root for my method, however the ‘service_template_provision_task’ is and it seems like the following works to get the list of VMs and their uid_ems values.

provisioned_vms = $evm.root['service_template_provision_task'].destination.vms

# log each vm's uid_ems
provisioned_vms.each do |vm|
   $evm.log("info", "VM id: <#{vm.uid_ems}>")
end