@@ -253,13 +253,30 @@ private void setUnmanagedInstanceTOHostAndCluster(UnmanagedInstanceTO vm, Manage
253253 hostClusterPair = hostClusterNamesMap .get (hostMorValue );
254254 } else {
255255 HostMO hostMO = new HostMO (_context , hostMor );
256- ClusterMO clusterMO = new ClusterMO (_context , hostMO .getHyperHostCluster ());
257- hostClusterPair = new Pair <>(hostMO .getHostName (), clusterMO .getName ());
256+ String hostName = hostMO .getHostName ();
257+ String clusterName = getClusterNameFromHostIncludingStandaloneHosts (hostMO , hostName );
258+ hostClusterPair = new Pair <>(hostName , clusterName );
258259 hostClusterNamesMap .put (hostMorValue , hostClusterPair );
259260 }
260261 vm .setHostName (hostClusterPair .first ());
261262 vm .setClusterName (hostClusterPair .second ());
262263 }
263264 }
264265
266+ /**
267+ * Return the cluster name of the host on the vCenter
268+ * @return null in case the host is standalone (doesn't belong to a cluster), cluster name otherwise
269+ */
270+ private String getClusterNameFromHostIncludingStandaloneHosts (HostMO hostMO , String hostName ) {
271+ try {
272+ ClusterMO clusterMO = new ClusterMO (_context , hostMO .getHyperHostCluster ());
273+ return clusterMO .getName ();
274+ } catch (Exception e ) {
275+ String msg = String .format ("Cannot find a cluster for host %s, assuming standalone host, " +
276+ "setting its cluster name as empty" , hostName );
277+ s_logger .info (msg );
278+ return null ;
279+ }
280+ }
281+
265282}
0 commit comments