VM Naming Change

We recently exposed a new method to the Automate service models, giving users the ability to call the VM naming method from an Automate method.

During lifecycle provisioning, VM naming runs when the provision task is created. Service provisioning uses Service dialogs which can override the VM name. The timing is an issue since the Service dialogs are processed after task creation.

While exposing the new update_vm_name method enables users to manually call the method within a state machine, our goal is to move VM naming to task processing which runs after the Service Dialog.

I called the following test method from my VM state machine preprovision state:


prov = $evm.root['miq_provision']
$evm.log('info', "testing VM Naming  change")
$evm.log('info', "vm_target_name before: <#{prov.options[:vm_target_name]}> ")

# Could use Service dialog value here to setup new name
test_vm_name = "test_new_name_here"
prov.update_vm_name("#{test_vm_name}$n{3}",:update_request => false)

$evm.log('info', "vm_target_name after: <#{prov.options[:vm_target_name]}> ")

update_vm_name method call:
takes a single argument to pass the new vm name:
prov.update_vm_name("#{test_vm_name}$n{3}")

optional 2 argument “:update_request = true/false” (default is true)
prov.update_vm_name("#{test_vm_name}$n{3}",:update_request => false)

1 Like

Here is an updated version of a sample vm naming method.
Call this early in the Infrastructure state machine. I used the 2nd slot (AcquireIPAddress) to run this.

prov = $evm.root['miq_provision']
dialog_options = prov.miq_request.options[:dialog]

if dialog_options
  $evm.log('info', "Checking for vm_name overrides: <#{prov.options[:vm_target_name]}> ")

  dialog_options.each do |key, value|
    next if value.blank?
    $evm.log("info", "Key #{key}, value #{value}")
    if /vm_name/i =~ key
      prov.update_vm_name("#{prov.options[:vm_target_name]}$n{4}",:update_request => false)
      $evm.log('info', "vm_naming updating vm_target_name after: <#{prov.options[:vm_target_name]}> ")
      exit MIQ_OK
    end
  end
end
2 Likes

The updated sample vm naming method shown above can be used to create a work around for the VM naming issue. We tested it in the second state(acquireipaddress) of the VM provisioning state machine.

The method calls the update_vm_name method if the service dialog contains a vm_name variable, and uses a $n{4} suffix.

*Note - Not tested on a service bundle. Would need to be modified to look for a specific vm_name service dialog variable.(option_1_vm_name, option2_vm_name…)

Can we use prepovision state machine to add the update_vm_name, will that work

A better vm_naming method for Infrastructure.


prov = $evm.root['miq_provision']
dialog_options = prov.miq_request.options[:dialog]

if dialog_options
  $evm.log('info', "Checking for vm_name overrides: <#{prov.options[:vm_target_name]}> ")
  
  dialog_options.each do |key, value|
    next if value.blank?
    $evm.log("info", "Key #{key}, value #{value}")
    if /vm_name/i =~ key
      # if value = changeme, then instantiate to find value of vm_prefix and use that
      if value == 'changeme' 
        vm_name_instance = '/Infrastructure/VM/Provisioning/Naming/default' 
        $evm.instantiate(vm_name_instance)
        obj = $evm.object(vm_name_instance)
        value = obj["vm_prefix"] || "cfme"
        $evm.log('info', "Using vm_prefix: <#{value.inspect}> ")
      end
      prov.update_vm_name("#{value}$n{4}",:update_request => false)
      $evm.log('info', "vm_naming updating vm_target_name after: <#{prov.options[:vm_target_name]}> ")
      exit MIQ_OK
    end
  end
end```
1 Like