Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 144140

Re: Create a VM directory tree in vCenter (with folder exists check)

$
0
0

Hello Joerg,

Thanks for your help.  It clarified what I had to return from the function.

 

I decided to accomplish this a little different way.  Not sure if it is any better, worse or indifferent but in my non-programmer head it made sense.

I changed the allSubFolders to have 2 entries (folder name & folder object).  Now in my found, I retrieve the subfolder name (array index), add 1 (get the next record which should be the object), and return that.

My testing seems to create the results I want but can you let me know if this is a bad programming standard or makes sense?

 

Thanks

 

//////////////////////////////////////////////////////////////////////////////
// CODE: Javascript                                                            //
// TITLE: BuildFolderTree                                                    //
// AUTHOR: Brandt Winchell                                                    //
// COLLABORATORS: robrtb12, tschoergez                                        //
// VERSION: 2.1                                                                //
// DATE MODIFIED: July 4, 2013                                                //
// PURPOSE:  Build a directory tree in vCenter VM & Template section        //
// ADDITIONAL INFO: !!Root folder must be created manually before            //
// running this code!!                                                        //
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// DECLARE VARIABLES
var folderBase = ["Dev","DMZ","Prod","UAT"];//base folders
var folderT1 = ["Servers","Workstations"]; //sub-folders of $folderBase
var folderT2 = ["Windows","Linux"]; //sub-folders of $folderT1
var folderT3 = ["Repo1","Repo2","Repo3","Repo4"]; //sub-folders of $folderT2
var folderT4 = ["T1","T2","T3"]; //sub-folder of $folderT3
//////////////////////////////////////////////////////////////////////////////
// GLOBAL EXCEPTION CATCH FOR NULL INPUT VARIABLES
if (parentFolder == null) {    throw "REFERENCE ERROR: $parentFolder IS NULL!!";    }
//////////////////////////////////////////////////////////////////////////////
// BUILD THE DIRECTORY TREE
// create the $folderBase level of directory
for (var a=0; a<folderBase.length; a++) {    var newBaseFolder = buildFolderTree(parentFolder, folderBase[a]);    var parentFolderObj = newBaseFolder;      // Create the $folderT1 level of folders    for (var b=0; b<folderT1.length; b++) {           var newT1Folder = buildFolderTree(newBaseFolder, folderT1[b]);        var parentFolderObj = newT1Folder;        // Create the $folderT2 level of folders        for (var c=0; c<folderT2.length; c++) {            var newT2Folder = buildFolderTree(newT1Folder, folderT2[c]);            var parentFolderObj = newT2Folder;            // Create the $folderT3 level of folders              for (var d=0; d<folderT3.length; d++) {                   var newT3Folder = buildFolderTree(newT2Folder, folderT3[d]);                var parentFolderObj = newT3Folder;                // Create the $folderT4 level of folders                for (var e=0; e<folderT4.length; e++) {                     var newT4Folder = buildFolderTree(newT3Folder, folderT4[e]);                    }                  }            }           }    }
//////////////////////////////////////////////////////////////////////////////  
// BUILD FUNCTION $buildFolderTree  
function buildFolderTree(parentFolderObj, folderName) {    //Get a list of sublfolders    var children = parentFolderObj.childEntity;    var allSubFolders = new Array();    for (var i in children) {        if (children[i] instanceof VcFolder) {            var subfolderParent = children[i];            var subName = subfolderParent.name; //Get only the folder name            if (debugOutput == true) {                System.log("subfolder Name($subName): " + subName);                }            allSubFolders.push(subName); //Push to array all subfolder names            allSubFolders.push(children[i]); //Push to array all subfolder objects            }        }        if (debugOutput == true) {            System.log("subfolder list($allSubFolders): " + allSubFolders);            }    //Create folder if the folder does not already exists    var found = allSubFolders.indexOf(folderName); //Does $folderName exists in array? True:Index# of subName, False:-1    if (debugOutput == true) {        System.log("Original found($found): " + found);        }        if (found != -1) {            System.log("Folder " + folderName + " already exists. Continue with next item");            var folderObjIndex = found+1; //Get index# of subfolder objects (next item in array)            if (debugOutput == true) {                System.log("New found($folderObjIndex): " + folderObjIndex);                }            return allSubFolders[folderObjIndex];            }        if (found == -1) {            System.log("Building folder " + folderName);            var newFolder = parentFolderObj.createFolder(folderName);            System.log("Built directory: " + parentFolderObj.name + "/" + folderName);            return newFolder;            }        }
//////////////////////////////////////////////////////////////////////////////

Viewing all articles
Browse latest Browse all 144140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>