diff --git a/api/src/org/apache/cloudstack/api/ApiConstants.java b/api/src/org/apache/cloudstack/api/ApiConstants.java index 761ff45196b..d9226c41c18 100755 --- a/api/src/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/org/apache/cloudstack/api/ApiConstants.java @@ -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; diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java index 567768dee13..0f11da96726 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java @@ -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 responses = _responseGenerator.createUserVmResponse("virtualmachine", result); + if (responses != null && !responses.isEmpty()) { + response = responses.get(0); + } response.setResponseName("virtualmachine"); this.setResponseObject(response); } else { diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 812f8384aba..41ad50c155e 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -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