diff options
author | Anthony Washington <anthony.washington@intel.com> | 2017-03-08 21:34:54 +0000 |
---|---|---|
committer | Anthony Washington <anthony.washington@intel.com> | 2017-03-20 21:17:09 +0000 |
commit | c4ea173b8195bfb238e1c41b76efdc97b7ab0b98 (patch) | |
tree | 251f56ca22b3716d96693b35bd089b3384e581bd | |
parent | 395177145622a5c37b8cfb53989084e2ff03ad98 (diff) | |
download | oslo-policy-c4ea173b8195bfb238e1c41b76efdc97b7ab0b98.tar.gz |
oslopolicy-sample-generator description support
With the idea of adding a more descriptive policy rule so
operators can understand what a policy rule controls. This
patch ensures `oslopolicy-sample-generator` adds the
api(s) and method(s) {if given} to policy description.
Change-Id: If4ca9bc191ac263f861373c432a1fafc3f7d596e
-rw-r--r-- | oslo_policy/generator.py | 13 | ||||
-rw-r--r-- | oslo_policy/tests/test_generator.py | 21 |
2 files changed, 25 insertions, 9 deletions
diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index 14284ad..45bb4ae 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -101,18 +101,25 @@ def _format_help_text(description): def _format_rule_default_yaml(default, include_help=True): - """Create a yaml node from the provided policy.RuleDefault. + """Create a yaml node from policy.RuleDefault or policy.DocumentedRuleDefault. - :param default: A policy.RuleDefault object + :param default: A policy.RuleDefault or policy.DocumentedRuleDefault object :returns: A string containing a yaml representation of the RuleDefault """ text = ('"%(name)s": "%(check_str)s"\n' % {'name': default.name, 'check_str': default.check_str}) + op = "" + if hasattr(default, 'operations'): + for operation in default.operations: + op += ('# %(method)s %(path)s\n' % + {'method': operation['method'], 'path': operation['path']}) if include_help: - text = ('%(help)s\n#%(text)s\n' % + text = ('%(help)s\n%(op)s#%(text)s\n' % {'help': _format_help_text(default.description), + 'op': op, 'text': text}) + return text diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index dca75f9..7452f3f 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -26,12 +26,17 @@ from oslo_policy.tests import base OPTS = {'base_rules': [policy.RuleDefault('admin', 'is_admin:True', description='Basic admin check'), - policy.RuleDefault('owner', - 'project_id:%(project_id)s', - description='This is a long ' - 'description to check ' - 'that line wrapping ' - 'functions properly')], + policy.DocumentedRuleDefault('owner', + ('project_id:%' + '(project_id)s'), + 'This is a long ' + 'description to check ' + 'that line wrapping ' + 'functions properly', + [{'path': '/foo/', + 'method': 'GET'}, + {'path': '/test/', + 'method': 'POST'}])], 'custom_field': [policy.RuleDefault('shared', 'field:networks:shared=True')], 'rules': [policy.RuleDefault('admin_or_owner', @@ -93,6 +98,8 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase): # This is a long description to check that line wrapping functions # properly +# GET /foo/ +# POST /test/ #"owner": "project_id:%(project_id)s" # @@ -130,6 +137,8 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase): # This is a long description to check that line wrapping functions # properly +# GET /foo/ +# POST /test/ #"owner": "project_id:%(project_id)s" # |