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,36 +30,54 @@ 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):
"""List templates"""
def _print_rows(self, parsed_args, rows):
fields = ['name', 'enabled'] fields = ['name', 'enabled']
field_labels = ['Name', 'Enabled'] field_labels = ['Name', 'Enabled']
if CONF.command.details: if parsed_args.details:
fields.extend(['server_type', 'os', 'coe']) fields.extend(['server_type', 'os', 'coe'])
field_labels.extend(['Server_Type', 'OS', 'COE']) field_labels.extend(['Server_Type', 'OS', 'COE'])
if CONF.command.paths: if parsed_args.paths:
fields.append('path') fields.append('path')
field_labels.append('Template Path') field_labels.append('Template Path')
return field_labels, [tuple([row[field] for field in fields])
for row in rows]
formatters = {key: operator.itemgetter(key) for key in fields} def get_parser(self, prog_name):
parser = super(TemplateList, self).get_parser(prog_name)
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')
cliutils.print_list(rows, fields, group = parser.add_mutually_exclusive_group()
formatters=formatters, group.add_argument('--enabled', action='store_true', dest='enabled',
field_labels=field_labels) help="display only enabled templates")
group.add_argument('--disabled', action='store_true', dest='disabled',
help="display only disabled templates")
return parser
def list_templates(): def take_action(self, parsed_args):
rows = [] rows = []
for entry_point, cls in tdef.TemplateDefinition.load_entry_points(): for entry_point, cls in tdef.TemplateDefinition.load_entry_points():
name = entry_point.name name = entry_point.name
if ((is_enabled(name) and not CONF.command.disabled) or if ((is_enabled(name) and not parsed_args.disabled) or
(not is_enabled(name) and not CONF.command.enabled)): (not is_enabled(name) and not parsed_args.enabled)):
definition = cls() definition = cls()
template = dict(name=name, enabled=is_enabled(name), template = dict(name=name, enabled=is_enabled(name),
path=definition.template_path) path=definition.template_path)
if CONF.command.details: if parsed_args.details:
for bay_type in definition.provides: for bay_type in definition.provides:
row = dict() row = dict()
row.update(template) row.update(template)
@ -66,31 +86,29 @@ def list_templates():
else: else:
rows.append(template) rows.append(template)
print_rows(rows) return self._print_rows(parsed_args, rows)
def add_command_parsers(subparsers): class TemplateCommandManager(commandmanager.CommandManager):
parser = subparsers.add_parser('list-templates') COMMANDS = {
parser.set_defaults(func=list_templates) "list-templates": TemplateList,
}
parser.add_argument('-d', '--details', action='store_true', def load_commands(self, namespace):
help='display the bay types provided by each template') for name, command_class in self.COMMANDS.items():
parser.add_argument('-p', '--paths', action='store_true', self.add_command(name, command_class)
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(): class TemplateManager(app.App):
command_opt = cfg.SubCommandOpt('command', def __init__(self):
title='Command', super(TemplateManager, self).__init__(
help='Available commands', description='Magnum Template Manager',
handler=add_command_parsers) version=version.version_info,
CONF.register_cli_opt(command_opt) command_manager=TemplateCommandManager(None),
deferred_help=True)
CONF(project='magnum')
CONF.command.func() def main(args=None):
if args is None:
args = sys.argv[1:]
return TemplateManager().run(args)

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'