Add ignore_missing to proxy find

Change-Id: I271480e82b4f3a22b1eb56b62e45db4efd9606a3
This commit is contained in:
TerryHowe
2015-07-09 11:53:23 -06:00
parent 0a256465d0
commit ba43b4a4d2
17 changed files with 460 additions and 161 deletions

View File

@@ -42,14 +42,20 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(cluster.Cluster, value, ignore_missing=ignore_missing) self._delete(cluster.Cluster, value, ignore_missing=ignore_missing)
def find_cluster(self, name_or_id): def find_cluster(self, name_or_id, ignore_missing=True):
"""Find a single cluster. """Find a single cluster.
:param name_or_id: The name or ID of a cluster. :param name_or_id: The name or ID of a cluster.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.cluster.v1.cluster.Cluster` object :returns: One :class:`~openstack.cluster.v1.cluster.Cluster` object
or None or None
""" """
return cluster.Cluster.find(self.session, name_or_id) return cluster.Cluster.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_cluster(self, name_or_id): def get_cluster(self, name_or_id):
"""Get a single cluster. """Get a single cluster.

View File

@@ -24,14 +24,20 @@ from openstack import resource
class Proxy(proxy.BaseProxy): class Proxy(proxy.BaseProxy):
def find_extension(self, name_or_id): def find_extension(self, name_or_id, ignore_missing=True):
"""Find a single extension """Find a single extension
:param name_or_id: The name or ID of an extension. :param name_or_id: The name or ID of an extension.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.extension.Extension` or :returns: One :class:`~openstack.compute.v2.extension.Extension` or
None None
""" """
return extension.Extension.find(self.session, name_or_id) return extension.Extension.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def extensions(self): def extensions(self):
"""Retrieve a generator of extensions """Retrieve a generator of extensions
@@ -41,13 +47,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._list(extension.Extension, paginated=False) return self._list(extension.Extension, paginated=False)
def find_flavor(self, name_or_id): def find_flavor(self, name_or_id, ignore_missing=True):
"""Find a single flavor """Find a single flavor
:param name_or_id: The name or ID of a flavor. :param name_or_id: The name or ID of a flavor.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor` or None :returns: One :class:`~openstack.compute.v2.flavor.Flavor` or None
""" """
return flavor.Flavor.find(self.session, name_or_id) return flavor.Flavor.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def create_flavor(self, **attrs): def create_flavor(self, **attrs):
"""Create a new flavor from attributes """Create a new flavor from attributes
@@ -131,13 +143,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(image.Image, value, ignore_missing=ignore_missing) self._delete(image.Image, value, ignore_missing=ignore_missing)
def find_image(self, name_or_id): def find_image(self, name_or_id, ignore_missing=True):
"""Find a single image """Find a single image
:param name_or_id: The name or ID of a image. :param name_or_id: The name or ID of a image.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.image.Image` or None :returns: One :class:`~openstack.compute.v2.image.Image` or None
""" """
return image.Image.find(self.session, name_or_id) return image.Image.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_image(self, value): def get_image(self, value):
"""Get a single image """Get a single image
@@ -205,13 +223,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._get(keypair.Keypair, value) return self._get(keypair.Keypair, value)
def find_keypair(self, name_or_id): def find_keypair(self, name_or_id, ignore_missing=True):
"""Find a single keypair """Find a single keypair
:param name_or_id: The name or ID of a keypair. :param name_or_id: The name or ID of a keypair.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.keypair.Keypair` or None :returns: One :class:`~openstack.compute.v2.keypair.Keypair` or None
""" """
return keypair.Keypair.find(self.session, name_or_id) return keypair.Keypair.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def keypairs(self): def keypairs(self):
"""Return a generator of keypairs """Return a generator of keypairs
@@ -271,13 +295,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(server.Server, value, ignore_missing=ignore_missing) self._delete(server.Server, value, ignore_missing=ignore_missing)
def find_server(self, name_or_id): def find_server(self, name_or_id, ignore_missing=True):
"""Find a single server """Find a single server
:param name_or_id: The name or ID of a server. :param name_or_id: The name or ID of a server.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.server.Server` or None :returns: One :class:`~openstack.compute.v2.server.Server` or None
""" """
return server.Server.find(self.session, name_or_id) return server.Server.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_server(self, value): def get_server(self, value):
"""Get a single server """Get a single server
@@ -385,14 +415,20 @@ class Proxy(proxy.BaseProxy):
self._delete(server_interface.ServerInterface, value, self._delete(server_interface.ServerInterface, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_server_interface(self, name_or_id): def find_server_interface(self, name_or_id, ignore_missing=True):
"""Find a single server interface """Find a single server interface
:param name_or_id: The name or ID of a server interface. :param name_or_id: The name or ID of a server interface.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.server_interface. :returns: One :class:`~openstack.compute.v2.server_interface.
ServerInterface` or None ServerInterface` or None
""" """
return server_interface.ServerInterface.find(self.session, name_or_id) return server_interface.ServerInterface.find(
self.session, name_or_id, ignore_missing=ignore_missing)
def get_server_interface(self, value): def get_server_interface(self, value):
"""Get a single server interface """Get a single server interface
@@ -431,13 +467,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._update(server_interface.ServerInterface, value, **attrs) return self._update(server_interface.ServerInterface, value, **attrs)
def find_server_ip(self, name_or_id): def find_server_ip(self, name_or_id, ignore_missing=True):
"""Find a single server IP """Find a single server IP
:param name_or_id: The name or ID of a server IP. :param name_or_id: The name or ID of a server IP.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.server_ip.ServerIP` or None :returns: One :class:`~openstack.compute.v2.server_ip.ServerIP` or None
""" """
return server_ip.ServerIP.find(self.session, name_or_id) return server_ip.ServerIP.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def server_ips(self): def server_ips(self):
"""Return a generator of server IPs """Return a generator of server IPs

View File

@@ -11,12 +11,12 @@
# under the License. # under the License.
from openstack.compute import compute_service from openstack.compute import compute_service
from openstack import exceptions
from openstack import resource from openstack import resource
class Keypair(resource.Resource): class Keypair(resource.Resource):
id_attribute = 'name' id_attribute = 'name'
name_attribute = None
resource_key = 'keypair' resource_key = 'keypair'
resources_key = 'keypairs' resources_key = 'keypairs'
base_path = '/os-keypairs' base_path = '/os-keypairs'
@@ -56,12 +56,3 @@ class Keypair(resource.Resource):
self._attrs = resp self._attrs = resp
self._reset_dirty() self._reset_dirty()
return self return self
@classmethod
def find(cls, session, name_or_id, path_args=None):
"""Find a keypair by name because list filtering does not work."""
try:
return cls.get_by_id(session, name_or_id)
except exceptions.HttpException:
pass
return None

View File

@@ -46,13 +46,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(database.Database, value, ignore_missing=ignore_missing) self._delete(database.Database, value, ignore_missing=ignore_missing)
def find_database(self, name_or_id): def find_database(self, name_or_id, ignore_missing=True):
"""Find a single database """Find a single database
:param name_or_id: The name or ID of a database. :param name_or_id: The name or ID of a database.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.database.v1.database.Database` or None :returns: One :class:`~openstack.database.v1.database.Database` or None
""" """
return database.Database.find(self.session, name_or_id) return database.Database.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def databases(self): def databases(self):
"""Return a generator of databases """Return a generator of databases
@@ -75,13 +81,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._get(database.Database, value) return self._get(database.Database, value)
def find_flavor(self, name_or_id): def find_flavor(self, name_or_id, ignore_missing=True):
"""Find a single flavor """Find a single flavor
:param name_or_id: The name or ID of a flavor. :param name_or_id: The name or ID of a flavor.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.database.v1.flavor.Flavor` or None :returns: One :class:`~openstack.database.v1.flavor.Flavor` or None
""" """
return flavor.Flavor.find(self.session, name_or_id) return flavor.Flavor.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_flavor(self, value): def get_flavor(self, value):
"""Get a single flavor """Get a single flavor
@@ -130,13 +142,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(instance.Instance, value, ignore_missing=ignore_missing) self._delete(instance.Instance, value, ignore_missing=ignore_missing)
def find_instance(self, name_or_id): def find_instance(self, name_or_id, ignore_missing=True):
"""Find a single instance """Find a single instance
:param name_or_id: The name or ID of a instance. :param name_or_id: The name or ID of a instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.database.v1.instance.Instance` or None :returns: One :class:`~openstack.database.v1.instance.Instance` or None
""" """
return instance.Instance.find(self.session, name_or_id) return instance.Instance.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_instance(self, value): def get_instance(self, value):
"""Get a single instance """Get a single instance
@@ -200,13 +218,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(user.User, value, ignore_missing=ignore_missing) self._delete(user.User, value, ignore_missing=ignore_missing)
def find_user(self, name_or_id): def find_user(self, name_or_id, ignore_missing=True):
"""Find a single user """Find a single user
:param name_or_id: The name or ID of a user. :param name_or_id: The name or ID of a user.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.database.v1.user.User` or None :returns: One :class:`~openstack.database.v1.user.User` or None
""" """
return user.User.find(self.session, name_or_id) return user.User.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def users(self): def users(self):
"""Return a generator of users """Return a generator of users

View File

@@ -45,13 +45,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(role.Role, value, ignore_missing=ignore_missing) self._delete(role.Role, value, ignore_missing=ignore_missing)
def find_role(self, name_or_id): def find_role(self, name_or_id, ignore_missing=True):
"""Find a single role """Find a single role
:param name_or_id: The name or ID of a role. :param name_or_id: The name or ID of a role.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v2.role.Role` or None :returns: One :class:`~openstack.identity.v2.role.Role` or None
""" """
return role.Role.find(self.session, name_or_id) return role.Role.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_role(self, value): def get_role(self, value):
"""Get a single role """Get a single role
@@ -113,13 +119,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(tenant.Tenant, value, ignore_missing=ignore_missing) self._delete(tenant.Tenant, value, ignore_missing=ignore_missing)
def find_tenant(self, name_or_id): def find_tenant(self, name_or_id, ignore_missing=True):
"""Find a single tenant """Find a single tenant
:param name_or_id: The name or ID of a tenant. :param name_or_id: The name or ID of a tenant.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v2.tenant.Tenant` or None :returns: One :class:`~openstack.identity.v2.tenant.Tenant` or None
""" """
return tenant.Tenant.find(self.session, name_or_id) return tenant.Tenant.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_tenant(self, value): def get_tenant(self, value):
"""Get a single tenant """Get a single tenant
@@ -181,13 +193,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(user.User, value, ignore_missing=ignore_missing) self._delete(user.User, value, ignore_missing=ignore_missing)
def find_user(self, name_or_id): def find_user(self, name_or_id, ignore_missing=True):
"""Find a single user """Find a single user
:param name_or_id: The name or ID of a user. :param name_or_id: The name or ID of a user.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v2.user.User` or None :returns: One :class:`~openstack.identity.v2.user.User` or None
""" """
return user.User.find(self.session, name_or_id) return user.User.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_user(self, value): def get_user(self, value):
"""Get a single user """Get a single user

View File

@@ -52,14 +52,20 @@ class Proxy(proxy.BaseProxy):
self._delete(credential.Credential, value, self._delete(credential.Credential, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_credential(self, name_or_id): def find_credential(self, name_or_id, ignore_missing=True):
"""Find a single credential """Find a single credential
:param name_or_id: The name or ID of a credential. :param name_or_id: The name or ID of a credential.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.credential.Credential` :returns: One :class:`~openstack.identity.v3.credential.Credential`
or None or None
""" """
return credential.Credential.find(self.session, name_or_id) return credential.Credential.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_credential(self, value): def get_credential(self, value):
"""Get a single credential """Get a single credential
@@ -124,13 +130,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(domain.Domain, value, ignore_missing=ignore_missing) self._delete(domain.Domain, value, ignore_missing=ignore_missing)
def find_domain(self, name_or_id): def find_domain(self, name_or_id, ignore_missing=True):
"""Find a single domain """Find a single domain
:param name_or_id: The name or ID of a domain. :param name_or_id: The name or ID of a domain.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.domain.Domain` or None :returns: One :class:`~openstack.identity.v3.domain.Domain` or None
""" """
return domain.Domain.find(self.session, name_or_id) return domain.Domain.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_domain(self, value): def get_domain(self, value):
"""Get a single domain """Get a single domain
@@ -193,13 +205,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(endpoint.Endpoint, value, ignore_missing=ignore_missing) self._delete(endpoint.Endpoint, value, ignore_missing=ignore_missing)
def find_endpoint(self, name_or_id): def find_endpoint(self, name_or_id, ignore_missing=True):
"""Find a single endpoint """Find a single endpoint
:param name_or_id: The name or ID of a endpoint. :param name_or_id: The name or ID of a endpoint.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.endpoint.Endpoint` or None :returns: One :class:`~openstack.identity.v3.endpoint.Endpoint` or None
""" """
return endpoint.Endpoint.find(self.session, name_or_id) return endpoint.Endpoint.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_endpoint(self, value): def get_endpoint(self, value):
"""Get a single endpoint """Get a single endpoint
@@ -264,13 +282,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(group.Group, value, ignore_missing=ignore_missing) self._delete(group.Group, value, ignore_missing=ignore_missing)
def find_group(self, name_or_id): def find_group(self, name_or_id, ignore_missing=True):
"""Find a single group """Find a single group
:param name_or_id: The name or ID of a group. :param name_or_id: The name or ID of a group.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.group.Group` or None :returns: One :class:`~openstack.identity.v3.group.Group` or None
""" """
return group.Group.find(self.session, name_or_id) return group.Group.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_group(self, value): def get_group(self, value):
"""Get a single group """Get a single group
@@ -334,13 +358,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(policy.Policy, value, ignore_missing=ignore_missing) self._delete(policy.Policy, value, ignore_missing=ignore_missing)
def find_policy(self, name_or_id): def find_policy(self, name_or_id, ignore_missing=True):
"""Find a single policy """Find a single policy
:param name_or_id: The name or ID of a policy. :param name_or_id: The name or ID of a policy.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.policy.Policy` or None :returns: One :class:`~openstack.identity.v3.policy.Policy` or None
""" """
return policy.Policy.find(self.session, name_or_id) return policy.Policy.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_policy(self, value): def get_policy(self, value):
"""Get a single policy """Get a single policy
@@ -403,13 +433,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(project.Project, value, ignore_missing=ignore_missing) self._delete(project.Project, value, ignore_missing=ignore_missing)
def find_project(self, name_or_id): def find_project(self, name_or_id, ignore_missing=True):
"""Find a single project """Find a single project
:param name_or_id: The name or ID of a project. :param name_or_id: The name or ID of a project.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.project.Project` or None :returns: One :class:`~openstack.identity.v3.project.Project` or None
""" """
return project.Project.find(self.session, name_or_id) return project.Project.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_project(self, value): def get_project(self, value):
"""Get a single project """Get a single project
@@ -472,13 +508,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(service.Service, value, ignore_missing=ignore_missing) self._delete(service.Service, value, ignore_missing=ignore_missing)
def find_service(self, name_or_id): def find_service(self, name_or_id, ignore_missing=True):
"""Find a single service """Find a single service
:param name_or_id: The name or ID of a service. :param name_or_id: The name or ID of a service.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.service.Service` or None :returns: One :class:`~openstack.identity.v3.service.Service` or None
""" """
return service.Service.find(self.session, name_or_id) return service.Service.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_service(self, value): def get_service(self, value):
"""Get a single service """Get a single service
@@ -541,13 +583,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(user.User, value, ignore_missing=ignore_missing) self._delete(user.User, value, ignore_missing=ignore_missing)
def find_user(self, name_or_id): def find_user(self, name_or_id, ignore_missing=True):
"""Find a single user """Find a single user
:param name_or_id: The name or ID of a user. :param name_or_id: The name or ID of a user.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.user.User` or None :returns: One :class:`~openstack.identity.v3.user.User` or None
""" """
return user.User.find(self.session, name_or_id) return user.User.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_user(self, value): def get_user(self, value):
"""Get a single user """Get a single user
@@ -610,13 +658,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(trust.Trust, value, ignore_missing=ignore_missing) self._delete(trust.Trust, value, ignore_missing=ignore_missing)
def find_trust(self, name_or_id): def find_trust(self, name_or_id, ignore_missing=True):
"""Find a single trust """Find a single trust
:param name_or_id: The name or ID of a trust. :param name_or_id: The name or ID of a trust.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.identity.v3.trust.Trust` or None :returns: One :class:`~openstack.identity.v3.trust.Trust` or None
""" """
return trust.Trust.find(self.session, name_or_id) return trust.Trust.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_trust(self, value): def get_trust(self, value):
"""Get a single trust """Get a single trust

View File

@@ -43,13 +43,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(image.Image, value, ignore_missing=ignore_missing) self._delete(image.Image, value, ignore_missing=ignore_missing)
def find_image(self, name_or_id): def find_image(self, name_or_id, ignore_missing=True):
"""Find a single image """Find a single image
:param name_or_id: The name or ID of a image. :param name_or_id: The name or ID of a image.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.image.v1.image.Image` or None :returns: One :class:`~openstack.image.v1.image.Image` or None
""" """
return image.Image.find(self.session, name_or_id) return image.Image.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_image(self, value): def get_image(self, value):
"""Get a single image """Get a single image

View File

@@ -45,13 +45,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(image.Image, value, ignore_missing=ignore_missing) self._delete(image.Image, value, ignore_missing=ignore_missing)
def find_image(self, name_or_id): def find_image(self, name_or_id, ignore_missing=True):
"""Find a single image """Find a single image
:param name_or_id: The name or ID of a image. :param name_or_id: The name or ID of a image.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.image.v2.image.Image` or None :returns: One :class:`~openstack.image.v2.image.Image` or None
""" """
return image.Image.find(self.session, name_or_id) return image.Image.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_image(self, value): def get_image(self, value):
"""Get a single image """Get a single image
@@ -113,13 +119,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(member.Member, value, ignore_missing=ignore_missing) self._delete(member.Member, value, ignore_missing=ignore_missing)
def find_member(self, name_or_id): def find_member(self, name_or_id, ignore_missing=True):
"""Find a single member """Find a single member
:param name_or_id: The name or ID of a member. :param name_or_id: The name or ID of a member.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.image.v2.member.Member` or None :returns: One :class:`~openstack.image.v2.member.Member` or None
""" """
return member.Member.find(self.session, name_or_id) return member.Member.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_member(self, value): def get_member(self, value):
"""Get a single member """Get a single member

View File

@@ -46,14 +46,20 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(container.Container, value, ignore_missing=ignore_missing) self._delete(container.Container, value, ignore_missing=ignore_missing)
def find_container(self, name_or_id): def find_container(self, name_or_id, ignore_missing=True):
"""Find a single container """Find a single container
:param name_or_id: The name or ID of a container. :param name_or_id: The name or ID of a container.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.container.Container` or :returns: One :class:`~openstack.compute.v2.container.Container` or
None None
""" """
return container.Container.find(self.session, name_or_id) return container.Container.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_container(self, value): def get_container(self, value):
"""Get a single container """Get a single container
@@ -118,13 +124,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(order.Order, value, ignore_missing=ignore_missing) self._delete(order.Order, value, ignore_missing=ignore_missing)
def find_order(self, name_or_id): def find_order(self, name_or_id, ignore_missing=True):
"""Find a single order """Find a single order
:param name_or_id: The name or ID of a order. :param name_or_id: The name or ID of a order.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.order.Order` or None :returns: One :class:`~openstack.compute.v2.order.Order` or None
""" """
return order.Order.find(self.session, name_or_id) return order.Order.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_order(self, value): def get_order(self, value):
"""Get a single order """Get a single order
@@ -188,13 +200,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(secret.Secret, value, ignore_missing=ignore_missing) self._delete(secret.Secret, value, ignore_missing=ignore_missing)
def find_secret(self, name_or_id): def find_secret(self, name_or_id, ignore_missing=True):
"""Find a single secret """Find a single secret
:param name_or_id: The name or ID of a secret. :param name_or_id: The name or ID of a secret.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.secret.Secret` or None :returns: One :class:`~openstack.compute.v2.secret.Secret` or None
""" """
return secret.Secret.find(self.session, name_or_id) return secret.Secret.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_secret(self, value): def get_secret(self, value):
"""Get a single secret """Get a single secret

View File

@@ -32,14 +32,20 @@ from openstack import proxy
class Proxy(proxy.BaseProxy): class Proxy(proxy.BaseProxy):
def find_extension(self, name_or_id): def find_extension(self, name_or_id, ignore_missing=True):
"""Find a single extension """Find a single extension
:param name_or_id: The name or ID of a extension. :param name_or_id: The name or ID of a extension.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.extension.Extension` :returns: One :class:`~openstack.network.v2.extension.Extension`
or None or None
""" """
return extension.Extension.find(self.session, name_or_id) return extension.Extension.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def extensions(self): def extensions(self):
"""Return a generator of extensions """Return a generator of extensions
@@ -85,14 +91,20 @@ class Proxy(proxy.BaseProxy):
""" """
return floating_ip.FloatingIP.find_available(self.session) return floating_ip.FloatingIP.find_available(self.session)
def find_ip(self, name_or_id): def find_ip(self, name_or_id, ignore_missing=True):
"""Find a single IP """Find a single IP
:param name_or_id: The name or ID of an IP. :param name_or_id: The name or ID of an IP.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.floating_ip.FloatingIP` :returns: One :class:`~openstack.network.v2.floating_ip.FloatingIP`
or None or None
""" """
return floating_ip.FloatingIP.find(self.session, name_or_id) return floating_ip.FloatingIP.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_ip(self, value): def get_ip(self, value):
"""Get a single floating ip """Get a single floating ip
@@ -158,14 +170,20 @@ class Proxy(proxy.BaseProxy):
self._delete(health_monitor.HealthMonitor, value, self._delete(health_monitor.HealthMonitor, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_health_monitor(self, name_or_id): def find_health_monitor(self, name_or_id, ignore_missing=True):
"""Find a single health monitor """Find a single health monitor
:param name_or_id: The name or ID of a health monitor. :param name_or_id: The name or ID of a health monitor.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.health_monitor. :returns: One :class:`~openstack.network.v2.health_monitor.
HealthMonitor` or None HealthMonitor` or None
""" """
return health_monitor.HealthMonitor.find(self.session, name_or_id) return health_monitor.HealthMonitor.find(
self.session, name_or_id, ignore_missing=ignore_missing)
def get_health_monitor(self, value): def get_health_monitor(self, value):
"""Get a single health monitor """Get a single health monitor
@@ -230,13 +248,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(listener.Listener, value, ignore_missing=ignore_missing) self._delete(listener.Listener, value, ignore_missing=ignore_missing)
def find_listener(self, name_or_id): def find_listener(self, name_or_id, ignore_missing=True):
"""Find a single listener """Find a single listener
:param name_or_id: The name or ID of a listener. :param name_or_id: The name or ID of a listener.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.listener.Listener` or None :returns: One :class:`~openstack.network.v2.listener.Listener` or None
""" """
return listener.Listener.find(self.session, name_or_id) return listener.Listener.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_listener(self, value): def get_listener(self, value):
"""Get a single listener """Get a single listener
@@ -302,14 +326,20 @@ class Proxy(proxy.BaseProxy):
self._delete(load_balancer.LoadBalancer, value, self._delete(load_balancer.LoadBalancer, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_load_balancer(self, name_or_id): def find_load_balancer(self, name_or_id, ignore_missing=True):
"""Find a single load balancer """Find a single load balancer
:param name_or_id: The name or ID of a load balancer. :param name_or_id: The name or ID of a load balancer.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.load_balancer.LoadBalancer` :returns: One :class:`~openstack.network.v2.load_balancer.LoadBalancer`
or None or None
""" """
return load_balancer.LoadBalancer.find(self.session, name_or_id) return load_balancer.LoadBalancer.find(
self.session, name_or_id, ignore_missing=ignore_missing)
def get_load_balancer(self, value): def get_load_balancer(self, value):
"""Get a single load balancer """Get a single load balancer
@@ -375,14 +405,20 @@ class Proxy(proxy.BaseProxy):
self._delete(metering_label.MeteringLabel, value, self._delete(metering_label.MeteringLabel, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_metering_label(self, name_or_id): def find_metering_label(self, name_or_id, ignore_missing=True):
"""Find a single metering label """Find a single metering label
:param name_or_id: The name or ID of a metering label. :param name_or_id: The name or ID of a metering label.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.metering_label. :returns: One :class:`~openstack.network.v2.metering_label.
MeteringLabel` or None MeteringLabel` or None
""" """
return metering_label.MeteringLabel.find(self.session, name_or_id) return metering_label.MeteringLabel.find(
self.session, name_or_id, ignore_missing=ignore_missing)
def get_metering_label(self, value): def get_metering_label(self, value):
"""Get a single metering label """Get a single metering label
@@ -451,15 +487,20 @@ class Proxy(proxy.BaseProxy):
self._delete(metering_label_rule.MeteringLabelRule, self._delete(metering_label_rule.MeteringLabelRule,
value, ignore_missing=ignore_missing) value, ignore_missing=ignore_missing)
def find_metering_label_rule(self, name_or_id): def find_metering_label_rule(self, name_or_id, ignore_missing=True):
"""Find a single metering label rule """Find a single metering label rule
:param name_or_id: The name or ID of a metering label rule. :param name_or_id: The name or ID of a metering label rule.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.metering_label_rule. :returns: One :class:`~openstack.network.v2.metering_label_rule.
MeteringLabelRule` or None MeteringLabelRule` or None
""" """
return metering_label_rule.MeteringLabelRule.find(self.session, return metering_label_rule.MeteringLabelRule.find(
name_or_id) self.session, name_or_id, ignore_missing=ignore_missing)
def get_metering_label_rule(self, value): def get_metering_label_rule(self, value):
"""Get a single metering label rule """Get a single metering label rule
@@ -529,13 +570,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(network.Network, value, ignore_missing=ignore_missing) self._delete(network.Network, value, ignore_missing=ignore_missing)
def find_network(self, name_or_id): def find_network(self, name_or_id, ignore_missing=True):
"""Find a single network """Find a single network
:param name_or_id: The name or ID of a network. :param name_or_id: The name or ID of a network.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.network.Network` or None :returns: One :class:`~openstack.network.v2.network.Network` or None
""" """
return network.Network.find(self.session, name_or_id) return network.Network.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_network(self, value): def get_network(self, value):
"""Get a single network """Get a single network
@@ -597,13 +644,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(_pool.Pool, value, ignore_missing=ignore_missing) self._delete(_pool.Pool, value, ignore_missing=ignore_missing)
def find_pool(self, name_or_id): def find_pool(self, name_or_id, ignore_missing=True):
"""Find a single pool """Find a single pool
:param name_or_id: The name or ID of a pool. :param name_or_id: The name or ID of a pool.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.pool.Pool` or None :returns: One :class:`~openstack.network.v2.pool.Pool` or None
""" """
return _pool.Pool.find(self.session, name_or_id) return _pool.Pool.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_pool(self, value): def get_pool(self, value):
"""Get a single pool """Get a single pool
@@ -759,13 +812,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(port.Port, value, ignore_missing=ignore_missing) self._delete(port.Port, value, ignore_missing=ignore_missing)
def find_port(self, name_or_id): def find_port(self, name_or_id, ignore_missing=True):
"""Find a single port """Find a single port
:param name_or_id: The name or ID of a port. :param name_or_id: The name or ID of a port.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.port.Port` or None :returns: One :class:`~openstack.network.v2.port.Port` or None
""" """
return port.Port.find(self.session, name_or_id) return port.Port.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_port(self, value): def get_port(self, value):
"""Get a single port """Get a single port
@@ -852,13 +911,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(router.Router, value, ignore_missing=ignore_missing) self._delete(router.Router, value, ignore_missing=ignore_missing)
def find_router(self, name_or_id): def find_router(self, name_or_id, ignore_missing=True):
"""Find a single router """Find a single router
:param name_or_id: The name or ID of a router. :param name_or_id: The name or ID of a router.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.router.Router` or None :returns: One :class:`~openstack.network.v2.router.Router` or None
""" """
return router.Router.find(self.session, name_or_id) return router.Router.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_router(self, value): def get_router(self, value):
"""Get a single router """Get a single router
@@ -928,14 +993,20 @@ class Proxy(proxy.BaseProxy):
self._delete(security_group.SecurityGroup, value, self._delete(security_group.SecurityGroup, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_security_group(self, name_or_id): def find_security_group(self, name_or_id, ignore_missing=True):
"""Find a single security group """Find a single security group
:param name_or_id: The name or ID of a security group. :param name_or_id: The name or ID of a security group.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.security_group. :returns: One :class:`~openstack.network.v2.security_group.
SecurityGroup` or None SecurityGroup` or None
""" """
return security_group.SecurityGroup.find(self.session, name_or_id) return security_group.SecurityGroup.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_security_group(self, value): def get_security_group(self, value):
"""Get a single security group """Get a single security group
@@ -1028,15 +1099,20 @@ class Proxy(proxy.BaseProxy):
self._delete(security_group_rule.SecurityGroupRule, self._delete(security_group_rule.SecurityGroupRule,
value, ignore_missing=ignore_missing) value, ignore_missing=ignore_missing)
def find_security_group_rule(self, name_or_id): def find_security_group_rule(self, name_or_id, ignore_missing=True):
"""Find a single security group rule """Find a single security group rule
:param name_or_id: The name or ID of a security group rule. :param name_or_id: The name or ID of a security group rule.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.security_group_rule. :returns: One :class:`~openstack.network.v2.security_group_rule.
SecurityGroupRule` or None SecurityGroupRule` or None
""" """
return security_group_rule.SecurityGroupRule.find(self.session, return security_group_rule.SecurityGroupRule.find(
name_or_id) self.session, name_or_id, ignore_missing=ignore_missing)
def get_security_group_rule(self, value): def get_security_group_rule(self, value):
"""Get a single security group rule """Get a single security group rule
@@ -1105,13 +1181,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(subnet.Subnet, value, ignore_missing=ignore_missing) self._delete(subnet.Subnet, value, ignore_missing=ignore_missing)
def find_subnet(self, name_or_id): def find_subnet(self, name_or_id, ignore_missing=True):
"""Find a single subnet """Find a single subnet
:param name_or_id: The name or ID of a subnet. :param name_or_id: The name or ID of a subnet.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.subnet.Subnet` or None :returns: One :class:`~openstack.network.v2.subnet.Subnet` or None
""" """
return subnet.Subnet.find(self.session, name_or_id) return subnet.Subnet.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_subnet(self, value): def get_subnet(self, value):
"""Get a single subnet """Get a single subnet
@@ -1175,14 +1257,20 @@ class Proxy(proxy.BaseProxy):
self._delete(vpn_service.VPNService, value, self._delete(vpn_service.VPNService, value,
ignore_missing=ignore_missing) ignore_missing=ignore_missing)
def find_vpn_service(self, name_or_id): def find_vpn_service(self, name_or_id, ignore_missing=True):
"""Find a single vpn service """Find a single vpn service
:param name_or_id: The name or ID of a vpn service. :param name_or_id: The name or ID of a vpn service.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.vpn_service.VPNService` :returns: One :class:`~openstack.network.v2.vpn_service.VPNService`
or None or None
""" """
return vpn_service.VPNService.find(self.session, name_or_id) return vpn_service.VPNService.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_vpn_service(self, value): def get_vpn_service(self, value):
"""Get a single vpn service """Get a single vpn service

View File

@@ -31,13 +31,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._create(stack.Stack, **attrs) return self._create(stack.Stack, **attrs)
def find_stack(self, name_or_id): def find_stack(self, name_or_id, ignore_missing=True):
"""Find a single stack """Find a single stack
:param name_or_id: The name or ID of a stack. :param name_or_id: The name or ID of a stack.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.orchestration.v1.stack.Stack` or None :returns: One :class:`~openstack.orchestration.v1.stack.Stack` or None
""" """
return stack.Stack.find(self.session, name_or_id) return stack.Stack.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def stacks(self): def stacks(self):
"""Return a generator of stacks """Return a generator of stacks

View File

@@ -920,7 +920,7 @@ class Resource(collections.MutableMapping):
return resp return resp
@classmethod @classmethod
def find(cls, session, name_or_id, path_args=None): def find(cls, session, name_or_id, path_args=None, ignore_missing=True):
"""Find a resource by its name or id. """Find a resource by its name or id.
:param session: The session to use for making this request. :param session: The session to use for making this request.
@@ -930,11 +930,18 @@ class Resource(collections.MutableMapping):
:param dict path_args: A dictionary of arguments to construct :param dict path_args: A dictionary of arguments to construct
a compound URL. a compound URL.
See `How path_args are used`_ for details. See `How path_args are used`_ for details.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:return: The :class:`Resource` object matching the given name or id :return: The :class:`Resource` object matching the given name or id
or None if nothing matches. or None if nothing matches.
:raises: :class:`openstack.exceptions.DuplicateResource` if more :raises: :class:`openstack.exceptions.DuplicateResource` if more
than one resource is found for this request. than one resource is found for this request.
:raises: :class:`openstack.exceptions.ResourceNotFound` if nothing
is found and ignore_missing is ``False``.
""" """
def get_one_match(data, attr): def get_one_match(data, attr):
if len(data) == 1: if len(data) == 1:
@@ -964,9 +971,14 @@ class Resource(collections.MutableMapping):
msg = (msg % (cls.get_resource_name(), name_or_id)) msg = (msg % (cls.get_resource_name(), name_or_id))
raise exceptions.DuplicateResource(msg) raise exceptions.DuplicateResource(msg)
return get_one_match(info, cls.name_attribute) result = get_one_match(info, cls.name_attribute)
if result is not None:
return result
if ignore_missing:
return None return None
raise exceptions.ResourceNotFound(
"No %s found for %s" % (cls.__name__, name_or_id))
def wait_for_status(session, resource, status=None, failures=None, def wait_for_status(session, resource, status=None, failures=None,

View File

@@ -49,13 +49,19 @@ class Proxy(proxy.BaseProxy):
""" """
self._delete(alarm.Alarm, value, ignore_missing=ignore_missing) self._delete(alarm.Alarm, value, ignore_missing=ignore_missing)
def find_alarm(self, name_or_id): def find_alarm(self, name_or_id, ignore_missing=True):
"""Find a single alarm """Find a single alarm
:param name_or_id: The name or ID of a alarm. :param name_or_id: The name or ID of a alarm.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.alarm.Alarm` or None :returns: One :class:`~openstack.telemetry.v2.alarm.Alarm` or None
""" """
return alarm.Alarm.find(self.session, name_or_id) return alarm.Alarm.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_alarm(self, value): def get_alarm(self, value):
"""Get a single alarm """Get a single alarm
@@ -90,14 +96,20 @@ class Proxy(proxy.BaseProxy):
""" """
return self._update(alarm.Alarm, value, **attrs) return self._update(alarm.Alarm, value, **attrs)
def find_alarm_change(self, name_or_id): def find_alarm_change(self, name_or_id, ignore_missing=True):
"""Find a single alarm change """Find a single alarm change
:param name_or_id: The name or ID of a alarm change. :param name_or_id: The name or ID of a alarm change.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.alarm_change.AlarmChange` :returns: One :class:`~openstack.telemetry.v2.alarm_change.AlarmChange`
or None or None
""" """
return alarm_change.AlarmChange.find(self.session, name_or_id) return alarm_change.AlarmChange.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def alarm_changes(self, value): def alarm_changes(self, value):
"""Return a generator of alarm changes """Return a generator of alarm changes
@@ -110,14 +122,20 @@ class Proxy(proxy.BaseProxy):
return self._list(alarm_change.AlarmChange, paginated=False, return self._list(alarm_change.AlarmChange, paginated=False,
path_args={'alarm_id': alarm_id}) path_args={'alarm_id': alarm_id})
def find_capability(self, name_or_id): def find_capability(self, name_or_id, ignore_missing=True):
"""Find a single capability """Find a single capability
:param name_or_id: The name or ID of a capability. :param name_or_id: The name or ID of a capability.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.capability.Capability` :returns: One :class:`~openstack.telemetry.v2.capability.Capability`
or None or None
""" """
return capability.Capability.find(self.session, name_or_id) return capability.Capability.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def capabilities(self): def capabilities(self):
"""Return a generator of capabilities """Return a generator of capabilities
@@ -127,13 +145,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._list(capability.Capability, paginated=False) return self._list(capability.Capability, paginated=False)
def find_meter(self, name_or_id): def find_meter(self, name_or_id, ignore_missing=True):
"""Find a single meter """Find a single meter
:param name_or_id: The name or ID of a meter. :param name_or_id: The name or ID of a meter.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.meter.Meter` or None :returns: One :class:`~openstack.telemetry.v2.meter.Meter` or None
""" """
return meter.Meter.find(self.session, name_or_id) return meter.Meter.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def meters(self): def meters(self):
"""Return a generator of meters """Return a generator of meters
@@ -143,14 +167,20 @@ class Proxy(proxy.BaseProxy):
""" """
return self._list(meter.Meter, paginated=False) return self._list(meter.Meter, paginated=False)
def find_resource(self, name_or_id): def find_resource(self, name_or_id, ignore_missing=True):
"""Find a single resource """Find a single resource
:param name_or_id: The name or ID of a resource. :param name_or_id: The name or ID of a resource.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.resource.Resource` or :returns: One :class:`~openstack.telemetry.v2.resource.Resource` or
None None
""" """
return resource.Resource.find(self.session, name_or_id) return resource.Resource.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def get_resource(self, value): def get_resource(self, value):
"""Get a single resource """Get a single resource
@@ -185,13 +215,19 @@ class Proxy(proxy.BaseProxy):
""" """
return self._create(sample.Sample, **attrs) return self._create(sample.Sample, **attrs)
def find_sample(self, name_or_id): def find_sample(self, name_or_id, ignore_missing=True):
"""Find a single sample """Find a single sample
:param name_or_id: The name or ID of a sample. :param name_or_id: The name or ID of a sample.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.sample.Sample` or None :returns: One :class:`~openstack.telemetry.v2.sample.Sample` or None
""" """
return sample.Sample.find(self.session, name_or_id) return sample.Sample.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def samples(self): def samples(self):
"""Return a generator of samples """Return a generator of samples
@@ -201,14 +237,20 @@ class Proxy(proxy.BaseProxy):
""" """
return self._list(sample.Sample, paginated=False) return self._list(sample.Sample, paginated=False)
def find_statistics(self, name_or_id): def find_statistics(self, name_or_id, ignore_missing=True):
"""Find a single statistics """Find a single statistics
:param name_or_id: The name or ID of a statistics. :param name_or_id: The name or ID of a statistics.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.telemetry.v2.statistics.Statistics` :returns: One :class:`~openstack.telemetry.v2.statistics.Statistics`
or None or None
""" """
return statistics.Statistics.find(self.session, name_or_id) return statistics.Statistics.find(self.session, name_or_id,
ignore_missing=ignore_missing)
def statistics(self, value): def statistics(self, value):
"""Return a generator of statistics """Return a generator of statistics

View File

@@ -10,11 +10,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mock
import testtools import testtools
from openstack.compute.v2 import keypair from openstack.compute.v2 import keypair
from openstack import exceptions
EXAMPLE = { EXAMPLE = {
'keypair': { 'keypair': {
@@ -44,24 +42,3 @@ class TestKeypair(testtools.TestCase):
self.assertEqual(EXAMPLE['keypair']['fingerprint'], sot.fingerprint) self.assertEqual(EXAMPLE['keypair']['fingerprint'], sot.fingerprint)
self.assertEqual(EXAMPLE['keypair']['name'], sot.name) self.assertEqual(EXAMPLE['keypair']['name'], sot.name)
self.assertEqual(EXAMPLE['keypair']['public_key'], sot.public_key) self.assertEqual(EXAMPLE['keypair']['public_key'], sot.public_key)
def test_find(self):
resp = mock.Mock()
resp.body = EXAMPLE
sess = mock.Mock()
sess.get = mock.MagicMock()
sess.get.return_value = resp
sot = keypair.Keypair()
result = sot.find(sess, "kato")
url = 'os-keypairs/kato'
sess.get.assert_called_with(url, service=sot.service)
self.assertEqual(EXAMPLE['keypair']['fingerprint'], result.fingerprint)
self.assertEqual(EXAMPLE['keypair']['name'], result.name)
self.assertEqual(EXAMPLE['keypair']['public_key'], result.public_key)
def test_find_not_found(self):
sess = mock.Mock()
sess.get = mock.MagicMock()
sess.get.side_effect = exceptions.HttpException("404")
sot = keypair.Keypair()
self.assertEqual(None, sot.find(sess, "kato"))

View File

@@ -69,21 +69,9 @@ class TestOrchestrationProxy(test_proxy_base.TestProxyBase):
self.assertEqual(0, mock_find.call_count) self.assertEqual(0, mock_find.call_count)
@mock.patch.object(stack.Stack, 'find') def test_resources_with_stack_name(self):
def test_resources_with_stack_name(self, mock_find): self.verify_find('openstack.orchestration.v1.stack.Stack.find',
stack_name = 'test_stack' self.proxy.find_stack)
stack_id = '1234'
stack_identity = {'id': stack_id, 'stack_name': stack_name}
stk = stack.Stack(attrs=stack_identity)
mock_find.return_value = stk
path_args = {'stack_id': stack_id, 'stack_name': stack_name}
self.verify_list(self.proxy.resources, resource.Resource,
paginated=False,
method_args=[stack_name],
expected_kwargs={'path_args': path_args})
mock_find.assert_called_once_with(mock.ANY, stack_name)
@mock.patch.object(stack.Stack, 'find') @mock.patch.object(stack.Stack, 'find')
@mock.patch.object(resource.Resource, 'list') @mock.patch.object(resource.Resource, 'list')

View File

@@ -123,8 +123,14 @@ class TestProxyBase(base.TestCase):
def verify_find(self, mock_method, test_method, **kwargs): def verify_find(self, mock_method, test_method, **kwargs):
self._verify(mock_method, test_method, method_args=["name_or_id"], self._verify(mock_method, test_method, method_args=["name_or_id"],
expected_args=["name_or_id"], expected_result="result", expected_args=["name_or_id"],
**kwargs) expected_kwargs={'ignore_missing': True},
expected_result="result", **kwargs)
self._verify(mock_method, test_method,
method_args=["name_or_id", False],
expected_args=["name_or_id"],
expected_kwargs={'ignore_missing': False},
expected_result="result", **kwargs)
def verify_find2(self, mock_method, test_method, path_args, **kwargs): def verify_find2(self, mock_method, test_method, path_args, **kwargs):
method_args = ["name_or_id"] method_args = ["name_or_id"]

View File

@@ -1282,6 +1282,15 @@ class TestFind(base.TestCase):
self.assertEqual(None, FakeResource.find(self.mock_session, self.NAME)) self.assertEqual(None, FakeResource.find(self.mock_session, self.NAME))
def test_nada_not_ignored(self):
self.mock_get.side_effect = [
exceptions.HttpException(404, 'not found'),
FakeResponse({FakeResource.resources_key: []})
]
self.assertRaises(exceptions.ResourceNotFound, FakeResource.find,
self.mock_session, self.NAME, ignore_missing=False)
class TestWaitForStatus(base.TestCase): class TestWaitForStatus(base.TestCase):