CLOUDSTACK-4894: DestroyVirtualMachine API - added "expunge" admin-only parameter. When passed as true, the vm gets expunged immediately (false by default to preserve the initial command behavior)

This commit is contained in:
Alena Prokharchyk 2013-10-21 09:43:29 -07:00
parent b6a13d1257
commit 2e75e8ced0
3 changed files with 40 additions and 6 deletions

View File

@ -522,6 +522,7 @@ public class ApiConstants {
public static final String ROUTING = "isrouting";
public static final String SERVICE_STATE = "servicestate";
public static final String MAX_CONNECTIONS = "maxconnections";
public static final String EXPUNGE = "expunge";
public enum HostDetails {
all, capacity, events, stats, min;

View File

@ -16,6 +16,8 @@
// under the License.
package org.apache.cloudstack.api.command.user.vm;
import java.util.List;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
@ -29,7 +31,6 @@ import com.cloud.async.AsyncJob;
import com.cloud.event.EventTypes;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.user.Account;
import com.cloud.user.UserContext;
import com.cloud.uservm.UserVm;
@ -47,7 +48,12 @@ public class DestroyVMCmd extends BaseAsyncCmd {
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=UserVmResponse.class,
required=true, description="The ID of the virtual machine")
private Long id;
@Parameter(name=ApiConstants.EXPUNGE, type=CommandType.BOOLEAN,
description="If true is passed, the vm is expunged immediately. False by default. Parameter can be passed to the call by ROOT/Domain admin only", since="4.2.1")
private Boolean expunge;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -55,6 +61,13 @@ public class DestroyVMCmd extends BaseAsyncCmd {
public Long getId() {
return id;
}
public boolean getExpunge() {
if (expunge == null) {
return false;
}
return expunge;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
@ -96,11 +109,14 @@ public class DestroyVMCmd extends BaseAsyncCmd {
@Override
public void execute() throws ResourceUnavailableException, ConcurrentOperationException{
UserContext.current().setEventDetails("Vm Id: "+getId());
UserVm result;
result = _userVmService.destroyVm(this);
UserVm result = _userVmService.destroyVm(this);
UserVmResponse response = new UserVmResponse();
if (result != null) {
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
List<UserVmResponse> responses = _responseGenerator.createUserVmResponse("virtualmachine", result);
if (responses != null && !responses.isEmpty()) {
response = responses.get(0);
}
response.setResponseName("virtualmachine");
this.setResponseObject(response);
} else {

View File

@ -39,6 +39,7 @@ import org.apache.cloudstack.affinity.AffinityGroupService;
import org.apache.cloudstack.affinity.AffinityGroupVO;
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
@ -1981,7 +1982,23 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
@ActionEvent(eventType = EventTypes.EVENT_VM_DESTROY, eventDescription = "destroying Vm", async = true)
public UserVm destroyVm(DestroyVMCmd cmd)
throws ResourceUnavailableException, ConcurrentOperationException {
return destroyVm(cmd.getId());
UserContext ctx = UserContext.current();
long vmId = cmd.getId();
boolean expunge = cmd.getExpunge();
if (!_accountMgr.isAdmin(ctx.getCaller().getType()) && expunge) {
throw new PermissionDeniedException("Parameter " + ApiConstants.EXPUNGE + " can be passed by Admin only");
}
UserVm destroyedVm = destroyVm(vmId);
if (expunge) {
UserVmVO vm = _vmDao.findById(vmId);
if (!expunge(vm, ctx.getCallerUserId(), ctx.getCaller())) {
throw new CloudRuntimeException("Failed to expunge vm " + destroyedVm);
}
}
return destroyedVm;
}
@Override