Enthusiast
Enthusiast

getEntity() problems

Long story short, I need to get the entity for a VM.  I went through my books(which did not contain anything) and have been looking online.  Still missing something.

I am pulling in payload as I need it for additional stuff later in the script. Line 7 seems to run fine but I get an error in line 8.   Here is my error:  TypeError: Cannot find function getEntity in object . The only thing I can think of is that I am not getting the VC:VirtualMachine correct.  Any assistance would be greatly appreciated.

var executionContext = System.getContext();

var machine = payload.get("machine") ;

var vCACProps = machine.get("properties") ;

var vCACVmName = machine.get("name");

var vCACVms = Server.findAllForType("VC:VirtualMachine", "VirtualMachineName eq '" + vCACVmName + "'");

var vmEntity = vCACVms.getEntity();

0 Kudos
4 Replies
VMware Employee
VMware Employee

Hi,

Server.findAllForType() returns an array with objects, in your case, array with 0 or more elements of type of VC:VirtualMachine. And arrays does not have a method getEntity().

0 Kudos
Enthusiast
Enthusiast

So I could add [0] correct?  Is this the best way to solve this problem programmatically?

0 Kudos
VMware Employee
VMware Employee

Adding [0] is not the best way, for several reasons:

1) If the call returns an empty array (no machine found with this name), [0] will throw an error

2) if the call returns 2 or more elements (suppose there are several machines with such name), [0] will pick the first element, but that might not be what you want.

So, usually you would want to add some checks on the size of the array to handle all possible cases accordingly.

On the other hand, even if the call returns always an array with 1 element, and you use [0], you then will call getEntity() on this element. Are you sure VC:VirtualMachine has such method? I don't see it in API Explorer.

0 Kudos
Enthusiast
Enthusiast

I can confirm that this does work, so long as there is something to return as you said.

var executionContext = System.getContext();

var machine = payload.get("machine") ;

var vVACProps = machine.get("properties") ;

var vCACVmName = machine.get("name");

var vCACVms = Server.findAllForType("vCAC:VirtualMachine", "VirtualMachineName eq '" + vCACVmName + "'");

var vmEntity = vCACVms[0].getEntity();

I came up with the code from a couple different ways to try to resolve my issue.  I need the entity of the server I am building set to a variable.  This lets me update IP information from Men and Mice (external IPAM).  Here is the line that I am using.

actionResult = System.getModule("com.vmware.library.vcac").addUpdatePropertyFromVirtualMachineEntity(vCACHost,vmEntity,"VirtualMachine.Network0.Address",vmIP,isFalse,isFalse,isFalse,isFalse);

I have tried to get those properties to update in the VMPSMasterWorkflow32.MachineBuilding phase several different ways but this is the only way it will update the IP. If there is a better way to assign the entity to a variable I would love to test it out.  I am running vRA 7.3, vRO 7.3 on vSphere 6.5.

0 Kudos