VMware Cloud Community
pabloramos
Enthusiast
Enthusiast

how to anchor the getallvmfolder to specific Datacenter

I run a workflow to retrieve a VMFolder resource to place new vm.

I used code from this post : https://communities.vmware.com/message/2110436 to put together a script that takes path string attribute to then traverse all of the VMFolders in order to get the last element in the path attribute (VMFolder).

I am running into little anomaly where in some cases the path is the same, only diff is that they exist in different Datacenters.

This is section of the code that retrieves all of the vm folders. I assumed since I was passing the cluster resource it would only retrieve folders that were in that Datacenter. Probably not right assumption.

How can  I modify the folders variable to only run the getallVMFolders on specific Datacenter

var sdkConnection_new = cluster.sdkConnection;  

System.log("Path has "+foldercount+" elements, last element "+VMFolderName);

var folders = sdkConnection_new.getAllVmFolders();

// result is an array of type VcFolder

//

0 Kudos
1 Reply
iiliev
VMware Employee
VMware Employee

Something like this should work

var folders = []; 

function enumerate(folder) { 

  if (folder == null) return; 

  folders.push(folder); 

  var children = folder.childEntity; 

  for each (var child in children) { 

    if (child instanceof VcFolder) { 

       enumerate(child); 

    } 

  } 

// assuming 'dc' is the datacenter object 

enumerate(dc.vmFolder); 

// print the result

for each (var f in folders) { 

  System.log("folder name -> " + f.name);