diff --git a/api/src/org/apache/cloudstack/api/ApiConstants.java b/api/src/org/apache/cloudstack/api/ApiConstants.java index e2f225d5fcc..8e88a386fdc 100755 --- a/api/src/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/org/apache/cloudstack/api/ApiConstants.java @@ -519,6 +519,8 @@ public class ApiConstants { public static final String MAX_CONNECTIONS = "maxconnections"; public static final String SERVICE_STATE = "servicestate"; public static final String RESOURCE_TAGS = "resourcetags"; + 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 06959c15e75..b3e8d1f83fe 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.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; @@ -25,13 +27,11 @@ import org.apache.cloudstack.api.Parameter; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; - import org.apache.log4j.Logger; 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.uservm.UserVm; @@ -48,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 /////////////////////// ///////////////////////////////////////////////////// @@ -56,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/////////////////// @@ -97,11 +109,14 @@ public class DestroyVMCmd extends BaseAsyncCmd { @Override public void execute() throws ResourceUnavailableException, ConcurrentOperationException{ CallContext.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 6e879161276..05663db61b1 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -33,15 +33,13 @@ import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; -import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.ControlledEntity.ACLType; import org.apache.cloudstack.acl.SecurityChecker.AccessType; 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; @@ -75,6 +73,8 @@ import org.apache.cloudstack.managed.context.ManagedContextRunnable; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.to.TemplateObjectTO; +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; import com.cloud.agent.AgentManager; import com.cloud.agent.api.Answer; @@ -1934,7 +1934,23 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir @ActionEvent(eventType = EventTypes.EVENT_VM_DESTROY, eventDescription = "destroying Vm", async = true) public UserVm destroyVm(DestroyVMCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException { - return destroyVm(cmd.getId()); + CallContext ctx = CallContext.current(); + long vmId = cmd.getId(); + boolean expunge = cmd.getExpunge(); + + if (!_accountMgr.isAdmin(ctx.getCallingAccount().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.getCallingUserId(), ctx.getCallingAccount())) { + throw new CloudRuntimeException("Failed to expunge vm " + destroyedVm); + } + } + + return destroyedVm; } @Override