cleaning up find_resource method to support str/int ids, uuids, and integer-like names

This commit is contained in:
Brian Waldon 2011-08-22 15:13:26 -04:00
parent c42b374ed8
commit 79a532b910
3 changed files with 36 additions and 41 deletions

View File

@ -1,5 +1,9 @@
import uuid
import prettytable
from novaclient import exceptions
# Decorator for cli-args
def arg(*args, **kwargs):
@ -38,3 +42,28 @@ def print_dict(d):
pt.aligns = ['l', 'l']
[pt.add_row(list(r)) for r in d.iteritems()]
pt.printt(sortby='Property')
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)

View File

@ -750,39 +750,22 @@ def do_remove_fixed_ip(cs, args):
def _find_server(cs, server):
"""Get a server by name or ID."""
return _find_resource(cs.servers, server)
return utils.find_resource(cs.servers, server)
def _find_ipgroup(cs, group):
"""Get an IP group by name or ID."""
return _find_resource(cs.ipgroups, group)
return utils.find_resource(cs.ipgroups, group)
def _find_image(cs, image):
"""Get an image by name or ID."""
return _find_resource(cs.images, image)
return utils.find_resource(cs.images, image)
def _find_flavor(cs, flavor):
"""Get a flavor by name, ID, or RAM size."""
try:
return _find_resource(cs.flavors, flavor)
return utils.find_resource(cs.flavors, flavor)
except exceptions.NotFound:
return cs.flavors.find(ram=flavor)
def _find_resource(manager, name_or_id):
"""Helper for the _find_* methods."""
try:
if isinstance(name_or_id, int) or name_or_id.isdigit():
return manager.get(int(name_or_id))
try:
uuid.UUID(name_or_id)
return manager.get(name_or_id)
except ValueError:
return manager.find(name=name_or_id)
except exceptions.NotFound:
raise exceptions.CommandError(
"No %s with a name or ID of '%s' exists." %
(manager.resource_class.__name__.lower(), name_or_id))

View File

@ -503,39 +503,22 @@ def do_delete(cs, args):
def _find_server(cs, server):
"""Get a server by name or ID."""
return _find_resource(cs.servers, server)
return utils.find_resource(cs.servers, server)
def _find_image(cs, image):
"""Get an image by name or ID."""
return _find_resource(cs.images, image)
return utils.find_resource(cs.images, image)
def _find_flavor(cs, flavor):
"""Get a flavor by name, ID, or RAM size."""
try:
return _find_resource(cs.flavors, flavor)
return utils.find_resource(cs.flavors, flavor)
except exceptions.NotFound:
return cs.flavors.find(ram=flavor)
def _find_resource(manager, name_or_id):
"""Helper for the _find_* methods."""
try:
if isinstance(name_or_id, int) or name_or_id.isdigit():
return manager.get(int(name_or_id))
try:
uuid.UUID(name_or_id)
return manager.get(name_or_id)
except ValueError:
return manager.find(name=name_or_id)
except exceptions.NotFound:
raise exceptions.CommandError(
"No %s with a name or ID of '%s' exists." %
(manager.resource_class.__name__.lower(), name_or_id))
# --zone_username is required since --username is already used.
@utils.arg('zone', metavar='<zone_id>', help='ID of the zone', default=None)
@utils.arg('--api_url', dest='api_url', default=None, help='New URL.')