You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by al...@apache.org on 2013/10/21 20:11:20 UTC

git commit: updated refs/heads/master to 25d6e6e

Updated Branches:
  refs/heads/master 5c5326f6f -> 25d6e6ec6


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)

Conflicts:
	api/src/org/apache/cloudstack/api/ApiConstants.java
	api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/25d6e6ec
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/25d6e6ec
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/25d6e6ec

Branch: refs/heads/master
Commit: 25d6e6ec66b141fe728d68062ff04a74615fb310
Parents: 5c5326f
Author: Alena Prokharchyk <al...@citrix.com>
Authored: Mon Oct 21 09:43:29 2013 -0700
Committer: Alena Prokharchyk <al...@citrix.com>
Committed: Mon Oct 21 11:11:42 2013 -0700

----------------------------------------------------------------------
 .../org/apache/cloudstack/api/ApiConstants.java |  2 ++
 .../api/command/user/vm/DestroyVMCmd.java       | 27 +++++++++++++++-----
 server/src/com/cloud/vm/UserVmManagerImpl.java  | 24 ++++++++++++++---
 3 files changed, 43 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/25d6e6ec/api/src/org/apache/cloudstack/api/ApiConstants.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/ApiConstants.java b/api/src/org/apache/cloudstack/api/ApiConstants.java
index e2f225d..8e88a38 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;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/25d6e6ec/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java
----------------------------------------------------------------------
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 06959c1..b3e8d1f 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<UserVmResponse> responses =  _responseGenerator.createUserVmResponse("virtualmachine", result);
+            if (responses != null && !responses.isEmpty()) {
+                response = responses.get(0);
+            }
             response.setResponseName("virtualmachine");
             this.setResponseObject(response);
         } else {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/25d6e6ec/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index 6e87916..05663db 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