mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactoring the detach volume command
This commit is contained in:
parent
46f69a604b
commit
82fbae2553
@ -25,21 +25,19 @@ import java.util.Map;
|
|||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import com.cloud.api.BaseCmd;
|
import com.cloud.api.BaseCmd;
|
||||||
|
import com.cloud.api.Implementation;
|
||||||
import com.cloud.api.Parameter;
|
import com.cloud.api.Parameter;
|
||||||
import com.cloud.api.ServerApiException;
|
import com.cloud.api.ServerApiException;
|
||||||
|
import com.cloud.api.BaseCmd.Manager;
|
||||||
import com.cloud.storage.VolumeVO;
|
import com.cloud.storage.VolumeVO;
|
||||||
import com.cloud.user.Account;
|
import com.cloud.user.Account;
|
||||||
import com.cloud.utils.Pair;
|
import com.cloud.utils.Pair;
|
||||||
|
|
||||||
|
@Implementation(method="detachVolumeFromVM", manager=Manager.UserVmManager)
|
||||||
public class DetachVolumeCmd extends BaseCmd {
|
public class DetachVolumeCmd extends BaseCmd {
|
||||||
public static final Logger s_logger = Logger.getLogger(DetachVolumeCmd.class.getName());
|
public static final Logger s_logger = Logger.getLogger(DetachVolumeCmd.class.getName());
|
||||||
private static final String s_name = "detachvolumeresponse";
|
private static final String s_name = "detachvolumeresponse";
|
||||||
|
|
||||||
static {
|
|
||||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
|
||||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
//////////////// API parameters /////////////////////
|
//////////////// API parameters /////////////////////
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
@ -65,59 +63,62 @@ public class DetachVolumeCmd extends BaseCmd {
|
|||||||
return s_name;
|
return s_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Pair<Enum, Boolean>> getProperties() {
|
|
||||||
return s_properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getResultObjectName() {
|
public static String getResultObjectName() {
|
||||||
return "volume";
|
return "volume";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||||
Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
// Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||||
Long volumeId = (Long) params.get(BaseCmd.Properties.ID.getName());
|
// Long volumeId = (Long) params.get(BaseCmd.Properties.ID.getName());
|
||||||
|
//
|
||||||
boolean isAdmin;
|
// boolean isAdmin;
|
||||||
if (account == null) {
|
// if (account == null) {
|
||||||
// Admin API call
|
// // Admin API call
|
||||||
isAdmin = true;
|
// isAdmin = true;
|
||||||
} else {
|
// } else {
|
||||||
// User API call
|
// // User API call
|
||||||
isAdmin = isAdmin(account.getType());
|
// isAdmin = isAdmin(account.getType());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// // Check that the volume ID is valid
|
||||||
|
// VolumeVO volume = getManagementServer().findVolumeById(volumeId);
|
||||||
|
// if (volume == null)
|
||||||
|
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId);
|
||||||
|
//
|
||||||
|
// // If the account is not an admin, check that the volume is owned by the account that was passed in
|
||||||
|
// if (!isAdmin) {
|
||||||
|
// if (account.getId() != volume.getAccountId())
|
||||||
|
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName());
|
||||||
|
// } else if (account != null) {
|
||||||
|
// if (!getManagementServer().isChildDomain(account.getDomainId(), volume.getDomainId())) {
|
||||||
|
// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to detach volume with ID: " + volumeId + ", permission denied.");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// long jobId = getManagementServer().detachVolumeFromVMAsync(volumeId);
|
||||||
|
//
|
||||||
|
// if (jobId == 0) {
|
||||||
|
// s_logger.warn("Unable to schedule async-job for DetachVolume comamnd");
|
||||||
|
// } else {
|
||||||
|
// if(s_logger.isDebugEnabled())
|
||||||
|
// s_logger.debug("DetachVolume command has been accepted, job id: " + jobId);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||||
|
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||||
|
//
|
||||||
|
// return returnValues;
|
||||||
|
// } catch (Exception ex) {
|
||||||
|
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to detach volume: " + ex.getMessage());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// Check that the volume ID is valid
|
|
||||||
VolumeVO volume = getManagementServer().findVolumeById(volumeId);
|
|
||||||
if (volume == null)
|
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId);
|
|
||||||
|
|
||||||
// If the account is not an admin, check that the volume is owned by the account that was passed in
|
@Override
|
||||||
if (!isAdmin) {
|
public String getResponse() {
|
||||||
if (account.getId() != volume.getAccountId())
|
// TODO Auto-generated method stub
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName());
|
return null;
|
||||||
} else if (account != null) {
|
}
|
||||||
if (!getManagementServer().isChildDomain(account.getDomainId(), volume.getDomainId())) {
|
|
||||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to detach volume with ID: " + volumeId + ", permission denied.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
long jobId = getManagementServer().detachVolumeFromVMAsync(volumeId);
|
|
||||||
|
|
||||||
if (jobId == 0) {
|
|
||||||
s_logger.warn("Unable to schedule async-job for DetachVolume comamnd");
|
|
||||||
} else {
|
|
||||||
if(s_logger.isDebugEnabled())
|
|
||||||
s_logger.debug("DetachVolume command has been accepted, job id: " + jobId);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
|
||||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
|
||||||
|
|
||||||
return returnValues;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to detach volume: " + ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -633,8 +633,8 @@ public interface ManagementServer {
|
|||||||
* @volumeId
|
* @volumeId
|
||||||
* @throws InvalidParameterValueException, InternalErrorException
|
* @throws InvalidParameterValueException, InternalErrorException
|
||||||
*/
|
*/
|
||||||
void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException;
|
// void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException;
|
||||||
long detachVolumeFromVMAsync(long volumeId) throws InvalidParameterValueException;
|
// long detachVolumeFromVMAsync(long volumeId) throws InvalidParameterValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attaches an ISO to the virtual CDROM device of the specified VM. Will fail if the VM already has an ISO mounted.
|
* Attaches an ISO to the virtual CDROM device of the specified VM. Will fail if the VM already has an ISO mounted.
|
||||||
|
|||||||
@ -2091,10 +2091,10 @@ public class ManagementServerImpl implements ManagementServer {
|
|||||||
return _asyncMgr.submitAsyncJob(job);
|
return _asyncMgr.submitAsyncJob(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException {
|
// public void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException {
|
||||||
_vmMgr.detachVolumeFromVM(volumeId, startEventId);
|
// _vmMgr.detachVolumeFromVM(volumeId, startEventId);
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long detachVolumeFromVMAsync(long volumeId) throws InvalidParameterValueException {
|
public long detachVolumeFromVMAsync(long volumeId) throws InvalidParameterValueException {
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import com.cloud.agent.api.VmStatsEntry;
|
import com.cloud.agent.api.VmStatsEntry;
|
||||||
import com.cloud.api.ServerApiException;
|
import com.cloud.api.ServerApiException;
|
||||||
|
import com.cloud.api.commands.DetachVolumeCmd;
|
||||||
import com.cloud.api.commands.RebootVMCmd;
|
import com.cloud.api.commands.RebootVMCmd;
|
||||||
import com.cloud.api.commands.RecoverVMCmd;
|
import com.cloud.api.commands.RecoverVMCmd;
|
||||||
import com.cloud.api.commands.ResetVMPasswordCmd;
|
import com.cloud.api.commands.ResetVMPasswordCmd;
|
||||||
@ -124,8 +125,9 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
|
|||||||
* Detaches the specified volume from the VM it is currently attached to.
|
* Detaches the specified volume from the VM it is currently attached to.
|
||||||
* @param volumeId
|
* @param volumeId
|
||||||
* @throws InternalErrorException
|
* @throws InternalErrorException
|
||||||
|
* @throws InvalidParameterValueException
|
||||||
*/
|
*/
|
||||||
void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException;
|
void detachVolumeFromVM(DetachVolumeCmd cmmd) throws InternalErrorException, InvalidParameterValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attaches an ISO to the virtual CDROM device of the specified VM. Will eject any existing virtual CDROM if isoPath is null.
|
* Attaches an ISO to the virtual CDROM device of the specified VM. Will eject any existing virtual CDROM if isoPath is null.
|
||||||
|
|||||||
@ -66,6 +66,7 @@ import com.cloud.agent.manager.AgentManager;
|
|||||||
import com.cloud.alert.AlertManager;
|
import com.cloud.alert.AlertManager;
|
||||||
import com.cloud.api.BaseCmd;
|
import com.cloud.api.BaseCmd;
|
||||||
import com.cloud.api.ServerApiException;
|
import com.cloud.api.ServerApiException;
|
||||||
|
import com.cloud.api.commands.DetachVolumeCmd;
|
||||||
import com.cloud.api.commands.RebootVMCmd;
|
import com.cloud.api.commands.RebootVMCmd;
|
||||||
import com.cloud.api.commands.RecoverVMCmd;
|
import com.cloud.api.commands.RecoverVMCmd;
|
||||||
import com.cloud.api.commands.ResetVMPasswordCmd;
|
import com.cloud.api.commands.ResetVMPasswordCmd;
|
||||||
@ -87,6 +88,8 @@ import com.cloud.async.executor.UpgradeVMParam;
|
|||||||
import com.cloud.async.executor.VMExecutorHelper;
|
import com.cloud.async.executor.VMExecutorHelper;
|
||||||
import com.cloud.async.executor.VMOperationListener;
|
import com.cloud.async.executor.VMOperationListener;
|
||||||
import com.cloud.async.executor.VMOperationParam;
|
import com.cloud.async.executor.VMOperationParam;
|
||||||
|
import com.cloud.async.executor.VolumeOperationParam;
|
||||||
|
import com.cloud.async.executor.VolumeOperationParam.VolumeOp;
|
||||||
import com.cloud.capacity.dao.CapacityDao;
|
import com.cloud.capacity.dao.CapacityDao;
|
||||||
import com.cloud.configuration.ConfigurationManager;
|
import com.cloud.configuration.ConfigurationManager;
|
||||||
import com.cloud.configuration.ResourceCount.ResourceType;
|
import com.cloud.configuration.ResourceCount.ResourceType;
|
||||||
@ -483,25 +486,59 @@ public class UserVmManagerImpl implements UserVmManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void detachVolumeFromVM(long volumeId, long startEventId) throws InternalErrorException {
|
public void detachVolumeFromVM(DetachVolumeCmd cmmd) throws InternalErrorException, InvalidParameterValueException {
|
||||||
VolumeVO volume = _volsDao.findById(volumeId);
|
|
||||||
|
|
||||||
Long vmId = volume.getInstanceId();
|
Account account = (Account) UserContext.current().getAccountObject();
|
||||||
|
Long volumeId = cmmd.getId();
|
||||||
|
|
||||||
if (vmId == null) {
|
boolean isAdmin;
|
||||||
return;
|
if (account == null) {
|
||||||
|
// Admin API call
|
||||||
|
isAdmin = true;
|
||||||
|
} else {
|
||||||
|
// User API call
|
||||||
|
isAdmin = isAdmin(account.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
EventVO event = new EventVO();
|
// Check that the volume ID is valid
|
||||||
event.setType(EventTypes.EVENT_VOLUME_DETACH);
|
VolumeVO volume = _volsDao.findById(volumeId);
|
||||||
event.setUserId(1L);
|
if (volume == null)
|
||||||
event.setAccountId(volume.getAccountId());
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId);
|
||||||
event.setState(EventState.Started);
|
|
||||||
event.setStartId(startEventId);
|
// If the account is not an admin, check that the volume is owned by the account that was passed in
|
||||||
event.setDescription("Detaching volume: "+volumeId+" from Vm: "+vmId);
|
if (!isAdmin) {
|
||||||
_eventDao.persist(event);
|
if (account.getId() != volume.getAccountId())
|
||||||
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find volume with ID: " + volumeId + " for account: " + account.getAccountName());
|
||||||
UserVmVO vm = _vmDao.findById(vmId);
|
} else if (account != null) {
|
||||||
|
if (!_domainDao.isChildDomain(account.getDomainId(), volume.getDomainId())) {
|
||||||
|
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to detach volume with ID: " + volumeId + ", permission denied.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the volume is a data volume
|
||||||
|
if (volume.getVolumeType() != VolumeType.DATADISK) {
|
||||||
|
throw new InvalidParameterValueException("Please specify a data volume.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the volume is stored on shared storage
|
||||||
|
if (!_storageMgr.volumeOnSharedStoragePool(volume)) {
|
||||||
|
throw new InvalidParameterValueException("Please specify a volume that has been created on a shared storage pool.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Long vmId = volume.getInstanceId();
|
||||||
|
|
||||||
|
// Check that the volume is currently attached to a VM
|
||||||
|
if (vmId == null) {
|
||||||
|
throw new InvalidParameterValueException("The specified volume is not attached to a VM.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the VM is in the correct state
|
||||||
|
UserVmVO vm = _vmDao.findById(vmId);
|
||||||
|
if (vm.getState() != State.Running && vm.getState() != State.Stopped) {
|
||||||
|
throw new InvalidParameterValueException("Please specify a VM that is either running or stopped.");
|
||||||
|
}
|
||||||
|
|
||||||
|
long eventId = EventUtils.saveScheduledEvent(1L, volume.getAccountId(), EventTypes.EVENT_VOLUME_DETACH, "detaching volume: "+volumeId+" from Vm: "+vmId);
|
||||||
|
|
||||||
AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor();
|
AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor();
|
||||||
if(asyncExecutor != null) {
|
if(asyncExecutor != null) {
|
||||||
@ -528,12 +565,12 @@ public class UserVmManagerImpl implements UserVmManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
event = new EventVO();
|
EventVO event = new EventVO();
|
||||||
event.setAccountId(volume.getAccountId());
|
event.setAccountId(volume.getAccountId());
|
||||||
event.setUserId(1L);
|
event.setUserId(1L);
|
||||||
event.setType(EventTypes.EVENT_VOLUME_DETACH);
|
event.setType(EventTypes.EVENT_VOLUME_DETACH);
|
||||||
event.setState(EventState.Completed);
|
event.setState(EventState.Completed);
|
||||||
event.setStartId(startEventId);
|
event.setStartId(eventId);
|
||||||
if (!sendCommand || (answer != null && answer.getResult())) {
|
if (!sendCommand || (answer != null && answer.getResult())) {
|
||||||
// Mark the volume as detached
|
// Mark the volume as detached
|
||||||
_volsDao.detachVolume(volume.getId());
|
_volsDao.detachVolume(volume.getId());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user