2011-08-22 15:13:26 -04:00
|
|
|
import uuid
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
import prettytable
|
|
|
|
|
2011-08-22 15:13:26 -04:00
|
|
|
from novaclient import exceptions
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
# Decorator for cli-args
|
|
|
|
def arg(*args, **kwargs):
|
|
|
|
def _decorator(func):
|
|
|
|
# Because of the sematics of decorator composition if we just append
|
|
|
|
# to the options list positional options will appear to be backwards.
|
|
|
|
func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
|
|
|
|
return func
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
2011-11-17 05:12:46 -06:00
|
|
|
def unauthenticated(f):
|
|
|
|
"""
|
|
|
|
Adds 'unauthenticated' attribute to decorated function.
|
|
|
|
Usage:
|
|
|
|
@unauthenticated
|
|
|
|
def mymethod(f):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
f.unauthenticated = True
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
def isunauthenticated(f):
|
|
|
|
"""
|
|
|
|
Checks to see if the function is marked as not requiring authentication
|
|
|
|
with the @unauthenticated decorator. Returns True if decorator is
|
|
|
|
set to True, False otherwise.
|
|
|
|
"""
|
|
|
|
return getattr(f, 'unauthenticated', False)
|
|
|
|
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
def pretty_choice_list(l):
|
|
|
|
return ', '.join("'%s'" % i for i in l)
|
|
|
|
|
|
|
|
|
|
|
|
def print_list(objs, fields, formatters={}):
|
2011-10-30 17:20:47 -05:00
|
|
|
mixed_case_fields = ['serverId']
|
2011-08-03 17:41:33 -04:00
|
|
|
pt = prettytable.PrettyTable([f for f in fields], caching=False)
|
|
|
|
pt.aligns = ['l' for f in fields]
|
|
|
|
|
|
|
|
for o in objs:
|
|
|
|
row = []
|
|
|
|
for field in fields:
|
|
|
|
if field in formatters:
|
|
|
|
row.append(formatters[field](o))
|
|
|
|
else:
|
2011-10-30 17:20:47 -05:00
|
|
|
if field in mixed_case_fields:
|
|
|
|
field_name = field.replace(' ', '_')
|
2011-11-03 22:44:22 -05:00
|
|
|
else:
|
2011-10-30 17:20:47 -05:00
|
|
|
field_name = field.lower().replace(' ', '_')
|
2011-08-03 17:41:33 -04:00
|
|
|
data = getattr(o, field_name, '')
|
|
|
|
row.append(data)
|
|
|
|
pt.add_row(row)
|
|
|
|
|
|
|
|
pt.printt(sortby=fields[0])
|
|
|
|
|
|
|
|
|
|
|
|
def print_dict(d):
|
|
|
|
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
|
|
|
|
pt.aligns = ['l', 'l']
|
|
|
|
[pt.add_row(list(r)) for r in d.iteritems()]
|
|
|
|
pt.printt(sortby='Property')
|
2011-08-22 15:13:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
def find_resource(manager, name_or_id):
|
|
|
|
"""Helper for the _find_* methods."""
|
|
|
|
# first try to get entity as integer id
|
|
|
|
try:
|
|
|
|
if isinstance(name_or_id, int) or name_or_id.isdigit():
|
|
|
|
return manager.get(int(name_or_id))
|
|
|
|
except exceptions.NotFound:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# now try to get entity as uuid
|
|
|
|
try:
|
|
|
|
uuid.UUID(str(name_or_id))
|
|
|
|
return manager.get(name_or_id)
|
|
|
|
except (ValueError, exceptions.NotFound):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# finally try to find entity by name
|
|
|
|
try:
|
|
|
|
return manager.find(name=name_or_id)
|
|
|
|
except exceptions.NotFound:
|
|
|
|
msg = "No %s with a name or ID of '%s' exists." % \
|
|
|
|
(manager.resource_class.__name__.lower(), name_or_id)
|
|
|
|
raise exceptions.CommandError(msg)
|
2011-12-15 19:39:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _format_servers_list_networks(server):
|
|
|
|
output = []
|
|
|
|
for (network, addresses) in server.networks.items():
|
|
|
|
if len(addresses) == 0:
|
|
|
|
continue
|
|
|
|
addresses_csv = ', '.join(addresses)
|
|
|
|
group = "%s=%s" % (network, addresses_csv)
|
|
|
|
output.append(group)
|
|
|
|
|
|
|
|
return '; '.join(output)
|