Search in sources :

Example 51 with Account

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

the class Site2SiteVpnManagerImpl method deleteVpnConnection.

@Override
@ActionEvent(eventType = EventTypes.EVENT_S2S_VPN_CONNECTION_DELETE, eventDescription = "deleting s2s vpn connection", create = true)
public boolean deleteVpnConnection(final DeleteVpnConnectionCmd cmd) throws ResourceUnavailableException {
    CallContext.current().setEventDetails(" Id: " + cmd.getId());
    final Account caller = CallContext.current().getCallingAccount();
    final Long id = cmd.getId();
    final Site2SiteVpnConnectionVO conn = _vpnConnectionDao.findById(id);
    if (conn == null) {
        throw new InvalidParameterValueException("Fail to find site to site VPN connection " + id + " to delete!");
    }
    _accountMgr.checkAccess(caller, null, false, conn);
    stopVpnConnection(id);
    _vpnConnectionDao.remove(id);
    return true;
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Site2SiteVpnConnectionVO(com.cloud.network.dao.Site2SiteVpnConnectionVO) ActionEvent(com.cloud.event.ActionEvent)

Example 52 with Account

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

the class ManagementServerImpl method deleteSSHKeyPair.

@Override
public boolean deleteSSHKeyPair(final DeleteSSHKeyPairCmd 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) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
        final DomainVO domain = ApiDBUtils.findDomainById(owner.getDomainId());
        String domainUuid = String.valueOf(owner.getDomainId());
        if (domain != null) {
            domainUuid = domain.getUuid();
        }
        ex.addProxyObject(domainUuid, "domainId");
        throw ex;
    }
    return _sshKeyPairDao.deleteByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
}
Also used : Account(com.cloud.legacymodel.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO)

Example 53 with Account

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

the class ManagementServerImpl method listHostsForMigrationOfVM.

@Override
public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>> listHostsForMigrationOfVM(final Long vmId, final Long startIndex, final Long pageSize, final String keyword) {
    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 VM");
        }
        throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!");
    }
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    if (vm == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the VM with given id");
        throw ex;
    }
    if (vm.getState() != State.Running) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is not running, cannot migrate the vm" + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Running, cannot " + "migrate the vm with specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (_serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.pciDevice.toString()) != null) {
        s_logger.info(" Live Migration of GPU enabled VM : " + vm.getInstanceName() + " is not supported");
        // Return empty list.
        return new Ternary<>(new Pair<>(new ArrayList<HostVO>(), new Integer(0)), new ArrayList<>(), new HashMap<>());
    }
    if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.KVM)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(vm + " is not XenServer/KVM, cannot migrate this VM.");
        }
        throw new InvalidParameterValueException("Unsupported Hypervisor Type for VM migration, we support " + "XenServer/KVM only");
    }
    final long srcHostId = vm.getHostId();
    final Host srcHost = _hostDao.findById(srcHostId);
    if (srcHost == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Unable to find the host with id: " + srcHostId + " of this VM:" + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the host (with specified id) of VM with specified id");
        ex.addProxyObject(String.valueOf(srcHostId), "hostId");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    // Check if the vm can be migrated with storage.
    boolean canMigrateWithStorage = false;
    if (vm.getType() == VirtualMachineType.User) {
        final HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
        if (capabilities != null) {
            canMigrateWithStorage = capabilities.isStorageMotionSupported();
        }
    }
    // Check if the vm is using any disks on local storage.
    final VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm, null, _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()), null, null);
    final List<VolumeVO> volumes = _volumeDao.findCreatedByInstance(vmProfile.getId());
    boolean usesLocal = false;
    for (final VolumeVO volume : volumes) {
        final DiskOfferingVO diskOffering = _diskOfferingDao.findByIdIncludingRemoved(volume.getDiskOfferingId());
        final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, vmProfile.getHypervisorType());
        if (diskProfile.useLocalStorage()) {
            usesLocal = true;
            break;
        }
    }
    if (!canMigrateWithStorage && usesLocal) {
        throw new InvalidParameterValueException("Unsupported operation, VM uses Local storage, cannot migrate");
    }
    final HostType hostType = srcHost.getType();
    final Pair<List<HostVO>, Integer> allHostsPair;
    final List<HostVO> allHosts;
    final Map<Host, Boolean> requiresStorageMotion = new HashMap<>();
    final DataCenterDeployment plan;
    if (canMigrateWithStorage) {
        allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, srcHost.getDataCenterId(), null, null, null, keyword, null, null, srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
        allHosts = allHostsPair.first();
        allHosts.remove(srcHost);
        for (final VolumeVO volume : volumes) {
            final Long volClusterId = _poolDao.findById(volume.getPoolId()).getClusterId();
            // only check for volume which are not in zone wide primary store, as only those may require storage motion
            if (volClusterId != null) {
                for (final Iterator<HostVO> iterator = allHosts.iterator(); iterator.hasNext(); ) {
                    final Host host = iterator.next();
                    if (!host.getClusterId().equals(volClusterId) || usesLocal) {
                        if (hasSuitablePoolsForVolume(volume, host, vmProfile)) {
                            requiresStorageMotion.put(host, true);
                        } else {
                            iterator.remove();
                        }
                    }
                }
            }
        }
        plan = new DataCenterDeployment(srcHost.getDataCenterId(), null, null, null, null, null);
    } else {
        final Long cluster = srcHost.getClusterId();
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Searching for all hosts in cluster " + cluster + " for migrating VM " + vm);
        }
        allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, keyword, null, null, null, null);
        // Filter out the current host.
        allHosts = allHostsPair.first();
        allHosts.remove(srcHost);
        plan = new DataCenterDeployment(srcHost.getDataCenterId(), srcHost.getPodId(), srcHost.getClusterId(), null, null, null);
    }
    final Pair<List<? extends Host>, Integer> otherHosts = new Pair<>(allHosts, new Integer(allHosts.size()));
    List<Host> suitableHosts = new ArrayList<>();
    final ExcludeList excludes = new ExcludeList();
    excludes.addHost(srcHostId);
    // call affinitygroup chain
    final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId());
    if (vmGroupCount > 0) {
        for (final AffinityGroupProcessor processor : _affinityProcessors) {
            processor.process(vmProfile, plan, excludes);
        }
    }
    final Account account = vmProfile.getOwner();
    for (final HostVO host : allHosts) {
        final DedicatedResourceVO dedicatedResourceVO = dedicatedResourceDao.findByHostId(host.getId());
        if (dedicatedResourceVO != null && dedicatedResourceVO.getDomainId() != account.getDomainId()) {
            final Domain domain = _domainDao.findById(dedicatedResourceVO.getDomainId());
            if (domain != null) {
                s_logger.debug("Host " + host.getName() + " is dedicated to domain " + domain.getName() + " so not suitable for migration for VM " + vmProfile.getInstanceName());
            }
            excludes.addHost(host.getId());
        }
    }
    for (final HostAllocator allocator : hostAllocators) {
        if (canMigrateWithStorage) {
            suitableHosts = allocator.allocateTo(vmProfile, plan, HostType.Routing, excludes, allHosts, HostAllocator.RETURN_UPTO_ALL, false);
        } else {
            suitableHosts = allocator.allocateTo(vmProfile, plan, HostType.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false);
        }
        if (suitableHosts != null && !suitableHosts.isEmpty()) {
            break;
        }
    }
    if (s_logger.isDebugEnabled()) {
        if (suitableHosts.isEmpty()) {
            s_logger.debug("No suitable hosts found");
        } else {
            s_logger.debug("Hosts having capacity and suitable for migration: " + suitableHosts);
        }
    }
    return new Ternary<>(otherHosts, suitableHosts, requiresStorageMotion);
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) Account(com.cloud.legacymodel.user.Account) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HostAllocator(com.cloud.agent.manager.allocator.HostAllocator) HostType(com.cloud.model.enumeration.HostType) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) AffinityGroupProcessor(com.cloud.affinity.AffinityGroupProcessor) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) Ternary(com.cloud.legacymodel.utils.Ternary) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.legacymodel.dc.Host) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) HostVO(com.cloud.host.HostVO) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Domain(com.cloud.legacymodel.domain.Domain)

Example 54 with Account

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

the class ManagementServerImpl method getCloudIdentifierResponse.

@Override
public ArrayList<String> getCloudIdentifierResponse(final long userId) {
    final Account caller = getCaller();
    // verify that user exists
    User user = _accountMgr.getUserIncludingRemoved(userId);
    if (user == null || user.getRemoved() != null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find active user of specified id");
        ex.addProxyObject(String.valueOf(userId), "userId");
        throw ex;
    }
    // check permissions
    _accountMgr.checkAccess(caller, null, true, _accountMgr.getAccount(user.getAccountId()));
    String cloudIdentifier = _configDao.getValue("cloud.identifier");
    if (cloudIdentifier == null) {
        cloudIdentifier = "";
    }
    String signature = "";
    try {
        // get the user obj to get his secret key
        user = _accountMgr.getActiveUser(userId);
        final String secretKey = user.getSecretKey();
        final String input = cloudIdentifier;
        signature = signRequest(input, secretKey);
    } catch (final Exception e) {
        s_logger.warn("Exception whilst creating a signature:" + e);
    }
    final ArrayList<String> cloudParams = new ArrayList<>();
    cloudParams.add(cloudIdentifier);
    cloudParams.add(signature);
    return cloudParams;
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ArrayList(java.util.ArrayList) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException)

Example 55 with Account

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

the class ManagementServerImpl method upgradeStoppedSystemVm.

private VirtualMachine upgradeStoppedSystemVm(final Long systemVmId, final Long serviceOfferingId, final Map<String, String> customparameters) {
    final Account caller = getCaller();
    final VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(systemVmId, VirtualMachineType.ConsoleProxy, VirtualMachineType.SecondaryStorageVm);
    if (systemVm == null) {
        throw new InvalidParameterValueException("Unable to find SystemVm with id " + systemVmId);
    }
    _accountMgr.checkAccess(caller, null, true, systemVm);
    // Check that the specified service offering ID is valid
    ServiceOfferingVO newServiceOffering = _offeringDao.findById(serviceOfferingId);
    final ServiceOfferingVO currentServiceOffering = _offeringDao.findById(systemVmId, systemVm.getServiceOfferingId());
    _itMgr.checkIfCanUpgrade(systemVm, newServiceOffering);
    final boolean result = _itMgr.upgradeVmDb(systemVmId, serviceOfferingId);
    if (result) {
        return _vmInstanceDao.findById(systemVmId);
    } else {
        throw new CloudRuntimeException("Unable to upgrade system vm " + systemVm);
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VMInstanceVO(com.cloud.vm.VMInstanceVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

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