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:
I think I already know what the answer is going to be but maybe someone has a bright idea on this one.
Thanks
B
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);
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);
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);
Thanks
B
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.
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 **
So I did go through the VRO API docs to try to find some more info on the:
//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'
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");