Search in sources :

Example 66 with Account

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

the class TemplateAdapterBase method prepare.

@Override
public TemplateProfile prepare(final GetUploadParamsForTemplateCmd cmd) throws ResourceAllocationException {
    // check if the caller can operate with the template owner
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
    _accountMgr.checkAccess(caller, null, true, owner);
    final boolean isRouting = (cmd.isRoutingType() == null) ? false : cmd.isRoutingType();
    Long zoneId = cmd.getZoneId();
    // ignore passed zoneId if we are using region wide image store
    final List<ImageStoreVO> stores = _imgStoreDao.findRegionImageStores();
    if (stores != null && stores.size() > 0) {
        zoneId = -1L;
    }
    return prepare(false, CallContext.current().getCallingUserId(), cmd.getName(), cmd.getDisplayText(), cmd.getBits(), cmd.isPasswordEnabled(), cmd.getRequiresHvm(), null, cmd.isPublic(), cmd.isFeatured(), cmd.isExtractable(), cmd.getFormat(), cmd.getOsTypeId(), zoneId, HypervisorType.getType(cmd.getHypervisor()), cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled(), null, cmd.isDynamicallyScalable(), isRouting ? TemplateType.ROUTING : TemplateType.USER);
}
Also used : Account(com.cloud.user.Account) ImageStoreVO(com.cloud.storage.datastore.db.ImageStoreVO)

Example 67 with Account

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

the class UserVmManagerImpl method createVmGroup.

@DB
private InstanceGroupVO createVmGroup(final String groupName, final long accountId) {
    Account account = null;
    try {
        // to ensure
        account = _accountDao.acquireInLockTable(accountId);
        // created.
        if (account == null) {
            s_logger.warn("Failed to acquire lock on account");
            return null;
        }
        InstanceGroupVO group = _vmGroupDao.findByAccountAndName(accountId, groupName);
        if (group == null) {
            group = new InstanceGroupVO(groupName, accountId);
            group = _vmGroupDao.persist(group);
        }
        return group;
    } finally {
        if (account != null) {
            _accountDao.releaseFromLockTable(accountId);
        }
    }
}
Also used : Account(com.cloud.user.Account) DB(com.cloud.utils.db.DB)

Example 68 with Account

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

the class UserVmManagerImpl method recoverVirtualMachine.

@Override
@DB
public UserVm recoverVirtualMachine(final RecoverVMCmd cmd) throws ResourceAllocationException, CloudRuntimeException {
    final Long vmId = cmd.getId();
    final Account caller = CallContext.current().getCallingAccount();
    final Long userId = caller.getAccountId();
    // Verify input parameters
    final UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    // When trying to expunge, permission is denied when the caller is not an admin and the AllowUserExpungeRecoverVm is false for the caller.
    if (!_accountMgr.isAdmin(userId) && !AllowUserExpungeRecoverVm.valueIn(userId)) {
        throw new PermissionDeniedException("Recovering a vm can only be done by an Admin. Or when the allow.user.expunge.recover.vm key is set.");
    }
    if (vm.getRemoved() != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Unable to find vm or vm is removed: " + vmId);
        }
        throw new InvalidParameterValueException("Unable to find vm by id " + vmId);
    }
    if (vm.getState() != State.Destroyed) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("vm is not in the right state: " + vmId);
        }
        throw new InvalidParameterValueException("Vm with id " + vmId + " is not in the right state");
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Recovering vm " + vmId);
    }
    Transaction.execute(new TransactionCallbackWithExceptionNoReturn<ResourceAllocationException>() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) throws ResourceAllocationException {
            final Account account = _accountDao.lockRow(vm.getAccountId(), true);
            // if the account is deleted, throw error
            if (account.getRemoved() != null) {
                throw new CloudRuntimeException("Unable to recover VM as the account is deleted");
            }
            // Get serviceOffering for Virtual Machine
            final ServiceOfferingVO serviceOffering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
            // First check that the maximum number of UserVMs, CPU and Memory limit for the given
            // accountId will not be exceeded
            resourceLimitCheck(account, vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
            _haMgr.cancelDestroy(vm, vm.getHostId());
            try {
                if (!_itMgr.stateTransitTo(vm, VirtualMachine.Event.RecoveryRequested, null)) {
                    s_logger.debug("Unable to recover the vm because it is not in the correct state: " + vmId);
                    throw new InvalidParameterValueException("Unable to recover the vm because it is not in the correct state: " + vmId);
                }
            } catch (final NoTransitionException e) {
                throw new InvalidParameterValueException("Unable to recover the vm because it is not in the correct state: " + vmId);
            }
            // Recover the VM's disks
            final List<VolumeVO> volumes = _volsDao.findByInstance(vmId);
            for (final VolumeVO volume : volumes) {
                if (volume.getVolumeType().equals(Volume.Type.ROOT)) {
                    // Create an event
                    final Long templateId = volume.getTemplateId();
                    final Long diskOfferingId = volume.getDiskOfferingId();
                    Long offeringId = null;
                    if (diskOfferingId != null) {
                        final DiskOfferingVO offering = _diskOfferingDao.findById(diskOfferingId);
                        if (offering != null && offering.getType() == DiskOfferingVO.Type.Disk) {
                            offeringId = offering.getId();
                        }
                    }
                }
            }
            // Update Resource Count for the given account
            resourceCountIncrement(account.getId(), vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
        }
    });
    return _vmDao.findById(vmId);
}
Also used : Account(com.cloud.user.Account) TransactionStatus(com.cloud.utils.db.TransactionStatus) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) DB(com.cloud.utils.db.DB)

Example 69 with Account

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

the class UserVmManagerImpl method rebootVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_REBOOT, eventDescription = "rebooting Vm", async = true)
public UserVm rebootVirtualMachine(final RebootVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException {
    final Account caller = CallContext.current().getCallingAccount();
    final Long vmId = cmd.getId();
    // Verify input parameters
    final UserVmVO vmInstance = _vmDao.findById(vmId);
    if (vmInstance == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _accountMgr.checkAccess(caller, null, true, vmInstance);
    // If the VM is Volatile in nature, on reboot discard the VM's root disk and create a new root disk for it: by calling restoreVM
    final long serviceOfferingId = vmInstance.getServiceOfferingId();
    final ServiceOfferingVO offering = _serviceOfferingDao.findById(vmInstance.getId(), serviceOfferingId);
    if (offering != null && offering.getRemoved() == null) {
        if (offering.getVolatileVm()) {
            return restoreVMInternal(caller, vmInstance, null);
        }
    } else {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId + " corresponding to the vm");
    }
    return rebootVirtualMachine(CallContext.current().getCallingUserId(), vmId);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ActionEvent(com.cloud.event.ActionEvent)

Example 70 with Account

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

the class UserVmManagerImpl method updateVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_UPDATE, eventDescription = "updating Vm")
public UserVm updateVirtualMachine(final UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException {
    final String displayName = cmd.getDisplayName();
    final String group = cmd.getGroup();
    final Boolean ha = cmd.getHaEnable();
    final Boolean isDisplayVm = cmd.getDisplayVm();
    final Long id = cmd.getId();
    final Long osTypeId = cmd.getOsTypeId();
    final String userData = cmd.getUserData();
    final Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
    final String hostName = cmd.getHostName();
    final Map<String, String> details = cmd.getDetails();
    final Account caller = CallContext.current().getCallingAccount();
    // Input validation and permission checks
    final UserVmVO vmInstance = _vmDao.findById(id);
    if (vmInstance == null) {
        throw new InvalidParameterValueException("unable to find virtual machine with id " + id);
    }
    _accountMgr.checkAccess(caller, null, true, vmInstance);
    // If the flag is specified and is changed
    if (isDisplayVm != null && isDisplayVm != vmInstance.isDisplayVm()) {
        // update vm
        vmInstance.setDisplayVm(isDisplayVm);
        // Resource limit changes
        final ServiceOffering offering = _serviceOfferingDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId());
        _resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.user_vm, isDisplayVm);
        _resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.cpu, isDisplayVm, new Long(offering.getCpu()));
        _resourceLimitMgr.changeResourceCount(vmInstance.getAccountId(), ResourceType.memory, isDisplayVm, new Long(offering.getRamSize()));
        // take care of the root volume as well.
        final List<VolumeVO> rootVols = _volsDao.findByInstanceAndType(id, Volume.Type.ROOT);
        if (!rootVols.isEmpty()) {
            _volumeService.updateDisplay(rootVols.get(0), isDisplayVm);
        }
        // take care of the data volumes as well.
        final List<VolumeVO> dataVols = _volsDao.findByInstanceAndType(id, Volume.Type.DATADISK);
        for (final Volume dataVol : dataVols) {
            _volumeService.updateDisplay(dataVol, isDisplayVm);
        }
    }
    if (details != null && !details.isEmpty()) {
        _vmDao.loadDetails(vmInstance);
        for (final Map.Entry<String, String> entry : details.entrySet()) {
            if (entry instanceof Map.Entry) {
                vmInstance.setDetail(entry.getKey(), entry.getValue());
            }
        }
        _vmDao.saveDetails(vmInstance);
    }
    return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod(), cmd.getCustomId(), hostName, cmd.getInstanceName());
}
Also used : Account(com.cloud.user.Account) ServiceOffering(com.cloud.offering.ServiceOffering) Entry(java.util.Map.Entry) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Volume(com.cloud.storage.Volume) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

Account (com.cloud.user.Account)1088 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)308 ArrayList (java.util.ArrayList)293 ActionEvent (com.cloud.event.ActionEvent)243 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)216 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)207 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)170 User (com.cloud.user.User)149 List (java.util.List)147 DB (com.cloud.utils.db.DB)130 Test (org.junit.Test)123 Pair (com.cloud.utils.Pair)115 AccountVO (com.cloud.user.AccountVO)113 Network (com.cloud.network.Network)104 Filter (com.cloud.utils.db.Filter)103 TransactionStatus (com.cloud.utils.db.TransactionStatus)95 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)94 DomainVO (com.cloud.domain.DomainVO)91 Domain (com.cloud.domain.Domain)87 UserVO (com.cloud.user.UserVO)86