VMware Cloud Community
Uridium454
Enthusiast
Enthusiast
Jump to solution

Get VCVM from VM Name

I have a situation in which I have the name value of the VM in which I need to return the vCACVM object.  I have found a solution that seems to be working, but wonder if there is a better way to achieve the same outcome.

As a side note, I am hoping someone would be able to provide a brief explanation on the substringof() eq "true" found in the code. I understand it is attempting to match the value supplied in vmName, and match it to the value being found in VirtualMachineName and if it returns true, that is the vCACVM I am looking for.  I guess I am looking for a bit deeper understanding of the method, and perhaps some accompanying documentation explaining why it was available for use inside of Server.findAllForType().

Solution was derived from: https://communities.vmware.com/message/2622002#2622002.

Inputs:

     vmName(string)

Attributes:

     vcaccafehost(VCACCAFE:VCACHost)

Code:

var vm= Server.findAllForType("vCAC:VirtualMachine", "substringof('" + vmName + "',VirtualMachineName) eq true")

System.log("vCACObj: " + vm);

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

The second parameter of Server.findAllForType() is a plug-in-specific query. In this particular plug-in case, it is OData filter parameter.

You can check the documentation of substringof here - http://docs.oasis-open.org/odata/odata/v4.0/csprd01/part2-url-conventions/odata-v4.0-csprd01-part2-u...

In your code sample, the filter will look something like substringof('somename',VirtualMachineName) eq true , and this filter expression will match all entities whose VirtualMachineName property contains somename as a sub string, and so only these vms will be returned by Server.findAllForType() call.

View solution in original post

Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

The second parameter of Server.findAllForType() is a plug-in-specific query. In this particular plug-in case, it is OData filter parameter.

You can check the documentation of substringof here - http://docs.oasis-open.org/odata/odata/v4.0/csprd01/part2-url-conventions/odata-v4.0-csprd01-part2-u...

In your code sample, the filter will look something like substringof('somename',VirtualMachineName) eq true , and this filter expression will match all entities whose VirtualMachineName property contains somename as a sub string, and so only these vms will be returned by Server.findAllForType() call.

Reply
0 Kudos
Uridium454
Enthusiast
Enthusiast
Jump to solution

Great explanation and I appreciate the link to the documentation.  Thank you.

Reply
0 Kudos