Ask Your Question

Revision history [back]

Hi, For the comment you made i think you are looking for nova utils.py: https://github.com/openstack/python-novaclient/blob/stable/liberty/novaclient/utils.py

def find_resource(manager, name_or_id, *find_args): """Helper for the _find_ methods.""" # for str id which is not uuid (for Flavor, Keypair and hypervsior in cells # environments search currently) if getattr(manager, 'is_alphanum_id_allowed', False): try: return manager.get(name_or_id) except exceptions.NotFound: pass

# try to get entity as integer id
try:
    return manager.get(int(name_or_id))
except (TypeError, ValueError, exceptions.NotFound):
    pass

# now try to get entity as uuid
try:
    tmp_id = encodeutils.safe_encode(name_or_id)

    if six.PY3:
        tmp_id = tmp_id.decode()

    uuid.UUID(tmp_id)
    return manager.get(tmp_id)
except (TypeError, ValueError, exceptions.NotFound):
    pass

try:
    try:
        resource = getattr(manager, 'resource_class', None)
        name_attr = resource.NAME_ATTR if resource else 'name'
        kwargs = {name_attr: name_or_id}
        kwargs.update(find_args)
        return manager.find(**kwargs)
    except exceptions.NotFound:
        pass

    # finally try to find entity by human_id
    try:
        return manager.find(human_id=name_or_id, **find_args)
    except exceptions.NotFound:
        msg = (_("No %(class)s with a name or ID of '%(name)s' exists.") %
               {'class': manager.resource_class.__name__.lower(),
                'name': name_or_id})
        raise exceptions.CommandError(msg)
except exceptions.NoUniqueMatch:
    msg = (_("Multiple %(class)s matches found for '%(name)s', use an ID "
             "to be more specific.") %
           {'class': manager.resource_class.__name__.lower(),
            'name': name_or_id})
    raise exceptions.CommandError(msg)

Regards

Hi, For the comment you made i think you are looking for nova utils.py: https://github.com/openstack/python-novaclient/blob/stable/liberty/novaclient/utils.py

def find_resource(manager, name_or_id, *find_args):
**find_args):
    """Helper for the _find_ _find_* methods."""
    # for str id which is not uuid (for Flavor, Keypair and hypervsior in cells
    # environments search currently)
    if getattr(manager, 'is_alphanum_id_allowed', False):
        try:
            return manager.get(name_or_id)
        except exceptions.NotFound:
            pass

pass
# try to get entity as integer id
try:
    return manager.get(int(name_or_id))
except (TypeError, ValueError, exceptions.NotFound):
    pass

# now try to get entity as uuid
try:
    tmp_id = encodeutils.safe_encode(name_or_id)

    if six.PY3:
        tmp_id = tmp_id.decode()

    uuid.UUID(tmp_id)
    return manager.get(tmp_id)
except (TypeError, ValueError, exceptions.NotFound):
    pass

try:
    try:
        resource = getattr(manager, 'resource_class', None)
        name_attr = resource.NAME_ATTR if resource else 'name'
        kwargs = {name_attr: name_or_id}
        kwargs.update(find_args)
        return manager.find(**kwargs)
    except exceptions.NotFound:
        pass

    # finally try to find entity by human_id
    try:
        return manager.find(human_id=name_or_id, **find_args)
    except exceptions.NotFound:
        msg = (_("No %(class)s with a name or ID of '%(name)s' exists.") %
               {'class': manager.resource_class.__name__.lower(),
                'name': name_or_id})
        raise exceptions.CommandError(msg)
except exceptions.NoUniqueMatch:
    msg = (_("Multiple %(class)s matches found for '%(name)s', use an ID "
             "to be more specific.") %
           {'class': manager.resource_class.__name__.lower(),
            'name': name_or_id})
    raise exceptions.CommandError(msg)

Regards

Hi, For the comment you made i think you are looking for nova python-novaclient utils.py: https://github.com/openstack/python-novaclient/blob/stable/liberty/novaclient/utils.py

def find_resource(manager, name_or_id, **find_args):
    """Helper for the _find_* methods."""
    # for str id which is not uuid (for Flavor, Keypair and hypervsior in cells
    # environments search currently)
    if getattr(manager, 'is_alphanum_id_allowed', False):
        try:
            return manager.get(name_or_id)
        except exceptions.NotFound:
            pass
# try to get entity as integer id
try:
    return manager.get(int(name_or_id))
except (TypeError, ValueError, exceptions.NotFound):
    pass

# now try to get entity as uuid
try:
    tmp_id = encodeutils.safe_encode(name_or_id)

    if six.PY3:
        tmp_id = tmp_id.decode()

    uuid.UUID(tmp_id)
    return manager.get(tmp_id)
except (TypeError, ValueError, exceptions.NotFound):
    pass

try:
    try:
        resource = getattr(manager, 'resource_class', None)
        name_attr = resource.NAME_ATTR if resource else 'name'
        kwargs = {name_attr: name_or_id}
        kwargs.update(find_args)
        return manager.find(**kwargs)
    except exceptions.NotFound:
        pass

    # finally try to find entity by human_id
    try:
        return manager.find(human_id=name_or_id, **find_args)
    except exceptions.NotFound:
        msg = (_("No %(class)s with a name or ID of '%(name)s' exists.") %
               {'class': manager.resource_class.__name__.lower(),
                'name': name_or_id})
        raise exceptions.CommandError(msg)
except exceptions.NoUniqueMatch:
    msg = (_("Multiple %(class)s matches found for '%(name)s', use an ID "
             "to be more specific.") %
           {'class': manager.resource_class.__name__.lower(),
            'name': name_or_id})
    raise exceptions.CommandError(msg)

Regards