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