VMware Cloud Community
dacoulter
Contributor
Contributor

list clusters by datacenter

Is it possible to retreive a list of VC host clusters based on a VC data center name natively in vCO?  Or derive the VC data center name from a VcVlusterComputeResource object? I've looked at the VcClusterComputerResource/configuration/configurationEx and VcDatacenter properties/methods, but can't seem to find a way to accomplish this.

Here's a post that's similar to my use case, although I'm looking to do this in vCO versus PowerShell: http://communities.vmware.com/thread/231345

0 Kudos
8 Replies
tschoergez
Leadership
Leadership

Hi,

I don't know a direct way.

One workaround is to "crawl" through the object tree: Get the datacenter and the hostFolder-attribute of this. Then you can use the childEntity-Attribute of this VcFolder.

(you have to crawl deeper if you have layered folder-structure in your vCenter).

The other, maybe more elegant way is to use the VcPlugin.getAllComputeResources()-Method with the proper xpath-expression.

Problem: I have no idea, how this xpath-expression look like.

More infos about this here:

http://communities.vmware.com/thread/284052

Maybe you can also find some ideas here:

http://communities.vmware.com/thread/301381

I'm playing with the xpath right now, I'll come back if I find something helpful 🙂

Regards,

Joerg

mmarinov
VMware Employee
VMware Employee

Hi,

The method you are looking for is VcPlugin.getAllClusterComputeResources().

Unfortunately I have troubles with the xpaths expressions so any method from the VcPlugin that expects xpaths for some reason ignores them.

Theoretically speaking the correct invocation should be

var xpath = "xpath://parent.parent.name[compare(.,'your_datacenter_name') == 0]";

var clusters = VcPlugin, getAllClusterComputeResourses(null, xpath);

the reason parent.parent.name is used is

- cluster's parent is always the flder host

- the host folder's parent is always the datacenter name

I have checked the above by using the following snippet on my machine

var clusters = VcPlugin.getAllClusterComputeResources();
for (var i=0; i<clusters.length; i++) {
System.log("Clsuter: " + clusters[i].name);
System.log("Cluster name: " + clusters[i].parent.parent.name);
}

Hope this helps,

--Martin

Martin Marinov VMware Software Engineer If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points
tschoergez
Leadership
Leadership

Hi Martin,

First of all: You're right, in my post above I copy&pasted the wrong method. We're looking for a ClusterComputeResource 🙂

I cannot try it now, but I have two remarks:

1. what happens, if you have some subfolders between the datacenter and the cluster? will the parent.parent... still work?

2. Relating to Konstantin's post here http://communities.vmware.com/message/1673541#1673541:

What about the double slash // here?

Is there any documentation about the xpath yet?

Regards,

Joerg

0 Kudos
mmarinov
VMware Employee
VMware Employee

Hi Joerg,

From what I know about the VC data model:

- datacenter has four folders only, and they can be add/removed/changed - vm, host, datastore, network. Those are the folders that you see when you browse the VC inventory

- a cluster is always a child of the host folder. So I think we are on the safe site about parent.parent

as for the // I'm not aware there is a documentation yet. But I'll double check it tomorrow.

Regards,

--Martin

Martin Marinov VMware Software Engineer If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points
0 Kudos
dacoulter
Contributor
Contributor

Unfortunately the number of parents depends on how many folders you have in between your cluster and your datacenter.  Here's what I did, although not very elegant.

// ESXCluster is a VC:ClusterComputeResource

// Walk up the folder hiearchy until we find a Datacenter (only going up to 6 levels deep)

if (ESXCluster.parent.vimType != "Datacenter") {

    if (ESXCluster.parent.parent.vimType != "Datacenter") {

        if (ESXCluster.parent.parent.parent.vimType != "Datacenter") {

            if (ESXCluster.parent.parent.parent.parent.vimType != "Datacenter") {

                if (ESXCluster.parent.parent.parent.parent.parent.vimType != "Datacenter") {

                    if (ESXCluster.parent.parent.parent.parent.parent.parent.vimType != "Datacenter") {

                        throw "Unable to find DataCenter of cluster " + ESXCluster.name;

                    } else { DataCenterName = ESXCluster.parent.parent.parent.parent.parent.parent.name };

                } else { DataCenterName = ESXCluster.parent.parent.parent.parent.parent.name };

            } else { DataCenterName = ESXCluster.parent.parent.parent.parent.name };

        } else { DataCenterName = ESXCluster.parent.parent.parent.name };

    } else { DataCenterName = ESXCluster.parent.parent.name };

} else { DataCenterName = ESXCluster.parent.name };

0 Kudos
tschoergez
Leadership
Leadership

Hi!

You can use a recursion for a more elegant way to implement this.

Have a look at the System.getModule("com.vmware.library.vc.folder").getAllVirtualMachinesByFolderIncludingSubFolders()-Action for following:

if(!vmFolder){
    throw "UndefinedParameter: vmFolder mandatory input is not defined.";
}
var allVms = new Array();

getAllVmsInFolder(vmFolder);
return allVms;

function getAllVmsInFolder(folder) {
    var children = folder.childEntity;
    for (var i in children) {
        if (children[i] instanceof VcFolder) {
            getAllVmsInFolder(children[i]);
        }
        if (children[i] instanceof VcVirtualMachine) {
            allVms.push(children[i]);
        }
    }
}

It should not be too difficult to rewrite if for your requirements...

Regards,

Joerg

0 Kudos
dacoulter
Contributor
Contributor

Followup: Found an easy way to get the Datacenter name with an existing library action:

getDatacenterForVimObject

var datacenterName = System.getModule("com.vmware.library.vc.basic").getDatacenterForVimObject(esxClusterObject).name


Not sure how I missed that one before...

0 Kudos
cdecanini_
VMware Employee
VMware Employee

If you would not have missed it before you would have missed all this thread fun stuff Smiley Wink

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