Created
February 17, 2015 18:16
-
-
Save briancurtin/5dd902f504b7bfd98205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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