Hello,
I've created a small inventory script in vRO, which needs all vRA cafe VMs (type: VCACCAFE:CatalogResource) as input.
Loading all CatalogResources ist not an issue, this is done using an odata query and the results are consistent, that is, each time the method delivers the same amount of objects (assuming no changes are made in vRA).
The weird part begins, when I try to filter out non-VM objects from the array, that is everything, that is not of the virtual machine type: Deployments, Networks and other objects provisioned in vRA, that are of no use in the context of this workflow.
I tried different methods for determining if the CatalogResource object is a virtual machine, but each time the script is run, a different amount of objects is returned and as a result, the output is not consistent and cannot be used - some vRA vms are missing each time but also some are duplicated.
What would be an affective way to return all vRA vms as an array? Is there no method that would return consistent results? The only one I could think of, is to load all vCAC Vm objects first and the find the corresponding catalog resource for each, by name, but that would be slow.
Here's the code snippet I use for testing, input is the array of cafeHosts. WARNING - using it can result in excessive JVM Memory usage:
for (var j=0;j<3;j++){ System.log("====== iteration " + j); var vCACCAFEVMs = []; // Query each host for catalog resources; this will include cafe vms AND deployments; using odata query with pagination for each (host in cafeHosts){ var service = host.createCatalogClient().getCatalogConsumerResourceService(); var query = vCACCAFEOdataQuery.query(); var i; for (i = 1; i < 20; i++){ var items = service.getResourcesList(new vCACCAFEPageOdataRequest(i, 100, query)); if (items.length == 0) { System.debug("Breaking query of host " + host.name + " on iteration " + i); break } vCACCAFEVMs = vCACCAFEVMs.concat(items); } } System.log("Total Found CatalogResource items: " + vCACCAFEVMs.length); /*var result = []; for each (vm in vCACCAFEVMs){ result.push(vm.name) } System.log(result);*/ // Filter out non-VMs using .resourceTypeRef values var vCACCAFEVMs_v2 = vCACCAFEVMs.slice(); var i = vCACCAFEVMs_v2.length; while(i--){ if (vCACCAFEVMs_v2[i].resourceTypeRef.getId() != "Infrastructure.Virtual" && vCACCAFEVMs_v2[i].resourceTypeRef.getLabel() != "Virtual Machine") { vCACCAFEVMs_v2.splice(i, 1) } } System.log("CatalogResource after filtering out non-VMs using .resourceTypeRef value: " + vCACCAFEVMs_v2.length); var result = []; for each (vm in vCACCAFEVMs_v2){ result.push(vm.name) } System.log(result.sort()); // Filter out non-VMs using cafeVm to vcacVm convert var vCACCAFEVMs_v3 = vCACCAFEVMs.slice(); var i = vCACCAFEVMs_v3.length; while(i--){ var vCACVMs = Server.findAllForType("vCAC:VirtualMachine", "VirtualMachineName eq '" + vCACCAFEVMs_v3[i].name + "'"); if (vCACVMs[0] == undefined) { vCACCAFEVMs_v3.splice(i, 1) } } System.log("CatalogResource after filtering out non-VMs using cafeVm to vcacVm convert: " + vCACCAFEVMs_v3.length); var result = []; for each (vm in vCACCAFEVMs_v3){ result.push(vm.name) } System.log(result.sort()); }