VMware: set extra configs for virtual machine (#36521)

This fixes implementation of extra config for virtual machine.

Fixes: #32004

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
(cherry picked from commit 80fb836ce5)
This commit is contained in:
Abhijeet Kasurde 2018-02-21 21:44:44 +05:30 committed by GitHub
parent a3e02a2417
commit 101578b63f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -753,10 +753,14 @@ class PyVmomiHelper(object):
self.configspec.deviceChange.append(nic)
self.change_detected = True
def customize_customvalues(self, vm_obj):
def customize_customvalues(self, vm_obj, config_spec):
if len(self.params['customvalues']) == 0:
return
vm_custom_spec = config_spec
vm_custom_spec.extraConfig = []
changed = False
facts = self.gather_facts(vm_obj)
for kv in self.params['customvalues']:
if 'key' not in kv or 'value' not in kv:
@ -764,12 +768,15 @@ class PyVmomiHelper(object):
# If kv is not kv fetched from facts, change it
if kv['key'] not in facts['customvalues'] or facts['customvalues'][kv['key']] != kv['value']:
try:
vm_obj.setCustomValue(key=kv['key'], value=kv['value'])
self.change_detected = True
except Exception as e:
self.module.fail_json(msg="Failed to set custom value for key='%s' and value='%s'. Error was: %s"
% (kv['key'], kv['value'], to_text(e)))
option = vim.option.OptionValue()
option.key = kv['key']
option.value = kv['value']
vm_custom_spec.extraConfig.append(option)
changed = True
if changed:
self.change_detected = True
def customize_vm(self, vm_obj):
# Network settings
@ -1330,7 +1337,11 @@ class PyVmomiHelper(object):
task = vm.ReconfigVM_Task(annotation_spec)
self.wait_for_task(task)
self.customize_customvalues(vm_obj=vm)
if self.params['customvalues']:
vm_custom_spec = vim.vm.ConfigSpec()
self.customize_customvalues(vm_obj=vm, config_spec=vm_custom_spec)
task = vm.ReconfigVM_Task(vm_custom_spec)
self.wait_for_task(task)
if self.params['wait_for_ip_address'] or self.params['state'] in ['poweredon', 'restarted']:
self.set_powerstate(vm, 'poweredon', force=False)
@ -1358,7 +1369,7 @@ class PyVmomiHelper(object):
self.configure_cpu_and_memory(vm_obj=self.current_vm_obj)
self.configure_disks(vm_obj=self.current_vm_obj)
self.configure_network(vm_obj=self.current_vm_obj)
self.customize_customvalues(vm_obj=self.current_vm_obj)
self.customize_customvalues(vm_obj=self.current_vm_obj, config_spec=self.configspec)
if self.params['annotation'] and self.current_vm_obj.config.annotation != self.params['annotation']:
self.configspec.annotation = str(self.params['annotation'])