From 622bba1ab9b17e61bac2b1b135bd6ebafec71a64 Mon Sep 17 00:00:00 2001 From: John Garbutt Date: Fri, 9 Jun 2017 11:25:25 +0100 Subject: [PATCH] Use oslo.polcy DocumentedRuleDefault Remove the in tree hack of create_rule_default, and call through to the official oslo implementation of either DocumentedRuleDefault or RuleDefault depending on if a list of API operations is required. Change-Id: I9cb184c30f7af4f8ef937aa4ca388c42c427ca4e Implements: blueprint policy-docs --- nova/policies/base.py | 35 ++----------------- nova/policies/cells_scheduler.py | 12 +++---- nova/tests/unit/test_policy.py | 58 -------------------------------- 3 files changed, 8 insertions(+), 97 deletions(-) diff --git a/nova/policies/base.py b/nova/policies/base.py index 3bc1deb3e888..4c24c97e2178 100644 --- a/nova/policies/base.py +++ b/nova/policies/base.py @@ -28,39 +28,10 @@ rules = [ ] -def _unwrap_text(text): - - def _split_paragraphs(lines): - output = [] - for line in lines.split('\n'): - if line: - output.append(line.strip()) - elif output: - yield ' '.join(output) - output = [] - - if output: - yield ' '.join(output) - - return '\n\n'.join([x for x in _split_paragraphs(text)]) - - +# TODO(johngarbutt) we can remove this now def create_rule_default(name, check_str, description, operations): - # TODO(sneti): use DocumentedRuleDefault instead of RuleDefault when - # oslo.policy library is bumped to 1.21.0. The formatted_description hack - # can be removed then. - ops = "" - for operation in operations: - ops += ('%(method)s %(path)s\n' % - {'method': operation['method'], - 'path': operation['path']}) - template = """%(description)s\n\n%(operations)s\n""" - formatted_description = template % { - "description": _unwrap_text(description), - "operations": ops, - } - - return policy.RuleDefault(name, check_str, formatted_description) + return policy.DocumentedRuleDefault(name, check_str, + description, operations) def list_rules(): diff --git a/nova/policies/cells_scheduler.py b/nova/policies/cells_scheduler.py index bc0020c558bb..06d68bd1bf54 100644 --- a/nova/policies/cells_scheduler.py +++ b/nova/policies/cells_scheduler.py @@ -13,29 +13,27 @@ # License for the specific language governing permissions and limitations # under the License. -from nova.policies import base +from oslo_policy import policy POLICY_ROOT = 'cells_scheduler_filter:%s' cells_scheduler_policies = [ - base.create_rule_default( + policy.RuleDefault( POLICY_ROOT % 'DifferentCellFilter', 'is_admin:True', """Different cell filter to route a build away from a particular cell This policy is read by nova-scheduler process. -""", - []), - base.create_rule_default( +"""), + policy.RuleDefault( POLICY_ROOT % 'TargetCellFilter', 'is_admin:True', """Target cell filter to route a build to a particular cell This policy is read by nova-scheduler process. -""", - []) +""") ] diff --git a/nova/tests/unit/test_policy.py b/nova/tests/unit/test_policy.py index ad671cfedd55..c8db48840c42 100644 --- a/nova/tests/unit/test_policy.py +++ b/nova/tests/unit/test_policy.py @@ -25,7 +25,6 @@ import requests_mock import nova.conf from nova import context from nova import exception -from nova.policies import base from nova import policy from nova import test from nova.tests.unit import fake_policy @@ -236,63 +235,6 @@ class AdminRolePolicyTestCase(test.NoDBTestCase): self.context, action, self.target) -class PolicyDocsTestCase(test.NoDBTestCase): - - def test_create_rule_default(self): - rule_default = base.create_rule_default( - "name", "check_str", "description goes in here", - [{'method': 'GET', 'path': '/test_url'}, - {'method': 'POST', 'path': '/test_url'}]) - - expected = """description goes in here - -GET /test_url -POST /test_url - -""" - self.assertEqual(expected, rule_default.description) - self.assertEqual("name", rule_default.name) - self.assertEqual("check_str", rule_default.check_str) - - def test_create_rule_default_improper_formatting(self): - """Ensure we correctly handle various formatting misdemeanours.""" - rule_default = base.create_rule_default( - "name", - "check_str", - """Joe's special policy. - -This is Joe's special policy. Joe writes -really, really long sentences that should really be wrapped better than \ -they are. -Unfortunately we can't expect him -to just do -things -right, so we must fix the problem ourselves. - -Oh, Joe. You so silly. - -""", - [{'method': 'GET', 'path': '/test_url'}, - {'method': 'POST', 'path': '/test_url'}]) - - expected = """Joe's special policy. - -This is Joe's special policy. Joe writes really, really long sentences \ -that should really be wrapped better than they are. Unfortunately we \ -can't expect him to just do things right, so we must fix the problem \ -ourselves. - -Oh, Joe. You so silly. - -GET /test_url -POST /test_url - -""" - self.assertEqual(expected, rule_default.description) - self.assertEqual("name", rule_default.name) - self.assertEqual("check_str", rule_default.check_str) - - class RealRolePolicyTestCase(test.NoDBTestCase): def setUp(self): super(RealRolePolicyTestCase, self).setUp()