Use cliff instead of cliutils

The cliutils.py (magnum/openstack/common/cliutils.py) has been graduate
[1]. We need to remove the dependency from Magnum and replace it with
cliff [2]. This patch refactor the Magnum template manager by cliff.

[1] https://www.mail-archive.com/openstack-dev@lists.openstack.org/
msg67857.html
[2] http://docs.openstack.org/developer/cliff/

Change-Id: I25e2e2f653bdf6d1f484519894e00aa54f6477cc
Closes-Bug: #1528867
This commit is contained in:
houming-wang 2015-12-24 04:05:32 -05:00
parent 783713efd3
commit 6cf61f1421
2 changed files with 79 additions and 60 deletions

View File

@ -12,15 +12,17 @@
# under the License. # under the License.
"""Starter script for magnum-template-manage.""" """Starter script for magnum-template-manage."""
import operator import sys
from cliff import app
from cliff import commandmanager
from cliff import lister
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging
from magnum.conductor import template_definition as tdef from magnum.conductor import template_definition as tdef
from magnum.openstack.common import cliutils from magnum import version
LOG = logging.getLogger(__name__)
CONF = cfg.CONF CONF = cfg.CONF
@ -28,69 +30,85 @@ def is_enabled(name):
return name in CONF.bay.enabled_definitions return name in CONF.bay.enabled_definitions
def print_rows(rows): class TemplateList(lister.Lister):
fields = ['name', 'enabled'] """List templates"""
field_labels = ['Name', 'Enabled']
if CONF.command.details: def _print_rows(self, parsed_args, rows):
fields.extend(['server_type', 'os', 'coe']) fields = ['name', 'enabled']
field_labels.extend(['Server_Type', 'OS', 'COE']) field_labels = ['Name', 'Enabled']
if CONF.command.paths:
fields.append('path')
field_labels.append('Template Path')
formatters = {key: operator.itemgetter(key) for key in fields} if parsed_args.details:
fields.extend(['server_type', 'os', 'coe'])
field_labels.extend(['Server_Type', 'OS', 'COE'])
if parsed_args.paths:
fields.append('path')
field_labels.append('Template Path')
return field_labels, [tuple([row[field] for field in fields])
for row in rows]
cliutils.print_list(rows, fields, def get_parser(self, prog_name):
formatters=formatters, parser = super(TemplateList, self).get_parser(prog_name)
field_labels=field_labels) parser.add_argument('-d', '--details',
action='store_true',
dest='details',
help=('display the bay types provided by ')
('each template'))
parser.add_argument('-p', '--paths',
action='store_true',
dest='paths',
help='display the path to each template file')
group = parser.add_mutually_exclusive_group()
group.add_argument('--enabled', action='store_true', dest='enabled',
help="display only enabled templates")
group.add_argument('--disabled', action='store_true', dest='disabled',
help="display only disabled templates")
return parser
def take_action(self, parsed_args):
rows = []
for entry_point, cls in tdef.TemplateDefinition.load_entry_points():
name = entry_point.name
if ((is_enabled(name) and not parsed_args.disabled) or
(not is_enabled(name) and not parsed_args.enabled)):
definition = cls()
template = dict(name=name, enabled=is_enabled(name),
path=definition.template_path)
if parsed_args.details:
for bay_type in definition.provides:
row = dict()
row.update(template)
row.update(bay_type)
rows.append(row)
else:
rows.append(template)
return self._print_rows(parsed_args, rows)
def list_templates(): class TemplateCommandManager(commandmanager.CommandManager):
rows = [] COMMANDS = {
"list-templates": TemplateList,
}
for entry_point, cls in tdef.TemplateDefinition.load_entry_points(): def load_commands(self, namespace):
name = entry_point.name for name, command_class in self.COMMANDS.items():
if ((is_enabled(name) and not CONF.command.disabled) or self.add_command(name, command_class)
(not is_enabled(name) and not CONF.command.enabled)):
definition = cls()
template = dict(name=name, enabled=is_enabled(name),
path=definition.template_path)
if CONF.command.details:
for bay_type in definition.provides:
row = dict()
row.update(template)
row.update(bay_type)
rows.append(row)
else:
rows.append(template)
print_rows(rows)
def add_command_parsers(subparsers): class TemplateManager(app.App):
parser = subparsers.add_parser('list-templates') def __init__(self):
parser.set_defaults(func=list_templates) super(TemplateManager, self).__init__(
description='Magnum Template Manager',
parser.add_argument('-d', '--details', action='store_true', version=version.version_info,
help='display the bay types provided by each template') command_manager=TemplateCommandManager(None),
parser.add_argument('-p', '--paths', action='store_true', deferred_help=True)
help='display the path to each template file')
group = parser.add_mutually_exclusive_group()
group.add_argument('--enabled', action='store_true',
help="display only enabled templates")
group.add_argument('--disabled', action='store_true',
help="display only disabled templates")
def main(): def main(args=None):
command_opt = cfg.SubCommandOpt('command', if args is None:
title='Command', args = sys.argv[1:]
help='Available commands', return TemplateManager().run(args)
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
CONF(project='magnum')
CONF.command.func()

View File

@ -12,6 +12,7 @@ SQLAlchemy<1.1.0,>=0.9.9
WSME>=0.8 WSME>=0.8
WebOb>=1.2.3 WebOb>=1.2.3
alembic>=0.8.0 alembic>=0.8.0
cliff>=1.15.0 # Apache-2.0
decorator>=3.4.0 decorator>=3.4.0
docker-py>=1.4.0 # Apache-2.0 docker-py>=1.4.0 # Apache-2.0
enum34;python_version=='2.7' or python_version=='2.6' enum34;python_version=='2.7' or python_version=='2.6'