some fixes

This commit is contained in:
Harikrishna Patnala 2024-01-30 13:00:51 +05:30
parent 91f10143fd
commit 4898bbe30b
5 changed files with 38 additions and 24 deletions

View File

@ -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";

View File

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

View File

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

View File

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

View File

@ -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();
validationsForCheckVolumeOperation(volumeId);
final VolumeVO volume = _volsDao.findById(volumeId);
_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));
}
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);