Reply to Message

View discussion in a popup

Replying to:
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