odesey
Enthusiast
Enthusiast

vRO 8 - Array Iterator issue

Jump to solution

I am working on a workflow that will get a list of VM's from a text file. I convert the text file to an array like so:

var split = textList.content.split("\n");

split.forEach(function(uuid) {
    actionResult.push(uuid);
});

return actionResult;

 

Here is the problem, when I iterate over the array to find the VC:VirtualMachine object, only the last entry in the array is found. I have tried the following but they all have the same result, only the last VM in the array is found:

This script is inside an Action Item:

var VM_LIST = new Array();
VM_LIST = ["TEST-001", "TEST-002","TEST-003","TEST-004"]

VM_LIST.forEach(function(vm, i){
  //searching by UUID, same result, only finds the last VM in the array
  vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, false);

  //searching by UUID, with true as the last argument, does not find anything
  vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, true);

  //searching by VM name, same result, only finds the last VM in the array
  vCenterVm = VcPlugin.getAllVirtualMachines(null, vm)
})

 

 

 

My question is, why is this happening? Is there another way to iterate over an array of items and find a VM?

Environment: vRO 8.2
vCenter (2x) 6.7, (2x) 7.0
vRA: 8.2
vCenter Plugin version: VC 7.0.0.18048864
vReplicator Plugin version: VR 8.4.0.17638044

 

 

Labels (6)
0 Kudos
1 Solution

Accepted Solutions
odesey
Enthusiast
Enthusiast

I opened a case with VMware and they finally found the issue. Due to the fact that I was creating the text file on a windows machine I needed to do the following when reading the file into vRO:

 

\\ notice the \r that fixed the problem!
var split = textList.content.split("\r\n");

split.forEach(function(uuid) {
    actionResult.push(uuid);
});

return actionResult;

 

Thanks for all the help and suggestions.

View solution in original post

0 Kudos
21 Replies
eoinbyrne
Expert
Expert

Couple of questions

1. Are you sure the 4 VMs you're looking for all exist in the VcPlugin inventory? (Assuming yes to this, but worth asking anyway)

2. Are you referencing the variable vCenterVm further outside the foreach loop? The way the function works is that it will complete the full iteration over the array before moving on to the next line. This *may* be why it seems like only the last entry is found as when the function exits, the value stored in vCenterVm will be the last VM from the list. You can confirm this by adding

System.log("vCenterVm == " + vCenterVm);

after each reference inside the function body. 

If you say wanted to lookup the VMs from the array by name and build another array of the found VM objects then do this outside 

var foundVMs = [];

Then inside your function body add

if(vCenterVm != null)

foundVMs.push(vCenterVm);

after the object has been retrieved from the inventory

 

-HTH

 

0 Kudos
odesey
Enthusiast
Enthusiast

 

var VM_LIST = new Array();
var RESULTS = new Array();
VM_LIST = ["TEST-001", "TEST-002","TEST-003","TEST-004"]

VM_LIST.forEach(function(vm, i){
  //searching by UUID, same result, only finds the last VM in the array
  vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, false);

  //searching by UUID, with true as the last argument, does not find anything
  vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, true);

  //searching by VM name, same result, only finds the last VM in the array
  vCenterVm = VcPlugin.getAllVirtualMachines(null, vm)
  
  //this will show null or empty string EXCEPT for the last vm in the iteration
  System.log(vCenterVm)

  RESULTS.push(vCenterVm)
})

//this will only have ONE vm object and not 4
System.log(RESULTS)

 

 

I cleaned up the code about to show more detail.

 

Also if I run the same code with an array that has 4 machines with the same name like so:

["TEST-001", "TEST-001", "TEST-001", "TEST-001"]

Only the last VM will be found and added to the array....all the other VM's will return null

  1. Are you sure the 4 VMs you're looking for all exist in the VcPlugin inventory? (Assuming yes to this, but worth asking anyway)
    1. Yes
  2. Are you referencing the variable vCenterVm further outside the foreach loop? The way the function works is that it will complete the full iteration over the array before moving on to the next line. This *may* be why it seems like only the last entry is found as when the function exits, the value stored in vCenterVm will be the last VM from the list. You can confirm this by adding
    1. No, see the updated code above

 

0 Kudos
eoinbyrne
Expert
Expert

Can you add this to the top of your foreach function

 

System.log(vm)

 

This will print the value your search is using on each iteration

0 Kudos
odesey
Enthusiast
Enthusiast

That works as expected.

// this shows the correct VM name....so first run is TEST-001, then TEST-002...

System.log(vm)

 

0 Kudos
eoinbyrne
Expert
Expert

Might be the scoping of the vCenterVm object - can you try this code? It's possible that the repeated searches are clobbering the value of vCenterVm on each search so I've added checks for null to prevent that.... logic now is "try the next search IF the last one returned NULL"

var VM_LIST = new Array();
var RESULTS = new Array();
VM_LIST = ["TEST-001", "TEST-002","TEST-003","TEST-004"]

VM_LIST.forEach(function(vm, i){
	Sytem.log("Searching by UUID for " + vm);
	//searching by UUID, same result, only finds the last VM in the array
	
	// specify it as a locally scoped variable by putting "var" in front of it
	var vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, false);

	// don't clobber the value if we found it already!
	if(vCenterVm == null)
	{
		System.log("Not found by UUID - trying next strategy to find vm " + vm + ".....")
		//searching by UUID, with true as the last argument, does not find anything
		vCenterVm = sdkConnection.searchIndex.findByUuid(null, vm, true, true);
	}

	// don't clobber the value if we found it already!
	if(vCenterVm == null)
	{
		System.log("Still not found - trying next strategy to find vm " + vm + ".....")
		//searching by VM name, same result, only finds the last VM in the array
		vCenterVm = VcPlugin.getAllVirtualMachines(null, vm);
	}

	//this will show null or empty string EXCEPT for the last vm in the iteration
	System.log("Value we have for name " + vm + " is  --> " + vCenterVm);

	RESULTS.push(vCenterVm)
});

//this will only have ONE vm object and not 4
for each(var entry in RESULTS)
{
	System.log(instanceof(entry))
	System.log("entry has a value? : " + ((entry != null) ? "YES" : "NO"));
}
0 Kudos
odesey
Enthusiast
Enthusiast

Apologies for the late response, had some other issues arise that needed to be taken care off first.

Unfortunately I get the same results running your modified script, only the last VM in the array if found. All other log the following:

 

 

 

Searching by UUID for TEST-003
2021-06-15 14:07:21.394 -04:00info(converListToArray) Not found by UUID - trying next strategy to find vm TEST-003.....
2021-06-15 14:07:21.397 -04:00info(converListToArray) Still not found - trying next strategy to find vm TEST-003.....
2021-06-15 14:07:21.474 -04:00info(converListToArray) Value we have for name TEST-003 is  --> 

// same for all the others EXCEPT #10 which logs this:
2021-06-15 14:07:23.218 -04:00info(converListToArray) Value we have for name TEST-010 is  --> DynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.o11n.plugin.vsphere_gen.VirtualMachine_Wrapper] -- VALUE : Stub: moRef = (ManagedObjectReference: type = VirtualMachine, value = vm-34036, serverGuid = null), binding = https://<vcenter>:443/sdk,DynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.o11n.plugin.vsphere_gen.VirtualMachine_Wrapper] -- VALUE : Stub: moRef = (ManagedObjectReference: type = VirtualMachine, value = vm-36827, serverGuid = null), binding = https://<vcenter>:443/sdk

 

 

0 Kudos
eoinbyrne
Expert
Expert

A couple of observations

eoinbyrne_0-1623787687563.png

 

That method returns an array of VMs, not a single one. 

This one might need the VcDatacenter parameter?

eoinbyrne_1-1623787808226.png

 

Can you comment out the searches you're using and replace them with this and then try it?

var allMatchingVMs = VcPlugin.getAllVirtualMachines(null,
    "xpath:matches(translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
    'abcdefghijklmnopqrstuvwxyz'), '" +
    vm.toLowerCase() + "')")
if(allMatchingVMs != null)
{
    System.log("Found " + allMatchingVMs.length + " matches!");
    vCenterVm = allMatchingVMs[0];
}
else
{
    System.log("XPATH search did not find any match for name " + vm);
}

 

This will do an XPATH search on the full VM inventory & will return an array of VM entries. If the first 3 entries are not found using this approach then there is something amiss within the inventory or the relevant VC connections are not correctly created in vRO inventory

Also, it's better for me if I can see the full log trace & the code you're using. 

0 Kudos
odesey
Enthusiast
Enthusiast

 

2021-06-16 07:29:18.886 -04:00info(converListToArray) conver text file to array
2021-06-16 07:29:18.893 -04:00info(converListToArray) 10
2021-06-16 07:29:18.894 -04:00info(converListToArray) UUIDs to be processed
2021-06-16 07:29:18.895 -04:00info(converListToArray) TEST-001
,TEST-002
,TEST-003
,TEST-004
,TEST-005
,TEST-006
,TEST-007
,TEST-008
,TEST-009
,TEST-010
2021-06-16 07:29:18.905 -04:00info(converListToArray) Done listing vCenter servers
2021-06-16 07:29:18.906 -04:00info(converListToArray) Searching by UUID for TEST-001
2021-06-16 07:29:19.014 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.015 -04:00info(converListToArray) Searching by UUID for TEST-002
2021-06-16 07:29:19.088 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.089 -04:00info(converListToArray) Searching by UUID for TEST-003
2021-06-16 07:29:19.215 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.216 -04:00info(converListToArray) Searching by UUID for TEST-004
2021-06-16 07:29:19.286 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.287 -04:00info(converListToArray) Searching by UUID for TEST-005
2021-06-16 07:29:19.561 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.562 -04:00info(converListToArray) Searching by UUID for TEST-006
2021-06-16 07:29:19.857 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:19.858 -04:00info(converListToArray) Searching by UUID for TEST-007
2021-06-16 07:29:20.162 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:20.163 -04:00info(converListToArray) Searching by UUID for TEST-008
2021-06-16 07:29:20.459 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:20.460 -04:00info(converListToArray) Searching by UUID for TEST-009
2021-06-16 07:29:20.760 -04:00info(converListToArray) Found 0 matches!
2021-06-16 07:29:20.761 -04:00info(converListToArray) Searching by UUID for TEST-010
2021-06-16 07:29:21.060 -04:00info(converListToArray) Found 2 matches!

 

 

 

2021-06-16 07:32:51.253 -04:00INFO(converListToArray) conver text file to array
2021-06-16 07:32:51.254 -04:00INFO(converListToArray) 11
2021-06-16 07:32:51.255 -04:00INFO(converListToArray) UUIDs to be processed
2021-06-16 07:32:51.256 -04:00INFO(converListToArray) TEST-001
,TEST-002
,TEST-003
,TEST-004
,TEST-005
,TEST-006
,TEST-007
,TEST-008
,TEST-009
,TEST-010
,TEST-001
2021-06-16 07:32:51.262 -04:00INFO(converListToArray) Done listing vCenter servers
2021-06-16 07:32:51.263 -04:00INFO(converListToArray) Searching by UUID for TEST-001
2021-06-16 07:32:51.366 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:51.367 -04:00INFO(converListToArray) Searching by UUID for TEST-002
2021-06-16 07:32:51.435 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:51.436 -04:00INFO(converListToArray) Searching by UUID for TEST-003
2021-06-16 07:32:51.508 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:51.509 -04:00INFO(converListToArray) Searching by UUID for TEST-004
2021-06-16 07:32:51.580 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:51.581 -04:00INFO(converListToArray) Searching by UUID for TEST-005
2021-06-16 07:32:51.761 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:51.762 -04:00INFO(converListToArray) Searching by UUID for TEST-006
2021-06-16 07:32:52.060 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:52.061 -04:00INFO(converListToArray) Searching by UUID for TEST-007
2021-06-16 07:32:52.361 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:52.362 -04:00INFO(converListToArray) Searching by UUID for TEST-008
2021-06-16 07:32:52.659 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:52.660 -04:00INFO(converListToArray) Searching by UUID for TEST-009
2021-06-16 07:32:52.960 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:52.961 -04:00INFO(converListToArray) Searching by UUID for TEST-010
2021-06-16 07:32:53.287 -04:00INFO(converListToArray) Found 0 matches!
2021-06-16 07:32:53.288 -04:00INFO(converListToArray) Searching by UUID for TEST-001
2021-06-16 07:32:53.560 -04:00INFO(converListToArray) Found 2 matches!

 

 

Ok I posted two sets of logs, notice that in the first run, only TEST-010 has been found, but in the second run I made TEST-001 the first and the last VM in the Array. The first TEST-001 was not found but the second one (last in the array) was found.

 

A few questions.

 

I am using the 7.0 of the vCenter plugin

Version 7.0.0-18048864 (built 2021-05-19) located here:

https://communities.vmware.com/t5/vRealize-Orchestrator-Documents/vRO-vCenter-Server-plug-in-for-vSp...

 

Could it be a bug in this version? I specifically upgraded to support 2 7.0 vCenter servers we are planning to use. The VMs are all located in a 6.7 vCenter but I am wondering is the latest version of the plugin might has some issues.

 

Why are multiple VM's being found where there is only one VM with the name that I am searching for?

 

What version of Javascript (ecmascript) is vRO using? Commands like console.log() and arrow functions () => {} do not work

0 Kudos
eoinbyrne
Expert
Expert

Might be a compatibility issue if the underlying APIs are different. 

Are you able to revert to the 6.7 version of the VC plugin and try again?

As a last check, can you look in the vRO Client Inventory view and verify that you are able to expand & walk the tree under the VC plugin? If this causes some issues then it's simple. Also, while you're there, verify that you can see the VMs you're searching for in the tree. If they're not there but other elements are working then this would also indicate the area causing the trouble 

0 Kudos
eoinbyrne
Expert
Expert

Also, re the Javascript version - vRO uses the Rhino Javascript engine which is a Java implementation of the interpreter. You'd need to crack open the rhino.jar off the server and read the MANIFEST.MF to sort out which build/version it is

In general, the JS implementation is fairly accurate but newer syntax (such as the arrow operator) do not seem to be supported at this time. Also, one definite difference is that you must use System.log where you would normally use console.log with say Node.js or a browser engine.

You can use this site - vroapi.com - as a reference to the plugin objects, methods and attributes. If you look at the Intrinsics package you can find the reference material for the built-in classes provided by the server

 

0 Kudos
odesey
Enthusiast
Enthusiast

Thanks for all the suggestions and information. I opened a case with VMware support and they are investigating.

If anyone can confirm that you are able to iterate over a list of VM names\UUIDs and get results, please share the version of vRO 8 and plugins version that is working for you.

 

Thanks.

 

0 Kudos
xian_
Expert
Expert

VcPlugin.getAllVirtualMachines returns an array, so you need to concatenate the results instead of pushing them:

VM_LIST.forEach(function (vm, i) {
  vCenterVm = VcPlugin.getAllVirtualMachines(null, vm)
  System.log(vCenterVm)
  RESULTS = RESULTS.concat(vCenterVM)
})
System.log(RESULTS)

I suggest using xpath to define exact hostname match, as the code above does a substring search and you might find additional VMs!

So use instead:

  vCenterVm = VcPlugin.getAllVirtualMachines(null, "xpath:name='" + vm + "'")
0 Kudos
odesey
Enthusiast
Enthusiast

Thanks for the suggestion but here is the problem:

 

VM_LIST.forEach(function (vm, i) {
  vCenterVm = VcPlugin.getAllVirtualMachines(null, "xpath:name='" + vm + "'")
  System.log(vCenterVm) // this line is always null \ empty sting EXCEPT for the last VM in the array
  RESULTS = RESULTS.concat(vCenterVm) // this wont matter because vCenterVm is empty 
})

System.log(RESULTS) // only has one result

 

 

0 Kudos
xian_
Expert
Expert

Are you sure you have all VMs on vCenter you are looking for?

Can you check the vCenter inventory in vRO if your VMs are discovered?

0 Kudos
odesey
Enthusiast
Enthusiast

100% sure.

 

I can manually traverse the vCenter and see the VM's in question, also, if I create an array like so:

 

VM_LIST = ["TEST-001", "TEST-001","TEST-001","TEST-001"]

 

Notice that all the VM's have the same name, but in the script that you sent, only the last VM would be found.

0 Kudos
xian_
Expert
Expert

I did not mean vCenter, but the inventory of vRO:

xian__0-1624386583010.png

Can you see all your VMs in question?

0 Kudos
odesey
Enthusiast
Enthusiast

Yes I can.

Screen Shot 2021-06-22 at 2.35.34 PM.png

0 Kudos
xian_
Expert
Expert

Just tested and could not reproduce your issue. The plugin version is 6.5.0.17494695

Can you try to downgrade and retest?

0 Kudos
odesey
Enthusiast
Enthusiast

Where can I find the version of the vCenter Plugin you have? I do not see it listed here:

https://communities.vmware.com/t5/vRealize-Orchestrator-Documents/vRO-vCenter-Server-plug-in-for-vSp...

 

I also went back to the version of the plugin that came with the version of vRO:

VC 6.5.0.16904859

The issue persists.

 

One more thing, are you using a stand alone or integrate vRO instance? Mine is an integrated instance with vRA.

 

0 Kudos