VMware Cloud Community
FreddyFredFred
Hot Shot
Hot Shot
Jump to solution

get datacenter of vm

Given a a uuid of a vm, is there an easy way to find its datacenter?

I managed to figure out this piece of code seems to do the trick but it seems rather circular (find all datacenters then look through each one, filtering by itself, looking for the vm?) Is this the way to do it or is there something better?

var dcs=VcPlugin.getAllDatacenters();

for each (dc in dcs)

{

    temp = dc.sdkConnection.searchIndex.findAllByUuid(dc, vmUuid, true);

    if (temp == null)

        System.log("not found");

    else

        System.log("found vm in dc: "+dc.name);

}

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Check the following sample code. On first line, it finds the VM object looking in the entire vCenter inventory, and then simply walks through its parent object links up to the root, until it finds a parent of datacenter type.

Note: the code searches for the VM object in the first vCenter (VcPlugin.allSdkConnections[0]). If you have multiple registered vCenter servers, you can either iterate over all of them, or somehow choose one of the vCenters eg. by asking the user which vCenter to search the VM in.

var vms = VcPlugin.allSdkConnections[0].searchIndex.findAllByUuid(null, vmUuid, true, true);

if (vms == null || vms.length < 1) { 

  throw "No vm with such UUID found"; 

if (vms.length > 1) { 

  System.log("More than one vm with such UUID found; will use the first one"); 

var vm = vms[0]; 

var vmparent = vm.parent; 

while (vmparent != null && !(vmparent instanceof VcDatacenter)) { 

  vmparent = vmparent.parent; 

if (vmparent == null) { 

  System.log("not found."); 

} else { 

  System.log("found vm in dc: " + vmparent.name); 

}

View solution in original post

2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Check the following sample code. On first line, it finds the VM object looking in the entire vCenter inventory, and then simply walks through its parent object links up to the root, until it finds a parent of datacenter type.

Note: the code searches for the VM object in the first vCenter (VcPlugin.allSdkConnections[0]). If you have multiple registered vCenter servers, you can either iterate over all of them, or somehow choose one of the vCenters eg. by asking the user which vCenter to search the VM in.

var vms = VcPlugin.allSdkConnections[0].searchIndex.findAllByUuid(null, vmUuid, true, true);

if (vms == null || vms.length < 1) { 

  throw "No vm with such UUID found"; 

if (vms.length > 1) { 

  System.log("More than one vm with such UUID found; will use the first one"); 

var vm = vms[0]; 

var vmparent = vm.parent; 

while (vmparent != null && !(vmparent instanceof VcDatacenter)) { 

  vmparent = vmparent.parent; 

if (vmparent == null) { 

  System.log("not found."); 

} else { 

  System.log("found vm in dc: " + vmparent.name); 

}

FreddyFredFred
Hot Shot
Hot Shot
Jump to solution

Thanks Ilian. I replaced my code with yours and it's doing the job.

Reply
0 Kudos