sureshanaparti commented on code in PR #13897:
URL: https://github.com/apache/cloudstack/pull/13897#discussion_r3977421024


##########
plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/listener/OntapHostListener.java:
##########
@@ -139,62 +160,123 @@ public boolean hostConnect(long hostId, long poolId)  {
                 poolVO.setCapacityBytes(poolInfo.getCapacityBytes());
                 poolVO.setUsedBytes(poolInfo.getCapacityBytes() - 
poolInfo.getAvailableBytes());
                 _storagePoolDao.update(poolVO.getId(), poolVO);
-                logger.info("Updated storage pool capacity: {} GB, used: {} 
GB", poolInfo.getCapacityBytes() / (1024 * 1024 * 1024), 
(poolInfo.getCapacityBytes() - poolInfo.getAvailableBytes()) / (1024 * 1024 * 
1024));
+                logger.info("hostConnect: Updated storage pool capacity: {} 
GB, used: {} GB", poolInfo.getCapacityBytes() / (1024 * 1024 * 1024), 
(poolInfo.getCapacityBytes() - poolInfo.getAvailableBytes()) / (1024 * 1024 * 
1024));
             }
 
         } catch (Exception e) {
-            logger.error("Exception while connecting host {} to storage pool 
{}", host.getName(), pool.getName(), e);
+            logger.error("hostConnect: Exception while connecting host {} to 
storage pool {}", host.getName(), pool.getName(), e);
             // CRITICAL: Don't throw exception - it crashes the agent and 
causes restart loops
             // Return false to indicate failure without crashing
             return false;
         }
         return true;
     }
 
-    @Override
-    public boolean hostDisconnected(long hostId, long poolId) {
-        logger.info("Disconnect from host " + hostId + " from pool " + poolId);
+    private void updateNfsExportPolicyForConnectedHostIfNeeded(long poolId, 
long hostId, Host host, Map<String, String> detailsMap) {
+        if 
(!ProtocolType.NFS3.name().equalsIgnoreCase(detailsMap.get(OntapStorageConstants.PROTOCOL)))
 {
+            return;
+        }
 
-        Host hostToremove = _hostDao.findById(hostId);
-        if (hostToremove == null) {
-            logger.error("Failed to add host by HostListener as host was not 
found with id : {}", hostId);
-            return false;
+        if (!isNfs3EnabledOnHost(host)) {
+            throw new CloudRuntimeException("NFS protocol is not enabled on 
host with id: " + hostId);
         }
 
-        StoragePool pool = _storagePoolDao.findById(poolId);
-        if (pool == null) {
-            logger.error("Failed to disconnect host - storage pool not found 
with id: {}", poolId);
+        AccessGroup accessGroup = new AccessGroup();
+        accessGroup.setStoragePoolId(poolId);
+        accessGroup.setHostsToConnect(List.of((HostVO) host));
+
+        StorageStrategy strategy = 
OntapStorageUtils.getStrategyByStoragePoolDetails(detailsMap);
+        strategy.updateAccessGroup(accessGroup);
+        logger.info("hostConnect: 
updateNfsExportPolicyForConnectedHostIfNeeded: Updated NFS export policy rules 
for host {} on storage pool {}", host.getName(), poolId);
+    }
+
+    private boolean isNfs3EnabledOnHost(Host host) {
+        if (host == null) {
             return false;
         }
-        logger.info("Disconnecting host {} from ONTAP storage pool {}", 
hostToremove.getName(), pool.getName());
 
-        try {
-            DeleteStoragePoolCommand cmd = new DeleteStoragePoolCommand(pool);
-            Answer answer = _agentMgr.easySend(hostId, cmd);
-            if (answer != null && answer.getResult()) {
-                logger.info("Successfully disconnected host {} from ONTAP 
storage pool {}", hostToremove.getName(), pool.getName());
-                return true;
-            } else {
-                String errMsg = (answer != null) ? answer.getDetails() : 
"Unknown error";
-                logger.warn("Failed to disconnect host {} from storage pool 
{}. Error: {}", hostToremove.getName(), pool.getName(), errMsg);
-                return false;
-            }
-        } catch (Exception e) {
-            logger.error("Exception while disconnecting host {} from storage 
pool {}", hostToremove.getName(), pool.getName(), e);
+        String storageIp = host.getStorageIpAddress() != null ? 
host.getStorageIpAddress().trim() : "";
+        if (storageIp.isEmpty() && 
StringUtils.isBlank(host.getPrivateIpAddress())) {
+            logger.warn("isNfs3EnabledOnHost: Host {} is not eligible for NFS3 
protocol: both storage IP and private IP are empty",
+                    host.getId());
             return false;
         }
+
+        return true;
     }
 
     @Override
-    public boolean hostAboutToBeRemoved(long hostId) {
+    public boolean hostDisconnected(long hostId, long poolId) {
+        logger.info("hostDisconnected: Disconnecting host {} from pool {}", 
hostId, poolId);
+        // Note: This is not currently being called for NetApp ONTAP storage 
plugin.
         return false;
     }
 
+    @Override
+    public boolean hostAboutToBeRemoved(long hostId) {
+        logger.info("hostAboutToBeRemoved: Host {} is about to be removed", 
hostId);
+
+        Host host = _hostDao.findById(hostId);
+        if (host == null) {
+            logger.warn("hostAboutToBeRemoved: Host not found with id: {}, 
considering it as no-op", hostId);
+            return true;
+        }
+
+        List<StoragePoolHostVO> poolHostRefs = 
storagePoolHostDao.listByHostId(hostId);
+        if (poolHostRefs == null || poolHostRefs.isEmpty()) {
+            logger.debug("hostAboutToBeRemoved: No storage pool associations 
found for host {}", hostId);
+            return true;
+        }
+
+        for (StoragePoolHostVO ref : poolHostRefs) {
+            StoragePoolVO pool = _storagePoolDao.findById(ref.getPoolId());
+            if (pool != null) {
+                removeHostFromOntapPoolIfNeeded(pool, host);
+            }
+        }
+
+        logger.info("hostAboutToBeRemoved: Cleaned up ONTAP export policies 
for host {} about to be removed", hostId);
+        return true;
+    }
+
     @Override
     public boolean hostRemoved(long hostId, long clusterId) {
         return false;
     }
 
+    private void removeHostFromOntapPoolIfNeeded(StoragePoolVO pool, Host 
host) {
+        try {
+            Map<String, String> detailsMap = 
_storagePoolDetailsDao.listDetailsKeyPairs(pool.getId());
+            if (detailsMap == null || detailsMap.isEmpty()) {

Review Comment:
   ```suggestion
               if (MapUtils.isEmpty(detailsMap)) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to