def sync_quota(resource, owner, zone): owner = AccountService.get_user_by_name(owner) zone = ZoneModel.get_zone_by_name(zone) update_map = get_usage(resource, owner, zone) for quota_type, used in update_map.items(): try: # quota_model = QuotaModel.objects.filter(user=owner, zone=zone, quota_type=quota_type).first() # if not quota_model: # old_quota_model = QuotaModel.objects.filter(user=owner, quota_type=quota_type).first() # capacity = old_quota_model.capacity # quota_model = QuotaModel(user=owner, zone=zone, quota_type=quota_type, capacity=capacity) # quota_model.used = used # quota_model.save() QuotaModel.objects.update_or_create(user=owner, zone=zone, quota_type=quota_type, defaults={'used': used}) except Exception as exc: logger.debug("sync_quota exception happened %s ", exc.message)
def consume_quota(resource, owner, zone, req_data): owner = AccountService.get_user_by_name(owner) zone = ZoneModel.get_zone_by_name(zone) used = get_usage(resource, owner, zone) total = get_total(resource, owner, zone) needs = get_needs(resource, req_data) unenough_quotas = [] for resource_type in needs: if needs[resource_type] + used[resource_type] > total[resource_type]: unenough_quotas.append(resource_type) if len(unenough_quotas) > 0: return False, unenough_quotas else: for quota_type, need in needs.items(): new_used = needs[resource_type] + used[resource_type] QuotaModel.objects.update_or_create(user=owner, zone=zone, quota_type=quota_type, defaults={'used': new_used}) return True, []