VMware Cloud Community
MC2016
Enthusiast
Enthusiast
Jump to solution

Get all cluster in specific datacenter?

Hello All,

Working with VCO and trying to get all cluster that are part of a datacenter due to my cluster names are not unique across all my vcenter i have connected to orchestrator. What would be the best approach to accomplish this?

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Here is some vRO action scripting code to enumerate all clusters in the given input datacenter (variable dc in the code)

var allClusters = new Array();

getAllClustersInFolder(dc.hostFolder);

return allClusters;

function getAllClustersInFolder(folder) {

  var children = folder.childEntity;

  for (var i in children) {

    if (children[i] instanceof VcFolder) {

      getAllClustersInFolder(children[i]);

    }

    if (children[i] instanceof VcClusterComputeResource) {

      allClusters.push(children[i]);

    }

  }

}

The code recursively enumerated all the child entities found in datacenter object's host folder, and accumulate those of cluster type into the array allClusters

View solution in original post

8 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Here is some vRO action scripting code to enumerate all clusters in the given input datacenter (variable dc in the code)

var allClusters = new Array();

getAllClustersInFolder(dc.hostFolder);

return allClusters;

function getAllClustersInFolder(folder) {

  var children = folder.childEntity;

  for (var i in children) {

    if (children[i] instanceof VcFolder) {

      getAllClustersInFolder(children[i]);

    }

    if (children[i] instanceof VcClusterComputeResource) {

      allClusters.push(children[i]);

    }

  }

}

The code recursively enumerated all the child entities found in datacenter object's host folder, and accumulate those of cluster type into the array allClusters

MC2016
Enthusiast
Enthusiast
Jump to solution

Hello,

Using the code you provided I'm passing dc as input variable into action but get the following when i run workflow.

[2018-06-27 20:38:37.022] [I] XXX-GP-VB

[2018-06-27 20:38:37.028] [E] Error in (Workflow:deployVMShell / Scriptable task (item2)#6) TypeError: Cannot read property "childEntity" from undefined

[2018-06-27 20:38:37.035] [E] Workflow execution stack:

***

item: 'deployVMShell/item2', state: 'failed', business state: 'null', exception: 'TypeError: Cannot read property "childEntity" from undefined (Workflow:deployVMShell / Scriptable task (item2)#6)'

workflow: 'deployVMShell' (b9b7a6cc-0ee9-4b30-be42-bcaa0abfb094)

Appears my dc isn't getting passed through to action?

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Here is a package with a workflow and an action that works... maybe you can compare it to what you put together to see where you aren't making the connection.

iiliev
VMware Employee
VMware Employee
Jump to solution

Thanks, Paul,

MC2016​ - what is this XXX-GP-VB value in the log? Are you by any chance passing the name of your datacenter to dc input variable? If so, this would explain why you are getting this error, as the expression dc.hostFolder will be undefined if dc is a string.

For code to work, you need to pass a valid datacenter object to dc variable, not a string but an object instance of type VC:Datacenter (VcDatacenter)

MC2016
Enthusiast
Enthusiast
Jump to solution

Yes,

XXX-GP-VB is name of my datacenter in VSphere. Your reply explain why i'm getting error.

Thanks Paul.

Goal is to determine cluster to deploy too based on logic of workflow instead of asking user to select a cluster.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

OK, if you have datacenter name instead of datacenter instance, you have to add some scripting code to lookup the datacenter instance having such name, and once you find the instance, you can proceed with the code from reply #2

Here is a sample showing how to lookup datacenter instance dc given a datacenter name dcName:

var dcName = "XXX-GP-VB";

var dc = null;

var allDCs = VcPlugin.getAllDatacenters();

for each (var d in allDCs) {

  if (d.name == dcName) {

    dc = d; // found by name

    break;

  }

}

if (dc == null) {

  throw "No datacenter found with name " + dcName;

}

// proceed with the found datacenter dc

MC2016
Enthusiast
Enthusiast
Jump to solution

Alright,

So everything is working all the way through the action. How do i get the results of allClusters out of action?

my output attribute type is array/string. Does it need to be Array/VC:ClusterComputeResource like Pauls example?

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

It could be Array/string if you want to return only a list with names of the clusters, instead of a list with entire cluster instances. In this case, you need to replace the line:

allClusters.push(children[i]);

with:

allClusters.push(children[i].name);