Search in sources :

Example 16 with SSHKeyPairVO

use of com.cloud.user.SSHKeyPairVO in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method resetVMSSHKey.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_RESETSSHKEY, eventDescription = "resetting Vm SSHKey", async = true)
public UserVm resetVMSSHKey(final ResetVMSSHKeyCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException {
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId());
    final Long vmId = cmd.getId();
    final UserVmVO userVm = _vmDao.findById(cmd.getId());
    if (userVm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine by id" + cmd.getId());
    }
    _vmDao.loadDetails(userVm);
    final VMTemplateVO template = _templateDao.findByIdIncludingRemoved(userVm.getTemplateId());
    if (userVm.getState() == State.Error || userVm.getState() == State.Expunging) {
        s_logger.error("vm is not in the right state: " + vmId);
        throw new InvalidParameterValueException("Vm with specified id is not in the right state");
    }
    if (userVm.getState() != State.Stopped) {
        s_logger.error("vm is not in the right state: " + vmId);
        throw new InvalidParameterValueException("Vm " + userVm + " should be stopped to do SSH Key reset");
    }
    final SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
    if (s == null) {
        throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
    }
    _accountMgr.checkAccess(caller, null, true, userVm);
    String password = null;
    final String sshPublicKey = s.getPublicKey();
    if (template != null && template.getEnablePassword()) {
        password = _mgr.generateRandomPassword();
    }
    final boolean result = resetVMSSHKeyInternal(vmId, sshPublicKey, password);
    if (result) {
        userVm.setDetail("SSH.PublicKey", sshPublicKey);
        if (template != null && template.getEnablePassword()) {
            userVm.setPassword(password);
            // update the encrypted password in vm_details table too
            encryptAndStorePassword(userVm, password);
        }
        _vmDao.saveDetails(userVm);
    } else {
        throw new CloudRuntimeException("Failed to reset SSH Key for the virtual machine ");
    }
    return userVm;
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

SSHKeyPairVO (com.cloud.user.SSHKeyPairVO)16 Account (com.cloud.user.Account)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 Account (com.cloud.legacymodel.user.Account)4 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 ArrayList (java.util.ArrayList)3 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)2 DomainVO (com.cloud.domain.DomainVO)2 ActionEvent (com.cloud.event.ActionEvent)2 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)2 Filter (com.cloud.utils.db.Filter)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 SSHKeysHelper (com.cloud.utils.ssh.SSHKeysHelper)2 List (java.util.List)2 DataCenter (com.cloud.dc.DataCenter)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 KubernetesSupportedVersion (com.cloud.kubernetes.version.KubernetesSupportedVersion)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)1