List VM in a service dialog

Hello

In a service dialog, I need to list VMs.

I tried this, but it does not work :
vm = $evm.vmdb(‘vm’).find_by_name(‘dialog_vm’)
In my dropdown list, that is desperatly empty :frowning:

Do you have an idea ?

Thank you
Philippe

$evm.vmdb(‘vm’).find_by_name(‘dialog_vm’)

First: This code either finds a VM with the name “dialog_vm” or returns nil. I would guess you want something like this:

vm_name = $evm.root['dialog_vm_name'] # Get name from dialog
vm_list = $evm.vmdb(:vm).where("name LIKE ?", "%#{vm_name}%")  # Get all VMs with matching names
values_hash = {}
vm_list.each do |vm|
  values_hash[vm.id] = vm.name # Save the ID and the Name of the VM into values
end

Second: To display the values in a Service Dialog, you need to return it in a specific format from your method, as described here: https://pemcg.gitbooks.io/mastering-automation-in-cloudforms-4-2-and-manage/content/service_dialogs/chapter.html


Third: In this case you probably want to use a expression methods, as they are faster than instantiating a full ruby method. Although it is not really intuitive to use parameters in expression methods

https://manageiq.gitbook.io/mastering-cloudforms-automation-addendum/chapter#input-parameters

Thank you for your answer.

I tried this, but there are not list with my VM :
vm_name = $evm.root[‘dialog_param_var_host’] # Get name from dialog
vm_list = $evm.vmdb(:vm).where(“name LIKE ?”, “%#{vm_name}%”) # Get all VMs with matching names
values_hash = {}
vm_list.each do |vm|
values_hash[vm.id] = vm.name # Save the ID and the Name of the VM into values
end

I tried this but it is the same thing :
vm_name = EVX # VM starting by EVX
vm_list = $evm.vmdb(:vm).where(“name LIKE ?”, “%#{vm_name}%”) # Get all VMs with matching names
values_hash = {}
vm_list.each do |vm|
values_hash[vm.id] = vm.name # Save the ID and the Name of the VM into values
end

Thank you for your help

I found :slight_smile:

I added this to the end :
list_values = {
‘sort_by’ => :value,
‘data_type’ => :string,
‘required’ => false,
‘values’ => values_hash
}
list_values.each { |key, value| $evm.object[key] = value }

The only way to get data in or out of your method (or to tell ManageIQ to do something for you) is through the $evm variable.

If you haven’t come accross it yet, I highly recommend reading this book and the followup book which covers the major differences between 4.2 and 4.6