vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.01k stars 922 forks source link

Powershell get-view in pyvmomi #746

Open RalfAlbers opened 6 months ago

RalfAlbers commented 6 months ago

Is your feature request related to a problem? Please describe.

In VMware PowerCLI I can retrieve very quick with get-view with -filter and -property data from our large vCenter. With using -filter and -property, the data is filtered on vCenter side, not on client side. This reduces a lot of network traffic with each API Call. This is an example: Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn';'Guest.ToolsState'='ToolsOk'} -property Name

Describe the solution you'd like

How can In get this done with pyvmomi?

Describe alternatives you've considered

-

Additional context

-

prziborowski commented 6 months ago

I think https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/vminfo_quick.py is the best example for what you want. view = pchelper.get_container_view(si, obj_type=[vim.VirtualMachine]) part is for getting a view (note that you are responsible for calling view.Destroy() on it when you are done, which this sample doesn't.

vm_data = pchelper.collect_properties(si,
                                      view_ref=view,
                                      obj_type=vim.VirtualMachine,
                                      path_set=vm_properties,
                                      include_mors=True)

fetches the properties for the VM.

This sample doesn't have a filter in it. But you can add something like https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/filter_vms.py#L44 to it (that example is just made for 1 property filtering, so you'd have to compare arrays rather than [0] index).

RalfAlbers commented 6 months ago

https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/filter_vms.py#L44 is an filter after you had transfered the data from vcenter to your client. If I'm not wrong, the PowerCLI option -filter does a filtering on vCenter side. I'm looking for a way to achieve this with pyvmomi too.

prziborowski commented 6 months ago

I don't have any easy way to confirm if powercli does the filtering on the client or on the server (likely the easiest way would be to make vCenter config changes to log communication, or using something like wireshark).

The only reason I am thinking powercli does client filtering is that I haven't yet found anything in the PropertyCollector's FilterSpec that would provide server-side filtering based on values. It just allows setting the properties and objects that one is interested in.