VMware Cloud Community
lurims
Enthusiast
Enthusiast
Jump to solution

How to get ESXi host's cluster object?

I am trying to go through few hoops to get to it and finally will get it, but wondering if anyone already know how to get the cluster name or object of a ESXi host.  I would like to avoid getting all hosts and get hold of it.  As I have the host obj already in hand I would like to see if I can get it quickly than searching in the collection of all ESXi hosts.

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

It's one-liner:

var clusterObj = hostObj.parent;

I think you cannot have other objects in inventory hierarchy between ESXi host and its parent cluster, but if you want to be safe, you can iterate over the parent hierarchy until you find a parent of type VcClusterComputeResource (which is the cluster object you are looking for). Something like the following should work:

var clusterObj = hostObj.parent;

while (clusterObj != null) {

  if (clusterObj instanceof VcClusterComputeResource) break;

  clusterObj = clusterObj.parent;

}

if (clusterObj == null) throw "Cluster not found";

View solution in original post

0 Kudos
3 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

It's one-liner:

var clusterObj = hostObj.parent;

I think you cannot have other objects in inventory hierarchy between ESXi host and its parent cluster, but if you want to be safe, you can iterate over the parent hierarchy until you find a parent of type VcClusterComputeResource (which is the cluster object you are looking for). Something like the following should work:

var clusterObj = hostObj.parent;

while (clusterObj != null) {

  if (clusterObj instanceof VcClusterComputeResource) break;

  clusterObj = clusterObj.parent;

}

if (clusterObj == null) throw "Cluster not found";

0 Kudos
lurims
Enthusiast
Enthusiast
Jump to solution

Thought of parent, but not sure something like a resourcepool may come into picture in between in some envs.  That is a good idea to check the instance of, and I like that, thanks IIlian, you are super star!

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

It is possible the esx host is stand alone and not in a cluster.  Better check the parent object type to be sure if you are expecting a cluster.