
Modelled mostly after how Heat does it, but with some added power and also consistency between print_list and print_dict. We have print_list for printing of lists and print_dict for printing of show actions. Both functions take as arguments (aside from the data to print) also an optional dict of custom formatters (allowing to freely format the attributes before printing) and custom labels (allowing to tweak table headers and property names). The list is also sorted by one of the columns. The formatting is showcased on sample formatters and actions - rack-show and rack-list. Fixes bug 1213056 Change-Id: Ic14dbb930a5967e2634c1b4777e6705ab2a370ec
50 lines
1.8 KiB
Python
50 lines
1.8 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 tuskarclient.common.formatting as fmt
|
|
from tuskarclient.common import utils
|
|
from tuskarclient import exc
|
|
|
|
|
|
# TODO(jistr): This is PoC, not final implementation
|
|
@utils.arg('id', metavar="<NAME or ID>", help="Name or ID of rack to show.")
|
|
def do_rack_show(tuskar, args):
|
|
try:
|
|
rack = utils.find_resource(tuskar.racks, args.id)
|
|
except exc.HTTPNotFound:
|
|
raise exc.CommandError("Rack not found: %s" % args.id)
|
|
formatters = {
|
|
'capacities': fmt.capacities_formatter,
|
|
'chassis': fmt.resource_link_formatter,
|
|
'links': fmt.links_formatter,
|
|
'nodes': fmt.resource_links_formatter,
|
|
'resource_class': fmt.resource_link_formatter,
|
|
}
|
|
|
|
rack_dict = rack.to_dict()
|
|
# Workaround for API inconsistency, where empty chassis link
|
|
# prints out as '{}'.
|
|
if 'chassis' in rack_dict and not rack_dict['chassis']:
|
|
del rack_dict['chassis']
|
|
|
|
fmt.print_dict(rack_dict, formatters)
|
|
|
|
|
|
# TODO(jistr): This is PoC, not final implementation
|
|
def do_rack_list(tuskar, args):
|
|
racks = tuskar.racks.list()
|
|
fields = ['id', 'name', 'subnet', 'state', 'nodes']
|
|
labels = {'nodes': '# of nodes'}
|
|
formatters = {'nodes': len}
|
|
|
|
fmt.print_list(racks, fields, formatters, labels)
|