VMware Cloud Community
colinj
Contributor
Contributor

Finding a specific folder from a set with identical names

Howdy all,

I'm working with the following folder structure in vCenter and I'm struggling for the right way to find a specific folder because the subfolders are named the same for each group.

Group A
Sec
IAAS
Linux MOS
Windows MOS
Web
IAAS
Linux MOS
Windows MOS
Group B
Sec
IAAS
Linux MOS
Windows MOS
Web
IAAS
Linux MOS
Windows MOS

What I would like to do is grab the folder "Group B/Sec/IAAS" so that I can create a new VM there. I know that I can get the set of folders with a specific name with something like:

var vmOwner = "foo";
var strXPath = "xpath:child[name='" + vmOwner + "']";
var vmFolderList = sdkConnection.getAllVmFolders(null , strXPath)

And that works just fine. But if I search for a folder named "IAAS" I get all of them back but I can't tell which one I want.

So, is it possible to narrow down my search with a more complex xpath string? If so, what would that look like? Everything I've tried in terms of getting children or grandchildren but that has produced a syntax error.

suggestions?

colin j.

0 Kudos
1 Reply
thechaos
Enthusiast
Enthusiast

Hi,

had a similar problem, and solved this by specifing the complete path to the folder, and doing a folder search using javascript, this is my script (to give you an idea):


if (inDebug) {
    System.debug("Start looking for folder "+wfFolderPath);
}
var folderelements = wfFolderPath.split("/");
var foldercount = folderelements.length;
foldercount--;
if (foldercount < 1) {
    throw "Single folder path not supported";
}
//
// Locate the root node for the cluster
//
var parent = inCluster.parent;
var clusterRoot = inCluster;
while (parent.parent) {
    clusterRoot = parent;
    parent = parent.parent;
    if (inDebug) {
        System.debug("clusterRoot : "+clusterRoot.name);
        System.debug("parent      : "+parent.name);
    }
   
   
}

if (inDebug) {
    System.debug("Root node : "+parent.name);

}

var VMFolderOK     = false;
var VMFolderName = folderelements[foldercount];
outFolderObject = null;

var sdkConnection = inCluster.sdkConnection;

System.debug("Path has "+foldercount+" elements, last element "+VMFolderName);
var folders = sdkConnection.getAllVmFolders();
// result is an array of type VcFolder
//
for (f = 0; f < folders.length;f++) {
    if (folders[f].name.toLowerCase() == VMFolderName.toLowerCase()) {
        outFolderObject = folders[f];
        var upcounter = foldercount - 1;
        var parent = folders[f].parent;
        var folderRoot = folders[f];
        var ParentName;
        while (parent.parent && upcounter >= 0) {
            ParentName = parent.name.toLowerCase();
            //
            // Fix POCFarm_vm, mapp it to vm
            //
            if (ParentName == "pocfarm_vm") {
                if (inDebug) {
                    System.debug("Change POCFarm_vm to vm");
                }
                ParentName = "vm";
            }
            System.debug("Parent name "+ParentName);   
            if (ParentName == folderelements[upcounter].toLowerCase()) {
                System.debug("Parent "+ParentName+" match "+folderelements[upcounter]);
            } else {
                System.debug("Parent "+ParentName+" does not match "+folderelements[upcounter]);
                outFolderObject = null;
            }
            folderRoot = parent;
            upcounter--;
            parent = parent.parent;           
        }
        if (outFolderObject) {
            break;
        }
    }
}

if (outFolderObject) {
    System.debug("Path/Folder found");
} else {
    throw "Path/Folder not found or not unique";
}
System.log("Stop FindVMFolder");

Thomas