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


##########
plugins/storage/volume/default/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/CloudStackPrimaryDataStoreLifeCycleImpl.java:
##########
@@ -543,4 +544,45 @@ public void enableStoragePool(DataStore dataStore) {
     public void disableStoragePool(DataStore dataStore) {
         dataStoreHelper.disable(dataStore);
     }
-}
+
+    public boolean changeStoragePoolScopeToZone(DataStore store, ClusterScope 
clusterScope, HypervisorType hypervisorType) {
+        List<HostVO> hosts = 
_resourceMgr.listAllHostsInOneZoneNotInClusterByHypervisor(hypervisorType, 
clusterScope.getZoneId(), clusterScope.getScopeId());
+        s_logger.debug("In createPool. Attaching the pool to each of the 
hosts.");
+        List<HostVO> poolHosts = new ArrayList<HostVO>();
+        for (HostVO host : hosts) {
+            try {
+                storageMgr.connectHostToSharedPool(host.getId(), 
store.getId());
+            } catch (Exception e) {
+                s_logger.warn("Unable to establish a connection between " + 
host + " and " + store, e);

Review Comment:
   If you fail to establish the connection between a host and the storage, 
shouldn't the operation fail?



##########
plugins/storage/volume/default/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/CloudStackPrimaryDataStoreLifeCycleImpl.java:
##########
@@ -543,4 +544,45 @@ public void enableStoragePool(DataStore dataStore) {
     public void disableStoragePool(DataStore dataStore) {
         dataStoreHelper.disable(dataStore);
     }
-}
+
+    public boolean changeStoragePoolScopeToZone(DataStore store, ClusterScope 
clusterScope, HypervisorType hypervisorType) {
+        List<HostVO> hosts = 
_resourceMgr.listAllHostsInOneZoneNotInClusterByHypervisor(hypervisorType, 
clusterScope.getZoneId(), clusterScope.getScopeId());
+        s_logger.debug("In createPool. Attaching the pool to each of the 
hosts.");
+        List<HostVO> poolHosts = new ArrayList<HostVO>();
+        for (HostVO host : hosts) {
+            try {
+                storageMgr.connectHostToSharedPool(host.getId(), 
store.getId());
+            } catch (Exception e) {
+                s_logger.warn("Unable to establish a connection between " + 
host + " and " + store, e);
+            }
+        }
+        dataStoreHelper.switchToZone(store);
+        return true;
+    }
+
+    public boolean changeStoragePoolScopeToCluster(DataStore store, 
ClusterScope clusterScope, HypervisorType hypervisorType) {
+        Pair<List<StoragePoolHostVO>, Integer> hostPoolRecords = 
_storagePoolHostDao.listByPoolIdNotInCluster(clusterScope.getScopeId(), 
store.getId());
+        HypervisorType hType = null;
+        if (hostPoolRecords.second() > 0) {
+            hType = 
getHypervisorType(hostPoolRecords.first().get(0).getHostId());
+        }
+
+        StoragePool pool = (StoragePool) store;
+        for (StoragePoolHostVO host : hostPoolRecords.first()) {
+            DeleteStoragePoolCommand deleteCmd = new 
DeleteStoragePoolCommand(pool);
+            final Answer answer = agentMgr.easySend(host.getHostId(), 
deleteCmd);
+
+            if (answer != null && answer.getResult()) {
+                if (HypervisorType.KVM != hType) {
+                    break;
+                }
+            } else {
+                if (answer != null) {
+                    s_logger.debug("Failed to delete storage pool: " + 
answer.getResult());

Review Comment:
   same logic here, if you fail to delete the storage pool, the operation 
should fail.



##########
server/src/main/java/com/cloud/storage/StorageManagerImpl.java:
##########
@@ -1148,6 +1150,91 @@ 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 scopeType = 
ScopeType.validateAndGetScopeType(cmd.getScope());
+        if (scopeType != ScopeType.ZONE && scopeType != ScopeType.CLUSTER) {
+            throw new InvalidParameterValueException("Invalid scope " + 
scopeType.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.getStorageProviderName().equals(DataStoreProvider.DEFAULT_PRIMARY))
 {
+            throw new InvalidParameterValueException("Primary storage scope 
change is only supported with "
+                    + DataStoreProvider.DEFAULT_PRIMARY.toString() + " data 
store provider");
+        }
+
+        if 
(!primaryStorage.getPoolType().equals(Storage.StoragePoolType.NetworkFilesystem)
 &&
+            !primaryStorage.getPoolType().equals(Storage.StoragePoolType.RBD)) 
{
+            throw new InvalidParameterValueException("Primary storage scope 
change is not supported for protocol "
+                    + primaryStorage.getPoolType().toString());
+        }
+
+        HypervisorType hypervisorType = primaryStorage.getHypervisor();
+        Set<HypervisorType> supportedHypervisorTypes = 
Sets.newHashSet(HypervisorType.KVM, HypervisorType.VMware, 
HypervisorType.Simulator);
+        if (!supportedHypervisorTypes.contains(hypervisorType)) {
+            throw new InvalidParameterValueException("Primary storage scope 
change is not supported for hypervisor type " + hypervisorType);
+        }
+
+        if (StoragePoolStatus.Disabled != primaryStorage.getStatus()) {
+            throw new InvalidParameterValueException("Primary storage should 
be disabled for this operation");
+        }
+
+        if (scopeType == primaryStorage.getScope()) {
+            throw new InvalidParameterValueException("Invalid scope change");
+        }
+
+        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");
+        }
+
+        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 (scopeType == ScopeType.ZONE) {
+            ClusterScope clusterScope = new 
ClusterScope(primaryStorage.getClusterId(), null, zoneId);
+            lifeCycle.changeStoragePoolScopeToZone(primaryStore, clusterScope, 
hypervisorType);
+
+        } else {
+
+            Long clusterId = cmd.getClusterId();

Review Comment:
   As the clusterId is not required, the user might not inform it, thus we need 
to validate if it is valid.



##########
server/src/main/java/com/cloud/storage/StorageManagerImpl.java:
##########
@@ -1148,6 +1150,91 @@ 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 scopeType = 
ScopeType.validateAndGetScopeType(cmd.getScope());
+        if (scopeType != ScopeType.ZONE && scopeType != ScopeType.CLUSTER) {
+            throw new InvalidParameterValueException("Invalid scope " + 
scopeType.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.getStorageProviderName().equals(DataStoreProvider.DEFAULT_PRIMARY))
 {
+            throw new InvalidParameterValueException("Primary storage scope 
change is only supported with "
+                    + DataStoreProvider.DEFAULT_PRIMARY.toString() + " data 
store provider");
+        }
+
+        if 
(!primaryStorage.getPoolType().equals(Storage.StoragePoolType.NetworkFilesystem)
 &&
+            !primaryStorage.getPoolType().equals(Storage.StoragePoolType.RBD)) 
{
+            throw new InvalidParameterValueException("Primary storage scope 
change is not supported for protocol "
+                    + primaryStorage.getPoolType().toString());
+        }
+
+        HypervisorType hypervisorType = primaryStorage.getHypervisor();
+        Set<HypervisorType> supportedHypervisorTypes = 
Sets.newHashSet(HypervisorType.KVM, HypervisorType.VMware, 
HypervisorType.Simulator);

Review Comment:
   This set can be extracted to a class attribute.



-- 
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: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to