Skip to content

Instantly share code, notes, and snippets.

@briancurtin
Created February 17, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancurtin/5dd902f504b7bfd98205 to your computer and use it in GitHub Desktop.
Save briancurtin/5dd902f504b7bfd98205 to your computer and use it in GitHub Desktop.
@classmethod
def find(cls, session, name_or_id):
"""Find a resource by its name or id.
:param session: The session to use for making this request.
:type session: :class:`~openstack.session.Session`
:param name_or_id: The resource to find. This is a value which
will tried first as the ``id_attribute``
of the Resource, and additionally as the
``name_attribute``.
:return: The :class:`Resource` object matching the given name or id
or None if nothing matches.
"""
try:
res = cls({cls.id_attribute: name_or_id})
res.get(session)
return res
except exceptions.HttpException:
pass
try:
res = cls.list(session, limit=1, marker=name_or_id,
paginated=False)
return next(res)
except (exceptions.HttpException, StopIteration):
pass
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment