@Override public void checkQuota(NeedQuotaCheckMessage msg, Map<String, Quota.QuotaPair> pairs) { if (!new QuotaUtil().isAdminAccount(msg.getAccountUuid())) { if (msg instanceof StartVmInstanceMsg) { check((StartVmInstanceMsg) msg, pairs); } } }
private void check(APICreateSecurityGroupMsg msg, Map<String, QuotaPair> pairs) { long sgNum = pairs.get(SecurityGroupQuotaConstant.SG_NUM).getValue(); long sgn = getUsedSg(msg.getSession().getAccountUuid()); if (sgn + 1 > sgNum) { throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( msg.getSession().getAccountUuid(), SecurityGroupQuotaConstant.SG_NUM, sgNum)); } } };
private void checkVolumeSnapshotNumQuota(String currentAccountUuid, String resourceTargetOwnerAccountUuid, long volumeSnapshotNumAsked, Map<String, Quota.QuotaPair> pairs) { long volumeSnapshotNumQuota = pairs.get(VolumeSnapshotQuotaConstant.VOLUME_SNAPSHOT_NUM).getValue(); long volumeSnapshotNumUsed = getUsedVolumeSnapshotNum(resourceTargetOwnerAccountUuid); { QuotaUtil.QuotaCompareInfo quotaCompareInfo; quotaCompareInfo = new QuotaUtil.QuotaCompareInfo(); quotaCompareInfo.currentAccountUuid = currentAccountUuid; quotaCompareInfo.resourceTargetOwnerAccountUuid = resourceTargetOwnerAccountUuid; quotaCompareInfo.quotaName = VolumeSnapshotQuotaConstant.VOLUME_SNAPSHOT_NUM; quotaCompareInfo.quotaValue = volumeSnapshotNumQuota; quotaCompareInfo.currentUsed = volumeSnapshotNumUsed; quotaCompareInfo.request = volumeSnapshotNumAsked; new QuotaUtil().CheckQuota(quotaCompareInfo); } } };
private void checkStartVmInstance(String currentAccountUuid, String vmInstanceUuid, Map<String, Quota.QuotaPair> pairs) { String resourceTargetOwnerAccountUuid = new QuotaUtil().getResourceOwnerAccountUuid(vmInstanceUuid); checkVmInstanceQuota(currentAccountUuid, resourceTargetOwnerAccountUuid, vmInstanceUuid, pairs); }
@Transactional(readOnly = true) private void check(APIChangeResourceOwnerMsg msg, Map<String, Quota.QuotaPair> pairs) { String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = msg.getAccountUuid(); if (new QuotaUtil().isAdminAccount(resourceTargetOwnerAccountUuid)) { return; } SimpleQuery<AccountResourceRefVO> q = dbf.createQuery(AccountResourceRefVO.class); q.add(AccountResourceRefVO_.resourceUuid, Op.EQ, msg.getResourceUuid()); AccountResourceRefVO accResRefVO = q.find(); if (accResRefVO.getResourceType().equals(VmInstanceVO.class.getSimpleName())) { long eipNumQuota = pairs.get(EipQuotaConstant.EIP_NUM).getValue(); long usedEipNum = getUsedEipNum(resourceTargetOwnerAccountUuid); long askedEipNum = getVmEipNum(msg.getResourceUuid()); QuotaUtil.QuotaCompareInfo quotaCompareInfo; quotaCompareInfo = new QuotaUtil.QuotaCompareInfo(); quotaCompareInfo.currentAccountUuid = currentAccountUuid; quotaCompareInfo.resourceTargetOwnerAccountUuid = resourceTargetOwnerAccountUuid; quotaCompareInfo.quotaName = EipQuotaConstant.EIP_NUM; quotaCompareInfo.quotaValue = eipNumQuota; quotaCompareInfo.currentUsed = usedEipNum; quotaCompareInfo.request = askedEipNum; new QuotaUtil().CheckQuota(quotaCompareInfo); } } };
private void checkQuotaForChangeResourceOwner(APIChangeResourceOwnerMsg msg) { String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = msg.getAccountUuid(); if (new QuotaUtil().isAdminAccount(resourceTargetOwnerAccountUuid)) { return; } // check if change resource owner to self SimpleQuery<AccountResourceRefVO> queryAccResRefVO = dbf.createQuery(AccountResourceRefVO.class); queryAccResRefVO.add(AccountResourceRefVO_.resourceUuid, Op.EQ, msg.getResourceUuid()); AccountResourceRefVO accResRefVO = queryAccResRefVO.find(); String resourceOriginalOwnerAccountUuid = accResRefVO.getOwnerAccountUuid(); if (resourceTargetOwnerAccountUuid.equals(resourceOriginalOwnerAccountUuid)) { throw new ApiMessageInterceptionException(err(IdentityErrors.QUOTA_INVALID_OP, "Invalid ChangeResourceOwner operation." + "Original owner is the same as target owner." + "Current account is [uuid: %s]." + "The resource target owner account[uuid: %s]." + "The resource original owner account[uuid:%s].", currentAccountUuid, resourceTargetOwnerAccountUuid, resourceOriginalOwnerAccountUuid )); } // check quota Map<String, QuotaPair> pairs = new QuotaUtil().makeQuotaPairs(msg.getAccountUuid()); for (Quota quota : messageQuotaMap.get(APIChangeResourceOwnerMsg.class)) { quota.getOperator().checkQuota(msg, pairs); } }
String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = msg.getAccountUuid(); if (new QuotaUtil().isAdminAccount(resourceTargetOwnerAccountUuid)) { return; String resourceType = new QuotaUtil().getResourceType(msg.getResourceUuid()); long volumeSnapshotNumAsked; if (resourceType.equals(VmInstanceVO.class.getSimpleName())) {
throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( currentAccountUuid, VmQuotaConstant.VM_TOTAL_NUM, totalVmNumQuota)); throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( currentAccountUuid, VmQuotaConstant.VM_RUNNING_NUM, runningVmNumQuota)); throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( currentAccountUuid, VmQuotaConstant.VM_RUNNING_CPU_NUM, runningVmCpuNumQuota)); throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( currentAccountUuid, VmQuotaConstant.VM_RUNNING_MEMORY_SIZE, runningVmMemorySizeQuota)); long dataVolumeNumAsked = msg.getDataDiskOfferingUuids().size(); if (dataVolumeNumUsed + dataVolumeNumAsked > dataVolumeNumQuota) { throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( currentAccountUuid, VmQuotaConstant.DATA_VOLUME_NUM, dataVolumeNumQuota)); quotaCompareInfo.currentUsed = allVolumeSizeUsed; quotaCompareInfo.request = allVolumeSizeAsked; new QuotaUtil().CheckQuota(quotaCompareInfo);
@Override public void beforeDeliveryMessage(Message msg) { if (msg instanceof NeedQuotaCheckMessage) { if (((NeedQuotaCheckMessage) msg).getAccountUuid() == null || ((NeedQuotaCheckMessage) msg).getAccountUuid().equals("")) { // skip admin scheduler return; } List<Quota> quotas = acntMgr.getMessageQuotaMap().get(msg.getClass()); if (quotas == null || quotas.size() == 0) { return; } Map<String, Quota.QuotaPair> pairs = new QuotaUtil(). makeQuotaPairs(((NeedQuotaCheckMessage) msg).getAccountUuid()); for (Quota quota : quotas) { quota.getOperator().checkQuota((NeedQuotaCheckMessage) msg, pairs); } } } }, VolumeCreateSnapshotMsg.class, CreateVolumeSnapshotMsg.class);
private void check(APIRecoverDataVolumeMsg msg, Map<String, Quota.QuotaPair> pairs) { String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = new QuotaUtil().getResourceOwnerAccountUuid(msg.getVolumeUuid()); // check data volume num long dataVolumeNumQuota = pairs.get(VmQuotaConstant.DATA_VOLUME_NUM).getValue(); long dataVolumeNumUsed = new VmQuotaUtil().getUsedDataVolumeCount(resourceTargetOwnerAccountUuid); long dataVolumeNumAsked = 1; QuotaUtil.QuotaCompareInfo quotaCompareInfo; { quotaCompareInfo = new QuotaUtil.QuotaCompareInfo(); quotaCompareInfo.currentAccountUuid = currentAccountUuid; quotaCompareInfo.resourceTargetOwnerAccountUuid = resourceTargetOwnerAccountUuid; quotaCompareInfo.quotaName = VmQuotaConstant.DATA_VOLUME_NUM; quotaCompareInfo.quotaValue = dataVolumeNumQuota; quotaCompareInfo.currentUsed = dataVolumeNumUsed; quotaCompareInfo.request = dataVolumeNumAsked; new QuotaUtil().CheckQuota(quotaCompareInfo); } }
@Override public void checkQuota(APIMessage msg, Map<String, Quota.QuotaPair> pairs) { AccountType type = new QuotaUtil().getAccountType(msg.getSession().getAccountUuid()); if (type != AccountType.SystemAdmin) { if (msg instanceof APICreateVmInstanceMsg) { if (((APICreateVmInstanceMsg) msg).getStrategy(). equals(VmCreationStrategy.JustCreate.toString())) { return; } check((APICreateVmInstanceMsg) msg, pairs); } else if (msg instanceof APICreateDataVolumeMsg) { check((APICreateDataVolumeMsg) msg, pairs); } else if (msg instanceof APIRecoverDataVolumeMsg) { check((APIRecoverDataVolumeMsg) msg, pairs); } else if (msg instanceof APIStartVmInstanceMsg) { check((APIStartVmInstanceMsg) msg, pairs); } else if (msg instanceof APIChangeResourceOwnerMsg) { check((APIChangeResourceOwnerMsg) msg, pairs); } else if (msg instanceof APIRecoverVmInstanceMsg) { check((APIRecoverVmInstanceMsg) msg, pairs); } } else { if (msg instanceof APIChangeResourceOwnerMsg) { check((APIChangeResourceOwnerMsg) msg, pairs); } } }
String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = msg.getAccountUuid(); if (new QuotaUtil().isAdminAccount(resourceTargetOwnerAccountUuid)) { return; quotaCompareInfo.currentUsed = imageNumUsed; quotaCompareInfo.request = imageNumAsked; new QuotaUtil().CheckQuota(quotaCompareInfo); quotaCompareInfo.currentUsed = imageSizeUsed; quotaCompareInfo.request = imageSizeAsked; new QuotaUtil().CheckQuota(quotaCompareInfo);
String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = msg.getAccountUuid(); if (new QuotaUtil().isAdminAccount(resourceTargetOwnerAccountUuid)) { return; String resourceType = new QuotaUtil().getResourceType(msg.getResourceUuid()); if (resourceType.equals(VolumeVO.class.getSimpleName())) { String volumeUuid = msg.getResourceUuid();
private void check(APIMessage msg, Quota quota) { Map<String, QuotaPair> pairs = new QuotaUtil().makeQuotaPairs(msg.getSession().getAccountUuid()); quota.getOperator().checkQuota(msg, pairs); if (quota.getQuotaValidators() != null) { for (Quota.QuotaValidator q : quota.getQuotaValidators()) { q.checkQuota(msg, pairs); } } } }
@Transactional(readOnly = true) private void check(APIRecoverImageMsg msg, Map<String, Quota.QuotaPair> pairs) { String currentAccountUuid = msg.getSession().getAccountUuid(); String resourceTargetOwnerAccountUuid = new QuotaUtil().getResourceOwnerAccountUuid(msg.getImageUuid()); quotaCompareInfo.currentUsed = imageNumUsed; quotaCompareInfo.request = imageNumAsked; new QuotaUtil().CheckQuota(quotaCompareInfo); quotaCompareInfo.currentUsed = imageSizeUsed; quotaCompareInfo.request = imageSizeAsked; new QuotaUtil().CheckQuota(quotaCompareInfo);
private void check(APICreateVolumeSnapshotMsg msg, Map<String, Quota.QuotaPair> pairs) { String resourceTargetOwnerUuid = new QuotaUtil().getResourceOwnerAccountUuid(msg.getVolumeUuid()); checkVolumeSnapshotNumQuota(msg.getSession().getAccountUuid(), resourceTargetOwnerUuid, 1, pairs); }
@Override public void checkQuota(APIMessage msg, Map<String, QuotaPair> pairs) { if (!new QuotaUtil().isAdminAccount(msg.getSession().getAccountUuid())) { if (msg instanceof APICreateSecurityGroupMsg) { check((APICreateSecurityGroupMsg) msg, pairs); } } }
private void check(APICreateL2VxlanNetworkMsg msg, Map<String, Quota.QuotaPair> pairs) { long vxlanNum = pairs.get(VxlanNetworkQuotaConstant.VXLAN_NUM).getValue(); long vxlan = getUsedVxlan(msg.getSession().getAccountUuid()); if (vxlan + 1 > vxlanNum) { throw new ApiMessageInterceptionException(new QuotaUtil().buildQuataExceedError( msg.getSession().getAccountUuid(), VxlanNetworkQuotaConstant.VXLAN_NUM, vxlanNum)); } } };
@Transactional(readOnly = true) private void checkImageSizeQuota(long requiredImageSize, String currentAccountUuid, String resourceTargetOwnerAccountUuid, Map<String, Quota.QuotaPair> pairs) { long imageSizeQuota = pairs.get(ImageQuotaConstant.IMAGE_SIZE).getValue(); long imageSizeUsed = new ImageQuotaUtil().getUsedImageSize(resourceTargetOwnerAccountUuid); QuotaUtil.QuotaCompareInfo quotaCompareInfo = new QuotaUtil.QuotaCompareInfo(); quotaCompareInfo.currentAccountUuid = currentAccountUuid; quotaCompareInfo.resourceTargetOwnerAccountUuid = resourceTargetOwnerAccountUuid; quotaCompareInfo.quotaName = ImageQuotaConstant.IMAGE_SIZE; quotaCompareInfo.quotaValue = imageSizeQuota; quotaCompareInfo.currentUsed = imageSizeUsed; quotaCompareInfo.request = requiredImageSize; new QuotaUtil().CheckQuota(quotaCompareInfo); }
@Override public void allocate() { throwExceptionIfIAmTheFirstFlow(); final String vmInstanceUuid = spec.getVmInstance().getUuid(); final String accountUuid = Account.getAccountUuidOfResource(vmInstanceUuid); if (accountUuid == null || AccountConstant.isAdminPermission(accountUuid)) { next(candidates); return; } if (!spec.isFullAllocate()) { new VmQuotaOperator().checkVmCupAndMemoryCapacity(accountUuid, accountUuid, spec.getCpuCapacity(), spec.getMemoryCapacity(), new QuotaUtil().makeQuotaPairs(accountUuid)); next(candidates); return; } new VmQuotaOperator().checkVmInstanceQuota( accountUuid, accountUuid, vmInstanceUuid, new QuotaUtil().makeQuotaPairs(accountUuid)); next(candidates); } }