diff options
author | Zuul <zuul@review.opendev.org> | 2021-03-04 22:01:52 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2021-03-04 22:01:52 +0000 |
commit | c756724cda76cd91beefb7686e7f5fcc5f7a5a44 (patch) | |
tree | d2dbea29de42ca5dfc68456ac1751067ec8dbbc6 /openstack_dashboard/management | |
parent | adc0127abbb9e88344bcdf03c8a28ccb52d74539 (diff) | |
parent | b7bb76eb204ef1cbd6df9f416437263a67371ba5 (diff) | |
download | horizon-c756724cda76cd91beefb7686e7f5fcc5f7a5a44.tar.gz |
Merge "Support policy-in-code and deprecated policy"
Diffstat (limited to 'openstack_dashboard/management')
-rw-r--r-- | openstack_dashboard/management/commands/dump_default_policies.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/openstack_dashboard/management/commands/dump_default_policies.py b/openstack_dashboard/management/commands/dump_default_policies.py new file mode 100644 index 000000000..d25353636 --- /dev/null +++ b/openstack_dashboard/management/commands/dump_default_policies.py @@ -0,0 +1,82 @@ +# 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. + +import logging +import sys + +from django.core.management.base import BaseCommand +from oslo_policy import generator +import yaml + + +LOG = logging.getLogger(__name__) + + +def _load_default_policies(namespace): + defaults = generator.get_policies_dict([namespace]) + return defaults.get(namespace) + + +def _format_default_policy(default): + data = { + 'name': default.name, + 'check_str': default.check_str, + 'description': default.description, + } + data['operations'] = getattr(default, 'operations', []) + data['scope_types'] = getattr(default, 'scope_types', None) + + if default.deprecated_for_removal: + data['deprecated_for_removal'] = True + data['deprecated_since'] = default.deprecated_since + data['deprecated_reason'] = default.deprecated_reason + + if default.deprecated_rule: + data['deprecated_rule'] = { + 'name': default.deprecated_rule.name, + 'check_str': default.deprecated_rule.check_str, + } + data['deprecated_since'] = default.deprecated_since + data['deprecated_reason'] = default.deprecated_reason + + return data + + +def _write_yaml_file(policies, output_file): + stream = open(output_file, 'w') if output_file else sys.stdout + yaml.dump(policies, stream=stream) + if output_file: + stream.close() + + +class Command(BaseCommand): + help = ("Dump default policies of back-end services defined in codes " + "as YAML file so that horizon can load default policies.") + + def add_arguments(self, parser): + parser.add_argument( + '--namespace', + required=True, + help='Namespace under "oslo.policy.policies" to query.') + parser.add_argument( + '--output-file', + help='Path of the file to write to. Defaults to stdout.') + + def handle(self, *args, **options): + namespace = options['namespace'] + defaults = _load_default_policies(namespace) + if defaults is None: + LOG.error('The requested namespace "%s" is not found.', namespace) + sys.exit(1) + + policies = [_format_default_policy(default) for default in defaults] + _write_yaml_file(policies, options['output_file']) |