VMware {code} Community
scroger
Enthusiast
Enthusiast

ManagedObjectReference to HostSystem, get Host name



I'm new to the vSphere Management SDK, web client and web services.  I've been playing around with some of the samples and things are staring to make some sense. 


I'd like to get a list of all Hosts and their names from vCenter.   I have some sample web client Java code that gets an array of HostSystem ManagedObjectReferences using a QuerySpec from a DataService.


I'm sure it's a simple question, but how do I get the name of the HostSystem for this ManagedObjectReference?  I don't see a HostSystem class in the com.vmware.vim25 package, but some related classes like HostSystemInfo etc.  I assumed I could cast this ref to a HostSystem class, and get the name from some member function. 


Suggestions please.

0 Kudos
3 Replies
scroger
Enthusiast
Enthusiast



By the way, I tried to cast one of the Objects from array of ManagedObjectReferences to:


    com.vmware.vim25.ManagedObjectReference. 


While that code compiles, when I try to run the code, the returned Objects fail during the cast and I get a ClassCastException stating that:


    com.vmware.vim.binding.vmodl.ManagedObjectReference cannot be cast to a com.vmware.vim25.ManagedObjectReference


This is very frustrating.  Ideas?

0 Kudos
jonathanp
Expert
Expert

I do not know for Java... but it must be almost the same ways,...

For me I use C#... so I reference vmware.vim.. and use that. ( i use the one that come with powerCLI installation folder )

using VMware.Vim;

private VimClient client;

List<EntityViewBase> hostlist = new List<EntityViewBase>();

// then somewhere you connect....

connect()

{

client = new VimClient()

client.Connect("https://<hostip>/sdk");

client.login("username", "password");

hostlist = client.FindEntityViews(typeof(HostSystem), null, null, null );

}

dosomething()

{

foreach ( HostSystem H in hostlist )

     {

MessageBox.Show(H.Name);

     }

}

Maybe it can help... Only downside using this is, it is very slow to retrieve hosts in a VC with 20 + hosts.... ( 10+ minutes...)

I am trying to find a faster way.

Jonathan

0 Kudos
stumpr
Virtuoso
Virtuoso

I don't do much in C#, but the principles are the same when using many of the Find* or Filter* type methods in the utility kits across the SDKs.  You can get dramatic improvement in speed by using property filters.

For example, when using null for the fourth parameter, you're signaling you want all properties of all HostSystem objects returned.  The HostSystem ManagedView is probably the largest object in the vSphere inventory by XML size, so it'll take the most time to process and transfer over the network.  You can get a dramatic performance increase using an array of properties strings.  For example, to get the host name, it's likely just string[ ] hostProperties = ["name"], then call hostlist = client.FindEntityViews(typeof(HostSystem), null, null, hostProperties).  If you tune your property array based on values you expect to use in your code, then you'll cut down on the query times dramatically. 

You can also filter by various property values, say a specific name, but bear in mind in most cases, the SDK is making two calls to the API.  One to get all Hosts with the property value(s) you want to match against, and then a second call against the API to get the properties you want (all, or partial list).  In many cases this two part call is faster, since you do a minimal fast call, then a very narrow full property fetch.  But if you keep the process in mind you can optimize for speed even with very large inventories.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos