abh1sar commented on code in PR #8875:
URL: https://github.com/apache/cloudstack/pull/8875#discussion_r1557452273


##########
server/src/main/java/com/cloud/storage/StorageManagerImpl.java:
##########
@@ -1148,6 +1151,95 @@ public PrimaryDataStoreInfo 
updateStoragePool(UpdateStoragePoolCmd cmd) throws I
         return (PrimaryDataStoreInfo)_dataStoreMgr.getDataStore(pool.getId(), 
DataStoreRole.Primary);
     }
 
+    @Override
+    public boolean changeStoragePoolScope(ChangeStoragePoolScopeCmd cmd) 
throws IllegalArgumentException, InvalidParameterValueException, 
PermissionDeniedException {
+        Long id = cmd.getId();
+
+        Long accountId = cmd.getEntityOwnerId();
+        if (!_accountMgr.isRootAdmin(accountId)) {
+            throw new PermissionDeniedException("Only root admin can perform 
this operation");
+        }
+
+        ScopeType newScope = EnumUtils.getEnumIgnoreCase(ScopeType.class, 
cmd.getScope());
+        if (newScope != ScopeType.ZONE && newScope != ScopeType.CLUSTER) {
+            throw new InvalidParameterValueException("Invalid scope " + 
newScope.toString() + "for Primary storage");
+        }
+
+        StoragePoolVO primaryStorage = _storagePoolDao.findById(id);
+        if (primaryStorage == null) {
+            throw new IllegalArgumentException("Unable to find storage pool 
with ID: " + id);
+        }
+
+        if (!primaryStorage.getStatus().equals(StoragePoolStatus.Disabled)) {
+            throw new InvalidParameterValueException("Scope of the Primary 
storage with id "
+                    + primaryStorage.getUuid() +
+                    " cannot be changed, as it is not in the Disabled state");
+        }
+
+        ScopeType currentScope = primaryStorage.getScope();
+        if (currentScope.equals(newScope)) {
+            throw new InvalidParameterValueException("New scope must be 
different than the current scope");
+        }
+
+        HypervisorType hypervisorType;
+        if (currentScope.equals(ScopeType.CLUSTER)) {
+            /*
+             * For cluster wide primary storage the hypervisor type might not 
be set.
+             * So, get it from the clusterVO.
+             */
+            Long clusterId = primaryStorage.getClusterId();
+            ClusterVO clusterVO = _clusterDao.findById(clusterId);
+            hypervisorType = clusterVO.getHypervisorType();
+        } else {
+            hypervisorType = primaryStorage.getHypervisor();
+        }
+        if (!zoneWidePoolSupportedHypervisorTypes.contains(hypervisorType)) {
+            throw new InvalidParameterValueException("Primary storage scope 
change is not supported for hypervisor type " + hypervisorType);
+        }
+
+        String providerName = primaryStorage.getStorageProviderName();
+        DataStoreProvider storeProvider = 
_dataStoreProviderMgr.getDataStoreProvider(providerName);
+        PrimaryDataStoreLifeCycle lifeCycle = (PrimaryDataStoreLifeCycle) 
storeProvider.getDataStoreLifeCycle();
+        DataStore primaryStore = _dataStoreMgr.getPrimaryDataStore(id);
+
+        Long zoneId = primaryStorage.getDataCenterId();
+        DataCenterVO zone = _dcDao.findById(zoneId);
+        if (zone == null) {
+            throw new InvalidParameterValueException("Unable to find zone by 
id " + zoneId);
+        }
+        if (Grouping.AllocationState.Disabled == zone.getAllocationState()) {
+            throw new PermissionDeniedException("Cannot perform this 
operation, Zone is currently disabled: " + zoneId);
+        }
+
+        if (newScope.equals(ScopeType.ZONE)) {
+            ClusterScope clusterScope = new 
ClusterScope(primaryStorage.getClusterId(), null, zoneId);
+            lifeCycle.changeStoragePoolScopeToZone(primaryStore, clusterScope, 
hypervisorType);
+
+        } else {
+            Long clusterId = cmd.getClusterId();
+            if (clusterId == null) {
+                throw new IllegalArgumentException("Unable to find cluster 
with ID: " + clusterId);
+            }
+            ClusterVO clusterVO = _clusterDao.findById(clusterId);
+            if (clusterVO == null) {
+                throw new InvalidParameterValueException("Unable to find 
cluster by id " + clusterId);
+            }
+            if (Grouping.AllocationState.Disabled == 
clusterVO.getAllocationState()) {
+                throw new PermissionDeniedException("Cannot perform this 
operation, Cluster is currently disabled: " + zoneId);
+            }
+            List<VirtualMachine.State> states = Arrays.asList(State.Starting, 
State.Running, State.Stopping, State.Migrating, State.Restoring);
+            List<VolumeVO> volumesInOtherClusters = 
volumeDao.listByPoolIdVMStatesNotInCluster(clusterId, states, id);
+            if (volumesInOtherClusters.size() > 0) {
+                throw new CloudRuntimeException("Cannot change scope of the 
pool " + primaryStorage.getName() + " to cluster " + clusterVO.getName() + " as 
there are associated volumes present for other clusters");
+            }
+
+            ClusterScope clusterScope = new ClusterScope(clusterId, 
clusterVO.getPodId(), zoneId);
+            lifeCycle.changeStoragePoolScopeToCluster(primaryStore, 
clusterScope, hypervisorType);
+        }
+
+        return true;
+    }

Review Comment:
   Good idea. Will do this in next commit.



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