use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class ImplicitDedicationPlanner method orderClusters.
@Override
public List<Long> orderClusters(final VirtualMachineProfile vmProfile, final DeploymentPlan plan, final ExcludeList avoid) throws InsufficientServerCapacityException {
List<Long> clusterList = super.orderClusters(vmProfile, plan, avoid);
final Set<Long> hostsToAvoid = avoid.getHostsToAvoid();
final Account account = vmProfile.getOwner();
if (clusterList == null || clusterList.isEmpty()) {
return clusterList;
}
// Check if strict or preferred mode should be used.
final boolean preferred = isServiceOfferingUsingPlannerInPreferredMode(vmProfile.getServiceOfferingId());
// Get the list of all the hosts in the given clusters
final List<Long> allHosts = new ArrayList<>();
for (final Long cluster : clusterList) {
final List<HostVO> hostsInCluster = resourceMgr.listAllHostsInCluster(cluster);
for (final HostVO hostVO : hostsInCluster) {
allHosts.add(hostVO.getId());
}
}
// Go over all the hosts in the cluster and get a list of
// 1. All empty hosts, not running any vms.
// 2. Hosts running vms for this account and created by a service offering which uses an
// implicit dedication planner.
// 3. Hosts running vms created by implicit planner and in strict mode of other accounts.
// 4. Hosts running vms from other account or from this account but created by a service offering which uses
// any planner besides implicit.
final Set<Long> emptyHosts = new HashSet<>();
final Set<Long> hostRunningVmsOfAccount = new HashSet<>();
final Set<Long> hostRunningStrictImplicitVmsOfOtherAccounts = new HashSet<>();
final Set<Long> allOtherHosts = new HashSet<>();
for (final Long host : allHosts) {
final List<VMInstanceVO> vms = getVmsOnHost(host);
if (vms == null || vms.isEmpty()) {
emptyHosts.add(host);
} else if (checkHostSuitabilityForImplicitDedication(account.getAccountId(), vms)) {
hostRunningVmsOfAccount.add(host);
} else if (checkIfAllVmsCreatedInStrictMode(account.getAccountId(), vms)) {
hostRunningStrictImplicitVmsOfOtherAccounts.add(host);
} else {
allOtherHosts.add(host);
}
}
// Hosts running vms of other accounts created by ab implicit planner in strict mode should always be avoided.
avoid.addHostList(hostRunningStrictImplicitVmsOfOtherAccounts);
if (!hostRunningVmsOfAccount.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(hostRunningVmsOfAccount))) {
// Check if any of hosts that are running implicit dedicated vms are available (not in avoid list).
// If so, we'll try and use these hosts.
avoid.addHostList(emptyHosts);
avoid.addHostList(allOtherHosts);
clusterList = getUpdatedClusterList(clusterList, avoid.getHostsToAvoid());
} else if (!emptyHosts.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(emptyHosts))) {
// If there aren't implicit resources try on empty hosts
avoid.addHostList(allOtherHosts);
clusterList = getUpdatedClusterList(clusterList, avoid.getHostsToAvoid());
} else if (!preferred) {
// If in strict mode, there is nothing else to try.
clusterList = null;
} else {
// If in preferred mode, check if hosts are available to try, otherwise return an empty cluster list.
if (!allOtherHosts.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(allOtherHosts))) {
clusterList = getUpdatedClusterList(clusterList, avoid.getHostsToAvoid());
} else {
clusterList = null;
}
}
return clusterList;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VolumeJoinDaoImpl method newVolumeResponse.
@Override
public VolumeResponse newVolumeResponse(final ResponseView view, final VolumeJoinVO volume) {
final VolumeResponse volResponse = new VolumeResponse();
volResponse.setId(volume.getUuid());
if (volume.getName() != null) {
volResponse.setName(volume.getName());
} else {
volResponse.setName("");
}
volResponse.setZoneId(volume.getDataCenterUuid());
volResponse.setZoneName(volume.getDataCenterName());
volResponse.setVolumeType(volume.getVolumeType().toString());
volResponse.setDeviceId(volume.getDeviceId());
final long instanceId = volume.getVmId();
if (instanceId > 0 && volume.getState() != Volume.State.Destroy) {
volResponse.setVirtualMachineId(volume.getVmUuid());
volResponse.setVirtualMachineName(volume.getVmName());
volResponse.setVirtualMachineState(volume.getVmState().toString());
if (volume.getVmDisplayName() != null) {
volResponse.setVirtualMachineDisplayName(volume.getVmDisplayName());
} else {
volResponse.setVirtualMachineDisplayName(volume.getVmName());
}
}
volResponse.setProvisioningType(volume.getProvisioningType().toString());
// Show the virtual size of the volume
volResponse.setSize(volume.getSize());
volResponse.setMinIops(volume.getMinIops());
volResponse.setMaxIops(volume.getMaxIops());
volResponse.setIopsTotalRate(volume.getIopsTotalRate());
volResponse.setIopsRatePerGb(volume.getIopsRatePerGb());
volResponse.setCreated(volume.getCreated());
volResponse.setState(volume.getState().toString());
if (volume.getState() == Volume.State.UploadOp) {
// com.cloud.storage.VolumeHostVO volumeHostRef =
// ApiDBUtils.findVolumeHostRef(volume.getId(),
// volume.getDataCenterId());
volResponse.setSize(volume.getVolumeStoreSize());
volResponse.setCreated(volume.getCreatedOnStore());
if (view == ResponseView.Full) {
volResponse.setHypervisor(ApiDBUtils.getHypervisorTypeFromFormat(volume.getDataCenterId(), volume.getFormat()).toString());
}
if (volume.getDownloadState() != VMTemplateStatus.DOWNLOADED) {
final String volumeStatus;
if (volume.getDownloadState() == VMTemplateStatus.DOWNLOAD_IN_PROGRESS) {
if (volume.getDownloadPercent() == 100) {
volumeStatus = "Checking Volume";
} else {
volumeStatus = volume.getDownloadPercent() + "% Uploaded";
}
volResponse.setState("Uploading");
} else {
volumeStatus = volume.getErrorString();
if (volume.getDownloadState() == VMTemplateStatus.NOT_DOWNLOADED) {
volResponse.setState("UploadNotStarted");
} else {
volResponse.setState("UploadError");
}
}
volResponse.setStatus(volumeStatus);
} else if (volume.getDownloadState() == VMTemplateStatus.DOWNLOADED) {
volResponse.setStatus("Upload Complete");
volResponse.setState("Uploaded");
} else {
volResponse.setStatus("Successfully Installed");
}
}
final Account caller = CallContext.current().getCallingAccount();
// return the path on disk to root admins and admins
if (!_accountMgr.isNormalUser(caller.getId())) {
volResponse.setPath(volume.getPath());
}
// populate owner.
ApiResponseHelper.populateOwner(volResponse, volume);
// ApiDBUtils.findDiskOfferingById(volume.getDiskOfferingId());
if (volume.getDiskOfferingId() > 0) {
boolean isServiceOffering = false;
if (volume.getVolumeType().equals(VolumeType.ROOT)) {
isServiceOffering = true;
} else {
// can't rely on the fact that the volume is the datadisk as it might have been created as a root, and
// then detached later
final long offeringId = volume.getDiskOfferingId();
if (ApiDBUtils.findDiskOfferingById(offeringId) == null) {
isServiceOffering = true;
}
}
if (isServiceOffering) {
volResponse.setServiceOfferingId(volume.getDiskOfferingUuid());
volResponse.setServiceOfferingName(volume.getDiskOfferingName());
volResponse.setServiceOfferingDisplayText(volume.getDiskOfferingDisplayText());
} else {
volResponse.setDiskOfferingId(volume.getDiskOfferingUuid());
volResponse.setDiskOfferingName(volume.getDiskOfferingName());
volResponse.setDiskOfferingDisplayText(volume.getDiskOfferingDisplayText());
}
volResponse.setStorageType(volume.isUseLocalStorage() ? ServiceOffering.StorageType.local.toString() : ServiceOffering.StorageType.shared.toString());
volResponse.setBytesReadRate(volume.getBytesReadRate());
volResponse.setBytesWriteRate(volume.getBytesReadRate());
volResponse.setIopsReadRate(volume.getIopsWriteRate());
volResponse.setIopsWriteRate(volume.getIopsWriteRate());
}
if (!_accountMgr.isNormalUser(caller.getId())) {
// return hypervisor and storage pool info to root admins and admins
if (volume.getState() != Volume.State.UploadOp) {
if (volume.getHypervisorType() != null) {
volResponse.setHypervisor(volume.getHypervisorType().toString());
} else {
volResponse.setHypervisor(ApiDBUtils.getHypervisorTypeFromFormat(volume.getDataCenterId(), volume.getFormat()).toString());
}
}
final Long poolId = volume.getPoolId();
final String poolName = (poolId == null) ? "none" : volume.getPoolName();
volResponse.setStoragePoolName(poolName);
volResponse.setStoragePoolId(volume.getPoolUuid());
}
volResponse.setAttached(volume.getAttached());
volResponse.setDestroyed(volume.getState() == Volume.State.Destroy);
boolean isExtractable = true;
if (volume.getVolumeType() != VolumeType.DATADISK) {
// dependence.
if (volume.getTemplateId() > 0) {
// For ISO based volumes template
// = null and we allow extraction
// of all ISO based volumes
isExtractable = volume.isExtractable() && volume.getTemplateType() != TemplateType.SYSTEM;
}
}
// update tag information
final long tag_id = volume.getTagId();
if (tag_id > 0) {
final ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id);
if (vtag != null) {
volResponse.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
}
}
volResponse.setExtractable(isExtractable);
volResponse.setDisplayVolume(volume.isDisplayVolume());
volResponse.setChainInfo(volume.getChainInfo());
volResponse.setTemplateId(volume.getTemplateUuid());
volResponse.setTemplateName(volume.getTemplateName());
volResponse.setTemplateDisplayText(volume.getTemplateDisplayText());
volResponse.setIsoId(volume.getIsoUuid());
volResponse.setIsoName(volume.getIsoName());
volResponse.setIsoDisplayText(volume.getIsoDisplayText());
// set async job
if (volume.getJobId() != null) {
volResponse.setJobId(volume.getJobUuid());
volResponse.setJobStatus(volume.getJobStatus());
}
volResponse.setDiskController(volume.getDiskController());
volResponse.setDiskFormat(volume.getFormat());
volResponse.setObjectName("volume");
return volResponse;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class ResourceLimitManagerImpl method recalculateResourceCount.
@Override
public List<ResourceCountVO> recalculateResourceCount(final Long accountId, final Long domainId, final Integer typeId) throws InvalidParameterValueException, CloudRuntimeException, PermissionDeniedException {
final Account callerAccount = CallContext.current().getCallingAccount();
long count;
final List<ResourceCountVO> counts = new ArrayList<>();
List<ResourceType> resourceTypes = new ArrayList<>();
ResourceType resourceType = null;
if (typeId != null) {
for (final ResourceType type : Resource.ResourceType.values()) {
if (type.getOrdinal() == typeId.intValue()) {
resourceType = type;
}
}
if (resourceType == null) {
throw new InvalidParameterValueException("Please specify valid resource type");
}
}
final DomainVO domain = this._domainDao.findById(domainId);
if (domain == null) {
throw new InvalidParameterValueException("Please specify a valid domain ID.");
}
this._accountMgr.checkAccess(callerAccount, domain);
if (resourceType != null) {
resourceTypes.add(resourceType);
} else {
resourceTypes = Arrays.asList(Resource.ResourceType.values());
}
for (final 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;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class ResourceLimitManagerImpl method updateResourceLimit.
@Override
public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) {
final Account caller = CallContext.current().getCallingAccount();
if (max == null) {
max = new Long(Resource.RESOURCE_UNLIMITED);
} else if (max.longValue() < Resource.RESOURCE_UNLIMITED) {
throw new InvalidParameterValueException("Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
}
// Map resource type
ResourceType resourceType = null;
if (typeId != null) {
for (final ResourceType type : Resource.ResourceType.values()) {
if (type.getOrdinal() == typeId.intValue()) {
resourceType = type;
}
}
if (resourceType == null) {
throw new InvalidParameterValueException("Please specify valid resource type");
}
}
// Convert max storage size from GiB to bytes
if ((resourceType == ResourceType.primary_storage || resourceType == ResourceType.secondary_storage) && max >= 0) {
max = max * ResourceType.bytesToGiB;
}
ResourceOwnerType ownerType = null;
Long ownerId = null;
if (accountId != null) {
final Account account = this._entityMgr.findById(Account.class, accountId);
if (account == null) {
throw new InvalidParameterValueException("Unable to find account " + accountId);
}
if (account.getId() == Account.ACCOUNT_ID_SYSTEM) {
throw new InvalidParameterValueException("Can't update system account");
}
// only Unlimited value is accepted if account is Root Admin
if (this._accountMgr.isRootAdmin(account.getId()) && max.shortValue() != Resource.RESOURCE_UNLIMITED) {
throw new InvalidParameterValueException("Only " + Resource.RESOURCE_UNLIMITED + " limit is supported for Root Admin accounts");
}
if ((caller.getAccountId() == accountId.longValue()) && (this._accountMgr.isDomainAdmin(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
// If the admin is trying to update his own account, disallow.
throw new PermissionDeniedException("Unable to update resource limit for his own account " + accountId + ", permission denied");
}
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account);
} else {
this._accountMgr.checkAccess(caller, null, true, account);
}
ownerType = ResourceOwnerType.Account;
ownerId = accountId;
} else if (domainId != null) {
final Domain domain = this._entityMgr.findById(Domain.class, domainId);
this._accountMgr.checkAccess(caller, domain);
if (Domain.ROOT_DOMAIN == domainId.longValue()) {
// no one can add limits on ROOT domain, disallow...
throw new PermissionDeniedException("Cannot update resource limit for ROOT domain " + domainId + ", permission denied");
}
if ((caller.getDomainId() == domainId.longValue()) && caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
// if the admin is trying to update their own domain, disallow...
throw new PermissionDeniedException("Unable to update resource limit for domain " + domainId + ", permission denied");
}
final Long parentDomainId = domain.getParent();
if (parentDomainId != null) {
final DomainVO parentDomain = this._domainDao.findById(parentDomainId);
final long parentMaximum = findCorrectResourceLimitForDomain(parentDomain, resourceType);
if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
throw new InvalidParameterValueException("Domain " + domain.getName() + "(id: " + parentDomain.getId() + ") has maximum allowed resource limit " + parentMaximum + " for " + resourceType + ", please specify a value less that or equal to " + parentMaximum);
}
}
ownerType = ResourceOwnerType.Domain;
ownerId = domainId;
}
if (ownerId == null) {
throw new InvalidParameterValueException("AccountId or domainId have to be specified in order to update resource limit");
}
final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType);
if (limit != null) {
// Update the existing limit
this._resourceLimitDao.update(limit.getId(), max);
return this._resourceLimitDao.findById(limit.getId());
} else {
return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType));
}
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class ResourceLimitManagerImpl method searchForLimits.
@Override
public List<ResourceLimitVO> searchForLimits(final Long id, Long accountId, Long domainId, final Integer type, final Long startIndex, final Long pageSizeVal) {
final Account caller = CallContext.current().getCallingAccount();
final List<ResourceLimitVO> limits = new ArrayList<>();
final boolean isAccount;
if (!this._accountMgr.isAdmin(caller.getId())) {
accountId = caller.getId();
domainId = null;
} else {
if (domainId != null) {
// verify domain information and permissions
final Domain domain = this._domainDao.findById(domainId);
if (domain == null) {
// return empty set
return limits;
}
this._accountMgr.checkAccess(caller, domain);
if (accountId != null) {
// Verify account information and permissions
final Account account = this._accountDao.findById(accountId);
if (account == null) {
// return empty set
return limits;
}
this._accountMgr.checkAccess(caller, null, true, account);
domainId = null;
}
}
}
// Map resource type
ResourceType resourceType = null;
if (type != null) {
try {
resourceType = ResourceType.values()[type];
} catch (final ArrayIndexOutOfBoundsException e) {
throw new InvalidParameterValueException("Please specify a valid resource type.");
}
}
// If id is passed in, get the record and return it if permission check has passed
if (id != null) {
final ResourceLimitVO vo = this._resourceLimitDao.findById(id);
if (vo.getAccountId() != null) {
this._accountMgr.checkAccess(caller, null, true, this._accountDao.findById(vo.getAccountId()));
limits.add(vo);
} else if (vo.getDomainId() != null) {
this._accountMgr.checkAccess(caller, this._domainDao.findById(vo.getDomainId()));
limits.add(vo);
}
return limits;
}
// If account is not specified, default it to caller account
if (accountId == null) {
if (domainId == null) {
accountId = caller.getId();
isAccount = true;
} else {
isAccount = false;
}
} else {
isAccount = true;
}
final SearchBuilder<ResourceLimitVO> sb = this._resourceLimitDao.createSearchBuilder();
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
final SearchCriteria<ResourceLimitVO> sc = sb.create();
final Filter filter = new Filter(ResourceLimitVO.class, "id", true, startIndex, pageSizeVal);
if (accountId != null) {
sc.setParameters("accountId", accountId);
}
if (domainId != null) {
sc.setParameters("domainId", domainId);
sc.setParameters("accountId", (Object[]) null);
}
if (resourceType != null) {
sc.setParameters("type", resourceType);
}
final List<ResourceLimitVO> foundLimits = this._resourceLimitDao.search(sc, filter);
if (resourceType != null) {
if (foundLimits.isEmpty()) {
if (isAccount) {
limits.add(new ResourceLimitVO(resourceType, findCorrectResourceLimitForAccount(this._accountMgr.getAccount(accountId), resourceType), accountId, ResourceOwnerType.Account));
} else {
limits.add(new ResourceLimitVO(resourceType, findCorrectResourceLimitForDomain(this._domainDao.findById(domainId), resourceType), domainId, ResourceOwnerType.Domain));
}
} else {
limits.addAll(foundLimits);
}
} else {
limits.addAll(foundLimits);
// see if any limits are missing from the table, and if yes - get it from the config table and add
final ResourceType[] resourceTypes = ResourceCount.ResourceType.values();
if (foundLimits.size() != resourceTypes.length) {
final List<String> accountLimitStr = new ArrayList<>();
final List<String> domainLimitStr = new ArrayList<>();
for (final ResourceLimitVO foundLimit : foundLimits) {
if (foundLimit.getAccountId() != null) {
accountLimitStr.add(foundLimit.getType().toString());
} else {
domainLimitStr.add(foundLimit.getType().toString());
}
}
// get default from config values
if (isAccount) {
if (accountLimitStr.size() < resourceTypes.length) {
for (final ResourceType rt : resourceTypes) {
if (!accountLimitStr.contains(rt.toString()) && rt.supportsOwner(ResourceOwnerType.Account)) {
limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForAccount(this._accountMgr.getAccount(accountId), rt), accountId, ResourceOwnerType.Account));
}
}
}
} else {
if (domainLimitStr.size() < resourceTypes.length) {
for (final ResourceType rt : resourceTypes) {
if (!domainLimitStr.contains(rt.toString()) && rt.supportsOwner(ResourceOwnerType.Domain)) {
limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForDomain(this._domainDao.findById(domainId), rt), domainId, ResourceOwnerType.Domain));
}
}
}
}
}
}
return limits;
}
Aggregations