}
@Override
public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long podId, Long clusterId, URI url, String username, String password, List<String> hostTags) throws DiscoveryException {
Map<CitrixResourceBase, Map<String, String>> resources = new HashMap<CitrixResourceBase, Map<String, String>>();
Connection conn = null;
if (!url.getScheme().equals("http")) {
String msg = "urlString is not http so we're not taking care of the discovery for this: " + url;
s_logger.debug(msg);
return null;
}
if (clusterId == null) {
String msg = "must specify cluster Id when add host";
s_logger.debug(msg);
throw new RuntimeException(msg);
}
if (podId == null) {
String msg = "must specify pod Id when add host";
s_logger.debug(msg);
throw new RuntimeException(msg);
}
ClusterVO cluster = _clusterDao.findById(clusterId);
if(cluster == null || cluster.getHypervisorType() != HypervisorType.XenServer) {
if(s_logger.isInfoEnabled())
s_logger.info("invalid cluster id or cluster is not for XenServer hypervisors");
return null;
}
try {
List<HostVO> eHosts = _resourceMgr.listAllHostsInCluster(clusterId);
if( eHosts.size() > 0 ) {
HostVO eHost = eHosts.get(0);
_hostDao.loadDetails(eHost);
}
String hostname = url.getHost();
InetAddress ia = InetAddress.getByName(hostname);
String hostIp = ia.getHostAddress();
Queue<String> pass=new LinkedList<String>();
pass.add(password);
String masterIp = _connPool.getMasterIp(hostIp, username, pass);
conn = _connPool.masterConnect(masterIp, username, pass);
if (conn == null) {
String msg = "Unable to get a connection to " + url;
s_logger.debug(msg);
throw new DiscoveryException(msg);
}
Set<Pool> pools = Pool.getAll(conn);
Pool pool = pools.iterator().next();
Pool.Record pr = pool.getRecord(conn);
String poolUuid = pr.uuid;
Map<Host, Host.Record> hosts = Host.getAllRecords(conn);
/*set cluster hypervisor type to xenserver*/
ClusterVO clu = _clusterDao.findById(clusterId);
if ( clu.getGuid()== null ) {
setClusterGuid(clu, poolUuid);
} else {
List<HostVO> clusterHosts = _resourceMgr.listAllHostsInCluster(clusterId);
if( clusterHosts != null && clusterHosts.size() > 0) {
if (!clu.getGuid().equals(poolUuid)) {
if (hosts.size() == 1) {
if (!addHostsToPool(conn, hostIp, clusterId)) {
String msg = "Unable to add host(" + hostIp + ") to cluster " + clusterId;
s_logger.warn(msg);
throw new DiscoveryException(msg);
}
} else {
String msg = "Host (" + hostIp + ") is already in pool(" + poolUuid + "), can to join pool(" + clu.getGuid() + ")";
s_logger.warn(msg);
throw new DiscoveryException(msg);
}
}
} else {
setClusterGuid(clu, poolUuid);
}
}
// can not use this conn after this point, because this host may join a pool, this conn is retired
if (conn != null) {
try{
Session.logout(conn);
} catch (Exception e ) {
}
conn.dispose();
conn = null;
}
poolUuid = clu.getGuid();
_clusterDao.update(clusterId, clu);