Split v1/shell.py into different files
Splitting v1/shell.py into one file per resource makes the code more organized and in the long run helps to improve code readability. Change-Id: Ia57019548158ffe33a1eca444405107caa327936
This commit is contained in:
parent
af1fd9ef9c
commit
3b901b0134
ironicclient
@ -13,6 +13,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import textwrap
|
||||
@ -24,6 +25,47 @@ from ironicclient import exc
|
||||
from ironicclient.openstack.common import importutils
|
||||
|
||||
|
||||
class HelpFormatter(argparse.HelpFormatter):
|
||||
def start_section(self, heading):
|
||||
# Title-case the headings
|
||||
heading = '%s%s' % (heading[0].upper(), heading[1:])
|
||||
super(HelpFormatter, self).start_section(heading)
|
||||
|
||||
|
||||
def define_command(subparsers, command, callback, cmd_mapper):
|
||||
'''Define a command in the subparsers collection.
|
||||
|
||||
:param subparsers: subparsers collection where the command will go
|
||||
:param command: command name
|
||||
:param callback: function that will be used to process the command
|
||||
'''
|
||||
desc = callback.__doc__ or ''
|
||||
help = desc.strip().split('\n')[0]
|
||||
arguments = getattr(callback, 'arguments', [])
|
||||
|
||||
subparser = subparsers.add_parser(command, help=help,
|
||||
description=desc,
|
||||
add_help=False,
|
||||
formatter_class=HelpFormatter)
|
||||
subparser.add_argument('-h', '--help', action='help',
|
||||
help=argparse.SUPPRESS)
|
||||
cmd_mapper[command] = subparser
|
||||
for (args, kwargs) in arguments:
|
||||
subparser.add_argument(*args, **kwargs)
|
||||
subparser.set_defaults(func=callback)
|
||||
|
||||
|
||||
def define_commands_from_module(subparsers, command_module, cmd_mapper):
|
||||
'''Find all methods beginning with 'do_' in a module, and add them
|
||||
as commands into a subparsers collection.
|
||||
'''
|
||||
for method_name in (a for a in dir(command_module) if a.startswith('do_')):
|
||||
# Commands should be hypen-separated instead of underscores.
|
||||
command = method_name[3:].replace('_', '-')
|
||||
callback = getattr(command_module, method_name)
|
||||
define_command(subparsers, command, callback, cmd_mapper)
|
||||
|
||||
|
||||
# Decorator for cli-args
|
||||
def arg(*args, **kwargs):
|
||||
def _decorator(func):
|
||||
|
@ -173,31 +173,10 @@ class IronicShell(object):
|
||||
self.subcommands = {}
|
||||
subparsers = parser.add_subparsers(metavar='<subcommand>')
|
||||
submodule = utils.import_versioned_module(version, 'shell')
|
||||
self._find_actions(subparsers, submodule)
|
||||
self._find_actions(subparsers, self)
|
||||
|
||||
submodule.enhance_parser(parser, subparsers, self.subcommands)
|
||||
utils.define_commands_from_module(subparsers, self, self.subcommands)
|
||||
return parser
|
||||
|
||||
def _find_actions(self, subparsers, actions_module):
|
||||
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
||||
# I prefer to be hypen-separated instead of underscores.
|
||||
command = attr[3:].replace('_', '-')
|
||||
callback = getattr(actions_module, attr)
|
||||
desc = callback.__doc__ or ''
|
||||
help = desc.strip().split('\n')[0]
|
||||
arguments = getattr(callback, 'arguments', [])
|
||||
|
||||
subparser = subparsers.add_parser(command, help=help,
|
||||
description=desc,
|
||||
add_help=False,
|
||||
formatter_class=HelpFormatter)
|
||||
subparser.add_argument('-h', '--help', action='help',
|
||||
help=argparse.SUPPRESS)
|
||||
self.subcommands[command] = subparser
|
||||
for (args, kwargs) in arguments:
|
||||
subparser.add_argument(*args, **kwargs)
|
||||
subparser.set_defaults(func=callback)
|
||||
|
||||
def _setup_debugging(self, debug):
|
||||
if debug:
|
||||
logging.basicConfig(
|
||||
|
87
ironicclient/v1/chassis_shell.py
Normal file
87
ironicclient/v1/chassis_shell.py
Normal file
@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 Red Hat, Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from ironicclient.common import utils
|
||||
from ironicclient import exc
|
||||
|
||||
|
||||
@utils.arg('chassis', metavar='<chassis>', help="ID of chassis")
|
||||
def do_chassis_show(cc, args):
|
||||
"""Show a chassis."""
|
||||
try:
|
||||
chassis = cc.chassis.get(args.chassis)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Chassis not found: %s' % args.chassis)
|
||||
else:
|
||||
fields = ['uuid', 'description', 'extra']
|
||||
data = dict([(f, getattr(chassis, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
def do_chassis_list(cc, args):
|
||||
"""List chassis."""
|
||||
chassis = cc.chassis.list()
|
||||
field_labels = ['UUID', 'Description']
|
||||
fields = ['uuid', 'description']
|
||||
utils.print_list(chassis, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--description',
|
||||
metavar='<DESCRIPTION>',
|
||||
help='Free text description of the chassis')
|
||||
@utils.arg('--extra',
|
||||
metavar="<key=value>",
|
||||
action='append',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_chassis_create(cc, args):
|
||||
"""Create a new chassis."""
|
||||
field_list = ['description', 'extra']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
chassis = cc.chassis.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(chassis, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('chassis', metavar='<chassis>', help="ID of chassis")
|
||||
def do_chassis_delete(cc, args):
|
||||
"""Delete a chassis."""
|
||||
try:
|
||||
cc.chassis.delete(args.chassis)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Chassis not found: %s' % args.chassis)
|
||||
|
||||
|
||||
@utils.arg('node', metavar='<node>', help="ID of node")
|
||||
def do_node_show(cc, args):
|
||||
"""Show a node."""
|
||||
try:
|
||||
node = cc.node.get(args.node)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Node not found: %s' % args.node)
|
||||
else:
|
||||
fields = ['uuid', 'instance_uuid', 'power_state', 'target_power_state',
|
||||
'provision_state', 'target_provision_state', 'driver',
|
||||
'driver_info', 'properties', 'extra',
|
||||
'created_at', 'updated_at', 'reservation']
|
||||
data = dict([(f, getattr(node, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
88
ironicclient/v1/node_shell.py
Normal file
88
ironicclient/v1/node_shell.py
Normal file
@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 Red Hat, Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from ironicclient.common import utils
|
||||
from ironicclient import exc
|
||||
|
||||
|
||||
@utils.arg('node', metavar='<node>', help="ID of node")
|
||||
def do_node_show(cc, args):
|
||||
"""Show a node."""
|
||||
try:
|
||||
node = cc.node.get(args.node)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Node not found: %s' % args.node)
|
||||
else:
|
||||
fields = ['uuid', 'instance_uuid', 'power_state', 'target_power_state',
|
||||
'provision_state', 'target_provision_state', 'driver',
|
||||
'driver_info', 'properties', 'extra',
|
||||
'created_at', 'updated_at', 'reservation']
|
||||
data = dict([(f, getattr(node, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
def do_node_list(cc, args):
|
||||
"""List nodes."""
|
||||
nodes = cc.node.list()
|
||||
field_labels = ['UUID', 'Instance UUID',
|
||||
'Power State', 'Provisioning State']
|
||||
fields = ['uuid', 'instance_uuid', 'power_state', 'provision_state']
|
||||
utils.print_list(nodes, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--driver',
|
||||
metavar='<DRIVER>',
|
||||
help='Driver used to control the node. [REQUIRED]')
|
||||
@utils.arg('--driver_info',
|
||||
metavar='<key=value>',
|
||||
action='append',
|
||||
help='Key/value pairs used by the driver. '
|
||||
'Can be specified multiple times.')
|
||||
@utils.arg('--properties',
|
||||
metavar='<key=value>',
|
||||
action='append',
|
||||
help='Key/value pairs describing the physical characteristics '
|
||||
'of the node. This is exported to Nova and used by the '
|
||||
'scheduler. Can be specified multiple times.')
|
||||
@utils.arg('--extra',
|
||||
metavar='<key=value>',
|
||||
action='append',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_node_create(cc, args):
|
||||
"""Create a new node."""
|
||||
field_list = ['chassis_id', 'driver', 'driver_info', 'properties', 'extra']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'driver_info')
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
fields = utils.args_array_to_dict(fields, 'properties')
|
||||
node = cc.node.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(node, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('node', metavar='<node>', help="ID of node")
|
||||
def do_node_delete(cc, args):
|
||||
"""Delete a node."""
|
||||
try:
|
||||
cc.node.delete(args.node)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Node not found: %s' % args.node)
|
74
ironicclient/v1/port_shell.py
Normal file
74
ironicclient/v1/port_shell.py
Normal file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 Red Hat, Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from ironicclient.common import utils
|
||||
from ironicclient import exc
|
||||
|
||||
|
||||
@utils.arg('port', metavar='<port>', help="ID of port")
|
||||
def do_port_show(cc, args):
|
||||
"""Show a port."""
|
||||
try:
|
||||
port = cc.port.get(args.port)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Port not found: %s' % args.port)
|
||||
else:
|
||||
fields = ['uuid', 'address', 'extra']
|
||||
data = dict([(f, getattr(port, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
def do_port_list(cc, args):
|
||||
"""List ports."""
|
||||
port = cc.port.list()
|
||||
field_labels = ['UUID', 'Address']
|
||||
fields = ['uuid', 'address']
|
||||
utils.print_list(port, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--address',
|
||||
metavar='<ADDRESS>',
|
||||
help='MAC Address for this port.')
|
||||
@utils.arg('--node_id',
|
||||
metavar='<NODE_ID>',
|
||||
help='ID of the node that this port belongs to.')
|
||||
@utils.arg('--extra',
|
||||
metavar="<key=value>",
|
||||
action='append',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_port_create(cc, args):
|
||||
"""Create a new port."""
|
||||
field_list = ['address', 'extra', 'node_id']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
port = cc.port.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(port, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('port', metavar='<port>', help="ID of port")
|
||||
def do_port_delete(cc, args):
|
||||
"""Delete a port."""
|
||||
try:
|
||||
cc.port.delete(args.port)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Port not found: %s' % args.port)
|
@ -10,175 +10,26 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
from ironicclient.common import utils
|
||||
from ironicclient import exc
|
||||
from ironicclient.v1 import chassis_shell
|
||||
from ironicclient.v1 import node_shell
|
||||
from ironicclient.v1 import port_shell
|
||||
|
||||
COMMAND_MODULES = [
|
||||
chassis_shell,
|
||||
node_shell,
|
||||
port_shell,
|
||||
]
|
||||
|
||||
|
||||
@utils.arg('chassis', metavar='<chassis>', help="ID of chassis")
|
||||
def do_chassis_show(self, args):
|
||||
"""Show a chassis."""
|
||||
try:
|
||||
chassis = self.chassis.get(args.chassis)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Chassis not found: %s' % args.chassis)
|
||||
else:
|
||||
fields = ['uuid', 'description', 'extra']
|
||||
data = dict([(f, getattr(chassis, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
def enhance_parser(parser, subparsers, cmd_mapper):
|
||||
'''Take a basic (nonversioned) parser and enhance it with
|
||||
commands and options specific for this version of API.
|
||||
|
||||
|
||||
def do_chassis_list(self, args):
|
||||
"""List chassis."""
|
||||
chassis = self.chassis.list()
|
||||
field_labels = ['UUID', 'Description']
|
||||
fields = ['uuid', 'description']
|
||||
utils.print_list(chassis, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--description',
|
||||
metavar='<DESCRIPTION>',
|
||||
help='Free text description of the chassis')
|
||||
@utils.arg('--extra',
|
||||
metavar="<key=value>",
|
||||
action='append',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_chassis_create(self, args):
|
||||
"""Create a new chassis."""
|
||||
field_list = ['description', 'extra']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
chassis = self.chassis.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(chassis, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('chassis', metavar='<chassis>', help="ID of chassis")
|
||||
def do_chassis_delete(self, args):
|
||||
"""Delete a chassis."""
|
||||
try:
|
||||
self.chassis.delete(args.chassis)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Chassis not found: %s' % args.chassis)
|
||||
|
||||
|
||||
@utils.arg('node', metavar='<node>', help="ID of node")
|
||||
def do_node_show(self, args):
|
||||
"""Show a node."""
|
||||
try:
|
||||
node = self.node.get(args.node)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Node not found: %s' % args.node)
|
||||
else:
|
||||
fields = ['uuid', 'instance_uuid', 'power_state', 'target_power_state',
|
||||
'provision_state', 'target_provision_state', 'driver',
|
||||
'driver_info', 'properties', 'extra',
|
||||
'created_at', 'updated_at', 'reservation']
|
||||
data = dict([(f, getattr(node, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
def do_node_list(self, args):
|
||||
"""List nodes."""
|
||||
nodes = self.node.list()
|
||||
field_labels = ['UUID', 'Instance UUID',
|
||||
'Power State', 'Provisioning State']
|
||||
fields = ['uuid', 'instance_uuid', 'power_state', 'provision_state']
|
||||
utils.print_list(nodes, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--driver',
|
||||
metavar='<DRIVER>',
|
||||
help='Driver used to control the node. [REQUIRED]')
|
||||
@utils.arg('--driver_info',
|
||||
metavar='<key=value>',
|
||||
help='Key/value pairs used by the driver. '
|
||||
'Can be specified multiple times.')
|
||||
@utils.arg('--properties',
|
||||
metavar='<key=value>',
|
||||
help='Key/value pairs describing the physical characteristics '
|
||||
'of the node. This is exported to Nova and used by the '
|
||||
'scheduler. Can be specified multiple times.')
|
||||
@utils.arg('--extra',
|
||||
metavar='<key=value>',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_node_create(self, args):
|
||||
"""Create a new node."""
|
||||
field_list = ['chassis_id', 'driver', 'driver_info', 'properties', 'extra']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'driver_info')
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
fields = utils.args_array_to_dict(fields, 'properties')
|
||||
node = self.node.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(node, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('node', metavar='<node>', help="ID of node")
|
||||
def do_node_delete(self, args):
|
||||
"""Delete a node."""
|
||||
try:
|
||||
self.node.delete(args.node)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Node not found: %s' % args.node)
|
||||
|
||||
|
||||
@utils.arg('port', metavar='<port>', help="ID of port")
|
||||
def do_port_show(self, args):
|
||||
"""Show a port."""
|
||||
try:
|
||||
port = self.port.get(args.port)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Port not found: %s' % args.port)
|
||||
else:
|
||||
fields = ['uuid', 'address', 'extra']
|
||||
data = dict([(f, getattr(port, f, '')) for f in fields])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
def do_port_list(self, args):
|
||||
"""List ports."""
|
||||
port = self.port.list()
|
||||
field_labels = ['UUID', 'Address']
|
||||
fields = ['uuid', 'address']
|
||||
utils.print_list(port, fields, field_labels, sortby=1)
|
||||
|
||||
|
||||
@utils.arg('--address',
|
||||
metavar='<ADDRESS>',
|
||||
help='MAC Address for this port.')
|
||||
@utils.arg('--node_id',
|
||||
metavar='<NODE_ID>',
|
||||
help='ID of the node that this port belongs to.')
|
||||
@utils.arg('--extra',
|
||||
metavar="<key=value>",
|
||||
action='append',
|
||||
help="Record arbitrary key/value metadata. "
|
||||
"Can be specified multiple times.")
|
||||
def do_port_create(self, args):
|
||||
"""Create a new port."""
|
||||
field_list = ['address', 'extra', 'node_id']
|
||||
fields = dict((k, v) for (k, v) in vars(args).items()
|
||||
if k in field_list and not (v is None))
|
||||
fields = utils.args_array_to_dict(fields, 'extra')
|
||||
port = self.port.create(**fields)
|
||||
|
||||
field_list.append('uuid')
|
||||
data = dict([(f, getattr(port, f, '')) for f in field_list])
|
||||
utils.print_dict(data, wrap=72)
|
||||
|
||||
|
||||
@utils.arg('port', metavar='<port>', help="ID of port")
|
||||
def do_port_delete(self, args):
|
||||
"""Delete a port."""
|
||||
try:
|
||||
self.port.delete(args.port)
|
||||
except exc.HTTPNotFound:
|
||||
raise exc.CommandError('Port not found: %s' % args.port)
|
||||
:param parser: top level parser :param subparsers: top level
|
||||
parser's subparsers collection where subcommands will go
|
||||
'''
|
||||
for command_module in COMMAND_MODULES:
|
||||
utils.define_commands_from_module(subparsers, command_module,
|
||||
cmd_mapper)
|
||||
|
Loading…
x
Reference in New Issue
Block a user