CLOUDSTACK-2180: restoreVirtualMachine returns no password if the template is 
password enabled

New password is generated as part of restore vm(passwd enabled template) and 
send new password on VR

Signed-off-by: Abhinandan Prateek <[email protected]>


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

Branch: refs/heads/internallb
Commit: 1cb9bd531f4eaaef3a99608ab77cba1851b37e42
Parents: 10b6c1c
Author: Harikrishna Patnala <[email protected]>
Authored: Thu Apr 25 13:17:17 2013 +0530
Committer: Abhinandan Prateek <[email protected]>
Committed: Wed May 1 12:29:08 2013 +0530

----------------------------------------------------------------------
 api/src/com/cloud/vm/UserVmService.java            |    2 +-
 server/src/com/cloud/vm/UserVmManagerImpl.java     |   32 ++++++++++++--
 .../test/com/cloud/vm/MockUserVmManagerImpl.java   |    2 +-
 server/test/com/cloud/vm/UserVmManagerTest.java    |    2 +-
 4 files changed, 30 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1cb9bd53/api/src/com/cloud/vm/UserVmService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/vm/UserVmService.java 
b/api/src/com/cloud/vm/UserVmService.java
index 7e89cd3..c8ccb67 100755
--- a/api/src/com/cloud/vm/UserVmService.java
+++ b/api/src/com/cloud/vm/UserVmService.java
@@ -449,7 +449,7 @@ public interface UserVmService {
 
     VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);
 
-    UserVm restoreVM(RestoreVMCmd cmd);
+    UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, 
ResourceUnavailableException;
 
     UserVm upgradeVirtualMachine(ScaleVMCmd scaleVMCmd) throws 
ResourceUnavailableException, ConcurrentOperationException, 
ManagementServerException, VirtualMachineMigrationException;
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1cb9bd53/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 f58d6be..6179948 100755
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -485,7 +485,7 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Use
 
         _accountMgr.checkAccess(caller, null, true, userVm);
 
-        boolean result = resetVMPasswordInternal(cmd, password);
+        boolean result = resetVMPasswordInternal(vmId, password);
 
         if (result) {
             userVm.setPassword(password);
@@ -512,10 +512,9 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Use
         return userVm;
     }
 
-    private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd,
+    private boolean resetVMPasswordInternal(Long vmId,
             String password) throws ResourceUnavailableException,
             InsufficientCapacityException {
-        Long vmId = cmd.getId();
         Long userId = UserContext.current().getCallerUserId();
         VMInstanceVO vmInstance = _vmDao.findById(vmId);
 
@@ -4078,7 +4077,7 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Use
     }
 
     @Override
-    public UserVm restoreVM(RestoreVMCmd cmd) {
+    public UserVm restoreVM(RestoreVMCmd cmd) throws 
InsufficientCapacityException, ResourceUnavailableException {
         // Input validation
         Account caller = UserContext.current().getCaller();
 
@@ -4096,7 +4095,7 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Use
         return restoreVMInternal(caller, vm, newTemplateId);
     }
 
-    public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long 
newTemplateId){
+    public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long 
newTemplateId) throws InsufficientCapacityException, 
ResourceUnavailableException {
 
         Long userId = caller.getId();
         Account owner = _accountDao.findById(vm.getAccountId());
@@ -4190,6 +4189,29 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Use
         _volsDao.detachVolume(root.getId());
         this.volumeMgr.destroyVolume(root);
 
+        if (template.getEnablePassword()) {
+            String password = generateRandomPassword();
+            boolean result = resetVMPasswordInternal(vmId, password);
+            if (result) {
+                vm.setPassword(password);
+                _vmDao.loadDetails(vm);
+                // update the password in vm_details table too
+                // Check if an SSH key pair was selected for the instance and 
if so
+                // use it to encrypt & save the vm password
+                String sshPublicKey = vm.getDetail("SSH.PublicKey");
+                if (sshPublicKey != null && !sshPublicKey.equals("") && 
password != null && !password.equals("saved_password")) {
+                    String encryptedPasswd = 
RSAHelper.encryptWithSSHPublicKey(sshPublicKey, password);
+                    if (encryptedPasswd == null) {
+                        throw new CloudRuntimeException("VM reset is completed 
but error occurred when encrypting newly created password");
+                    }
+                    vm.setDetail("Encrypted.Password", encryptedPasswd);
+                    _vmDao.saveDetails(vm);
+                }
+            } else {
+                throw new CloudRuntimeException("VM reset is completed but 
failed to reset password for the virtual machine ");
+            }
+        }
+
         if (needRestart) {
             try {
                 _itMgr.start(vm, null, user, caller);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1cb9bd53/server/test/com/cloud/vm/MockUserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/MockUserVmManagerImpl.java 
b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
index d886fd8..22bbbe8 100644
--- a/server/test/com/cloud/vm/MockUserVmManagerImpl.java
+++ b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
@@ -401,7 +401,7 @@ public class MockUserVmManagerImpl extends ManagerBase 
implements UserVmManager,
     }
 
     @Override
-    public UserVm restoreVM(RestoreVMCmd cmd) {
+    public UserVm restoreVM(RestoreVMCmd cmd) throws 
InsufficientCapacityException, ResourceUnavailableException{
         // TODO Auto-generated method stub
         return null;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1cb9bd53/server/test/com/cloud/vm/UserVmManagerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/UserVmManagerTest.java 
b/server/test/com/cloud/vm/UserVmManagerTest.java
index e5e2ff2..08f2a9c 100755
--- a/server/test/com/cloud/vm/UserVmManagerTest.java
+++ b/server/test/com/cloud/vm/UserVmManagerTest.java
@@ -121,7 +121,7 @@ public class UserVmManagerTest {
 
     // Test restoreVm when VM state not in running/stopped case
     @Test(expected=CloudRuntimeException.class)
-    public void testRestoreVMF1() throws ResourceAllocationException {
+    public void testRestoreVMF1() throws ResourceAllocationException, 
InsufficientCapacityException, ResourceUnavailableException {
 
         when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
         when(_templateDao.findById(anyLong())).thenReturn(_templateMock);

Reply via email to