diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index d701d37e..ac1a9920 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -177,9 +177,10 @@ def _format_rule_default_yaml(default, include_help=True, comment_rule=True, op = "" if hasattr(default, 'operations'): for operation in default.operations: - op += ('# %(method)s %(path)s\n' % - {'method': operation['method'], - 'path': operation['path']}) + if operation['method'] and operation['path']: + op += ('# %(method)s %(path)s\n' % + {'method': operation['method'], + 'path': operation['path']}) intended_scope = "" if getattr(default, 'scope_types', None) is not None: intended_scope = ( @@ -427,7 +428,8 @@ def _convert_policy_json_to_yaml(namespace, policy_file, output_file=None): continue file_rule_check_str = file_policies.pop(default_rule.name) # Some rules might be still RuleDefault object so let's prepare - # empty 'operations' list for those. + # empty 'operations' list and rule name as description for + # those. operations = [{ 'method': '', 'path': '' @@ -441,7 +443,7 @@ def _convert_policy_json_to_yaml(namespace, policy_file, output_file=None): file_rule = policy.DocumentedRuleDefault( default_rule.name, file_rule_check_str, - default_rule.description, + default_rule.description or default_rule.name, operations, default_rule.deprecated_rule, default_rule.deprecated_for_removal, diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index 28355369..8df0ec85 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -861,15 +861,9 @@ class ConvertJsonToYamlTestCase(base.PolicyBaseTestCase): deprecated_since='ussuri', scope_types=['system'] ), - policy.DocumentedRuleDefault( + policy.RuleDefault( name='rule2_name', check_str='rule:admin', - description='test_rule2', - operations=[{'path': '/test', 'method': 'PUT'}], - deprecated_rule=deprecated_policy, - deprecated_reason='testing2', - deprecated_since='ussuri', - scope_types=['system', 'project'] ) ] self.extensions = [] @@ -886,9 +880,7 @@ class ConvertJsonToYamlTestCase(base.PolicyBaseTestCase): # Intended scope(s): system #"rule1_name": "rule:admin" -# test_rule2 -# PUT /test -# Intended scope(s): system, project +# rule2_name "rule2_name": "rule:overridden" # WARNING: Below rules are either deprecated rules @@ -959,9 +951,7 @@ class ConvertJsonToYamlTestCase(base.PolicyBaseTestCase): def test_overridden_rules_uncommented_in_yaml_file(self): converted_policy_data = self._test_convert_json_to_yaml_file() - uncommented_overridden_rule = '''# test_rule2 -# PUT /test -# Intended scope(s): system, project + uncommented_overridden_rule = '''# rule2_name "rule2_name": "rule:overridden" '''