Pilu1978
Enthusiast
Enthusiast

HostList

Hi,

I need some help on the below.

I have two input parameters clusterName and hostName. I want when I type the cluster name in the cluster name field, all hosts in that clusters should be populated automatically to the hostName field like a drop down list.

Tags (1)
4 Replies
Hejahida82
VMware Employee
VMware Employee

So it sounds like you want an action with one input parameter - the clusterName, which returns an array of strings - the hostNames.

If you are only inputting the clusterName then you will first need to retrieve the cluster object within the action. There is example code for this on the VMware code site here​ you can reuse the code and just remove the return statements, replacing them with a line like cluster = clusters[0]; as you don't want to return the cluster object as the output of the action.

Then once you have a value for the cluster variable you can use its host property to retrieve the ESXi host objects which are members of the cluster. Then in a loop you can collect the names of the hosts into an array and return the array as the output.

Your final code should look something like this:

var hostNames = new Array();

var clusters = VcPlugin.getAllClusterComputeResources(null,

"xpath:matches(translate(name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), '" + clusterName.toLowerCase() + "')");

if (clusters.length == 1) {

var cluster = clusters[0];

}

var hosts =cluster.host;

for (var i=0; i< hosts.length; i++){

     hostNames.push(host[i].name;

}

return hostNames;

This code doesn't contain any error checking, it will function if the correct cluster name is entered, and will fail with an error if the wrong data is input or the connection to vCenter isn't available for example. I would recommend adding some code to handle these type of situations before you use this for anything other than testing.

If you attach the action to the hostName field and bind the clusterName input parameter to the clusterName field it should populate the hostName field with a list of names when you enter a cluster name into the box.

0 Kudos
haloOne
Contributor
Contributor

check out action "getAllHostSystemsOfCluster"  in com.vmware.library.vc.cluster

Hejahida82
VMware Employee
VMware Employee

Even better than using custom code - should have checked for an out of the box action first like haloOneSmiley Happy

iiliev
VMware Employee
VMware Employee

Both proposed solutions are not exactly equivalent - the out of the box action takes a cluster object as an input and return an array with host objects, and your custom code operates on names that are strings, so both have valid use cases.

BTW, working with names as strings has the drawback that vCenter object names are not unique; for example, you can have multiple clusters named ClusterXYZ, so if you are looking for hosts on particular cluster then there should be some code to handle this potential name ambiguity.