How to Get ESX Version from vCenter?
There is a recent question in vSphere(VI) Java API forum about this. On its face, it’s very easy because most people know how to get hold of the version as follows:
String version = si.getAboutInfo().getVersion();
The si in the above code is the variable of ServiceInstance object. If you have never used the API yet, please try this Getting Started Tutorial which shows how to get your first program running from scratch in 5 minutes.
If you are connecting to a vCenter server and try to get the version of a HostSystem the vCenter manages, it’s not so obvious. But it’s definitely doable. Here is the solution assuming you already get hold of the HostSystem object as host variable here:
String version = host.getConfig().getProduct().getVersion();
Here you know why. First, the aboutInfo is now called product although they are of the same type. Second, it’s hidden within the config property.
Before taking the code away, I would like to share with you an important tip for better performance. You should consider the following code:
String version = (String) host.getPropertyByPath(“config.product.version”);
Or,
AboutInfo ai = ((AboutInfo) host.getPropertyByPath(“config.product”); String version = ai.getVersion();
Why?
Because config object could be huge and it takes time for the vCenter to grab the data and serialize it to XML, and for VI Java API to de-serialize XML back to data object. You can expect getConfig() call take about one second to finish. Most of that time is on vCenter side for grabbing data. So to avoid this, you can just fetch the sub-properties with much faster speed.
This tip works as well if you connect to ESX directly.
Nice tip Steve. How can I search your blog posts for similar tips on using the SDK? because I don’t see tags on the posts. If you’d tagged this one like sdk-tips or something then it would’ve been great help. Anyways, you and your work rocks! keep us entertained with it
Sorry, I see the tags now, dunno how I missed them earlier on.
Thanks Monis,
Need your comments as well. Keep them coming!
-Steve