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
This commit is contained in:
Anthony Washington 2017-03-08 21:34:54 +00:00
parent 3951771456
commit c4ea173b81
2 changed files with 25 additions and 9 deletions

View File

@ -101,18 +101,25 @@ def _format_help_text(description):
def _format_rule_default_yaml(default, include_help=True): 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 :returns: A string containing a yaml representation of the RuleDefault
""" """
text = ('"%(name)s": "%(check_str)s"\n' % text = ('"%(name)s": "%(check_str)s"\n' %
{'name': default.name, {'name': default.name,
'check_str': default.check_str}) '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: 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), {'help': _format_help_text(default.description),
'op': op,
'text': text}) 'text': text})
return text return text

View File

@ -26,12 +26,17 @@ from oslo_policy.tests import base
OPTS = {'base_rules': [policy.RuleDefault('admin', 'is_admin:True', OPTS = {'base_rules': [policy.RuleDefault('admin', 'is_admin:True',
description='Basic admin check'), description='Basic admin check'),
policy.RuleDefault('owner', policy.DocumentedRuleDefault('owner',
'project_id:%(project_id)s', ('project_id:%'
description='This is a long ' '(project_id)s'),
'description to check ' 'This is a long '
'that line wrapping ' 'description to check '
'functions properly')], 'that line wrapping '
'functions properly',
[{'path': '/foo/',
'method': 'GET'},
{'path': '/test/',
'method': 'POST'}])],
'custom_field': [policy.RuleDefault('shared', 'custom_field': [policy.RuleDefault('shared',
'field:networks:shared=True')], 'field:networks:shared=True')],
'rules': [policy.RuleDefault('admin_or_owner', '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 # This is a long description to check that line wrapping functions
# properly # properly
# GET /foo/
# POST /test/
#"owner": "project_id:%(project_id)s" #"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 # This is a long description to check that line wrapping functions
# properly # properly
# GET /foo/
# POST /test/
#"owner": "project_id:%(project_id)s" #"owner": "project_id:%(project_id)s"
# #