Use parent's project id in the quota enforcement for some resources

Some of the resources, like e.g. QoS rules don't have tenant or
project_id attribute as they belong to the project to which QoS policy
(their parent) belongs to.
Because of that we should get project id for such resources from their
parent object instead of looking for it in its attributes.

Related-Bug: #2022043
Change-Id: I3e6625eb237ad442ab904f59de5c2959d4090700
This commit is contained in:
Slawek Kaplonski
2024-06-25 12:43:48 +02:00
parent 305e1451bb
commit 8460c3b964

View File

@@ -15,12 +15,14 @@
import collections
from neutron_lib import context
from neutron_lib.db import api as db_api
from neutron_lib import exceptions
from oslo_log import log as logging
from pecan import hooks
from neutron import manager
from neutron.pecan_wsgi.hooks import utils
from neutron import quota
from neutron.quota import resource_registry
@@ -38,8 +40,19 @@ class QuotaEnforcementHook(hooks.PecanHook):
if state.request.method != 'POST' or not resource or not items:
return
plugin = manager.NeutronManager.get_plugin_for_resource(collection)
parent_id = state.request.context.get('parent_id')
parent_project_id = None
if parent_id and any(not x.get('tenant_id') for x in items):
parent_getter = getattr(
plugin, 'get_%s' % utils.get_controller(state).parent)
try:
parent_project_id = parent_getter(
context.get_admin_context(), parent_id).get('project_id')
except exceptions.NotFound:
pass
# Store requested resource amounts grouping them by tenant
deltas = collections.Counter(map(lambda x: x['tenant_id'], items))
deltas = collections.Counter(
map(lambda x: x.get('tenant_id', parent_project_id), items))
# Perform quota enforcement
reservations = []
neutron_context = state.request.context.get('neutron_context')