rajiv-jain-netapp commented on code in PR #13053:
URL: https://github.com/apache/cloudstack/pull/13053#discussion_r3489462443


##########
plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedSANStrategy.java:
##########
@@ -286,24 +371,229 @@ public AccessGroup getAccessGroup(Map<String, String> 
values) {
             AccessGroup accessGroup = new AccessGroup();
             accessGroup.setIgroup(igroup);
             return accessGroup;
-        } catch (Exception e) {
-            String errMsg = e.getMessage();
-            if (errMsg != null && errMsg.contains("not found")) {
-                logger.warn("getAccessGroup: Igroup '{}' not found on SVM '{}' 
({}). Returning null.", igroupName, svmName, errMsg);
+        } catch (FeignException e) {
+            if (e.status() == 404) {
+                logger.warn("getAccessGroup: Igroup '{}' not found on SVM '{}' 
(status 404). Returning null.", igroupName, svmName);
                 return null;
             }
-            logger.error("Exception occurred while fetching Igroup, Exception: 
{}", errMsg);
-            throw new CloudRuntimeException("Failed to fetch Igroup details: " 
+ errMsg);
+            logger.error("FeignException occurred while fetching Igroup, 
Status: {}, Exception: {}", e.status(), e.getMessage());
+            throw new CloudRuntimeException("Failed to fetch Igroup details: " 
+ e.getMessage());
+        } catch (Exception e) {
+            logger.error("Exception occurred while fetching Igroup, Exception: 
{}", e.getMessage());
+            throw new CloudRuntimeException("Failed to fetch Igroup details: " 
+ e.getMessage());
         }
     }
 
     public Map<String, String> enableLogicalAccess(Map<String, String> values) 
{
-        return null;
+        logger.info("enableLogicalAccess : Create LunMap");
+        logger.debug("enableLogicalAccess : Creating LunMap with values {} ", 
values);
+        Map<String, String> response = null;
+        if (values == null) {
+            logger.error("enableLogicalAccess: LunMap creation failed. Invalid 
request values: null");
+            throw new CloudRuntimeException(" Failed to create LunMap, invalid 
request");
+        }
+        String svmName = values.get(OntapStorageConstants.SVM_DOT_NAME);
+        String lunName = values.get(OntapStorageConstants.LUN_DOT_NAME);
+        String igroupName = values.get(OntapStorageConstants.IGROUP_DOT_NAME);
+        if (svmName == null || lunName == null || igroupName == null || 
svmName.isEmpty() || lunName.isEmpty() || igroupName.isEmpty()) {
+            logger.error("enableLogicalAccess: LunMap creation failed. Invalid 
request values: {}", values);
+            throw new CloudRuntimeException(" Failed to create LunMap, invalid 
request");
+        }
+        try {
+            // Get AuthHeader
+            String authHeader = 
OntapStorageUtils.generateAuthHeader(storage.getUsername(), 
storage.getPassword());
+            // Create LunMap
+            LunMap lunMapRequest = new LunMap();
+            Svm svm = new Svm();
+            svm.setName(svmName);
+            lunMapRequest.setSvm(svm);
+            //Set Lun name
+            Lun lun = new Lun();
+            lun.setName(lunName);
+            lunMapRequest.setLun(lun);
+            //Set Igroup name
+            Igroup igroup = new Igroup();
+            igroup.setName(igroupName);
+            lunMapRequest.setIgroup(igroup);
+            try {
+                sanFeignClient.createLunMap(authHeader, true, lunMapRequest);
+            } catch (Exception feignEx) {
+                String errMsg = feignEx.getMessage();
+                if (errMsg != null && errMsg.contains(("LUN already mapped to 
this group"))) {
+                    logger.warn("enableLogicalAccess: LunMap for Lun: {} and 
igroup: {} already exists.", lunName, igroupName);
+                } else {
+                    logger.error("enableLogicalAccess: Exception during Feign 
call: {}", feignEx.getMessage(), feignEx);
+                    throw feignEx;
+                }
+            }
+            // Get the LunMap details
+            OntapResponse<LunMap> lunMapResponse = null;
+            try {
+                lunMapResponse = sanFeignClient.getLunMapResponse(authHeader,
+                        Map.of(
+                                OntapStorageConstants.SVM_DOT_NAME, svmName,
+                                OntapStorageConstants.LUN_DOT_NAME, lunName,
+                                OntapStorageConstants.IGROUP_DOT_NAME, 
igroupName,
+                                OntapStorageConstants.FIELDS, 
OntapStorageConstants.LOGICAL_UNIT_NUMBER
+                        ));
+                response = Map.of(
+                        OntapStorageConstants.LOGICAL_UNIT_NUMBER, 
lunMapResponse.getRecords().get(0).getLogicalUnitNumber().toString()
+                );
+            } catch (Exception e) {
+                logger.error("enableLogicalAccess: Failed to fetch LunMap 
details for Lun: {} and igroup: {}, Exception: {}", lunName, igroupName, e);
+                throw new CloudRuntimeException("Failed to fetch LunMap 
details for Lun: " + lunName + " and igroup: " + igroupName);
+            }
+            logger.debug("enableLogicalAccess: LunMap created successfully, 
LunMap: {}", lunMapResponse.getRecords().get(0));
+            logger.info("enableLogicalAccess: LunMap created successfully.");
+        } catch (Exception e) {
+            logger.error("Exception occurred while creating LunMap", e);
+            throw new CloudRuntimeException("Failed to create LunMap: " + 
e.getMessage());
+        }
+        return response;
     }
 
-    public void disableLogicalAccess(Map<String, String> values) {}
+    public void disableLogicalAccess(Map<String, String> values) {
+        logger.info("disableLogicalAccess : Delete LunMap");
+        logger.debug("disableLogicalAccess : Deleting LunMap with values {} ", 
values);
+        if (values == null) {
+            logger.error("disableLogicalAccess: LunMap deletion failed. 
Invalid request values: null");
+            throw new CloudRuntimeException(" Failed to delete LunMap, invalid 
request");
+        }
+        String lunUUID = values.get(OntapStorageConstants.LUN_DOT_UUID);
+        String igroupUUID = values.get(OntapStorageConstants.IGROUP_DOT_UUID);
+        if (lunUUID == null || igroupUUID == null || lunUUID.isEmpty() || 
igroupUUID.isEmpty()) {
+            logger.error("disableLogicalAccess: LunMap deletion failed. 
Invalid request values: {}", values);
+            throw new CloudRuntimeException(" Failed to delete LunMap, invalid 
request");
+        }
+        try {
+            String authHeader = 
OntapStorageUtils.generateAuthHeader(storage.getUsername(), 
storage.getPassword());
+            sanFeignClient.deleteLunMap(authHeader, lunUUID, igroupUUID);
+            logger.info("disableLogicalAccess: LunMap deleted successfully.");
+        } catch (FeignException e) {
+            if (e.status() == 404) {
+                logger.warn("disableLogicalAccess: LunMap with Lun UUID: {} 
and igroup UUID: {} does not exist, skipping deletion", lunUUID, igroupUUID);
+                return;
+            }
+            logger.error("FeignException occurred while deleting LunMap, 
Status: {}, Exception: {}", e.status(), e.getMessage());
+            throw new CloudRuntimeException("Failed to delete LunMap: " + 
e.getMessage());
+        } catch (Exception e) {
+            logger.error("Exception occurred while deleting LunMap, Exception: 
{}", e.getMessage());
+            throw new CloudRuntimeException("Failed to delete LunMap: " + 
e.getMessage());
+        }
+    }
 
+    // GET-only helper: fetch LUN-map and return logical unit number if it 
exists; otherwise return null
     public Map<String, String> getLogicalAccess(Map<String, String> values) {
+        logger.info("getLogicalAccess : Fetch LunMap");
+        logger.debug("getLogicalAccess : Fetching LunMap with values {} ", 
values);
+        if (values == null) {
+            logger.error("getLogicalAccess: Invalid request values: null");
+            throw new CloudRuntimeException(" Invalid request");
+        }
+        String svmName = values.get(OntapStorageConstants.SVM_DOT_NAME);
+        String lunName = values.get(OntapStorageConstants.LUN_DOT_NAME);
+        String igroupName = values.get(OntapStorageConstants.IGROUP_DOT_NAME);
+        if (svmName == null || lunName == null || igroupName == null || 
svmName.isEmpty() || lunName.isEmpty() || igroupName.isEmpty()) {
+            logger.error("getLogicalAccess: Invalid request values: {}", 
values);
+            throw new CloudRuntimeException(" Invalid request");
+        }
+        try {
+            String authHeader = 
OntapStorageUtils.generateAuthHeader(storage.getUsername(), 
storage.getPassword());
+            OntapResponse<LunMap> lunMapResponse = 
sanFeignClient.getLunMapResponse(authHeader,
+                    Map.of(
+                            OntapStorageConstants.SVM_DOT_NAME, svmName,
+                            OntapStorageConstants.LUN_DOT_NAME, lunName,
+                            OntapStorageConstants.IGROUP_DOT_NAME, igroupName,
+                            OntapStorageConstants.FIELDS, 
OntapStorageConstants.LOGICAL_UNIT_NUMBER
+                    ));
+            if (lunMapResponse != null && lunMapResponse.getRecords() != null 
&& !lunMapResponse.getRecords().isEmpty()) {
+                String lunNumber = 
lunMapResponse.getRecords().get(0).getLogicalUnitNumber() != null ?
+                        
lunMapResponse.getRecords().get(0).getLogicalUnitNumber().toString() : null;
+                return lunNumber != null ? 
Map.of(OntapStorageConstants.LOGICAL_UNIT_NUMBER, lunNumber) : null;
+            }
+        } catch (Exception e) {
+            logger.warn("getLogicalAccess: LunMap not found for Lun: {} and 
igroup: {} ({}).", lunName, igroupName, e.getMessage());
+        }
         return null;
     }
+
+    @Override
+    public String ensureLunMapped(String svmName, String lunName, String 
accessGroupName) {
+        logger.info("ensureLunMapped: Ensuring LUN [{}] is mapped to igroup 
[{}] on SVM [{}]", lunName, accessGroupName, svmName);
+
+        // Check existing map first
+        Map<String, String> getMap = Map.of(
+                OntapStorageConstants.LUN_DOT_NAME, lunName,
+                OntapStorageConstants.SVM_DOT_NAME, svmName,
+                OntapStorageConstants.IGROUP_DOT_NAME, accessGroupName
+        );
+        Map<String, String> mapResp = getLogicalAccess(getMap);
+        if (mapResp != null && 
mapResp.containsKey(OntapStorageConstants.LOGICAL_UNIT_NUMBER)) {
+            String lunNumber = 
mapResp.get(OntapStorageConstants.LOGICAL_UNIT_NUMBER);
+            logger.info("ensureLunMapped: Existing LunMap found for LUN [{}] 
in igroup [{}] with LUN number [{}]", lunName, accessGroupName, lunNumber);
+            return lunNumber;
+        }
+
+        // Create if not exists
+        Map<String, String> enableMap = Map.of(
+                OntapStorageConstants.LUN_DOT_NAME, lunName,
+                OntapStorageConstants.SVM_DOT_NAME, svmName,
+                OntapStorageConstants.IGROUP_DOT_NAME, accessGroupName
+        );
+        Map<String, String> response = enableLogicalAccess(enableMap);

Review Comment:
   Hi @winterhazel we kept it as Map by which in future, if we need to add more 
query parameters then without changing the method signature, we can achieve 
this.



-- 
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