So If I have a VclVM object, is there a way to get a SdkConnection to the vCenter or host that is running that particular vm?
Best guess I have is to get the name of the vCenter and loop through VcPlugin.allSdkConnections to see if the name matches (assuming that there is a property that has the name of the vcenter in it...)
var hostRef = VclVirtualMachine.getVMHostVimRef();
System.log(hostRef.VimServerRef.name); // This ouputs the server name of the vcenter that hosts the vm
Anyone have a better suggestion?
Thanks,
Magnus
Some sample code that can help:
var vApp = vm.parent;
var vdc = vApp.parent;
var org = vdc.parent;
var host = org.parent;
var providerVdcReference = vdc.toAdminObject().providerVdcReference;
var providerVdc = host.getEntityByReference(VclFinderType.providerVdc ,providerVdcReference);
var adminExtProviderVdc = providerVdc.toAdminExtensionObject();var vimServerUrl = adminExtProviderVdc.getVimServer().url;
//Check we have the right vCenter server for that
function getHostname(url){
// expected input is in form of https://myhost:443 or https://myhost.domain.com:443
return url.substring(url.indexOf("://") + 3,url.lastIndexOf(":"));
}
var allVimServers = VcPlugin.allSdkConnections;
var gotRightVimServer = false;
for each (var vimServer in allVimServers) {
System.log(vimServer.name);
if (System.getModule("com.vmware.pso").isHostIpMatch(getHostname(vimServer.name),getHostname(vimServerUrl))) {
gotRightVimServer = true;
break;
}
}
isHostIpMatch:
var host1Ip = System.resolveHostName(host1);
var host2Ip = System.resolveHostName(host2);
System.log("host1Ip = " + host1Ip)
System.log("host2Ip = " +host2Ip)
return (host1Ip == host2Ip);
Christophe.
Some sample code that can help:
var vApp = vm.parent;
var vdc = vApp.parent;
var org = vdc.parent;
var host = org.parent;
var providerVdcReference = vdc.toAdminObject().providerVdcReference;
var providerVdc = host.getEntityByReference(VclFinderType.providerVdc ,providerVdcReference);
var adminExtProviderVdc = providerVdc.toAdminExtensionObject();var vimServerUrl = adminExtProviderVdc.getVimServer().url;
//Check we have the right vCenter server for that
function getHostname(url){
// expected input is in form of https://myhost:443 or https://myhost.domain.com:443
return url.substring(url.indexOf("://") + 3,url.lastIndexOf(":"));
}
var allVimServers = VcPlugin.allSdkConnections;
var gotRightVimServer = false;
for each (var vimServer in allVimServers) {
System.log(vimServer.name);
if (System.getModule("com.vmware.pso").isHostIpMatch(getHostname(vimServer.name),getHostname(vimServerUrl))) {
gotRightVimServer = true;
break;
}
}
isHostIpMatch:
var host1Ip = System.resolveHostName(host1);
var host2Ip = System.resolveHostName(host2);
System.log("host1Ip = " + host1Ip)
System.log("host2Ip = " +host2Ip)
return (host1Ip == host2Ip);
Christophe.
Perfect, thanks.
Just in case someone else finds this and wants to use it, I had to make a small change.
From
var vimServerUrl = adminExtProviderVdc.getVimServer().url;
To
var vimServerUrl = adminExtProviderVdc.getVimServers()[0].url;
Looks like the API has been updated to support multiple vCenters per Vdc...
Good catch. I forgot to mention this was code I wrote for vCD 1.5.