Skip to content

Commit 2e75e8c

Browse files
author
Alena Prokharchyk
committed
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)
1 parent b6a13d1 commit 2e75e8c

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎api/src/org/apache/cloudstack/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ public class ApiConstants {
522522
public static final String ROUTING = "isrouting";
523523
public static final String SERVICE_STATE = "servicestate";
524524
public static final String MAX_CONNECTIONS = "maxconnections";
525+
public static final String EXPUNGE = "expunge";
525526

526527
public enum HostDetails {
527528
all, capacity, events, stats, min;

‎api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.vm;
1818

19+
import java.util.List;
20+
1921
import org.apache.cloudstack.api.APICommand;
2022
import org.apache.cloudstack.api.ApiConstants;
2123
import org.apache.cloudstack.api.ApiErrorCode;
@@ -29,7 +31,6 @@
2931
import com.cloud.event.EventTypes;
3032
import com.cloud.exception.ConcurrentOperationException;
3133
import com.cloud.exception.ResourceUnavailableException;
32-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
3334
import com.cloud.user.Account;
3435
import com.cloud.user.UserContext;
3536
import com.cloud.uservm.UserVm;
@@ -47,14 +48,26 @@ public class DestroyVMCmd extends BaseAsyncCmd {
4748
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=UserVmResponse.class,
4849
required=true, description="The ID of the virtual machine")
4950
private Long id;
50-
51+
52+
53+
@Parameter(name=ApiConstants.EXPUNGE, type=CommandType.BOOLEAN,
54+
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")
55+
private Boolean expunge;
56+
5157
/////////////////////////////////////////////////////
5258
/////////////////// Accessors ///////////////////////
5359
/////////////////////////////////////////////////////
5460

5561
public Long getId() {
5662
return id;
5763
}
64+
65+
public boolean getExpunge() {
66+
if (expunge == null) {
67+
return false;
68+
}
69+
return expunge;
70+
}
5871

5972
/////////////////////////////////////////////////////
6073
/////////////// API Implementation///////////////////
@@ -96,11 +109,14 @@ public Long getInstanceId() {
96109
@Override
97110
public void execute() throws ResourceUnavailableException, ConcurrentOperationException{
98111
UserContext.current().setEventDetails("Vm Id: "+getId());
99-
UserVm result;
100-
result = _userVmService.destroyVm(this);
112+
UserVm result = _userVmService.destroyVm(this);
101113

114+
UserVmResponse response = new UserVmResponse();
102115
if (result != null) {
103-
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
116+
List<UserVmResponse> responses = _responseGenerator.createUserVmResponse("virtualmachine", result);
117+
if (responses != null && !responses.isEmpty()) {
118+
response = responses.get(0);
119+
}
104120
response.setResponseName("virtualmachine");
105121
this.setResponseObject(response);
106122
} else {

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.cloudstack.affinity.AffinityGroupVO;
4040
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
4141
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
42+
import org.apache.cloudstack.api.ApiConstants;
4243
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
4344
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
4445
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
@@ -1981,7 +1982,23 @@ public UserVm rebootVirtualMachine(RebootVMCmd cmd)
19811982
@ActionEvent(eventType = EventTypes.EVENT_VM_DESTROY, eventDescription = "destroying Vm", async = true)
19821983
public UserVm destroyVm(DestroyVMCmd cmd)
19831984
throws ResourceUnavailableException, ConcurrentOperationException {
1984-
return destroyVm(cmd.getId());
1985+
UserContext ctx = UserContext.current();
1986+
long vmId = cmd.getId();
1987+
boolean expunge = cmd.getExpunge();
1988+
1989+
if (!_accountMgr.isAdmin(ctx.getCaller().getType()) && expunge) {
1990+
throw new PermissionDeniedException("Parameter " + ApiConstants.EXPUNGE + " can be passed by Admin only");
1991+
}
1992+
1993+
UserVm destroyedVm = destroyVm(vmId);
1994+
if (expunge) {
1995+
UserVmVO vm = _vmDao.findById(vmId);
1996+
if (!expunge(vm, ctx.getCallerUserId(), ctx.getCaller())) {
1997+
throw new CloudRuntimeException("Failed to expunge vm " + destroyedVm);
1998+
}
1999+
}
2000+
2001+
return destroyedVm;
19852002
}
19862003

19872004
@Override

0 commit comments

Comments
 (0)