Files
neutron/neutron/pecan_wsgi/hooks/quota_enforcement.py
Brandon Logan 6659428669 Pecan routing for agent schedulers
For pecan to support existing agent scheduler controllers, a couple of shim
pecan controllers need to be added.  These shim controllers will be used if
there are extensions that have legacy controllers that have not been
registered.  The shim controller is just a passthrough to those legacy
controllers.  This may have the added benefit of support existing out of
tree extensions that have defined their legacy extension controllers the
same way.

Changes to how the router(s) controllers determines whether something
is a member action has been changed a bit to support this.

Closes-Bug: #1552978
Co-Authored-By: Kevin Benton <kevin@benton.pub>
Change-Id: Icec56676d83b604c3db3377838076d6429d61e48
2016-03-08 06:35:49 +00:00

74 lines
2.9 KiB
Python

# Copyright (c) 2015 Mirantis, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
from oslo_log import log as logging
from pecan import hooks
from neutron.common import exceptions
from neutron import manager
from neutron import quota
from neutron.quota import resource_registry
LOG = logging.getLogger(__name__)
class QuotaEnforcementHook(hooks.PecanHook):
priority = 130
def before(self, state):
resource = state.request.context.get('resource')
items = state.request.context.get('resources')
if state.request.method != 'POST' or not resource or not items:
return
plugin = manager.NeutronManager.get_plugin_for_resource(resource)
# Store requested resource amounts grouping them by tenant
deltas = collections.Counter(map(lambda x: x['tenant_id'], items))
# Perform quota enforcement
reservations = []
neutron_context = state.request.context.get('neutron_context')
for (tenant_id, delta) in deltas.items():
try:
reservation = quota.QUOTAS.make_reservation(
neutron_context,
tenant_id,
{resource: delta},
plugin)
LOG.debug("Made reservation on behalf of %(tenant_id)s "
"for: %(delta)s",
{'tenant_id': tenant_id, 'delta': {resource: delta}})
reservations.append(reservation)
except exceptions.QuotaResourceUnknown as e:
# Quotas cannot be enforced on this resource
LOG.debug(e)
# Save the reservations in the request context so that they can be
# retrieved in the 'after' hook
state.request.context['reservations'] = reservations
def after(self, state):
# Commit reservation(s)
reservations = state.request.context.get('reservations')
if not reservations:
return
neutron_context = state.request.context.get('neutron_context')
with neutron_context.session.begin():
# Commit the reservation(s)
for reservation in reservations:
quota.QUOTAS.commit_reservation(
neutron_context, reservation.reservation_id)
resource_registry.set_resources_dirty(neutron_context)