ymichalak
Hot Shot
Hot Shot

vRO - Get all virtual machines by OS type from Business Group Name

Hello,

we try to get all virtual machine by OS Type (Windows or Linux) from a Business Group name.

We have try this, with a Business Group name & OS type (windows or linux) like an input but the workflow take 1 minute to parse 100 virtual machines.

One minute is not a good end user experience when he launch a vRA form.

Do you know an other method more faster to get this information?

const PAGE_LIMIT = 100;  
service = vcacHost.createCatalogClient().getCatalogConsumerResourceService();  
  
// create a custom search request
var conditions = new Array();
conditions[0] = vCACCAFEFilterParam.substringOf("organization/subTenant/id", vCACCAFEFilterParam.string(businessGroupId));
conditions[1] = vCACCAFEFilterParam.substringOf("resourceType/id", vCACCAFEFilterParam.string("Infrastructure.Virtual")); 

var filter = new Array();   
filter[0] = vCACCAFEFilterParam.and(conditions);
var query = vCACCAFEOdataQuery.query().addFilter(filter);
query.setTop(PAGE_LIMIT);  
query.setSkip(0);  
  
var vCACVirtualMachineList = service.getResourcesList(new vCACCAFEPageOdataRequest(query)); 
if (vCACVirtualMachineList.length == 0) {
    System.warn("Catalog Resource not found for the given Request.");
}

var resources = []; 
do {  
	var vCACVirtualMachineList = service.getResourcesList(new vCACCAFEPageOdataRequest(query));
	resources = resources.concat(vCACVirtualMachineList);  
	//System.log("Found Resources: " + vCACVirtualMachineList.length);  
	query.setSkip(resources.length);  
	oDataRequest = new vCACCAFEPageOdataRequest(query);
	} while (vCACVirtualMachineList.length > 0); 


if (resources.length == 0) {
    System.warn("Catalog Resource not found for the given Request.");
} else {
	System.log("found " + resources.length + " catalog Resource");
}

var vmNameList = new Array();
for (keyVVML in resources) {
	try {
		vCACVirtualMachine = resources[keyVVML];
		//System.debug("vCACVirtualMachine : " + vCACVirtualMachine);
		
		var resourceId = vCACVirtualMachine.id;
		//System.debug("vCACVirtualMachine.id : " + resourceId);
		
		var resource = vCACCAFEEntitiesFinder.getCatalogResource(vcacHost,resourceId);
		//System.log("resource : " + resource);
		// extract type to filter
		var resourceType = resource.getResourceTypeRef().getLabel();
		//System.log("resourceType : " + resourceType);
		
		// extract Id
		var providerBinding = resource.getProviderBinding();
		var bindingId = providerBinding.getBindingId();
	
		// extract provider to filter
		var provider = providerBinding.getProviderRef();
		var providerLabel = provider.getLabel();
		
		if ((resourceType == "Virtual Machine") && (providerLabel == "Infrastructure Service")){
			vCACVm = Server.findForType("vCAC:VirtualMachine", bindingId);
			//System.log("cat");
			if (vCACVm != null) {
				var vmName = vCACVm.virtualMachineName;
				System.log("Found a VM with name : " + vmName);
				//checking that the os matches the type expected
				vmGuestOs = vCACVm.guestOS;
				System.log("vmGuestOs : " + vmGuestOs);

				switch (vmGuestOs) {
					case "Microsoft Windows 10 (64-bit)" : // OR
					case "Microsoft Windows Server 2016 or later (64-bit)" : // OR
					case "Microsoft Windows Server 2019 (64-bit)" :
						if (osType == "windows") {
							// you have a windows like and this is what we need
							System.debug("detected a windows, and expecting osType " + osType + " : adding");
							vmNameList.push(vmName);
						} else {
							// sorry, you expect a linux and there is no such
							System.debug("detected a windows, and expecting osType " + osType + " : skipping");
						}
						break;

					case "SUSE Linux Enterprise 11 (64-bit)" :
					case "SUSE Linux Enterprise 12 (64-bit)" :
					case "SUSE Linux Enterprise 15 (64-bit)" :
					case "Red Hat Enterprise Linux 7 (64-bit)" :
					case "Red Hat Enterprise Linux 8 (64-bit)" :
						if (osType == "linux") {
							// you have a linux like and this is what we need
							System.debug("detected a linux, and expecting osType " + osType + " : adding");
							vmNameList.push(vmName);
						} else {
							// sorry, you expect a windows and there is no such
							System.debug("detected a linux, and expecting osType " + osType + " : skipping");
						}
						break;

					default :
						System.warn("Not a supported OS " + vmGuestOs);
						break;
				}						
			}
		}
	}
	catch (exception) {
		System.log("there is something wrong somewhere : " + exception);
	}
}
System.log("Found the given list : " + vmNameList);

vCACVirtualMachineList = vmNameList;
// vCACVirtualMachineList = resources;

 

0 Kudos
0 Replies