Invalid property? A Trick With vSphere PropertyCollector
As I discussed extensively in my book, the PropertyCollector is very powerful yet not easy to use. There was a question posted at vSphere Java API forum related to the property collector which I think worths sharing here. Although it’s found using vSphere Java API, but it really goes beyond the API to the vSphere API itself.
The problem was not able to retrieve the info.vmfs property of a Datastore managed object using vSphere Java API, but OK with others like info.freeSpace, info.maxFileSize, info.name, etc. If MOB is used, the info.vmfs can be retrieved without any problem.
vSphere Java API wraps up the PropertyCollector using getter methods for all the direct properties in very managed object. Mostly you don’t need to touch the PropertyCollector at all.
In some cases, you do want to get hold of the sub properties directly to save CPU cycle and bandwidth. That is why I decided to open up the getPropertyByPath() method with which you can provide the property path and get the value in one call.
Now, what is the cause of the problem?
It’s related the typing of the property. The info property is typed as DatastoreInfo. It does NOT have a sub property called vmfs. But DatastoreInfo is an abstract type which is extended to VmfsDatastoreInfo which does have vmfs sub property. There are other possibilities, for example, DatastoreInfo to NasDatastoreInfo which does NOT have vmfs sub property at all. So before being certain about the real type, we cannot request for info.vmfs.
How to solve this issue?
It’s pretty easy. You want to get the info with getInfo() method, and check the instance type before casting it to VmfsDatastoreInfo. Once it’s casted, you can use the vmfs as easy as one getVmfs() call.
For future tricks and tips, feel free to subscribe to this feed, and follow me at Twitter.
Author: Steve Jin is the author of VMware VI and vSphere SDK (Prentice Hall), creator of VMware vSphere Java API. For future articles, please subscribe to Email or RSS, and follow on Twitter.

Hi Steve,
Thanks for the wonderful API. I’m still reading your VMware VI and vSphere SDK book.
I was going through the PropertyCollector and especially liked the getPropertiesByPaths method. I tried to get multiple properties in one go (and in my observation) it came out throwing InvalidProperty exception when one of the property paths was incorrect. I didn’t get values for the other valid ones as well. Is there a way to check if a “property path is valid” before we call getPropertiesByPaths? or Is there a way to get the valid property paths for a given ManagedEntity?
Thanks you.
Regards,
Tulsi (Thulasinathan Kandasamy)
Oops. I pasted the method-name (getPropertiesByPaths) instead of my name.
Regards,
Tulsi
I got another question. Is there a way to get specific types of ManagedEntities that have given values for the given properties?
Hi Tulsi,
Do you want to get type information or a collection of instances of a specific type?
Steve
Hi Steve,
I want to get the type information. Say, for a ManagedEntity type like “HostSystem” I want to know all the supported/valid property-paths like below
name
summary.config.product.apiType
summary.hardware.numNics
etc.,
Regards,
Tulsi