VMware Cloud Community
mythrandir52
Contributor
Contributor

find virtual machines by business group name and virtual machine name filter

I am trying to develop logic to find all virtual machines from a given business group and with a name filter.  What i would like to get back is a VCAC:VirtualMachine object with all of the machines.

I have been trying to pull together bits and pieces from the net with no luck.  Eventually i want to move this from a workflow to an action so i can drive form elements in vra asd.

Inputs:

BusinessGroupName(string)

ServerName(string)

General Attributes:

vcaccafehost(VCACCAFE:VCACHost)

vms(vCAC:Entity)

Scriptable Task:

service = vcaccafehost.createCatalogClient().getCatalogConsumerResourceService();

var conditions = new Array();

conditions[0] = vCACCAFEFilterParam.substringOf("name", vCACCAFEFilterParam.string(ServerName));

conditions[1] = vCACCAFEFilterParam.substringOf("organization/subTenant/id", vCACCAFEFilterParam.string(BusinessGroupName));

var filter = new Array();

filter[0] = vCACCAFEFilterParam.and(conditions);

var query = vCACCAFEOdataQuery.query().addFilter(filter);

var items = service.getResourcesList(new vCACCAFEPageOdataRequest(query));

System.log(items);

for each (var item in items) {

  System.log("name:"+ item);

}

I am on a learning curve for java script and the vcac cafe.

Help  Please....

Truth is a three edged sword
Reply
0 Kudos
3 Replies
Hejahida82
VMware Employee
VMware Employee

Hi mythrandir52,

Couple of things to look at in your script, but you aren't far from achieving your aim.

It's not clear what you are specifying as the value for BusinessGroupName but this needs to be the Id of the Business Group and not the Name. You can find this in the vRO inventory for the business group as the id if you are only going to be using the script for one business group, otherwise you will need to add some code to find this value in advance for the business group you want to use e.g.

businessGroupObject = vCACCAFEEntitiesFinder.findBusinessGroups(cafeHost,nameofBusinessGroup)[0];

businessGroupName = businessGroupObject.getId();

shot4.jpg

Also the script you have will find Catalog Resource objects (vCACCAFE:CatalogResource) and not IaaS VMs (vCAC:VirtualMachine) or IaaS Entities (vCAC:Entity)

So if you use the business group Id for the value of BusinessGroupName then your script will work and you will get an array of vCACCAFE:CatalogResource back as the value of items. if you change the code slightly in the last loop to:

for each( var item in items){

     System.log("name:" + item.name);

}

You will get the name of each of the catalog resource displayed in the log rather than the object which is easier to read, and see that you have the correct thing you were looking for.

This might be enough for what you want to do with the vRA ASD form, but if not I have included a method for finding the IaaS VM/Entity from these catalog resources, it's not ideal for a vRA ASD form though.

If you want to find the IaaS VM or IaaS Entity objects you can perform a lookup to find the associated objects for each catalog resource you have found, use the code at How to get the actual IaaS VM from a provisioned vCAC Catalog Resource VM? to find the IaaS VM/Entity. Put this code into a new workflow and then call it in your main workflow as part of a for each loop like the screenshots below (you could also put all of the code inside your existing workflow and use it inside a for each loop within the code):

shot1.JPG

The first scriptable task is your code to find the catalog resources, the foreach element loops through these catalog resources and finds the IaaS entities/VMs, the last scriptable task simply listed these entities/vms in the log file

Bind the variables in the for each element like the below:

shot2.JPG

Where catalogResources is the array of results from the first scriptable task.

That will give you an array of IaaS VMs/entities for each catalog resource found.

Reply
0 Kudos
mythrandir52
Contributor
Contributor

I am have no luck getting this to work.  Can you help me with the full code i cant get it to work.

Truth is a three edged sword
Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee

You haven't said which bit you are having diffiulties with, so here is what I had in my lab for the example I provided, sorry about the slightly confusing names for some of the variables.

If you need more help can you let me know what you code you have and what the problem is, e.g. does it generate an error or just not return any results?

For the first scripting task I have pretty much the same code as you originally listed:

service = vcacCafeHost.createCatalogClient().getCatalogConsumerResourceService();

var conditions = new Array();

conditions[0] = vCACCAFEFilterParam.substringOf("name", vCACCAFEFilterParam.string(serverName));

conditions[1] = vCACCAFEFilterParam.substringOf("organization/subTenant/id", vCACCAFEFilterParam.string(businessGroupName));

var filter = new Array();

filter[0] = vCACCAFEFilterParam.and(conditions);

var query = vCACCAFEOdataQuery.query().addFilter(filter);

var vcacvms = service.getResourcesList(new vCACCAFEPageOdataRequest(query));

I have manually entered my business group Id as the value of the businessGroupName as I was only testing against one business group.

vcacCafeHost is of course my vRA host (vCACCAFE:VCACHost type)

vcacvms is mapped to an array of vCACCAFE:CatalogResource

serverName is the text I want to search for, in this case it was a two letter prefix that I knew was used within the names of the VMs inside my environment.

The array of vCACCAFE:CatalogResource is passed out from the script as an out attribute.

The for each element loops through a single workflow which has the following code:

var resourceId = catalogResource.Id;

var resource = vCACCAFEEntitiesFinder.getCatalogResource(vcacHost,resourceId);

var resourceType = resource.getResourceTypeRef().getLabel();

System.log("resource type: " + resourceType);

var providerBinding = resource.getProviderBinding();

var bindingId = providerBinding.getBindingId();

var provider = providerBinding.getProviderRef();

if ((resourceType == "Virtual Machine") && (provider.getLabel() == "iaas-service")){

  System.log("It's an IaaS VM!");

  vCACVm = Server.findForType("vCAC:VirtualMachine", bindingId);

  System.log("It's name is " + vCACVm.virtualMachineName);

}

vcacHost is my vCACCAFE:vCACHost again

catalogResource is the array of vCACCAFE:CatalogRsource passed out from the earlier script. The for each loop will take the next value from this array each type it runs and assign it to the attribute named catalogResource, this is how it is possible to map an array to an attribute which expects a single value of a particular type.

The workflow outputs as an Out Parameter (so it can be used in the main workflow) the variable named vCACVm, the for each element is set to accumulate the workflow outputs (i.e. the value of vCACVm each time) in an attribute called vCACVms.

So now you have an array containing vCAC:VirtualMachine objects representing all VMs found within the specified business group, which have a name property contianing the value you asked it to look for.

To see what results were returned I added the last scripting element in the main workflow which just contains the following lines:

System.log("vCAC VMs: " + vCACVms);

System.log("Binding Id is: " + vCACVms[0].virtualMachineID);

Reply
0 Kudos