Search in sources :

Example 56 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method getVMPassword.

@Override
public String getVMPassword(final GetVMPasswordCmd cmd) {
    final Account caller = getCaller();
    final UserVmVO vm = _userVmDao.findById(cmd.getId());
    if (vm == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("No VM with specified id found.");
        ex.addProxyObject(cmd.getId().toString(), "vmId");
        throw ex;
    }
    // make permission check
    _accountMgr.checkAccess(caller, null, true, vm);
    _userVmDao.loadDetails(vm);
    final String password = vm.getDetail("Encrypted.Password");
    if (password == null || password.equals("")) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("No password for VM with specified id found. " + "If VM is created from password enabled template and SSH keypair is assigned to VM then only password can be retrieved.");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    return password;
}
Also used : Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException)

Example 57 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method updateDomain.

@Override
@DB
public DomainVO updateDomain(final UpdateDomainCmd cmd) {
    final Long domainId = cmd.getId();
    final String domainName = cmd.getDomainName();
    final String networkDomain = cmd.getNetworkDomain();
    // check if domain exists in the system
    final DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
        ex.addProxyObject(domainId.toString(), "domainId");
        throw ex;
    } else if (domain.getParent() == null && domainName != null) {
        // name
        throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
    }
    final Account caller = getCaller();
    _accountMgr.checkAccess(caller, domain);
    // domain name is unique under the parent domain
    if (domainName != null) {
        final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("name", SearchCriteria.Op.EQ, domainName);
        sc.addAnd("parent", SearchCriteria.Op.EQ, domain.getParent());
        final List<DomainVO> domains = _domainDao.search(sc, null);
        final boolean sameDomain = domains.size() == 1 && domains.get(0).getId() == domainId;
        if (!domains.isEmpty() && !sameDomain) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
            ex.addProxyObject(domain.getUuid(), "domainId");
            throw ex;
        }
    }
    // validate network domain
    if (networkDomain != null && !networkDomain.isEmpty()) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters " + "'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (domainName != null) {
                final String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName);
                updateDomainChildren(domain, updatedDomainPath);
                domain.setName(domainName);
                domain.setPath(updatedDomainPath);
            }
            if (networkDomain != null) {
                if (networkDomain.isEmpty()) {
                    domain.setNetworkDomain(null);
                } else {
                    domain.setNetworkDomain(networkDomain);
                }
            }
            _domainDao.update(domainId, domain);
        }
    });
    return _domainDao.findById(domainId);
}
Also used : DomainVO(com.cloud.domain.DomainVO) Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 58 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method createSSHKeyPair.

@Override
public SSHKeyPair createSSHKeyPair(final CreateSSHKeyPairCmd cmd) {
    final Account caller = getCaller();
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    final Long projectId = cmd.getProjectId();
    final Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, projectId);
    final SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
    if (s != null) {
        throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' already exists.");
    }
    final SSHKeysHelper keys = new SSHKeysHelper();
    final String name = cmd.getName();
    final String publicKey = keys.getPublicKey();
    final String fingerprint = keys.getPublicKeyFingerPrint();
    final String privateKey = keys.getPrivateKey();
    return createAndSaveSSHKeyPair(name, fingerprint, publicKey, privateKey, owner);
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) SSHKeysHelper(com.cloud.utils.ssh.SSHKeysHelper) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO)

Example 59 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method listStoragePoolsForMigrationOfVolume.

@Override
public Pair<List<? extends StoragePool>, List<? extends StoragePool>> listStoragePoolsForMigrationOfVolume(final Long volumeId) {
    final Account caller = getCaller();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the volume");
        }
        throw new PermissionDeniedException("No permission to migrate volume, only root admin can migrate a volume");
    }
    final VolumeVO volume = _volumeDao.findById(volumeId);
    if (volume == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find volume with" + " specified id.");
        ex.addProxyObject(volumeId.toString(), "volumeId");
        throw ex;
    }
    // Volume must be attached to an instance for live migration.
    final List<StoragePool> allPools = new ArrayList<>();
    final List<StoragePool> suitablePools = new ArrayList<>();
    // Volume must be in Ready state to be migrated.
    if (!Volume.State.Ready.equals(volume.getState())) {
        s_logger.info("Volume " + volume + " must be in ready state for migration.");
        return new Pair<>(allPools, suitablePools);
    }
    if (!_volumeMgr.volumeOnSharedStoragePool(volume)) {
        s_logger.info("Volume " + volume + " is on local storage. It cannot be migrated to another pool.");
        return new Pair<>(allPools, suitablePools);
    }
    final Long instanceId = volume.getInstanceId();
    VMInstanceVO vm = null;
    if (instanceId != null) {
        vm = _vmInstanceDao.findById(instanceId);
    }
    if (vm == null) {
        s_logger.info("Volume " + volume + " isn't attached to any vm. Looking for storage pools in the " + "zone to which this volumes can be migrated.");
    } else if (vm.getState() != State.Running) {
        s_logger.info("Volume " + volume + " isn't attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
    } else {
        s_logger.info("Volume " + volume + " is attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
        boolean storageMotionSupported = false;
        // Check if the underlying hypervisor supports storage motion.
        final Long hostId = vm.getHostId();
        if (hostId != null) {
            final HostVO host = _hostDao.findById(hostId);
            HypervisorCapabilitiesVO capabilities = null;
            if (host != null) {
                capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(host.getHypervisorType(), host.getHypervisorVersion());
            } else {
                s_logger.error("Details of the host on which the vm " + vm + ", to which volume " + volume + " is " + "attached, couldn't be retrieved.");
            }
            if (capabilities != null) {
                storageMotionSupported = capabilities.isStorageMotionSupported();
            } else {
                s_logger.error("Capabilities for host " + host + " couldn't be retrieved.");
            }
        }
        if (!storageMotionSupported) {
            s_logger.info("Volume " + volume + " is attached to a running vm and the hypervisor doesn't support" + " storage motion.");
            return new Pair<>(allPools, suitablePools);
        }
    }
    // Source pool of the volume.
    final StoragePoolVO srcVolumePool = _poolDao.findById(volume.getPoolId());
    // Get all the pools available. Only shared pools are considered because only a volume on a shared pools
    // can be live migrated while the virtual machine stays on the same host.
    final List<StoragePoolVO> storagePools;
    if (srcVolumePool.getClusterId() == null) {
        storagePools = _poolDao.findZoneWideStoragePoolsByTags(volume.getDataCenterId(), null);
    } else {
        storagePools = _poolDao.findPoolsByTags(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null);
    }
    storagePools.remove(srcVolumePool);
    for (final StoragePoolVO pool : storagePools) {
        if (pool.isShared()) {
            allPools.add((StoragePool) dataStoreMgr.getPrimaryDataStore(pool.getId()));
        }
    }
    // Get all the suitable pools.
    // Exclude the current pool from the list of pools to which the volume can be migrated.
    final ExcludeList avoid = new ExcludeList();
    avoid.addPool(srcVolumePool.getId());
    // Volume stays in the same cluster after migration.
    final DataCenterDeployment plan = new DataCenterDeployment(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null, null, null);
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
    final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
    // Call the storage pool allocator to find the list of storage pools.
    for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
        final List<StoragePool> pools = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
        if (pools != null && !pools.isEmpty()) {
            suitablePools.addAll(pools);
            break;
        }
    }
    return new Pair<>(allPools, suitablePools);
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.legacymodel.user.Account) StoragePool(com.cloud.legacymodel.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) HostVO(com.cloud.host.HostVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) StoragePoolAllocator(com.cloud.engine.subsystem.api.storage.StoragePoolAllocator) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair)

Example 60 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method updateVmGroup.

@Override
public InstanceGroupVO updateVmGroup(final UpdateVMGroupCmd cmd) {
    final Account caller = getCaller();
    final Long groupId = cmd.getId();
    final String groupName = cmd.getGroupName();
    // Verify input parameters
    final InstanceGroupVO group = _vmGroupDao.findById(groupId.longValue());
    if (group == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a vm group with specified groupId");
        ex.addProxyObject(groupId.toString(), "groupId");
        throw ex;
    }
    _accountMgr.checkAccess(caller, null, true, group);
    // Check if name is already in use by this account (exclude this group)
    final boolean isNameInUse = _vmGroupDao.isNameInUse(group.getAccountId(), groupName);
    if (isNameInUse && !group.getName().equals(groupName)) {
        throw new InvalidParameterValueException("Unable to update vm group, a group with name " + groupName + " already exists for account");
    }
    if (groupName != null) {
        _vmGroupDao.updateVmGroup(groupId, groupName);
    }
    return _vmGroupDao.findById(groupId);
}
Also used : Account(com.cloud.legacymodel.user.Account) InstanceGroupVO(com.cloud.vm.InstanceGroupVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException)

Aggregations

Account (com.cloud.legacymodel.user.Account)435 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)229 ActionEvent (com.cloud.event.ActionEvent)120 ArrayList (java.util.ArrayList)103 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)98 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)78 User (com.cloud.legacymodel.user.User)73 DB (com.cloud.utils.db.DB)59 List (java.util.List)58 Pair (com.cloud.legacymodel.utils.Pair)53 Network (com.cloud.legacymodel.network.Network)48 CallContext (com.cloud.context.CallContext)47 DomainVO (com.cloud.domain.DomainVO)47 UserAccount (com.cloud.legacymodel.user.UserAccount)47 Filter (com.cloud.utils.db.Filter)47 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 Domain (com.cloud.legacymodel.domain.Domain)39 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)37 Test (org.junit.Test)36 Ternary (com.cloud.legacymodel.utils.Ternary)34