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


##########
server/src/main/java/org/apache/cloudstack/storage/volume/VolumeImportUnmanageManagerImpl.java:
##########
@@ -0,0 +1,442 @@
+// 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.storage.volume;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.GetVolumesOnStorageAnswer;
+import com.cloud.agent.api.GetVolumesOnStorageCommand;
+import com.cloud.agent.api.to.StorageFilerTO;
+import com.cloud.configuration.ConfigurationManager;
+import com.cloud.configuration.Resource;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.event.EventTypes;
+import com.cloud.event.UsageEventUtils;
+import com.cloud.exception.PermissionDeniedException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.hypervisor.Hypervisor;
+import com.cloud.offering.DiskOffering;
+import com.cloud.storage.DiskOfferingVO;
+import com.cloud.storage.ScopeType;
+import com.cloud.storage.Storage;
+import com.cloud.storage.StoragePoolHostVO;
+import com.cloud.storage.Volume;
+import com.cloud.storage.VolumeVO;
+import com.cloud.storage.dao.DiskOfferingDao;
+import com.cloud.storage.dao.StoragePoolHostDao;
+import com.cloud.storage.dao.VMTemplatePoolDao;
+import com.cloud.storage.dao.VolumeDao;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.ResourceLimitService;
+import com.cloud.utils.Pair;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.DiskProfile;
+
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.ResponseGenerator;
+import org.apache.cloudstack.api.ResponseObject;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.command.admin.volume.ImportVolumeCmd;
+import org.apache.cloudstack.api.command.admin.volume.ListVolumesForImportCmd;
+import org.apache.cloudstack.api.command.admin.volume.UnmanageVolumeCmd;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.VolumeForImportResponse;
+import org.apache.cloudstack.api.response.VolumeResponse;
+import org.apache.cloudstack.context.CallContext;
+import 
org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
+import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
+import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import javax.inject.Inject;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+public class VolumeImportUnmanageManagerImpl implements 
VolumeImportUnmanageService {
+    protected Logger logger = 
LogManager.getLogger(VolumeImportUnmanageManagerImpl.class);
+
+    private static final List<Hypervisor.HypervisorType> 
volumeImportUnmanageSupportedHypervisors =
+            Arrays.asList(Hypervisor.HypervisorType.KVM);
+
+    @Inject
+    private AccountManager accountMgr;
+    @Inject
+    private AgentManager agentManager;
+    @Inject
+    private HostDao hostDao;
+    @Inject
+    private DiskOfferingDao diskOfferingDao;
+    @Inject
+    private ResourceLimitService resourceLimitService;
+    @Inject
+    private ResponseGenerator responseGenerator;
+    @Inject
+    private VolumeDao volumeDao;
+    @Inject
+    private PrimaryDataStoreDao primaryDataStoreDao;
+    @Inject
+    private StoragePoolHostDao storagePoolHostDao;
+    @Inject
+    private ConfigurationManager configMgr;
+    @Inject
+    private DataCenterDao dcDao;
+    @Inject
+    private VolumeOrchestrationService volumeManager;
+    @Inject
+    private VMTemplatePoolDao templatePoolDao;
+
+    static final String DEFAULT_DISK_OFFERING_NAME = "Default Custom Offering 
for Volume Import";
+    static final String DEFAULT_DISK_OFFERING_UNIQUE_NAME = "Volume-Import";
+    static final String DISK_OFFERING_NAME_SUFFIX_LOCAL = " - Local Storage";
+    static final String DISK_OFFERING_UNIQUE_NAME_SUFFIX_LOCAL = "-Local";
+
+    private void logFailureAndThrowException(String msg) {
+        logger.error(msg);
+        throw new CloudRuntimeException(msg);
+    }
+
+    @Override
+    public List<Class<?>> getCommands() {
+        final List<Class<?>> cmdList = new ArrayList<>();
+        cmdList.add(ListVolumesForImportCmd.class);
+        cmdList.add(ImportVolumeCmd.class);
+        cmdList.add(UnmanageVolumeCmd.class);
+        return cmdList;
+    }
+
+    @Override
+    public ListResponse<VolumeForImportResponse> 
listVolumesForImport(ListVolumesForImportCmd cmd) {
+        Long poolId = cmd.getStorageId();
+        String path = cmd.getPath();
+        String keyword = cmd.getKeyword();
+        if (StringUtils.isNotBlank(keyword)) {
+            keyword = keyword.trim();
+        }
+
+        StoragePoolVO pool = checkIfPoolAvailable(poolId);
+        List<VolumeOnStorageTO> volumes = listVolumesForImportInternal(poolId, 
path, keyword);
+
+        List<VolumeForImportResponse> responses = new ArrayList<>();
+        for (VolumeOnStorageTO volume : volumes) {
+            if (checkIfVolumeManaged(pool, volume.getPath()) || 
checkIfVolumeForTemplate(pool, volume.getPath())) {
+                continue;
+            }
+            responses.add(createVolumeForImportResponse(volume, pool));
+        }
+        ListResponse<VolumeForImportResponse> listResponses = new 
ListResponse<>();
+        listResponses.setResponses(responses, responses.size());
+        return listResponses;
+    }
+
+    @Override
+    public VolumeResponse importVolume(ImportVolumeCmd cmd) {
+        // 1. verify owner
+        final Account caller = CallContext.current().getCallingAccount();
+        if (caller.getType() != Account.Type.ADMIN) {
+            throw new PermissionDeniedException(String.format("Cannot import 
VM as the caller account [%s] is not ROOT Admin.", caller.getUuid()));
+        }
+        Account owner = accountMgr.finalizeOwner(caller, cmd.getAccountName(), 
cmd.getDomainId(), cmd.getProjectId());
+        if (owner == null) {
+            logFailureAndThrowException("Cannot import volume due to unknown 
owner");
+        }
+
+        // 2. check if pool exists and not in maintenance
+        Long poolId = cmd.getStorageId();
+        StoragePoolVO pool = checkIfPoolAvailable(poolId);
+
+        // 3. check if the volume already exists in cloudstack by path
+        String volumePath = cmd.getPath();
+        if (StringUtils.isBlank(volumePath)) {
+            logFailureAndThrowException("Volume path is null or blank: " + 
volumePath);
+        }
+        if (checkIfVolumeManaged(pool, volumePath)){
+            logFailureAndThrowException("Volume is already managed by 
CloudStack: " + volumePath);
+        }
+        if (checkIfVolumeForTemplate(pool, volumePath)) {
+            logFailureAndThrowException("Volume is a base image of a template: 
" + volumePath);
+        }
+
+        // 4. send a command to hypervisor to check
+        List<VolumeOnStorageTO> volumes = listVolumesForImportInternal(poolId, 
volumePath, null);
+        if (CollectionUtils.isEmpty(volumes)) {
+            logFailureAndThrowException("Cannot find volume on storage pool: " 
+ volumePath);
+        }
+
+        VolumeOnStorageTO volume = volumes.get(0);
+
+        // check if volume is locked
+        checkIfVolumeIsLocked(volume);
+        checkIfVolumeIsEncrypted(volume);
+
+        // 5. check resource limitation
+        checkResourceLimitForImportVolume(owner, volume);
+
+        // 6. get disk offering
+        DiskOfferingVO diskOffering = getOrCreateDiskOffering(owner, 
cmd.getDiskOfferingId(), pool.getDataCenterId(), pool.isLocal());
+
+        // 7. create records
+        String volumeName = StringUtils.isNotBlank(cmd.getName()) ? 
cmd.getName().trim() : volumePath;
+        VolumeVO volumeVO = importVolumeInternal(volume, diskOffering, owner, 
pool, volumeName);
+
+        // 8. Update resource count
+        updateResourceLimitForVolumeImport(volumeVO);
+
+        // 9. Publish event
+        publicUsageEventForVolumeImportAndUnmanage(volumeVO, true);
+
+        return 
responseGenerator.createVolumeResponse(ResponseObject.ResponseView.Full, 
volumeVO);
+    }
+
+    protected List<VolumeOnStorageTO> listVolumesForImportInternal(Long 
poolId, String volumePath, String keyword) {
+        StoragePoolVO pool = checkIfPoolAvailable(poolId);
+
+        Pair<HostVO, String> hostAndLocalPath = 
findHostAndLocalPathForVolumeImport(pool);
+        HostVO host = hostAndLocalPath.first();
+        if 
(!volumeImportUnmanageSupportedHypervisors.contains(host.getHypervisorType())) {
+            logFailureAndThrowException("Import VM is not supported for 
hypervisor: " + host.getHypervisorType());
+        }
+
+        StorageFilerTO storageTO = new StorageFilerTO(pool);
+        GetVolumesOnStorageCommand command = new 
GetVolumesOnStorageCommand(storageTO, volumePath, keyword);
+        Answer answer = agentManager.easySend(host.getId(), command);
+        if (answer == null || !(answer instanceof GetVolumesOnStorageAnswer)) {
+            logFailureAndThrowException("Cannot get volumes on storage pool 
via host " + host.getName());
+        }
+        if (!answer.getResult()) {
+            logFailureAndThrowException("Volume cannot be imported due to " + 
answer.getDetails());
+        }
+        return  ((GetVolumesOnStorageAnswer) answer).getVolumes();
+    }
+
+    @Override
+    public boolean unmanageVolume(long volumeId) {
+        // 1. check if volume can be unmanaged
+        VolumeVO volume = checkIfVolumeCanBeUnmanaged(volumeId);
+
+        // 2. check if pool available
+        StoragePoolVO pool = checkIfPoolAvailable(volume.getPoolId());
+
+        // 3. Update resource count
+        updateResourceLimitForVolumeUnmanage(volume);
+
+        // 4. publish events
+        publicUsageEventForVolumeImportAndUnmanage(volume, false);
+
+        // 5. update the state/removed of record
+        unmanageVolumeFromDatabase(volume);
+
+        return true;
+    }
+
+    protected StoragePoolVO checkIfPoolAvailable(Long poolId) {
+        StoragePoolVO pool = primaryDataStoreDao.findById(poolId);
+        if (pool == null) {
+            logFailureAndThrowException("Storage pool does not exist: ID = " + 
poolId);
+        }
+        if (pool.isInMaintenance()) {
+            logFailureAndThrowException("Storage pool is in maintenance: " + 
pool.getName());
+        }
+        return pool;
+    }
+
+    protected Pair<HostVO, String> 
findHostAndLocalPathForVolumeImport(StoragePoolVO pool) {
+        List<HostVO> hosts = new ArrayList<>();
+        if (ScopeType.HOST.equals(pool.getScope())) {
+            List<StoragePoolHostVO> storagePoolHostVOs = 
storagePoolHostDao.listByPoolId(pool.getId());
+            if (CollectionUtils.isNotEmpty(storagePoolHostVOs)) {
+                for (StoragePoolHostVO storagePoolHostVO : storagePoolHostVOs) 
{
+                    HostVO host = 
hostDao.findById(storagePoolHostVO.getHostId());
+                    if (host != null) {
+                        return new Pair<>(host, 
storagePoolHostVO.getLocalPath());
+                    }
+                }
+            }
+        } else if (ScopeType.CLUSTER.equals(pool.getScope())) {
+            hosts = hostDao.findHypervisorHostInCluster((pool.getClusterId()));
+        } else if (ScopeType.ZONE.equals(pool.getScope())) {
+            hosts = 
hostDao.listAllHostsUpByZoneAndHypervisor(pool.getDataCenterId(), 
pool.getHypervisor());
+        }

Review Comment:
   thanks for the suggestion, I will refactor the block



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