I can't for the life of me get this method to work, as per the SDK and Github documentation I should be able to call it like such "vim.CustomFieldsManager(myVMRef, intKeyOfCustomField, strValueToPutThere)" but it refuses to accept those arguments, the closest I can get is when I put the VM's reference in twice (making four arguments, ie ref,ref,key,value) and this time it bombed differently with an "The request refers to an unexpected or unknown type."
It should be more like the following (not tested, just working off the top of my head)
customFieldsManager = si.RetrieveContent().customFieldsManager
customFieldsManager.SetField(entity=myVM, key=intKeyOfCustomField ,value=strValueToPutThere)
Thank you for the response stumpr, I can confirm the successful way to reach the CustomFieldsManager is via the vim reference but when I tried feeding it the arguments like you suggest it returns the following error instead:
TypeError: _InvokeMethod() takes at least 2 non-keyword arguments (1 given)
I've even tried juggling the values around in various combos but it always gives an error along those lines or a mismatch in type.
Just drop down to the object then and use the setCustomValue method. I'll fire up my VCSIM and validate it as well.
myVM.setCustomValue(key=myFieldDefKey, value=myStringValue)
Oh that's fantastic I didn't realize that method existed and yes it worked great 🙂 for anyone else trying this the key it wants is actually a string of the CustomField's name which actually resolves something else I was trying to do which was poll the CustomFieldManager.field array to get the key->name pairs to so I could do this. Now I don't even need to do that, which is nice because that doesn't seem to work either.
Thank you stumpr
It worked but I used the field name, not the key value:
>>> cfm.field
(vim.CustomFieldsManager.FieldDef) [
(vim.CustomFieldsManager.FieldDef) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 1,
name = 'FieldTest',
type = str,
managedObjectType = vim.VirtualMachine,
fieldDefPrivileges = <unset>,
fieldInstancePrivileges = <unset>
}
]
>>> vm.setCustomValue(key='FieldTest', value='ValueTest')
>>> vm.customValue
(vim.CustomFieldsManager.Value) [
(vim.CustomFieldsManager.StringValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 1,
value = 'ValueTest'
}
]
Jinx
By the way, the customFieldsManager.SetField call worked as well, I think pyvmomi just prefers the actual object (it'll extract the _moId value from it):
>>> cfm.SetField(entity=vm, key=1, value='ValueTest2')
>>> vm.customValue
(vim.CustomFieldsManager.Value) [
(vim.CustomFieldsManager.StringValue) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 1,
value = 'ValueTest2'
}
]