VMware Cloud Community
stangrig
Contributor
Contributor
Jump to solution

vCO - get all clusters in a datacenter

Do you know how I can get a list of all Clusters that are in a certain Datacenter?

Thanks in advance

Tags (3)
1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee
Jump to solution

This scriptable task code can do that...

Input: datacenter (VC:Datacenter)

Output: clusters (Array/VC:ClusterComputeResource)

Script:

var clusters = new Array;

var folder = datacenter.hostFolder;

var children = folder.childEntity;

for each (child in children){

    if (child instanceof VcClusterComputeResource){

        clusters.push(child);

        System.debug("Cluster added: "+child.name);

    }

}

If the above code is to be used in an action rather than a scriptable task, just add the following as the first line (helpful when used in Presentation settings:

if(datacenter == null) return null;

And this to the last line:

return clusters;

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 vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

5 Replies
qc4vmware
Virtuoso
Virtuoso
Jump to solution

VcPlugin.getAllClusterComputeResources() .  You can add an XPath query as a filtering mechanism or loop through the results and see if the datacenter matches what you are looking for.

Burke-
VMware Employee
VMware Employee
Jump to solution

This scriptable task code can do that...

Input: datacenter (VC:Datacenter)

Output: clusters (Array/VC:ClusterComputeResource)

Script:

var clusters = new Array;

var folder = datacenter.hostFolder;

var children = folder.childEntity;

for each (child in children){

    if (child instanceof VcClusterComputeResource){

        clusters.push(child);

        System.debug("Cluster added: "+child.name);

    }

}

If the above code is to be used in an action rather than a scriptable task, just add the following as the first line (helpful when used in Presentation settings:

if(datacenter == null) return null;

And this to the last line:

return clusters;

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 vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
stangrig
Contributor
Contributor
Jump to solution

Perfect! Just missing return

var clusters = new Array; 

var folder = datacenter.hostFolder; 

var children = folder.childEntity; 

for each (child in children){ 

    if (child instanceof VcClusterComputeResource){ 

        clusters.push(child); 

        System.debug("Cluster added: "+child.name); 

    } 

return clusters

Reply
0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Show off!

Reply
0 Kudos
Windspirit
Hot Shot
Hot Shot
Jump to solution

Hi,

late but...here is a function that recursively searches (if there are more folders in between the Cluster and the Datacenter)

function getChildren(folder){

    for each (child in folder.childEntity){

        if (child instanceof VcClusterComputeResource){

            output.push(child);            

            System.debug("Cluster added: "+child.name);

        }

        if (child instanceof VcFolder){

            System.debug("found folder "+child.name);

            getChildren(child);

        }

    }

    return output;

}  

if (datacenter){

    var output = new Array;

    return getChildren(datacenter.hostFolder);

} else {

    throw ("no Datacenter provided");

}