Search in sources :

Example 1 with AccountVO

use of com.cloud.user.AccountVO 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)

Example 2 with AccountVO

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

the class QuotaResponseBuilderImpl method addQuotaCredits.

@Override
public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Double amount, Long updatedBy, Boolean enforce) {
    Date despositedOn = _quotaService.computeAdjustedTime(new Date());
    QuotaBalanceVO qb = _quotaBalanceDao.findLaterBalanceEntry(accountId, domainId, despositedOn);
    if (qb != null) {
        throw new InvalidParameterValueException("Incorrect deposit date: " + despositedOn + " there are balance entries after this date");
    }
    QuotaCreditsVO credits = new QuotaCreditsVO(accountId, domainId, new BigDecimal(amount), updatedBy);
    credits.setUpdatedOn(despositedOn);
    QuotaCreditsVO result = _quotaCreditsDao.saveCredits(credits);
    final AccountVO account = _accountDao.findById(accountId);
    if (account == null) {
        throw new InvalidParameterValueException("Account does not exist with account id " + accountId);
    }
    final boolean lockAccountEnforcement = "true".equalsIgnoreCase(QuotaConfig.QuotaEnableEnforcement.value());
    final BigDecimal currentAccountBalance = _quotaBalanceDao.lastQuotaBalance(accountId, domainId, startOfNextDay(new Date(despositedOn.getTime())));
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("AddQuotaCredits: Depositing " + amount + " on adjusted date " + despositedOn + ", current balance " + currentAccountBalance);
    }
    // update quota account with the balance
    _quotaService.saveQuotaAccount(account, currentAccountBalance, despositedOn);
    if (lockAccountEnforcement) {
        if (currentAccountBalance.compareTo(new BigDecimal(0)) >= 0) {
            if (account.getState() == Account.State.locked) {
                s_logger.info("UnLocking account " + account.getAccountName() + " , due to positive balance " + currentAccountBalance);
                _accountMgr.enableAccount(account.getAccountName(), domainId, accountId);
            }
        } else {
            // currentAccountBalance < 0 then lock the account
            if (_quotaManager.isLockable(account) && account.getState() == Account.State.enabled && enforce) {
                s_logger.info("Locking account " + account.getAccountName() + " , due to negative balance " + currentAccountBalance);
                _accountMgr.lockAccount(account.getAccountName(), domainId, accountId);
            }
        }
    }
    String creditor = String.valueOf(Account.ACCOUNT_ID_SYSTEM);
    User creditorUser = _userDao.getUser(updatedBy);
    if (creditorUser != null) {
        creditor = creditorUser.getUsername();
    }
    QuotaCreditsResponse response = new QuotaCreditsResponse(result, creditor);
    response.setCurrency(QuotaConfig.QuotaCurrencySymbol.value());
    return response;
}
Also used : User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) QuotaBalanceVO(org.apache.cloudstack.quota.vo.QuotaBalanceVO) QuotaCreditsVO(org.apache.cloudstack.quota.vo.QuotaCreditsVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 3 with AccountVO

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

the class QuotaResponseBuilderImplTest method testAddQuotaCredits.

@Test
public void testAddQuotaCredits() {
    final long accountId = 2L;
    final long domainId = 1L;
    final double amount = 11.0;
    final long updatedBy = 2L;
    QuotaCreditsVO credit = new QuotaCreditsVO();
    credit.setCredit(new BigDecimal(amount));
    Mockito.when(quotaCreditsDao.saveCredits(Mockito.any(QuotaCreditsVO.class))).thenReturn(credit);
    Mockito.when(quotaBalanceDao.lastQuotaBalance(Mockito.anyLong(), Mockito.anyLong(), Mockito.any(Date.class))).thenReturn(new BigDecimal(111));
    Mockito.when(quotaService.computeAdjustedTime(Mockito.any(Date.class))).thenReturn(new Date());
    AccountVO account = new AccountVO();
    account.setState(Account.State.locked);
    Mockito.when(accountDao.findById(Mockito.anyLong())).thenReturn(account);
    QuotaCreditsResponse resp = quotaResponseBuilder.addQuotaCredits(accountId, domainId, amount, updatedBy, true);
    assertTrue(resp.getCredits().compareTo(credit.getCredit()) == 0);
}
Also used : QuotaCreditsVO(org.apache.cloudstack.quota.vo.QuotaCreditsVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 4 with AccountVO

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

the class VmwareDatacenterApiUnitTest method testSetUp.

@Before
public void testSetUp() {
    Mockito.when(_configDao.isPremium()).thenReturn(true);
    ComponentContext.initComponentsLifeCycle();
    MockitoAnnotations.initMocks(this);
    DataCenterVO zone = new DataCenterVO(UUID.randomUUID().toString(), "test", "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null, NetworkType.Basic, null, null, true, true, null, null);
    zoneId = 1L;
    HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), zoneId, "192.168.56.1", "192.168.56.0/24", 8, "test");
    podId = 1L;
    AccountVO acct = new AccountVO(200L);
    acct.setType(Account.ACCOUNT_TYPE_ADMIN);
    acct.setAccountName("admin");
    acct.setDomainId(domainId);
    UserVO user1 = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user1, acct);
    when(_accountDao.findByIdIncludingRemoved(0L)).thenReturn(acct);
    dc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, user, password);
    vmwareDcs = new ArrayList<VmwareDatacenterVO>();
    vmwareDcs.add(dc);
    vmwareDcId = dc.getId();
    cluster = new ClusterVO(zone.getId(), pod.getId(), "vmwarecluster");
    cluster.setHypervisorType(HypervisorType.VMware.toString());
    cluster.setClusterType(ClusterType.ExternalManaged);
    cluster.setManagedState(ManagedState.Managed);
    clusterId = 1L;
    clusterList = new ArrayList<ClusterVO>();
    clusterList.add(cluster);
    clusterDetails = new ClusterDetailsVO(clusterId, "url", url);
    dcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDcId);
    Mockito.when(_dcDao.persist(Matchers.any(DataCenterVO.class))).thenReturn(zone);
    Mockito.when(_dcDao.findById(1L)).thenReturn(zone);
    Mockito.when(_podDao.persist(Matchers.any(HostPodVO.class))).thenReturn(pod);
    Mockito.when(_podDao.findById(1L)).thenReturn(pod);
    Mockito.when(_clusterDao.persist(Matchers.any(ClusterVO.class))).thenReturn(cluster);
    Mockito.when(_clusterDao.findById(1L)).thenReturn(cluster);
    Mockito.when(_clusterDao.listByZoneId(1L)).thenReturn(null);
    Mockito.when(_clusterDao.expunge(1L)).thenReturn(true);
    Mockito.when(_clusterDetailsDao.persist(Matchers.any(ClusterDetailsVO.class))).thenReturn(clusterDetails);
    Mockito.when(_clusterDetailsDao.expunge(1L)).thenReturn(true);
    Mockito.when(_vmwareDcDao.persist(Matchers.any(VmwareDatacenterVO.class))).thenReturn(dc);
    Mockito.when(_vmwareDcDao.findById(1L)).thenReturn(null);
    Mockito.when(_vmwareDcDao.expunge(1L)).thenReturn(true);
    Mockito.when(_vmwareDcDao.getVmwareDatacenterByNameAndVcenter(vmwareDcName, vCenterHost)).thenReturn(null);
    Mockito.when(_vmwareDcZoneMapDao.persist(Matchers.any(VmwareDatacenterZoneMapVO.class))).thenReturn(dcZoneMap);
    Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(null);
    Mockito.when(_vmwareDcZoneMapDao.expunge(1L)).thenReturn(true);
    Mockito.when(addCmd.getZoneId()).thenReturn(1L);
    Mockito.when(addCmd.getVcenter()).thenReturn(vCenterHost);
    Mockito.when(addCmd.getUsername()).thenReturn(user);
    Mockito.when(addCmd.getPassword()).thenReturn(password);
    Mockito.when(addCmd.getName()).thenReturn(vmwareDcName);
    Mockito.when(removeCmd.getZoneId()).thenReturn(1L);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) UserVO(com.cloud.user.UserVO) HostPodVO(com.cloud.dc.HostPodVO) AccountVO(com.cloud.user.AccountVO) ClusterDetailsVO(com.cloud.dc.ClusterDetailsVO) Before(org.junit.Before)

Example 5 with AccountVO

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

the class ConfigurationManagerImpl method markDefaultZone.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_MARK_DEFAULT_ZONE, eventDescription = "Marking account with the " + "default zone", async = true)
public AccountVO markDefaultZone(final String accountName, final long domainId, final long defaultZoneId) {
    // Check if the account exists
    final Account account = _accountDao.findEnabledAccount(accountName, domainId);
    if (account == null) {
        s_logger.error("Unable to find account by name: " + accountName + " in domain " + domainId);
        throw new InvalidParameterValueException("Account by name: " + accountName + " doesn't exist in domain " + domainId);
    }
    // Don't allow modification of system account
    if (account.getId() == Account.ACCOUNT_ID_SYSTEM) {
        throw new InvalidParameterValueException("Can not modify system account");
    }
    final AccountVO acctForUpdate = _accountDao.findById(account.getId());
    acctForUpdate.setDefaultZoneId(defaultZoneId);
    if (_accountDao.update(account.getId(), acctForUpdate)) {
        CallContext.current().setEventDetails("Default zone id= " + defaultZoneId);
        return _accountDao.findById(account.getId());
    } else {
        return null;
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) AccountVO(com.cloud.user.AccountVO) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

AccountVO (com.cloud.user.AccountVO)139 Account (com.cloud.user.Account)65 Test (org.junit.Test)52 UserVO (com.cloud.user.UserVO)44 Field (java.lang.reflect.Field)41 ArrayList (java.util.ArrayList)40 DomainVO (com.cloud.domain.DomainVO)32 AccountManager (com.cloud.user.AccountManager)27 Before (org.junit.Before)22 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)21 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)21 Date (java.util.Date)16 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)16 DomainDao (com.cloud.domain.dao.DomainDao)14 SslCertDao (com.cloud.network.dao.SslCertDao)14 AgentManager (com.cloud.agent.AgentManager)13 IPAddressDao (com.cloud.network.dao.IPAddressDao)13 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)13 NetworkDao (com.cloud.network.dao.NetworkDao)13 NetworkVO (com.cloud.network.dao.NetworkVO)13