VMware {code} Community
canadapatrick
Contributor
Contributor

Python pyvmomi and how to set a variable to ManagedObjectReference (type,value)

Trying to code something based on vmware ansible collection but specifically for a VM (change DRS or other HA settings).

Starting point: refer to vmware_cluster_drs module which calls drsConfig and vim.cluster.DrsConfigInfo

I am trying to instead use drsVmConfigSpec and dasVmConfigSpec which calls vim.cluster.DasVmConfigSpec / vim.cluster.DrsVmConfigSpec (info).

info for both have a key property with type key ManagedObjectReference to a VirtualMachine (type and value).

Challenge is how to set the key to a ManagedObjectReference

See below for what works in PowerCLI:

$spec.dasVmConfigSpec[0].info.key = New-Object VMware.Vim.ManagedObjectReference

How to do same in python:

<varname>.info.key = vmodl.ManagedObjectReference  ?

https://developer.vmware.com/apis/1355/vsphere
Type - > API reference to Data Object - ClusterConfigSpecEx(vim.cluster.ConfigSpecEx)

drsVmConfigSpec* ClusterDrsVmConfigSpec[]

Data Object Description: Updates the per-virtual-machine DRS configuration.

Sample of what we have tried and no success yet in setting key to be the type of ManagedObjectReference.
Also tried as array etc.

cluster_config_spec = vim.cluster.ConfigSpecEx()
cluster_config_spec.drsVmConfigSpec = vim.cluster.DrsVmConfigSpec()
cluster_config_spec.drsVmConfigSpec.info = vim.cluster.DrsConfigInfo()
cluster_config_spec.drsVmConfigSpec.info.key = vmodl.ManagedObjectReference
cluster_config_spec.drsVmConfigSpec.info.key.type = "VirtualMachine"
cluster_config_spec.drsVmConfigSpec.info.key.value = self.params.get('vm_moid')
cluster_config_spec.drsVmConfigSpec.info.enabled = self.enable_drs
cluster_config_spec.drsVmConfigSpec.info.behavior = self.params.get('drs_default_vm_behavior')

PowerCLI equivalent: New-Object VMware.Vim.ManagedObjectReference

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Setting-VM-overrides-through-PowerCLI/...

$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.dasVmConfigSpec = New-Object VMware.Vim.ClusterDasVmConfigSpec[] (1)
$spec.dasVmConfigSpec[0] = New-Object VMware.Vim.ClusterDasVmConfigSpec
$spec.dasVmConfigSpec[0].operation = "add"
$spec.dasVmConfigSpec[0].info = New-Object VMware.Vim.ClusterDasVmConfigInfo
$spec.dasVmConfigSpec[0].info.key = New-Object VMware.Vim.ManagedObjectReference
$spec.dasVmConfigSpec[0].info.key.type = "VirtualMachine"
$spec.dasVmConfigSpec[0].info.key.value = $vm.ExtensionData.MoRef.Value

Reply
0 Kudos
2 Replies
canadapatrick
Contributor
Contributor

Further details ... here are a few samples of what does not work and errors in ansible:

cluster_config_spec.drsVmConfigSpec.info.key = (type=vim.VirtualMachine, value=self.params.get('vm_moid'))
# Error: "msg": "Unable to import community.vmware.vmware_cluster_drs_vm due to invalid syntax",

cluster_config_spec.drsVmConfigSpec.info.key = vim.VirtualMachine(self.params.get('vm_moid'))
# Error: "msg": "('A specified parameter was not correct: drsVmConfigSpec.info.key', None)"

cluster_config_spec.drsVmConfigSpec.info.key type = "VirtualMachine"
# Error: AttributeError: 'NoneType' object has no attribute 'type'\

cluster_config_spec.drsVmConfigSpec.info.key = ("VirtualMachine", self.params.get('vm_moid'))
# Error: For "key" expected type vim.VirtualMachine, but got tuple\n",

Reply
0 Kudos
canadapatrick
Contributor
Contributor

FYI - Got something to work!
 
Can see my VM is in VM overrides with correct setting after running.
 
Code snippets below (vm_moid is the moid from vmware_guest_info output)
 

                vm_config_spec = vim.cluster.ConfigSpecEx()
                vm_config_spec.drsVmConfigSpec = [vim.cluster.DrsVmConfigSpec()]
                vm_config_spec.drsVmConfigSpec[0].operation = "add"
                vm_config_spec.drsVmConfigSpec[0].info = vim.cluster.DrsVmConfigInfo()
                vm_config_spec.drsVmConfigSpec[0].info.key = vim.VirtualMachine(self.params.get('vm_moid'))
                vm_config_spec.drsVmConfigSpec[0].info.behavior = self.params.get('drs_vm_behavior')
                vm_config_spec.drsVmConfigSpec[0].info.enabled = self.params.get('enable')

                try:
                    task = self.cluster.ReconfigureComputeResource_Task(vm_config_spec, True)
                    changed, result = wait_for_task(task)

. . .
 
Reply
0 Kudos