From ba43b4a4d21eb0a0c56ff750b205c64a1e2fa44f Mon Sep 17 00:00:00 2001 From: TerryHowe Date: Thu, 9 Jul 2015 11:53:23 -0600 Subject: [PATCH] Add ignore_missing to proxy find Change-Id: I271480e82b4f3a22b1eb56b62e45db4efd9606a3 --- openstack/cluster/v1/_proxy.py | 10 +- openstack/compute/v2/_proxy.py | 70 ++++++-- openstack/compute/v2/keypair.py | 11 +- openstack/database/v1/_proxy.py | 40 ++++- openstack/identity/v2/_proxy.py | 30 +++- openstack/identity/v3/_proxy.py | 90 ++++++++--- openstack/image/v1/_proxy.py | 10 +- openstack/image/v2/_proxy.py | 20 ++- openstack/key_management/v1/_proxy.py | 30 +++- openstack/network/v2/_proxy.py | 152 ++++++++++++++---- openstack/orchestration/v1/_proxy.py | 10 +- openstack/resource.py | 18 ++- openstack/telemetry/v2/_proxy.py | 70 ++++++-- .../tests/unit/compute/v2/test_keypair.py | 23 --- .../tests/unit/orchestration/v1/test_proxy.py | 18 +-- openstack/tests/unit/test_proxy_base.py | 10 +- openstack/tests/unit/test_resource.py | 9 ++ 17 files changed, 460 insertions(+), 161 deletions(-) diff --git a/openstack/cluster/v1/_proxy.py b/openstack/cluster/v1/_proxy.py index 455dd35b..3d430551 100644 --- a/openstack/cluster/v1/_proxy.py +++ b/openstack/cluster/v1/_proxy.py @@ -42,14 +42,20 @@ class Proxy(proxy.BaseProxy): """ 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. :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 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): """Get a single cluster. diff --git a/openstack/compute/v2/_proxy.py b/openstack/compute/v2/_proxy.py index e49353e6..7b1c8f45 100644 --- a/openstack/compute/v2/_proxy.py +++ b/openstack/compute/v2/_proxy.py @@ -24,14 +24,20 @@ from openstack import resource 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 :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 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): """Retrieve a generator of extensions @@ -41,13 +47,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Create a new flavor from attributes @@ -131,13 +143,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single image @@ -205,13 +223,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of keypairs @@ -271,13 +295,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single server @@ -385,14 +415,20 @@ class Proxy(proxy.BaseProxy): self._delete(server_interface.ServerInterface, value, 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 :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. 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): """Get a single server interface @@ -431,13 +467,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of server IPs diff --git a/openstack/compute/v2/keypair.py b/openstack/compute/v2/keypair.py index 87fb6f94..6cbfa970 100644 --- a/openstack/compute/v2/keypair.py +++ b/openstack/compute/v2/keypair.py @@ -11,12 +11,12 @@ # under the License. from openstack.compute import compute_service -from openstack import exceptions from openstack import resource class Keypair(resource.Resource): id_attribute = 'name' + name_attribute = None resource_key = 'keypair' resources_key = 'keypairs' base_path = '/os-keypairs' @@ -56,12 +56,3 @@ class Keypair(resource.Resource): self._attrs = resp self._reset_dirty() 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 diff --git a/openstack/database/v1/_proxy.py b/openstack/database/v1/_proxy.py index 4ecee2df..f4855e75 100644 --- a/openstack/database/v1/_proxy.py +++ b/openstack/database/v1/_proxy.py @@ -46,13 +46,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of databases @@ -75,13 +81,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single flavor @@ -130,13 +142,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single instance @@ -200,13 +218,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of users diff --git a/openstack/identity/v2/_proxy.py b/openstack/identity/v2/_proxy.py index 5e25297f..f98d058e 100644 --- a/openstack/identity/v2/_proxy.py +++ b/openstack/identity/v2/_proxy.py @@ -45,13 +45,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single role @@ -113,13 +119,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single tenant @@ -181,13 +193,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single user diff --git a/openstack/identity/v3/_proxy.py b/openstack/identity/v3/_proxy.py index 776d7076..63affe72 100644 --- a/openstack/identity/v3/_proxy.py +++ b/openstack/identity/v3/_proxy.py @@ -52,14 +52,20 @@ class Proxy(proxy.BaseProxy): self._delete(credential.Credential, value, 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 :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` 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): """Get a single credential @@ -124,13 +130,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single domain @@ -193,13 +205,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single endpoint @@ -264,13 +282,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single group @@ -334,13 +358,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single policy @@ -403,13 +433,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single project @@ -472,13 +508,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single service @@ -541,13 +583,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single user @@ -610,13 +658,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single trust diff --git a/openstack/image/v1/_proxy.py b/openstack/image/v1/_proxy.py index f7d01999..6fd4b0ab 100644 --- a/openstack/image/v1/_proxy.py +++ b/openstack/image/v1/_proxy.py @@ -43,13 +43,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single image diff --git a/openstack/image/v2/_proxy.py b/openstack/image/v2/_proxy.py index e62b8c1e..2988024e 100644 --- a/openstack/image/v2/_proxy.py +++ b/openstack/image/v2/_proxy.py @@ -45,13 +45,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single image @@ -113,13 +119,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single member diff --git a/openstack/key_management/v1/_proxy.py b/openstack/key_management/v1/_proxy.py index ea858aab..95d738b9 100644 --- a/openstack/key_management/v1/_proxy.py +++ b/openstack/key_management/v1/_proxy.py @@ -46,14 +46,20 @@ class Proxy(proxy.BaseProxy): """ 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 :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 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): """Get a single container @@ -118,13 +124,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single order @@ -188,13 +200,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single secret diff --git a/openstack/network/v2/_proxy.py b/openstack/network/v2/_proxy.py index 637e1715..d55307c0 100644 --- a/openstack/network/v2/_proxy.py +++ b/openstack/network/v2/_proxy.py @@ -32,14 +32,20 @@ from openstack import proxy 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 :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` 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): """Return a generator of extensions @@ -85,14 +91,20 @@ class Proxy(proxy.BaseProxy): """ 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 :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` 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): """Get a single floating ip @@ -158,14 +170,20 @@ class Proxy(proxy.BaseProxy): self._delete(health_monitor.HealthMonitor, value, 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 :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. 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): """Get a single health monitor @@ -230,13 +248,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single listener @@ -302,14 +326,20 @@ class Proxy(proxy.BaseProxy): self._delete(load_balancer.LoadBalancer, value, 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 :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` 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): """Get a single load balancer @@ -375,14 +405,20 @@ class Proxy(proxy.BaseProxy): self._delete(metering_label.MeteringLabel, value, 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 :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. 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): """Get a single metering label @@ -451,15 +487,20 @@ class Proxy(proxy.BaseProxy): self._delete(metering_label_rule.MeteringLabelRule, 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 :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. MeteringLabelRule` or None """ - return metering_label_rule.MeteringLabelRule.find(self.session, - name_or_id) + return metering_label_rule.MeteringLabelRule.find( + self.session, name_or_id, ignore_missing=ignore_missing) def get_metering_label_rule(self, value): """Get a single metering label rule @@ -529,13 +570,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single network @@ -597,13 +644,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single pool @@ -759,13 +812,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single port @@ -852,13 +911,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single router @@ -928,14 +993,20 @@ class Proxy(proxy.BaseProxy): self._delete(security_group.SecurityGroup, value, 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 :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. 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): """Get a single security group @@ -1028,15 +1099,20 @@ class Proxy(proxy.BaseProxy): self._delete(security_group_rule.SecurityGroupRule, 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 :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. SecurityGroupRule` or None """ - return security_group_rule.SecurityGroupRule.find(self.session, - name_or_id) + return security_group_rule.SecurityGroupRule.find( + self.session, name_or_id, ignore_missing=ignore_missing) def get_security_group_rule(self, value): """Get a single security group rule @@ -1105,13 +1181,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single subnet @@ -1175,14 +1257,20 @@ class Proxy(proxy.BaseProxy): self._delete(vpn_service.VPNService, value, 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 :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` 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): """Get a single vpn service diff --git a/openstack/orchestration/v1/_proxy.py b/openstack/orchestration/v1/_proxy.py index a37e4f11..40270ad9 100644 --- a/openstack/orchestration/v1/_proxy.py +++ b/openstack/orchestration/v1/_proxy.py @@ -31,13 +31,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of stacks diff --git a/openstack/resource.py b/openstack/resource.py index 41f49e95..3fb08253 100644 --- a/openstack/resource.py +++ b/openstack/resource.py @@ -920,7 +920,7 @@ class Resource(collections.MutableMapping): return resp @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. :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 a compound URL. 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 or None if nothing matches. :raises: :class:`openstack.exceptions.DuplicateResource` if more 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): if len(data) == 1: @@ -964,9 +971,14 @@ class Resource(collections.MutableMapping): msg = (msg % (cls.get_resource_name(), name_or_id)) 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 - return None + if ignore_missing: + 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, diff --git a/openstack/telemetry/v2/_proxy.py b/openstack/telemetry/v2/_proxy.py index 8b2127a1..7deb54e9 100644 --- a/openstack/telemetry/v2/_proxy.py +++ b/openstack/telemetry/v2/_proxy.py @@ -49,13 +49,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Get a single alarm @@ -90,14 +96,20 @@ class Proxy(proxy.BaseProxy): """ 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 :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` 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): """Return a generator of alarm changes @@ -110,14 +122,20 @@ class Proxy(proxy.BaseProxy): return self._list(alarm_change.AlarmChange, paginated=False, 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 :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` 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): """Return a generator of capabilities @@ -127,13 +145,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of meters @@ -143,14 +167,20 @@ class Proxy(proxy.BaseProxy): """ 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 :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 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): """Get a single resource @@ -185,13 +215,19 @@ class Proxy(proxy.BaseProxy): """ 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 :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 """ - 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): """Return a generator of samples @@ -201,14 +237,20 @@ class Proxy(proxy.BaseProxy): """ 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 :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` 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): """Return a generator of statistics diff --git a/openstack/tests/unit/compute/v2/test_keypair.py b/openstack/tests/unit/compute/v2/test_keypair.py index 9dae0c84..47b8446c 100644 --- a/openstack/tests/unit/compute/v2/test_keypair.py +++ b/openstack/tests/unit/compute/v2/test_keypair.py @@ -10,11 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. -import mock import testtools from openstack.compute.v2 import keypair -from openstack import exceptions EXAMPLE = { 'keypair': { @@ -44,24 +42,3 @@ class TestKeypair(testtools.TestCase): self.assertEqual(EXAMPLE['keypair']['fingerprint'], sot.fingerprint) self.assertEqual(EXAMPLE['keypair']['name'], sot.name) 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")) diff --git a/openstack/tests/unit/orchestration/v1/test_proxy.py b/openstack/tests/unit/orchestration/v1/test_proxy.py index 4258d975..3846162d 100644 --- a/openstack/tests/unit/orchestration/v1/test_proxy.py +++ b/openstack/tests/unit/orchestration/v1/test_proxy.py @@ -69,21 +69,9 @@ class TestOrchestrationProxy(test_proxy_base.TestProxyBase): self.assertEqual(0, mock_find.call_count) - @mock.patch.object(stack.Stack, 'find') - def test_resources_with_stack_name(self, mock_find): - stack_name = 'test_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) + def test_resources_with_stack_name(self): + self.verify_find('openstack.orchestration.v1.stack.Stack.find', + self.proxy.find_stack) @mock.patch.object(stack.Stack, 'find') @mock.patch.object(resource.Resource, 'list') diff --git a/openstack/tests/unit/test_proxy_base.py b/openstack/tests/unit/test_proxy_base.py index 2d14d28f..35b4b5a7 100644 --- a/openstack/tests/unit/test_proxy_base.py +++ b/openstack/tests/unit/test_proxy_base.py @@ -123,8 +123,14 @@ class TestProxyBase(base.TestCase): def verify_find(self, mock_method, test_method, **kwargs): self._verify(mock_method, test_method, method_args=["name_or_id"], - expected_args=["name_or_id"], expected_result="result", - **kwargs) + expected_args=["name_or_id"], + 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): method_args = ["name_or_id"] diff --git a/openstack/tests/unit/test_resource.py b/openstack/tests/unit/test_resource.py index 46b145e9..56725832 100644 --- a/openstack/tests/unit/test_resource.py +++ b/openstack/tests/unit/test_resource.py @@ -1282,6 +1282,15 @@ class TestFind(base.TestCase): 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):