Implement enforcement model logic in Manager

This commit adds the necessary bits to return the enforcement model
used by a deployment in the limit Manager. A subsequent patch will
expose this functionality via the API.

This commit also starts introdcuing the concept of enforcement
models and puts the flat model into its own module. This will make
more sense and be easier to maintain as we implement more models.

Change-Id: I32227eb0023e6b6ce699909fabb60a63a07f0969
Related-Bug: 1765193
Related-Bug: 1768572
This commit is contained in:
Lance Bragstad 2018-04-19 15:22:50 +00:00 committed by wangxiyuan
parent a558eb0bab
commit fd4b737133
4 changed files with 77 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from keystone.common import manager
from keystone.common import provider_api
import keystone.conf
from keystone import exception
from keystone.limit import models
CONF = keystone.conf.CONF
@ -35,6 +36,10 @@ class Manager(manager.Manager):
unified_limit_driver = CONF.unified_limit.driver
super(Manager, self).__init__(unified_limit_driver)
self.enforcement_model = models.get_enforcement_model_from_config(
CONF.unified_limit.enforcement_model
)
def _assert_resource_exist(self, unified_limit, target):
try:
service_id = unified_limit.get('service_id')
@ -56,6 +61,13 @@ class Manager(manager.Manager):
raise exception.ValidationError(attribute='project_id',
target=target)
def get_model(self):
"""Return information of the configured enforcement model."""
return {
'name': self.enforcement_model.name,
'description': self.enforcement_model.description
}
def create_registered_limits(self, registered_limits):
for registered_limit in registered_limits:
self._assert_resource_exist(registered_limit, 'registered_limit')

View File

@ -0,0 +1,29 @@
# 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.
from keystone.limit.models import flat
def get_enforcement_model_from_config(enforcement_model):
"""Factory that returns an enforcement model object based on configuration.
:param enforcement_model str: A string, usually from a configuration
option, representing the name of the
enforcement model
:returns: an `Model` object
"""
# NOTE(lbragstad): The configuration option set is strictly checked by the
# ``oslo.config`` object. If someone passes in a garbage value, it will
# fail before it gets to this point.
if enforcement_model == 'flat':
return flat.Model()

View File

@ -0,0 +1,22 @@
# 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.
# TODO(lbragstad): This should inherit from an abstract interface so that we
# ensure all models implement the same things.
class Model(object):
name = 'flat'
description = (
'Limit enforcement and validation does not take project hierarchy '
'into consideration.'
)

View File

@ -394,6 +394,20 @@ class RegisteredLimitTests(object):
class LimitTests(object):
def test_default_enforcement_model_is_flat(self):
expected = {
'description': ('Limit enforcement and validation does not take '
'project hierarchy into consideration.'),
'name': 'flat'
}
self.assertEqual(expected, PROVIDERS.unified_limit_api.get_model())
def test_registering_unsupported_enforcement_model_fails(self):
self.assertRaises(
ValueError, self.config_fixture.config, group='unified_limit',
enforcement_model=uuid.uuid4().hex
)
def test_create_limit(self):
# create one, return it.
limit_1 = unit.new_limit_ref(