def __call__(names, config, verbose=False, refresh=False):
# TODO?: we might want to embed get_resource_inventory()
# within ConfigManager (even though it would make it NICEMAN specific)
# This would allow to make more sensible error messages etc
cm = ResourceManager.get_config_manager(config)
inventory_path = cm.getpath('general', 'inventory_file')
inventory = ResourceManager.get_inventory(inventory_path)
id_length = 19 # todo: make it possible to output them long
template = '{:<20} {:<20} {:<%(id_length)s} {:<10}' % locals()
ui.message(template.format('RESOURCE NAME', 'TYPE', 'ID', 'STATUS'))
ui.message(template.format('-------------', '----', '--', '------'))
for name in sorted(inventory):
if name.startswith('_'):
continue
# if refresh:
inventory_resource = inventory[name]
try:
config = dict(cm.items(inventory_resource['type'].split('-')[0]))
except NoSectionError:
config = {}
config.update(inventory_resource)
env_resource = ResourceManager.factory(config)
try:
if refresh:
env_resource.connect()
# TODO: handle the actual refreshing in the inventory
inventory_resource['id'] = env_resource.id
inventory_resource['status'] = env_resource.status
if not env_resource.id:
# continue # A missing ID indicates a deleted resource.
inventory_resource['id'] = 'DELETED'
except ResourceError as exc:
ui.error("%s resource query error: %s" % (name, exc_str(exc)))
for f in 'id', 'status':
inventory_resource[f] = inventory_resource.get(f, "?")
msgargs = (
name,
inventory_resource['type'],
inventory_resource['id'][:id_length],
inventory_resource['status']
)
ui.message(template.format(*msgargs))
lgr.debug('list result: {}, {}, {}, {}'.format(*msgargs))
# if not refresh:
# ui.message('(Use --refresh option to view current status.)')
#
# if refresh:
# niceman.interface.base.set_resource_inventory(inventory)
评论列表
文章目录