horizon/openstack_dashboard/management/commands/dump_default_policies.py
Akihiro Motoki b7bb76eb20 Support policy-in-code and deprecated policy
This commit allows horizon to handle deprecated policy rules.
The approach is explained in the document updated by this change.

oslo.policy requirement is updated. oslo.policy 3.2.0 is chosen
just because it is the first release in Victoria cycle.
requirements.txt and lower-constraints.txt are updated accordingly
including oslo.policy dependencies.

Change-Id: If5059d03f6bd7e94796065aa1b51c0c23ac85f5e
2021-01-27 14:02:27 +00:00

83 lines
2.7 KiB
Python

# 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'])