VMware {code} Community
macquivr
Contributor
Contributor

Getting info in one call

I'm using vijava, I call searchManagedEntities("Virtual Machines") or whatever to get back a array of MORs (essentially)

But if I want to return the specifics of each VM to my end user, I have to them make a separate network call saying more or less this is MOR, what's its name,

what's it uuid and so on.

Let me be specific so that people might see exactly what I'm doing and suggest either a totally different way (expected) or a tweak on my algorithm

Purpose: I want to return a Java array object (call it Pojo) containing information about all the VMs on my ESX Host

ManagedEntity[] mes = null;

mes = new InventoryFolder(rootFolder).searchManagedEntities("VirtualMacine")

Pojo[] data = new Pojo[http://mes.length|http://mes.length];

for (i=0;i<mes.length;i++) {

VirtualMachine vmo = (VirtualMachine) mes[i];

Pojo = new Pojo();

VirtualMachineSummary s = vm.getSummary();

VirtualMachineRuntimeInfo r = vm.getRuntime();

VirtuaMachineConfigSummary vs = s.getConfig();

GuestInfo = vm.getGuest();

Pojo.setName(vs.getName());

Pojo.setUuid(vs.getUUid()) ;

Pojo.setAnnotation(vs.getAnnotation());

Pojo.setGuiestId(s.getGuest().getGuestId());

Pojo.SetHostName(g.getHostName());

/// I think you get the idea.

}

This is excureatingly (sp?) inefficient, please help me populate my Pojo making as few Network calls as possible.

Thanks.

- JT

0 Kudos
2 Replies
mbaldw15
Contributor
Contributor

If you can get all of the MORs that you want, then this method should work.

Esentially what you do is you call the "retreiveProperties" method of your VimPortType with the PropertySpec stating all the properties you want, the type of object the MORs reference, and the array of MORs you want the properties of. Then the returned data contains one entry for each MOR with the properties of the MOR and the MOR that the properties are for. If that isn't clear, then hopefully this will help.


//Create the object spec
//mors = ManagedObjectReference[] containing either the vms or hosts you want
//String type = "HostSystem" if getting from host, "VirtualMachine" if getting vms
//String[] props = The name of the properties I want from this type, ex: {"config", "summary", "datastore"}
ManagedObjectReference pc = _sic.getPropertyCollector();
ObjectSpec[] os = new ObjectSpec[http://mors.length|http://mors.length];
for(int i = 0; i < mors.length; i++){
	os[i] = new ObjectSpec();
	os[i].setObj(mors[i]);
}

//Create the property spec
PropertySpec pSpec = new PropertySpec();
pSpec.setType(type);
pSpec.setAll(false); //whether or not all properties of the object are read. If this property is set to true, the pathSet property is ignored.
pSpec.setPathSet(props);

//Create the property filter spec
PropertyFilterSpec pfs = new PropertyFilterSpec();
pfs.setObjectSet(os);
pfs.setPropSet(new PropertySpec[] {pSpec});

ObjectContent[] objs;
try {
	objs = _service.retrieveProperties(pc, new PropertyFilterSpec[] {pfs});
} catch (Exception e) {
	e.printStackTrace();
	return;
}

//cycle through all the returned data
for(int i = 0; objs != null && i < objs.length; i++){
	ManagedObjectReference mor = objs[i].getObj(); //the mor that the returned data is for
	DynamicProperty[] dProps = objs[i].getPropSet();
	System.out.println("MOR: " + mor);
	for(int j=0; dProps!=null && j < dProps.length; j++){
		System.out.print(dProps[j].getName() + " = "); //contains the name of the property
		System.out.println(dProps[j].getVal()); //contains the value of the property

	}
	//store the hashMap and the MOR somewhere so you can reference it later
}

Is that what you were thinking of or were you looking for something else?

0 Kudos
macquivr
Contributor
Contributor

I will digest your response and let you know, thank you for the prompt reply.

- JT

0 Kudos