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


##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/ChangeStoragePoolScopeCmd.java:
##########
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.cloudstack.api.command.admin.storage;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.ClusterResponse;
+import org.apache.cloudstack.api.response.StoragePoolResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.context.CallContext;
+
+import com.cloud.event.EventTypes;
+import com.cloud.storage.StorageService;
+
+@APICommand(name = "changeStoragePoolScope", description = "Changes the scope 
of a storage pool.", responseObject = SuccessResponse.class,
+        requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
+public class ChangeStoragePoolScopeCmd extends BaseAsyncCmd {
+
+    @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = 
StoragePoolResponse.class, required = true, description = "the Id of the 
storage pool")
+    private Long id;
+
+    @Parameter(name = ApiConstants.SCOPE, type = CommandType.STRING, required 
= true, description = "the scope of the storage: cluster or zone")
+    private String scope;
+
+    @Parameter(name = ApiConstants.CLUSTER_ID, type = CommandType.UUID, 
entityType = ClusterResponse.class, description = "the CLuster ID to use for 
the storage pool's scope")
+    private Long clusterId;
+
+    @Inject
+    public StorageService _storageService;

Review Comment:
   this has been injected in `BaseCmd`, it is not needed here



##########
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)) 
{

Review Comment:
   can define a  List or Set like `supportedStoragePoolTypes`



##########
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()) {

Review Comment:
   this duplicates
   ```
           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");
           }
   ```



##########
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.");

Review Comment:
   typo in the debug message, not `createPool`



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/ChangeStoragePoolScopeCmd.java:
##########
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.cloudstack.api.command.admin.storage;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.ClusterResponse;
+import org.apache.cloudstack.api.response.StoragePoolResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.context.CallContext;
+
+import com.cloud.event.EventTypes;
+import com.cloud.storage.StorageService;
+
+@APICommand(name = "changeStoragePoolScope", description = "Changes the scope 
of a storage pool.", responseObject = SuccessResponse.class,
+        requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
+public class ChangeStoragePoolScopeCmd extends BaseAsyncCmd {
+
+    @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = 
StoragePoolResponse.class, required = true, description = "the Id of the 
storage pool")
+    private Long id;
+
+    @Parameter(name = ApiConstants.SCOPE, type = CommandType.STRING, required 
= true, description = "the scope of the storage: cluster or zone")
+    private String scope;
+
+    @Parameter(name = ApiConstants.CLUSTER_ID, type = CommandType.UUID, 
entityType = ClusterResponse.class, description = "the CLuster ID to use for 
the storage pool's scope")

Review Comment:
   `CLuster` -> `Cluster`
   maybe better to mention `This is required if the scope is cluster"



##########
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()) {

Review Comment:
   use `equals` instead of `==` ?



##########
ui/src/config/section/infra/primaryStorages.js:
##########
@@ -133,12 +133,27 @@ export default {
       dataView: true,
       show: (record) => { return ['Maintenance', 'PrepareForMaintenance', 
'ErrorInMaintenance'].includes(record.state) }
     },
+    {
+      api: 'changeStoragePoolScope',
+      icon: 'arrow-right-outlined',
+      label: 'label.action.change.primary.storage.scope',
+      dataView: true,
+      popup: true,
+      show: (record) => {
+        return (
+          record.state === 'Disabled' &&
+          (record.hypervisor === 'Simulator' || record.hypervisor === 'KVM' || 
record.hypervisor === 'VMware') &&
+          (record.type === 'NetworkFilesystem' || record.type === 'RBD') &&
+          record.provider === 'DefaultPrimary'
+        )
+      },
+      component: shallowRef(defineAsyncComponent(() => 
import('@/views/infra/changeStoragePoolScope.vue')))
+    },
     {
       api: 'deleteStoragePool',
       icon: 'delete-outlined',
       label: 'label.action.delete.primary.storage',
       dataView: true,
-      args: ['forced'],

Review Comment:
   is there a reason to remove this line ?



##########
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());

Review Comment:
   can `scopeType` be null ?



##########
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) {

Review Comment:
   use `equals` instead of `==` ?



##########
plugins/storage/volume/adaptive/src/main/java/org/apache/cloudstack/storage/datastore/lifecycle/AdaptiveDataStoreLifeCycleImpl.java:
##########
@@ -404,4 +404,14 @@ public void disableStoragePool(DataStore store) {
         s_logger.info("Disabling storage pool [" + store.getName() + "]");
         _dataStoreHelper.disable(store);
     }
+
+    @Override
+    public boolean changeStoragePoolScopeToZone(DataStore store, ClusterScope 
clusterScope, HypervisorType hypervisorType) {

Review Comment:
   the same code has been used in many classes.
   is it better to inherit from a new class like 
`AbstractDataStoreLifeCycleImpl.java` ?



##########
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();
+            ClusterVO clusterVO = _clusterDao.findById(clusterId);

Review Comment:
   can `clusterVO` be null or `Disabled` ?



##########
engine/storage/src/main/java/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java:
##########
@@ -265,4 +268,45 @@ public boolean deletePrimaryDataStore(DataStore store) {
         return true;
     }
 
-}
+    public void switchToZone(DataStore store) {
+        StoragePoolVO pool = dataStoreDao.findById(store.getId());
+        CapacityVO capacity = _capacityDao.findByHostIdType(store.getId(), 
Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED);
+        Transaction.execute(new TransactionCallbackNoReturn() {
+            public void doInTransactionWithoutResult(TransactionStatus status) 
{
+                pool.setScope(ScopeType.ZONE);
+                pool.setPodId(null);
+                pool.setClusterId(null);
+                dataStoreDao.update(pool.getId(), pool);

Review Comment:
   `pool.setHypervisor(xxx)` ?
   this is important for zone-wide pools



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/storage/ChangeStoragePoolScopeCmd.java:
##########
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.cloudstack.api.command.admin.storage;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.ClusterResponse;
+import org.apache.cloudstack.api.response.StoragePoolResponse;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.context.CallContext;
+
+import com.cloud.event.EventTypes;
+import com.cloud.storage.StorageService;
+
+@APICommand(name = "changeStoragePoolScope", description = "Changes the scope 
of a storage pool.", responseObject = SuccessResponse.class,
+        requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)

Review Comment:
   add `since` ?



##########
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()) {

Review Comment:
   use `equals` instead of `==` ?



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