This is an automated email from the ASF dual-hosted git repository.

harikrishna pushed a commit to branch CheckVolumeAPI
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 4898bbe30b6674639136200dca748987068b49bf
Author: Harikrishna Patnala <[email protected]>
AuthorDate: Tue Jan 30 13:00:51 2024 +0530

    some fixes
---
 .../org/apache/cloudstack/api/ApiConstants.java    |  1 -
 .../admin/volume/RecoverVolumeCmdByAdmin.java      |  2 +-
 .../com/cloud/vm/VmWorkCheckAndRepairVolume.java   |  1 +
 .../storage/volume/VolumeServiceImpl.java          |  3 ++
 .../com/cloud/storage/VolumeApiServiceImpl.java    | 53 +++++++++++++---------
 5 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java 
b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
index e3616900a8b..1c2f77921be 100644
--- a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
+++ b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
@@ -379,7 +379,6 @@ public class ApiConstants {
     public static final String RECEIVED_BYTES = "receivedbytes";
     public static final String RECONNECT = "reconnect";
     public static final String RECOVER = "recover";
-
     public static final String REPAIR = "repair";
     public static final String REQUIRES_HVM = "requireshvm";
     public static final String RESOURCE_NAME = "resourcename";
diff --git 
a/api/src/main/java/org/apache/cloudstack/api/command/admin/volume/RecoverVolumeCmdByAdmin.java
 
b/api/src/main/java/org/apache/cloudstack/api/command/admin/volume/RecoverVolumeCmdByAdmin.java
index 92c62fa8662..f51aeec9719 100644
--- 
a/api/src/main/java/org/apache/cloudstack/api/command/admin/volume/RecoverVolumeCmdByAdmin.java
+++ 
b/api/src/main/java/org/apache/cloudstack/api/command/admin/volume/RecoverVolumeCmdByAdmin.java
@@ -30,7 +30,7 @@ import org.apache.cloudstack.context.CallContext;
 
 import com.cloud.storage.Volume;
 
-@APICommand(name = "recoverVolume", description = "Recovers a Destroy volume, 
curr", responseObject = VolumeResponse.class,  responseView = 
ResponseView.Full, entityType = {Volume.class},
+@APICommand(name = "recoverVolume", description = "Recovers a Destroy 
volume.", responseObject = VolumeResponse.class,  responseView = 
ResponseView.Full, entityType = {Volume.class},
             since = "4.14.0",
             authorized = {RoleType.Admin},
             requestHasSensitiveInfo = false,
diff --git 
a/engine/components-api/src/main/java/com/cloud/vm/VmWorkCheckAndRepairVolume.java
 
b/engine/components-api/src/main/java/com/cloud/vm/VmWorkCheckAndRepairVolume.java
index f4053dd1741..7e62f08fe61 100644
--- 
a/engine/components-api/src/main/java/com/cloud/vm/VmWorkCheckAndRepairVolume.java
+++ 
b/engine/components-api/src/main/java/com/cloud/vm/VmWorkCheckAndRepairVolume.java
@@ -14,6 +14,7 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
+
 package com.cloud.vm;
 
 public class VmWorkCheckAndRepairVolume extends VmWork {
diff --git 
a/engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
 
b/engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
index 8c839a9b4d4..698a585b987 100644
--- 
a/engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
+++ 
b/engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
@@ -87,6 +87,8 @@ import org.springframework.stereotype.Component;
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.ModifyTargetsCommand;
+import com.cloud.agent.api.storage.CheckVolumeAndRepairAnswer;
+import com.cloud.agent.api.storage.CheckVolumeAndRepairCommand;
 import com.cloud.agent.api.storage.ListVolumeAnswer;
 import com.cloud.agent.api.storage.ListVolumeCommand;
 import com.cloud.agent.api.storage.ResizeVolumeCommand;
@@ -110,6 +112,7 @@ import com.cloud.org.Cluster;
 import com.cloud.org.Grouping.AllocationState;
 import com.cloud.resource.ResourceState;
 import com.cloud.server.ManagementService;
+import com.cloud.storage.CheckAndRepairVolumePayload;
 import com.cloud.storage.DataStoreRole;
 import com.cloud.storage.RegisterVolumePayload;
 import com.cloud.storage.ScopeType;
diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java 
b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
index a82cc1f1bd8..50d1a400664 100644
--- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
+++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
@@ -1822,38 +1822,19 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
     @Override
     @ActionEvent(eventType = EventTypes.EVENT_VOLUME_CHECK, eventDescription = 
"checking volume and repair if needed", async = true)
     public Pair<String, String> checkAndRepairVolume(CheckVolumeAndRepairCmd 
cmd) throws ResourceAllocationException {
-        Account caller = CallContext.current().getCallingAccount();
-
-        // Verify input parameters
         long volumeId = cmd.getId();
         boolean repair = cmd.getRepair();
-        final VolumeVO volume = _volsDao.findById(volumeId);
 
-        _accountMgr.checkAccess(caller, null, true, volume);
+        validationsForCheckVolumeOperation(volumeId);
 
+        final VolumeVO volume = _volsDao.findById(volumeId);
         Long vmId = volume.getInstanceId();
         UserVmVO vm = null;
         if (vmId != null) {
             vm = _userVmDao.findById(vmId);
-            if (vm == null) {
-                throw new InvalidParameterValueException(String.format("VM not 
found, please check the VM to which this volume %d is attached", volumeId));
-            }
-            if (vm.getState() != State.Stopped) {
-                throw new InvalidParameterValueException(String.format("VM to 
which the volume %d is attached should be in stopped state", volumeId));
-            }
-        }
-
-        if (volume.getState() != Volume.State.Ready) {
-            throw new InvalidParameterValueException(String.format("VolumeId: 
%d is not in Ready state", volumeId));
-        }
-
-        HypervisorType hypervisorType = 
_volsDao.getHypervisorType(volume.getId());
-        if (!HypervisorType.KVM.equals(hypervisorType)) {
-            throw new InvalidParameterValueException(String.format("Check and 
Repair volumes is supported only for KVM hypervisor"));
         }
 
         if (vm != null) {
-            _accountMgr.checkAccess(caller, null, true, vm);
             // serialize VM operation
             AsyncJobExecutionContext jobContext = 
AsyncJobExecutionContext.getCurrentExecutionContext();
             if 
(jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) {
@@ -1906,6 +1887,36 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
         }
     }
 
+    private void validationsForCheckVolumeOperation(long volumeId) {
+        final VolumeVO volume = _volsDao.findById(volumeId);
+        Account caller = CallContext.current().getCallingAccount();
+        _accountMgr.checkAccess(caller, null, true, volume);
+
+        Long vmId = volume.getInstanceId();
+        UserVmVO vm = null;
+        if (vmId != null) {
+            vm = _userVmDao.findById(vmId);
+            if (vm == null) {
+                throw new InvalidParameterValueException(String.format("VM not 
found, please check the VM to which this volume %d is attached", volumeId));
+            }
+
+            _accountMgr.checkAccess(caller, null, true, vm);
+
+            if (vm.getState() != State.Stopped) {
+                throw new InvalidParameterValueException(String.format("VM to 
which the volume %d is attached should be in stopped state", volumeId));
+            }
+        }
+
+        if (volume.getState() != Volume.State.Ready) {
+            throw new InvalidParameterValueException(String.format("VolumeId: 
%d is not in Ready state", volumeId));
+        }
+
+        HypervisorType hypervisorType = 
_volsDao.getHypervisorType(volume.getId());
+        if (!HypervisorType.KVM.equals(hypervisorType)) {
+            throw new InvalidParameterValueException(String.format("Check and 
Repair volumes is supported only for KVM hypervisor"));
+        }
+    }
+
     private Pair<String, String> orchestrateCheckVolumeAndRepair(Long 
volumeId, boolean repair) {
 
         VolumeInfo volume = volFactory.getVolume(volumeId);

Reply via email to