Search in sources :

Example 21 with Account

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

the class AffinityGroupServiceImpl method createAffinityGroup.

@DB
@Override
public AffinityGroup createAffinityGroup(final String accountName, final Long projectId, final Long domainId, final String affinityGroupName, final String affinityGroupType, final String description) {
    // validate the affinityGroupType
    final Map<String, AffinityGroupProcessor> typeProcessorMap = getAffinityTypeToProcessorMap();
    if (typeProcessorMap == null || typeProcessorMap.isEmpty()) {
        throw new InvalidParameterValueException("Unable to create affinity group, no Affinity Group Types configured");
    }
    final AffinityGroupProcessor processor = typeProcessorMap.get(affinityGroupType);
    if (processor == null) {
        throw new InvalidParameterValueException("Unable to create affinity group, invalid affinity group type" + affinityGroupType);
    }
    final Account caller = CallContext.current().getCallingAccount();
    if (processor.isAdminControlledGroup() && !_accountMgr.isRootAdmin(caller.getId())) {
        throw new PermissionDeniedException("Cannot create the affinity group");
    }
    final ControlledEntity.ACLType aclType;
    final Account owner;
    boolean domainLevel = false;
    if (projectId == null && domainId != null && accountName == null) {
        verifyAccessToDomainWideProcessor(caller, processor);
        final DomainVO domain = getDomain(domainId);
        _accountMgr.checkAccess(caller, domain);
        // domain level group, owner is SYSTEM.
        owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
        aclType = ControlledEntity.ACLType.Domain;
        domainLevel = true;
    } else {
        owner = _accountMgr.finalizeOwner(caller, accountName, domainId, projectId);
        aclType = ControlledEntity.ACLType.Account;
    }
    verifyAffinityGroupNameInUse(owner.getAccountId(), owner.getDomainId(), affinityGroupName);
    verifyDomainLevelAffinityGroupName(domainLevel, owner.getDomainId(), affinityGroupName);
    final AffinityGroupVO group = createAffinityGroup(processor, owner, aclType, affinityGroupName, affinityGroupType, description, domainLevel, domainId);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Created affinity group =" + affinityGroupName);
    }
    return group;
}
Also used : Account(com.cloud.legacymodel.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ControlledEntity(com.cloud.legacymodel.acl.ControlledEntity) ACLType(com.cloud.legacymodel.acl.ControlledEntity.ACLType) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 22 with Account

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

the class AffinityGroupServiceImpl method getAffinityGroupByName.

private AffinityGroupVO getAffinityGroupByName(final String account, final Long projectId, final Long domainId, final String affinityGroupName) {
    final AffinityGroupVO group;
    if (account == null && domainId != null) {
        group = _affinityGroupDao.findDomainLevelGroupByName(domainId, affinityGroupName);
    } else {
        final Long accountId = _accountMgr.finalyzeAccountId(account, domainId, projectId, true);
        if (accountId == null) {
            final Account caller = CallContext.current().getCallingAccount();
            group = _affinityGroupDao.findByAccountAndName(caller.getAccountId(), affinityGroupName);
        } else {
            group = _affinityGroupDao.findByAccountAndName(accountId, affinityGroupName);
        }
    }
    return group;
}
Also used : Account(com.cloud.legacymodel.user.Account)

Example 23 with Account

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

the class ConfigurationManagerImpl method createServiceOffering.

protected ServiceOfferingVO createServiceOffering(final long userId, final boolean isSystem, final VirtualMachineType vmType, final String name, final Integer cpu, final Integer ramSize, final String displayText, final String provisioningType, final boolean localStorageRequired, final boolean offerHA, final boolean limitResourceUse, final boolean volatileVm, String tags, final Long domainId, final String hostTag, final Integer networkRate, final String deploymentPlanner, final Map<String, String> details, final Boolean isCustomizedIops, Long minIops, Long maxIops, Long bytesReadRate, Long bytesWriteRate, Long iopsReadRate, Long iopsWriteRate, Long iopsTotalRate, final boolean iopsRatePerGb, final Integer hypervisorSnapshotReserve) {
    // Check if user exists in the system
    final User user = _userDao.findById(userId);
    if (user == null || user.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find active user by id " + userId);
    }
    final Account account = _accountDao.findById(user.getAccountId());
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        if (domainId == null) {
            throw new InvalidParameterValueException("Unable to create public service offering by id " + userId + " because it is domain-admin");
        }
        if (tags != null || hostTag != null) {
            throw new InvalidParameterValueException("Unable to create service offering with storage tags or host tags by id " + userId + " because it is domain-admin");
        }
        if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
            throw new InvalidParameterValueException("Unable to create service offering by another domain admin with id " + userId);
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException("Unable to create service offering by id " + userId + " because it is not root-admin or domain-admin");
    }
    final StorageProvisioningType typedProvisioningType = StorageProvisioningType.valueOf(provisioningType);
    tags = StringUtils.cleanupTags(tags);
    ServiceOfferingVO offering = new ServiceOfferingVO(name, cpu, ramSize, networkRate, null, offerHA, limitResourceUse, volatileVm, displayText, typedProvisioningType, localStorageRequired, false, tags, isSystem, vmType, domainId, hostTag, deploymentPlanner);
    if (isCustomizedIops != null) {
        bytesReadRate = null;
        bytesWriteRate = null;
        iopsReadRate = null;
        iopsWriteRate = null;
        if (isCustomizedIops) {
            minIops = null;
            maxIops = null;
        } else {
            if (minIops == null && maxIops == null) {
                minIops = 0L;
                maxIops = 0L;
            } else {
                if (minIops == null || minIops <= 0) {
                    throw new InvalidParameterValueException("The min IOPS must be greater than 0.");
                }
                if (maxIops == null) {
                    maxIops = 0L;
                }
                if (minIops > maxIops) {
                    throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
                }
            }
        }
    } else {
        minIops = null;
        maxIops = null;
    }
    offering.setCustomizedIops(isCustomizedIops);
    offering.setMinIops(minIops);
    offering.setMaxIops(maxIops);
    if (bytesReadRate != null && bytesReadRate > 0) {
        offering.setBytesReadRate(bytesReadRate);
    }
    if (bytesWriteRate != null && bytesWriteRate > 0) {
        offering.setBytesWriteRate(bytesWriteRate);
    }
    if (iopsReadRate != null && iopsReadRate > 0) {
        offering.setIopsReadRate(iopsReadRate);
    }
    if (iopsWriteRate != null && iopsWriteRate > 0) {
        offering.setIopsWriteRate(iopsWriteRate);
    }
    if (iopsTotalRate != null && iopsTotalRate > 0) {
        if (iopsWriteRate != null && iopsWriteRate > 0 || iopsReadRate != null && iopsReadRate > 0) {
            throw new InvalidParameterValueException("Total IOPS rate cannot be used together with IOPS read rate or IOPS write rate");
        }
        offering.setIopsTotalRate(iopsTotalRate);
    }
    if (iopsRatePerGb) {
        offering.setIopsRatePerGb(iopsRatePerGb);
    }
    if (hypervisorSnapshotReserve != null && hypervisorSnapshotReserve < 0) {
        throw new InvalidParameterValueException("If provided, Hypervisor Snapshot Reserve must be greater than or equal to 0.");
    }
    offering.setHypervisorSnapshotReserve(hypervisorSnapshotReserve);
    List<ServiceOfferingDetailsVO> detailsVO = null;
    if (details != null) {
        // To have correct input, either both gpu card name and VGPU type should be passed or nothing should be passed.
        // Use XOR condition to verify that.
        final boolean entry1 = details.containsKey(GPU.Keys.pciDevice.toString());
        final boolean entry2 = details.containsKey(GPU.Keys.vgpuType.toString());
        if ((entry1 || entry2) && !(entry1 && entry2)) {
            throw new InvalidParameterValueException("Please specify the pciDevice and vgpuType correctly.");
        }
        detailsVO = new ArrayList<>();
        for (final Entry<String, String> detailEntry : details.entrySet()) {
            if (detailEntry.getKey().equals(GPU.Keys.pciDevice.toString())) {
                if (detailEntry.getValue() == null) {
                    throw new InvalidParameterValueException("Please specify a GPU Card.");
                }
            }
            if (detailEntry.getKey().equals(GPU.Keys.vgpuType.toString())) {
                if (detailEntry.getValue() == null) {
                    throw new InvalidParameterValueException("vGPUType value cannot be null");
                }
            }
            detailsVO.add(new ServiceOfferingDetailsVO(offering.getId(), detailEntry.getKey(), detailEntry.getValue(), true));
        }
    }
    if ((offering = _serviceOfferingDao.persist(offering)) != null) {
        if (detailsVO != null && !detailsVO.isEmpty()) {
            for (int index = 0; index < detailsVO.size(); index++) {
                detailsVO.get(index).setResourceId(offering.getId());
            }
            _serviceOfferingDetailsDao.saveDetails(detailsVO);
        }
        CallContext.current().setEventDetails("Service offering id=" + offering.getId());
        return offering;
    } else {
        return null;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) StorageProvisioningType(com.cloud.model.enumeration.StorageProvisioningType)

Example 24 with Account

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

the class ConfigurationManagerImpl method deleteServiceOffering.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_DELETE, eventDescription = "deleting service offering")
public boolean deleteServiceOffering(final DeleteServiceOfferingCmd cmd) {
    final Long offeringId = cmd.getId();
    Long userId = CallContext.current().getCallingUserId();
    if (userId == null) {
        userId = Long.valueOf(User.UID_SYSTEM);
    }
    // Verify service offering id
    final ServiceOfferingVO offering = _serviceOfferingDao.findById(offeringId);
    if (offering == null) {
        throw new InvalidParameterValueException("unable to find service offering " + offeringId);
    }
    if (offering.getDefaultUse()) {
        throw new InvalidParameterValueException("Default service offerings cannot be deleted");
    }
    final User user = _userDao.findById(userId);
    if (user == null || user.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find active user by id " + userId);
    }
    final Account account = _accountDao.findById(user.getAccountId());
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        if (offering.getDomainId() == null) {
            throw new InvalidParameterValueException("Unable to delete public service offering by id " + userId + " because it is domain-admin");
        }
        if (!_domainDao.isChildDomain(account.getDomainId(), offering.getDomainId())) {
            throw new InvalidParameterValueException("Unable to delete service offering by another domain admin with id " + userId);
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException("Unable to delete service offering by id " + userId + " because it is not root-admin or domain-admin");
    }
    offering.setState(DiskOffering.State.Inactive);
    if (_serviceOfferingDao.update(offeringId, offering)) {
        CallContext.current().setEventDetails("Service offering id=" + offeringId);
        return true;
    } else {
        return false;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ActionEvent(com.cloud.event.ActionEvent)

Example 25 with Account

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

the class ConfigurationManagerImpl method updateConfiguration.

@Override
@ActionEvent(eventType = EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, eventDescription = "updating configuration")
public Configuration updateConfiguration(final UpdateCfgCmd cmd) throws InvalidParameterValueException {
    final Long userId = CallContext.current().getCallingUserId();
    final Account caller = CallContext.current().getCallingAccount();
    final User user = _userDao.findById(userId);
    final String name = cmd.getCfgName();
    String value = cmd.getValue();
    final Long zoneId = cmd.getZoneId();
    final Long clusterId = cmd.getClusterId();
    final Long storagepoolId = cmd.getStoragepoolId();
    final Long accountId = cmd.getAccountId();
    CallContext.current().setEventDetails(" Name: " + name + " New Value: " + (name.toLowerCase().contains("password") ? "*****" : value == null ? "" : value));
    // check if config value exists
    final ConfigurationVO config = _configDao.findByName(name);
    final String catergory;
    // FIX ME - All configuration parameters are not moved from config.java to configKey
    if (config == null) {
        if (_configDepot.get(name) == null) {
            s_logger.warn("Probably the component manager where configuration variable " + name + " is defined needs to implement Configurable interface");
            throw new InvalidParameterValueException("Config parameter with name " + name + " doesn't exist");
        }
        catergory = _configDepot.get(name).category();
    } else {
        catergory = config.getCategory();
    }
    if (value == null) {
        return _configDao.findByName(name);
    }
    value = value.trim();
    if (value.isEmpty() || value.equals("null")) {
        value = null;
    }
    String scope = null;
    Long id = null;
    int paramCountCheck = 0;
    // Non-ROOT may only update the Account scope
    if (!_accountMgr.isRootAdmin(caller.getId()) && accountId == null) {
        throw new InvalidParameterValueException("Please specify AccountId to update the config for the given account.");
    }
    if (accountId != null) {
        final Account accountToUpdate = _accountDao.findById(accountId);
        _accountMgr.checkAccess(caller, null, true, accountToUpdate);
        scope = ConfigKey.Scope.Account.toString();
        id = accountId;
        paramCountCheck++;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        if (zoneId != null) {
            scope = ConfigKey.Scope.Zone.toString();
            id = zoneId;
            paramCountCheck++;
        }
        if (clusterId != null) {
            scope = ConfigKey.Scope.Cluster.toString();
            id = clusterId;
            paramCountCheck++;
        }
        if (storagepoolId != null) {
            scope = ConfigKey.Scope.StoragePool.toString();
            id = storagepoolId;
            paramCountCheck++;
        }
    }
    if (paramCountCheck > 1) {
        throw new InvalidParameterValueException("cannot handle multiple IDs, provide only one ID corresponding to the scope");
    }
    final String updatedValue = updateConfiguration(userId, name, catergory, value, scope, id);
    if (value == null && updatedValue == null || updatedValue.equalsIgnoreCase(value)) {
        return _configDao.findByName(name);
    } else {
        throw new CloudRuntimeException("Unable to update configuration parameter " + name);
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) ConfigurationVO(com.cloud.framework.config.impl.ConfigurationVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

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