ullbergm
Enthusiast
Enthusiast

How to get a SdkConnection from a VclVM

Jump to solution

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

Check out my orchestration blog here: http://ullberg.us/orchestrate/
0 Kudos
1 Solution

Accepted Solutions
cdecanini_
VMware Employee
VMware Employee

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;
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(":"));
}

//Check we have the right vCenter server for that
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.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

0 Kudos
4 Replies
cdecanini_
VMware Employee
VMware Employee

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;
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(":"));
}

//Check we have the right vCenter server for that
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.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

0 Kudos
ullbergm
Enthusiast
Enthusiast

Perfect, thanks. Smiley Happy

Check out my orchestration blog here: http://ullberg.us/orchestrate/
0 Kudos
ullbergm
Enthusiast
Enthusiast

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...

Check out my orchestration blog here: http://ullberg.us/orchestrate/
0 Kudos
cdecanini_
VMware Employee
VMware Employee

Good catch. I forgot to mention this was code I wrote for vCD 1.5.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos