VMware Cloud Community
BWinchell
Enthusiast
Enthusiast
Jump to solution

Find VM folders via Host/Cluster or full VMfolder path

Hello,

I have an interesting one here.  In the process of building my new VM deployment workflow, I have run into a snag.  You need to declare a VM folder for any new VMs being created.  I want to get a VMfolder based on ESXi host and/or cluster or the full path to the VM folder (/vm/site1/servers/critical).

I know VMfolders are actually part of the VC/VM and not host/cluster but, is there a way to work backwards.

The don'ts:

  • Do not want to use configuration items (too clumsy for maintaining)
  • Do not want to dump all the VC folders into an array, then have to cycle through all of them looking for which one has the specific path
    • Too time consuming in larger deployments
  • Cannot use a VM and look for a folder from that perspective as the folder might be empty of VMs and since this is a VM deployment, you cannot use the existing one you need to deploy (chicken egg)

I think I already know what the answer is going to be but maybe someone has a bright idea on this one.

Thanks

B

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Try the following:

// Search index object for the first registered vCenter

var index = VcPlugin.allSdkConnections[0].searchIndex;

// Inventory path of the folder to search for (note: starts with datacenter name; replace datacenter123 with your datacenter name)

var invPath = "/datacenter123/vm/site1/servers/critical/";

var folder = index.findByInventoryPath(invPath);

System.log("folder: " + folder);

View solution in original post

Reply
0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Try the following:

// Search index object for the first registered vCenter

var index = VcPlugin.allSdkConnections[0].searchIndex;

// Inventory path of the folder to search for (note: starts with datacenter name; replace datacenter123 with your datacenter name)

var invPath = "/datacenter123/vm/site1/servers/critical/";

var folder = index.findByInventoryPath(invPath);

System.log("folder: " + folder);

Reply
0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

Thanks.  That is what I was looking for.  I am glad I did not have to dump my entire folder structure.

Can you explain the:

var index = VcPlugin.allSdkConnections[0].searchIndex;

System.log("var index: " + index);

  • exactly what is this doing?
  • what will happen if you have multiple VC linked?
    • I currently only have one VC but want to have the code setup in case I get to that point.
      • When I dump the VcPlugin.allSdkConnections, it returns the URL FQDN (alias in my case).  I am assuming it is pulling that connection information via the VCO registration?

Thanks

B

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

VcPlugin.allSdkConnections returns an array of all available VcSdkConnection objects, each one representing a registered vCenter server instance.


VcPlugin.allSdkConnections[0] returns the first element from the array. This is fine for sample code; in production you usually add an input parameter allowing the user to select an SdkConnection (and thus a vCenter server instance), or as an option you may implement some logic to iterate over all connections and select one using some criteria.


VcPlugin.allSdkConnections[0].searchIndex returns reference to SearchIndex service for the given connection. SearchIndex service allows client code to perform efficient queries over vCenter inventory.


For more info, check vRO API Explorer or online documentation for VcSdkConnection and VcSearchIndex.

Reply
0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

I am still confused about the SDK stuff.  And more than likely I am not fully understanding the difference in the objects.  I am trying to ensure the VC I am connected to is the one I expect.

**as you can tell, I am not a developer Smiley Happy **

So I did go through the VRO API docs to try to find some more info on the:

  • VcPlugin.allSdkConnections
    • (vCenter Server 6.0 Plug-In API Reference for vRealize Orchestrator)
      • VcPlugin.allSdkConnections = returns all SDK conntions (via the API explorer)
      • VcPlugin.allSdkConnections = Object represents a connection to a vCenter host (online docs)
        • Questions:
          • if this represents a connection from your VRO to VC (that it is registered to), could you ever have a VRO registered to multiple VCs?
            • If the answer is no:
              • Ignore the code below as it will be mainly pointless (to ensure the correct VC)
            • If the answer above is yes:
              • I cannot figure out how to query the connection to ensure it is the vCenter I actually want
  • E.g. 

    //ensure you are connected to the correct VC

    var targetVc = "vcenter.domain.local";

    var sdkType = "VimHost";

    var sdk = VcPlugin.allSdkConnections; //will return name of the vCenter that is registered in VRO

    var dcs = [];

    for (var i in sdk) {  //cycle through each entry in the array

    if ((sdk[i].vimType == sdkType) && (sdk[i].name == targetVc)) {  //look for an entry that is a "vimHost" type and the object name = vcenter.domain.local - neither of these actually exist but need something like this

       dcs.push(sdk[i]);

    }

    }

    // some error code checking to ensure the array is not empty (will not bore you with that)

    // set object

    if (dcs.length == 1) {  //can only have one VC with the above name

    var vCenter = dcs[0];

    }

    //Find the correct datacentre

    var vcDatacentreName = "MyPerfectRobustReliableVC";

    var allDatacentres = VcPlugin.getAllDatacenters();  //get all datacentres configured in your VC

    var vcDatacentres = [];

    for (var i in allDatacentres) {

    if (allDatacentres[i].name == vcDatacentreName) {

       vcDatacentres.push(allDatacentres[i];

    }

    }

    // some error code checking to ensure the array is not emtpy

    // set object

    if (vcDatacentres.length ==1) {  //can only have one datacentre in a VC with the above name

    var vcDatacentre = vcDatacentres[0];

    }

    // Basically I have 2 variables set with objects

    // vCenter = [VcSdkConnectio]-[class com.vmware.vmo.plugin.vi4.Vimhost] -- VALUE: vcenter.domain.local

    // vcDatacentre = [VcDatacenter]-[class com.vmware.vmo.plugin.vi4.model.VimDatacenter] -- VALUE: Datacenter<datacenter-2>'MyPerfectRobustReliableVC'

                • My issue comes in on line 07.  I cannot find any actions/queries/methods etc... to run against the object.  Always come up as "undefined"
                  • Finding the datacentre portion works fine.
Reply
0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

I think I found a way to validate my connection.  This should do the trick:

//Module logging

  System.log("-----------------BEGIN-LOG------------------------------");

  System.log("Module=SetVmFolder");

var vcDatacentreString = vcDatacentre.name; //root VM folder name

var sdkUrl = sdkProtocol + "://" + targetVcenter + "." + adDomainSuffix + ":" + sdkPort + "/sdk"; //should look something like http://vcenter.domain.local:443/sdk

var allSdk = VcPlugin.allSdkConnections;

var vcs = [];

  //Debug

  if (debugOutput == true) {

  System.debug("DEBUG_LOG_Module=SetVmFolder: var sdkUrl = " + sdkUrl);

  System.debug("DEBUG_LOG_Module=SetVmFolder: var allSdk = " + allSdk);

  }

// Validate SDK connection to target vCenter

for (var i in allSdk) {

  if (allSdk[i].displayName == sdkUrl) { //ensure current SDK connection is pointing to expected vCenter

  vcs.push(allSdk[i]);

  }

  //Debug

  if (debugOutput == true) {

  System.debug("DEBUG_LOG_Module=SetVmFolder: var allSdk[i] = " + allSdk[i]);

  System.debug("DEBUG_LOG_Module=SetVmFolder: var allSdk[i].displayName = " + allSdk[i].displayName);

  }

}

System.log("Target vCenter SDK connection: " + vcs)

// Error check

if (vcs.length != 1) {

  System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

  System.error("Module=SetVmFolder");

  System.error("Too many/few vCenters match SDK string");

  System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

  throw exception;

  }

// Set SDK serch index and find inventory

var sdkIndex = vcs[0].searchIndex;

var vmEnvClsTLA = vmEnvironmentClass.match(new RegExp(vmEnvClsRegEx, "")); //set next level folder name based on VM environment (DEV, UAT, TEST, PROD)

var vmEnvClsTLA = vmEnvClsTLA[0];

var vmOSCls = vmOS.match(new RegExp(vmOSClsRegEx, "")); //set next level folder name based on VM OS (Windows2012, Windows10, Ubuntu16.04)

var vmOSCls = vmOSCls[1];

if (vmEnvClsTLA == null) {

  System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

  System.error("Module=SetvmFolder");

  System.error("VM environment class prefix cannot equal NULL");

  System.error("!!!!!!!!!!!!!!!!!!!!!Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

  throw exception;

  }

  //Debug

  if (debugOutput == true) {

  System.debug("DEBUG_LOG_Module=SetVmFolder: var vmEnvClsTLA = " + vmEnvClsTLA);

  }

var invPath = "/" + vcDatacentreString + vmFolderGlobalPrefix + vmSiteLocTLA + vmSiteLocTLAsuffix + "/" + vmEnvClsTLA + "/" + vmOSCls; //something like /datacenter1/vm/Site1/DEV/Servers

  //Debug

  if (debugOutput == true) {

  System.debug("DEBUG_LOG_Module=SetVmFolder: var invPath = " + invPath);

  }

// Find folders within VM folder path 

var folder = sdkIndex.findByInventoryPath(invPath); 

System.log("VM folder path for new VM: " + invPath);

System.log("Target VM folder for new VM: " + folder);

//Module logging

  System.log("-----------------END-LOG------------------------------");

  System.log("Module=SetVmFolder");

Reply
0 Kudos