@@ -985,7 +985,6 @@ public UserVm resetVMUserData(ResetVMUserDataCmd cmd) throws ResourceUnavailable
985985 @ Override
986986 @ ActionEvent (eventType = EventTypes .EVENT_VM_RESETSSHKEY , eventDescription = "resetting Vm SSHKey" , async = true )
987987 public UserVm resetVMSSHKey (ResetVMSSHKeyCmd cmd ) throws ResourceUnavailableException , InsufficientCapacityException {
988-
989988 Account caller = CallContext .current ().getCallingAccount ();
990989 Account owner = _accountMgr .finalizeOwner (caller , cmd .getAccountName (), cmd .getDomainId (), cmd .getProjectId ());
991990 Long vmId = cmd .getId ();
@@ -1011,32 +1010,40 @@ public UserVm resetVMSSHKey(ResetVMSSHKeyCmd cmd) throws ResourceUnavailableExce
10111010 throw new InvalidParameterValueException ("Vm " + userVm + " should be stopped to do SSH Key reset" );
10121011 }
10131012
1014- if (cmd .getNames () == null || cmd .getNames ().isEmpty ()) {
1013+ List <String > names = cmd .getNames ();
1014+ if (names == null || names .isEmpty ()) {
10151015 throw new InvalidParameterValueException ("'keypair' or 'keypairs' must be specified" );
10161016 }
10171017
1018+ userVm = resetVMSSHKeyInternal (userVm , owner , names );
1019+ return userVm ;
1020+ }
1021+
1022+ private UserVmVO resetVMSSHKeyInternal (UserVmVO userVm , Account owner , List <String > names ) throws ResourceUnavailableException , InsufficientCapacityException {
1023+ Account caller = CallContext .current ().getCallingAccount ();
1024+
10181025 String keypairnames = "" ;
10191026 String sshPublicKeys = "" ;
10201027 List <SSHKeyPairVO > pairs = new ArrayList <>();
10211028
1022- pairs = _sshKeyPairDao .findByNames (owner .getAccountId (), owner .getDomainId (), cmd . getNames () );
1023- if (pairs == null || pairs .size () != cmd . getNames () .size ()) {
1029+ pairs = _sshKeyPairDao .findByNames (owner .getAccountId (), owner .getDomainId (), names );
1030+ if (pairs == null || pairs .size () != names .size ()) {
10241031 throw new InvalidParameterValueException ("Not all specified keypairs exist" );
10251032 }
10261033 sshPublicKeys = pairs .stream ().map (p -> p .getPublicKey ()).collect (Collectors .joining ("\n " ));
1027- keypairnames = String .join ("," , cmd . getNames () );
1034+ keypairnames = String .join ("," , names );
10281035
10291036 _accountMgr .checkAccess (caller , null , true , userVm );
10301037
1031- boolean result = resetVMSSHKeyInternal (vmId , sshPublicKeys , keypairnames );
1038+ boolean result = resetVMSSHKeyInternal (userVm . getId () , sshPublicKeys , keypairnames );
10321039
1033- UserVmVO vm = _vmDao .findById (vmId );
1040+ UserVmVO vm = _vmDao .findById (userVm . getId () );
10341041 _vmDao .loadDetails (vm );
10351042 if (!result ) {
10361043 throw new CloudRuntimeException ("Failed to reset SSH Key for the virtual machine " );
10371044 }
10381045
1039- removeEncryptedPasswordFromUserVmVoDetails (vmId );
1046+ removeEncryptedPasswordFromUserVmVoDetails (userVm . getId () );
10401047
10411048 _vmDao .loadDetails (userVm );
10421049 return userVm ;
@@ -8957,22 +8964,22 @@ public UserVm restoreVMFromBackup(CreateVMFromBackupCmd cmd) throws ResourceUnav
89578964 UserVm vm = startVirtualMachine (vmId , null , null , null , diskOfferingMap , additonalParams , null );
89588965
89598966 boolean status = false ;
8960- try {
8961- VirtualMachineEntity vmEntity = _orchSrvc .getVirtualMachine (vm .getUuid ());
8962- status = vmEntity .stop (Long .toString (CallContext .current ().getCallingUserId ()));
8963- if (!status ) {
8964- // todo : error handling
8965- }
8966- } catch (ResourceUnavailableException e ) {
8967- throw new CloudRuntimeException ("Unable to contact the agent to stop the instance before restore " + e );
8968- // todo : error handling
8969- } catch (CloudException e ) {
8970- throw new CloudRuntimeException ("Unable to contact the agent to stop the instance before restore " + e );
8971- // todo : error handling
8967+ status = stopVirtualMachine (CallContext .current ().getCallingUserId (), vm .getId ()) ;
8968+ if (!status ) {
8969+ expungeVm (vm .getId ());
8970+ throw new CloudRuntimeException ("Unable to stop the instance before restore " );
89728971 }
89738972
89748973 backupManager .restoreBackupToVM (cmd .getBackupId (), vmId );
89758974
8975+ Account owner = _accountService .getActiveAccountById (cmd .getEntityOwnerId ());
8976+ UserVmVO userVm = _vmDao .findById (vmId );
8977+
8978+ List <String > sshKeyPairNames = cmd .getSSHKeyPairNames ();
8979+ if (sshKeyPairNames != null && !sshKeyPairNames .isEmpty ()) {
8980+ vm = resetVMSSHKeyInternal (userVm , owner , sshKeyPairNames );
8981+ }
8982+
89768983 if (cmd .getStartVm ()) {
89778984 vm = startVirtualMachine (vmId , null , null , null , diskOfferingMap , additonalParams , null );
89788985 }
0 commit comments