Search in sources :

Example 61 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class CertServiceTest method runUploadSslCertExpiredCert.

@Test
public void runUploadSslCertExpiredCert() throws IOException, IllegalAccessException, NoSuchFieldException {
    // Reading appropritate files
    final String certFile = URLDecoder.decode(getClass().getResource("/certs/expired_cert.crt").getFile(), Charset.defaultCharset().name());
    final String keyFile = URLDecoder.decode(getClass().getResource("/certs/rsa_self_signed.key").getFile(), Charset.defaultCharset().name());
    final String cert = readFileToString(new File(certFile));
    final String key = readFileToString(new File(keyFile));
    final CertServiceImpl certService = new CertServiceImpl();
    // setting mock objects
    certService._accountMgr = Mockito.mock(AccountManager.class);
    final Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    when(certService._accountMgr.getAccount(anyLong())).thenReturn(account);
    certService._domainDao = Mockito.mock(DomainDao.class);
    final DomainVO domain = new DomainVO("networkdomain", 1L, 1L, "networkdomain");
    when(certService._domainDao.findByIdIncludingRemoved(anyLong())).thenReturn(domain);
    certService._sslCertDao = Mockito.mock(SslCertDao.class);
    when(certService._sslCertDao.persist(Matchers.any(SslCertVO.class))).thenReturn(new SslCertVO());
    // creating the command
    final UploadSslCertCmd uploadCmd = new UploadSslCertCmdExtn();
    final Class<?> klazz = uploadCmd.getClass().getSuperclass();
    final Field certField = klazz.getDeclaredField("cert");
    certField.setAccessible(true);
    certField.set(uploadCmd, cert);
    final Field keyField = klazz.getDeclaredField("key");
    keyField.setAccessible(true);
    keyField.set(uploadCmd, key);
    try {
        certService.uploadSslCert(uploadCmd);
        Assert.fail("Given an expired certificate, upload should fail");
    } catch (final Exception e) {
        System.out.println(e.getMessage());
        Assert.assertTrue(e.getMessage().contains("Parsing certificate/key failed: NotAfter:"));
    }
}
Also used : Account(com.cloud.user.Account) SslCertDao(com.cloud.network.dao.SslCertDao) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) AccountVO(com.cloud.user.AccountVO) IOException(java.io.IOException) DomainVO(com.cloud.domain.DomainVO) Field(java.lang.reflect.Field) SslCertVO(com.cloud.network.dao.SslCertVO) DomainDao(com.cloud.domain.dao.DomainDao) AccountManager(com.cloud.user.AccountManager) UploadSslCertCmd(org.apache.cloudstack.api.command.user.loadbalancer.UploadSslCertCmd) File(java.io.File) Test(org.junit.Test)

Example 62 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ConfigurationServerImpl method updateResourceCount.

@DB
public void updateResourceCount() {
    ResourceType[] resourceTypes = Resource.ResourceType.values();
    List<AccountVO> accounts = _accountDao.listAll();
    List<DomainVO> domains = _domainDao.listAll();
    List<ResourceCountVO> domainResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Domain);
    List<ResourceCountVO> accountResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Account);
    final List<ResourceType> accountSupportedResourceTypes = new ArrayList<ResourceType>();
    final List<ResourceType> domainSupportedResourceTypes = new ArrayList<ResourceType>();
    for (ResourceType resourceType : resourceTypes) {
        if (resourceType.supportsOwner(ResourceOwnerType.Account)) {
            accountSupportedResourceTypes.add(resourceType);
        }
        if (resourceType.supportsOwner(ResourceOwnerType.Domain)) {
            domainSupportedResourceTypes.add(resourceType);
        }
    }
    final int accountExpectedCount = accountSupportedResourceTypes.size();
    final int domainExpectedCount = domainSupportedResourceTypes.size();
    if ((domainResourceCount.size() < domainExpectedCount * domains.size())) {
        s_logger.debug("resource_count table has records missing for some domains...going to insert them");
        for (final DomainVO domain : domains) {
            // Lock domain
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _domainDao.lockRow(domain.getId(), true);
                    List<ResourceCountVO> domainCounts = _resourceCountDao.listByOwnerId(domain.getId(), ResourceOwnerType.Domain);
                    List<String> domainCountStr = new ArrayList<String>();
                    for (ResourceCountVO domainCount : domainCounts) {
                        domainCountStr.add(domainCount.getType().toString());
                    }
                    if (domainCountStr.size() < domainExpectedCount) {
                        for (ResourceType resourceType : domainSupportedResourceTypes) {
                            if (!domainCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for domain id=" + domain.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
    if ((accountResourceCount.size() < accountExpectedCount * accounts.size())) {
        s_logger.debug("resource_count table has records missing for some accounts...going to insert them");
        for (final AccountVO account : accounts) {
            // lock account
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    _accountDao.lockRow(account.getId(), true);
                    List<ResourceCountVO> accountCounts = _resourceCountDao.listByOwnerId(account.getId(), ResourceOwnerType.Account);
                    List<String> accountCountStr = new ArrayList<String>();
                    for (ResourceCountVO accountCount : accountCounts) {
                        accountCountStr.add(accountCount.getType().toString());
                    }
                    if (accountCountStr.size() < accountExpectedCount) {
                        for (ResourceType resourceType : accountSupportedResourceTypes) {
                            if (!accountCountStr.contains(resourceType.toString())) {
                                ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account);
                                s_logger.debug("Inserting resource count of type " + resourceType + " for account id=" + account.getId());
                                _resourceCountDao.persist(resourceCountVO);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) ResourceType(com.cloud.configuration.Resource.ResourceType) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) AccountVO(com.cloud.user.AccountVO) DomainVO(com.cloud.domain.DomainVO) ResourceCountVO(com.cloud.configuration.ResourceCountVO) List(java.util.List) ArrayList(java.util.ArrayList) DB(com.cloud.utils.db.DB)

Example 63 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ResourceLimitManagerImpl method recalculateResourceCount.

@Override
public List<ResourceCountVO> recalculateResourceCount(Long accountId, Long domainId, Integer typeId) throws InvalidParameterValueException, CloudRuntimeException, PermissionDeniedException {
    Account callerAccount = CallContext.current().getCallingAccount();
    long count = 0;
    List<ResourceCountVO> counts = new ArrayList<ResourceCountVO>();
    List<ResourceType> resourceTypes = new ArrayList<ResourceType>();
    ResourceType resourceType = null;
    if (typeId != null) {
        for (ResourceType type : Resource.ResourceType.values()) {
            if (type.getOrdinal() == typeId.intValue()) {
                resourceType = type;
            }
        }
        if (resourceType == null) {
            throw new InvalidParameterValueException("Please specify valid resource type");
        }
    }
    DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("Please specify a valid domain ID.");
    }
    _accountMgr.checkAccess(callerAccount, domain);
    if (resourceType != null) {
        resourceTypes.add(resourceType);
    } else {
        resourceTypes = Arrays.asList(Resource.ResourceType.values());
    }
    for (ResourceType type : resourceTypes) {
        if (accountId != null) {
            if (type.supportsOwner(ResourceOwnerType.Account)) {
                count = recalculateAccountResourceCount(accountId, type);
                counts.add(new ResourceCountVO(type, count, accountId, ResourceOwnerType.Account));
            }
        } else {
            if (type.supportsOwner(ResourceOwnerType.Domain)) {
                count = recalculateDomainResourceCount(domainId, type);
                counts.add(new ResourceCountVO(type, count, domainId, ResourceOwnerType.Domain));
            }
        }
    }
    return counts;
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ArrayList(java.util.ArrayList) ResourceType(com.cloud.configuration.Resource.ResourceType)

Example 64 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class ManagementServerImpl method deleteEvents.

@Override
public boolean deleteEvents(final DeleteEventsCmd cmd) {
    final Account caller = getCaller();
    final List<Long> ids = cmd.getIds();
    boolean result = true;
    List<Long> permittedAccountIds = new ArrayList<Long>();
    if (_accountMgr.isNormalUser(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        permittedAccountIds.add(caller.getId());
    } else {
        final DomainVO domain = _domainDao.findById(caller.getDomainId());
        final List<Long> permittedDomainIds = _domainDao.getDomainChildrenIds(domain.getPath());
        permittedAccountIds = _accountDao.getAccountIdsForDomains(permittedDomainIds);
    }
    final List<EventVO> events = _eventDao.listToArchiveOrDeleteEvents(ids, cmd.getType(), cmd.getStartDate(), cmd.getEndDate(), permittedAccountIds);
    final ControlledEntity[] sameOwnerEvents = events.toArray(new ControlledEntity[events.size()]);
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, false, sameOwnerEvents);
    if (ids != null && events.size() < ids.size()) {
        result = false;
        return result;
    }
    for (final EventVO event : events) {
        _eventDao.remove(event.getId());
    }
    return result;
}
Also used : Account(com.cloud.user.Account) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) DomainVO(com.cloud.domain.DomainVO) ControlledEntity(org.apache.cloudstack.acl.ControlledEntity) ArrayList(java.util.ArrayList) EventVO(com.cloud.event.EventVO)

Example 65 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class AssignLoadBalancerTest method setUp.

@Before
public void setUp() {
    assignToLbRuleCmd = new AssignToLoadBalancerRuleCmd() {
    };
    // ComponentContext.initComponentsLifeCycle();
    AccountVO account = new AccountVO(accountName, domainId, "networkDomain", Account.ACCOUNT_TYPE_NORMAL, "uuid");
    DomainVO domain = new DomainVO("rootDomain", 5L, 5L, "networkDomain");
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
}
Also used : DomainVO(com.cloud.domain.DomainVO) AssignToLoadBalancerRuleCmd(org.apache.cloudstack.api.command.user.loadbalancer.AssignToLoadBalancerRuleCmd) UserVO(com.cloud.user.UserVO) AccountVO(com.cloud.user.AccountVO) Before(org.junit.Before)

Aggregations

DomainVO (com.cloud.domain.DomainVO)196 Account (com.cloud.user.Account)85 AccountVO (com.cloud.user.AccountVO)64 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)53 DomainDao (com.cloud.domain.dao.DomainDao)30 Field (java.lang.reflect.Field)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 SslCertDao (com.cloud.network.dao.SslCertDao)29 AccountManager (com.cloud.user.AccountManager)29 SslCertVO (com.cloud.network.dao.SslCertVO)27 List (java.util.List)26 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)24 Pair (com.cloud.utils.Pair)24 Domain (com.cloud.domain.Domain)23 Filter (com.cloud.utils.db.Filter)23 File (java.io.File)23 IOException (java.io.IOException)23 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)23 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22