|
21 | 21 | import com.cloud.agent.AgentManager; |
22 | 22 | import com.cloud.agent.api.Answer; |
23 | 23 | import com.cloud.agent.api.CleanupPersistentNetworkResourceCommand; |
| 24 | +import com.cloud.agent.api.DeleteStoragePoolCommand; |
24 | 25 | import com.cloud.agent.api.ModifyStoragePoolAnswer; |
25 | 26 | import com.cloud.agent.api.ModifyStoragePoolCommand; |
26 | 27 | import com.cloud.agent.api.SetupPersistentNetworkCommand; |
|
45 | 46 | import com.cloud.storage.dao.StoragePoolHostDao; |
46 | 47 | import com.cloud.utils.exception.CloudRuntimeException; |
47 | 48 |
|
| 49 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; |
48 | 50 | import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager; |
49 | 51 | import org.apache.cloudstack.engine.subsystem.api.storage.HypervisorHostListener; |
50 | 52 | import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
@@ -207,8 +209,41 @@ private void updateStoragePoolHostVOAndDetails(StoragePool pool, long hostId, Mo |
207 | 209 |
|
208 | 210 | @Override |
209 | 211 | public boolean hostDisconnected(long hostId, long poolId) { |
210 | | - // TODO Auto-generated method stub |
211 | | - return false; |
| 212 | + HostVO host = hostDao.findById(hostId); |
| 213 | + if (host == null) { |
| 214 | + logger.error("Failed to disconnect host by HostListener as host was not found with id : " + hostId); |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + DataStore dataStore = dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary); |
| 219 | + StoragePool storagePool = (StoragePool) dataStore; |
| 220 | + DeleteStoragePoolCommand cmd = new DeleteStoragePoolCommand(storagePool); |
| 221 | + Answer answer = sendDeleteStoragePoolCommand(cmd, storagePool, host); |
| 222 | + if (!answer.getResult()) { |
| 223 | + logger.error("Failed to disconnect storage pool: " + storagePool + " and host: " + host); |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + StoragePoolHostVO storagePoolHost = storagePoolHostDao.findByPoolHost(poolId, hostId); |
| 228 | + if (storagePoolHost != null) { |
| 229 | + storagePoolHostDao.deleteStoragePoolHostDetails(hostId, poolId); |
| 230 | + } |
| 231 | + logger.info("Connection removed between storage pool: " + storagePool + " and host: " + host); |
| 232 | + return true; |
| 233 | + } |
| 234 | + |
| 235 | + private Answer sendDeleteStoragePoolCommand(DeleteStoragePoolCommand cmd, StoragePool storagePool, HostVO host) { |
| 236 | + Answer answer = agentMgr.easySend(host.getId(), cmd); |
| 237 | + if (answer == null) { |
| 238 | + throw new CloudRuntimeException(String.format("Unable to get an answer to the delete storage pool command for storage pool %s, sent to host %s", storagePool, host)); |
| 239 | + } |
| 240 | + |
| 241 | + if (!answer.getResult()) { |
| 242 | + String msg = "Unable to detach storage pool " + storagePool + " from the host " + host; |
| 243 | + alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, storagePool.getDataCenterId(), storagePool.getPodId(), msg, msg); |
| 244 | + } |
| 245 | + |
| 246 | + return answer; |
212 | 247 | } |
213 | 248 |
|
214 | 249 | @Override |
|
0 commit comments