pyvmomi-community-samples icon indicating copy to clipboard operation
pyvmomi-community-samples copied to clipboard

How to get custom attributes from specifc vm ?

Open XDavidT opened this issue 4 years ago • 3 comments

I try to use searchIndex to get vm by UUID, but still no option to get the custom attributes. Can someone show me example how to do so ? The main goal is to export Csv from Vcenter, update all the custom attributes and after excel update, upload the change back, and before update, I want to check if the value was changed.

XDavidT avatar Jun 28 '21 13:06 XDavidT

I don't know if custom attributes in the UI is part of the customValue property of the object, or if it is something else. The documentation (https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.vcenterhost.doc/GUID-621BC36D-D077-4AE2-9604-42791057AFAF.html) lumps it with tagging, which pyVmomi can't do.

So I'd recommend experimenting. Set a custom attribute for a VM the way you want it, then do your lookup for the vm with searchIndex, then do print(vm.customValue) and see if it lists 1 or more keys. If it does, then you'll be leveraging the CustomFieldsManager.

The CustomFieldsManager has a SetField method where you would update the VM's value for particular keys. The key would be something you could find from CustomFieldManager.field array.

prziborowski avatar Jun 28 '21 17:06 prziborowski

I don't know if custom attributes in the UI is part of the customValue property of the object, or if it is something else. The documentation (https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.vcenterhost.doc/GUID-621BC36D-D077-4AE2-9604-42791057AFAF.html) lumps it with tagging, which pyVmomi can't do.

So I'd recommend experimenting. Set a custom attribute for a VM the way you want it, then do your lookup for the vm with searchIndex, then do print(vm.customValue) and see if it lists 1 or more keys. If it does, then you'll be leveraging the CustomFieldsManager.

The CustomFieldsManager has a SetField method where you would update the VM's value for particular keys. The key would be something you could find from CustomFieldManager.field array.

Great, but in this way I can access to the fields values, how do I reach to keys ?

XDavidT avatar Jun 29 '21 10:06 XDavidT

According to the API docs: customValue is vim.CustomFieldsManager.Value, which has a key. The value part would be from the derived class vim.CustomFieldsManager.StringValue, so you should have both key/value.

And the key is a reference to the CustomFieldManager.field[].key

fields = si.content.customFieldsManager.field
for customValue in vm.customValue:
   field = [x for x in fields if x.key == customValue.key][0]
   print("Field: %s, Value: %s" % (field.name, customValue.value))

prziborowski avatar Jun 29 '21 17:06 prziborowski