Search in sources :

Example 16 with Account

use of com.cloud.user.Account in project CloudStack-archive by CloudStack-extras.

the class UsageManagerImpl method createVolumeHelperEvent.

private void createVolumeHelperEvent(UsageEventVO event) {
    Long doId = -1L;
    long zoneId = -1L;
    Long templateId = -1L;
    long size = -1L;
    long volId = event.getResourceId();
    if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
        doId = event.getOfferingId();
        zoneId = event.getZoneId();
        templateId = event.getTemplateId();
        size = event.getSize();
    }
    if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
        SearchCriteria<UsageVolumeVO> sc = m_usageVolumeDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, volId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageVolumeVO> volumesVOs = m_usageVolumeDao.search(sc, null);
        if (volumesVOs.size() > 0) {
            //This is a safeguard to avoid double counting of volumes.
            s_logger.error("Found duplicate usage entry for volume: " + volId + " assigned to account: " + event.getAccountId() + "; marking as deleted...");
        }
        for (UsageVolumeVO volumesVO : volumesVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting volume: " + volumesVO.getId() + " from account: " + volumesVO.getAccountId());
            }
            volumesVO.setDeleted(event.getCreateDate());
            m_usageVolumeDao.update(volumesVO);
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("create volume with id : " + volId + " for account: " + event.getAccountId());
        }
        Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageVolumeVO volumeVO = new UsageVolumeVO(volId, zoneId, event.getAccountId(), acct.getDomainId(), doId, templateId, size, event.getCreateDate(), null);
        m_usageVolumeDao.persist(volumeVO);
    } else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
        SearchCriteria<UsageVolumeVO> sc = m_usageVolumeDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, volId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageVolumeVO> volumesVOs = m_usageVolumeDao.search(sc, null);
        if (volumesVOs.size() > 1) {
            s_logger.warn("More that one usage entry for volume: " + volId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageVolumeVO volumesVO : volumesVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting volume: " + volumesVO.getId() + " from account: " + volumesVO.getAccountId());
            }
            // there really shouldn't be more than one
            volumesVO.setDeleted(event.getCreateDate());
            m_usageVolumeDao.update(volumesVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 17 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class CreateAutoScaleVmProfileCmd method getAccountId.

public long getAccountId() {
    if (accountId != null) {
        return accountId;
    }
    Account account = null;
    if (autoscaleUserId != null) {
        User user = _entityMgr.findById(User.class, autoscaleUserId);
        account = _entityMgr.findById(Account.class, user.getAccountId());
    } else {
        account = CallContext.current().getCallingAccount();
    }
    accountId = account.getAccountId();
    domainId = account.getDomainId();
    return accountId;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User)

Example 18 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class CreateAffinityGroupCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Account caller = CallContext.current().getCallingAccount();
    //For domain wide affinity groups (if the affinity group processor type allows it)
    if (projectId == null && domainId != null && accountName == null && _accountService.isRootAdmin(caller.getId())) {
        return Account.ACCOUNT_ID_SYSTEM;
    }
    Account owner = _accountService.finalizeOwner(caller, accountName, domainId, projectId);
    if (owner == null) {
        return caller.getAccountId();
    }
    return owner.getAccountId();
}
Also used : Account(com.cloud.user.Account)

Example 19 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class DeleteAffinityGroupCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Account caller = CallContext.current().getCallingAccount();
    //For domain wide affinity groups (if the affinity group processor type allows it)
    if (projectId == null && domainId != null && accountName == null && _accountService.isRootAdmin(caller.getId())) {
        return Account.ACCOUNT_ID_SYSTEM;
    }
    Account owner = _accountService.finalizeOwner(caller, accountName, domainId, projectId);
    if (owner == null) {
        return caller.getAccountId();
    }
    return owner.getAccountId();
}
Also used : Account(com.cloud.user.Account)

Example 20 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class QuotaServiceImpl method findQuotaBalanceVO.

@Override
public List<QuotaBalanceVO> findQuotaBalanceVO(Long accountId, String accountName, Long domainId, Date startDate, Date endDate) {
    if ((accountId == null) && (accountName != null) && (domainId != null)) {
        Account userAccount = null;
        Account caller = CallContext.current().getCallingAccount();
        if (_domainDao.isChildDomain(caller.getDomainId(), domainId)) {
            Filter filter = new Filter(AccountVO.class, "id", Boolean.FALSE, null, null);
            List<AccountVO> accounts = _accountDao.listAccounts(accountName, domainId, filter);
            if (!accounts.isEmpty()) {
                userAccount = accounts.get(0);
            }
            if (userAccount != null) {
                accountId = userAccount.getId();
            } else {
                throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
            }
        } else {
            throw new PermissionDeniedException("Invalid Domain Id or Account");
        }
    }
    startDate = startDate == null ? new Date() : startDate;
    if (endDate == null) {
        // adjust start date to end of day as there is no end date
        Date adjustedStartDate = computeAdjustedTime(_respBldr.startOfNextDay(startDate));
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("getQuotaBalance1: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", on or before " + adjustedStartDate);
        }
        List<QuotaBalanceVO> qbrecords = _quotaBalanceDao.lastQuotaBalanceVO(accountId, domainId, adjustedStartDate);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Found records size=" + qbrecords.size());
        }
        if (qbrecords.isEmpty()) {
            s_logger.info("Incorrect Date there are no quota records before this date " + adjustedStartDate);
            return qbrecords;
        } else {
            return qbrecords;
        }
    } else {
        Date adjustedStartDate = computeAdjustedTime(startDate);
        if (endDate.after(_respBldr.startOfNextDay())) {
            throw new InvalidParameterValueException("Incorrect Date Range. End date:" + endDate + " should not be in future. ");
        } else if (startDate.before(endDate)) {
            Date adjustedEndDate = computeAdjustedTime(endDate);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("getQuotaBalance2: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate);
            }
            List<QuotaBalanceVO> qbrecords = _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("getQuotaBalance3: Found records size=" + qbrecords.size());
            }
            if (qbrecords.isEmpty()) {
                s_logger.info("There are no quota records between these dates start date " + adjustedStartDate + " and end date:" + endDate);
                return qbrecords;
            } else {
                return qbrecords;
            }
        } else {
            throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate);
        }
    }
}
Also used : Account(com.cloud.user.Account) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ArrayList(java.util.ArrayList) List(java.util.List) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date)

Aggregations

Account (com.cloud.user.Account)566 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)254 ArrayList (java.util.ArrayList)152 ActionEvent (com.cloud.event.ActionEvent)114 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)98 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)82 List (java.util.List)80 Test (org.junit.Test)73 User (com.cloud.user.User)66 AccountVO (com.cloud.user.AccountVO)64 DB (com.cloud.utils.db.DB)61 Network (com.cloud.network.Network)60 Pair (com.cloud.utils.Pair)52 DataCenter (com.cloud.dc.DataCenter)49 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)46 Filter (com.cloud.utils.db.Filter)46 CallContext (org.apache.cloudstack.context.CallContext)45 DomainVO (com.cloud.domain.DomainVO)44 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 NetworkVO (com.cloud.network.dao.NetworkVO)43