hawks76
Enthusiast
Enthusiast

vRO 8.x Find VM by Name

I've recently started working with vRA 8.8 and working on migrating our processes over.  My custom naming workflow/action searches vCenter for existing vm name.  in vRA 7.6, the plugin used was vCAC and vCACEntityManager to search based on name.  Looks like the equivalent in vRA 8 is Server.findAllForType("VRA:Machine"). I'm trying to filter based on vm name, but i can't seem to get the correct syntax. 

What i've tried so far is Server.findAllForType("VRA:Machine",hostname) and Server.findAllForType("VRA:Machine","name="'"hostname"'").

Any idea what i'm missing?  Google hasn't turned up much help on this.

Thanks

0 Kudos
3 Replies
scott28tt
VMware Employee
VMware Employee

As vRA has it’s own forum area, I have reported your thread to moderators, they should now move it.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
getvrohelp
Contributor
Contributor

This is a simple way to achieve what you're looking for in 8. Hope it helps.

Your input is a string named "hostname"

Your output is vcVM in this case

**dev-vcSdkConnection is a config element of type VC:SdkConnection. if you have multiple create multiple with unique names

---- code is below ----
 
//Getting the needed attributes from the configuration elements:
 
var vcSdkConnection = vcacConfiguration.getAttributeWithKey("dev-vcSdkConnection").value;

//Finding the Vcenter VM object from Hostname
var vmArray = vcSdkConnection.getAllVirtualMachines(null, hostname);
vcVm = vmArray[0];

if (hostname == vcVm.name) {
    System.log("Found VM " + vcVm.name);
}
    else {
        System.debug(vmArray);
        throw "vCVm Name " + vcVm.name + " Does not match input hostname of " + hostname;
    }
0 Kudos
bdamian
Expert
Expert

Hi, I assume that you are trying to find a VM in vRA8 inventory (not in vCenter directly). I was trying with Server.findAllForType("VRA:Machine", query) but it doesn't seems to work. 

What you can do is to use the ability to execute HTTP Rest queries to vRA. The following code does the trick but is not really ready for production (need some more work):

var hosts = VraHostManager.findHostsByType("vra-onprem");
var host = hosts[0]; // I have only one host. If not, find the correct one checking "vraHost" property.

var restClient = host.createRestClient();
var request = restClient.createRequest("GET", "/iaas/api/machines?$filter=name+eq+'vmName'", null);
var response = restClient.execute(request);

if (response.statusCode==200) {
var vms = JSON.parse(response.contentAsString);
// You can look for any property, for example:
System.log(vms.content[0].name);
}

 

---
Damián Bacalov
vExpert 2017-2018-2019-2020-2021-2022
https://www.linkedin.com/in/damianbacalov/
https://tecnologiaimasd.blogspot.com/
twitter @bdamian
0 Kudos