def find(cls, session, name_or_id, ignore_missing=True, **params): # No direct request possible, thus go directly to list data = cls.list(session, **params) result = None for maybe_result in data: # Since ID might be both int and str force cast id_value = str(cls._get_id(maybe_result)) name_value = maybe_result.name if str(name_or_id) in (id_value, name_value): if 'host' in params and maybe_result['host'] != params['host']: continue # Only allow one resource to be found. If we already # found a match, raise an exception to show it. if result is None: result = maybe_result else: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.__name__, name_or_id)) raise exceptions.DuplicateResource(msg) if result is not None: return result if ignore_missing: return None raise exceptions.ResourceNotFound( "No %s found for %s" % (cls.__name__, name_or_id))
def test_validation(self): self.mock_find_segment.side_effect = [ "seg1", exceptions.ResourceNotFound(), exceptions.DuplicateResource() ] self.assertTrue(self.constraint.validate("foo", self.ctx)) self.assertFalse(self.constraint.validate("bar", self.ctx)) self.assertFalse(self.constraint.validate("baz", self.ctx))
def get_one_match(generator, attr, raise_exception): try: first = next(generator) if any(generator): if raise_exception: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.get_resource_name(), name_or_id)) raise exceptions.DuplicateResource(msg) return None except StopIteration: return None result = cls.existing(**first) value = getattr(result, attr, False) if value == name_or_id: return result return None
def _get_one_match(cls, name_or_id, results): """Given a list of results, return the match""" the_result = None for maybe_result in results: id_value = cls._get_id(maybe_result) name_value = maybe_result.name if (id_value == name_or_id) or (name_value == name_or_id): # Only allow one resource to be found. If we already # found a match, raise an exception to show it. if the_result is None: the_result = maybe_result else: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.__name__, name_or_id)) raise exceptions.DuplicateResource(msg) return the_result
def _get_one_match(name_or_id): """Given a list of results, return the match""" the_result = None for maybe_result in ip_cache: id_value = maybe_result.id ip_value = maybe_result.floating_ip_address if (id_value == name_or_id) or (ip_value == name_or_id): # Only allow one resource to be found. If we already # found a match, raise an exception to show it. if the_result is None: the_result = maybe_result else: msg = "More than one %s exists with the name '%s'." msg = (msg % (_floating_ip.FloatingIP, name_or_id)) raise sdk_exceptions.DuplicateResource(msg) return the_result
def create(self, session, prepend_key=True, requires_id=True, endpoint_override=None, headers=None, uri=None): if not self.allow_create: raise exceptions.MethodNotSupported(self, "create") session = self._get_session(session) request = self._prepare_request(requires_id=False, prepend_key=prepend_key) # PATH is different if uri: request.url = uri elif self.create_path: request.url = self.create_path req_args = self._prepare_override_args( endpoint_override=endpoint_override, request_headers=request.headers, additional_headers=headers) response = session.post(request.url, json=request.body, **req_args) if response.status_code == 400: body = response.json() error = body.get('error', None) if error: if error['error_code'] == 'KMS.1104': msg = (_('Key with given alias %s already exist') % self.key_alias) raise exceptions.DuplicateResource(msg) else: msg = (_('Service returned an error with code=%s') % error['error_code']) raise exceptions.SDKException(msg) self._translate_response(response) return self
def find(cls, session, name_or_id, path_args=None): try: info = cls.list(session, id=name_or_id, fields=cls.id_attribute, path_args=path_args) if len(info) == 1: return info[0] except exceptions.HttpException: pass if cls.name_attribute: params = {cls.name_attribute: name_or_id, 'fields': cls.id_attribute} info = cls.list(session, path_args=path_args, **params) if len(info) == 1: return info[0] if len(info) > 1: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.resource_name, name_or_id)) raise exceptions.DuplicateResource(msg) msg = ("No %s with a name or ID of '%s' exists." % (cls.resource_name, name_or_id)) raise exceptions.ResourceNotFound(msg)
def find(cls, session, name_or_id, path_args=None): """Find a resource by its name or id. :param session: The session to use for making this request. :type session: :class:`~openstack.session.Session` :param resource_id: This resource's identifier, if needed by the request. The default is ``None``. :param dict path_args: A dictionary of arguments to construct a compound URL. See `How path_args are used`_ for details. :return: The :class:`Resource` object matching the given name or id or None if nothing matches. """ try: args = { cls.id_attribute: name_or_id, 'fields': cls.id_attribute, 'path_args': path_args, } info = cls.page(session, limit=2, **args) if len(info) == 1: return cls.existing(**info[0]) except exceptions.HttpException: pass if cls.name_attribute: params = { cls.name_attribute: name_or_id, 'fields': cls.id_attribute } info = cls.page(session, limit=2, path_args=path_args, **params) if len(info) == 1: return cls.existing(**info[0]) if len(info) > 1: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.get_resource_name(), name_or_id)) raise exceptions.DuplicateResource(msg) return None
def get_one_match(results, the_id, the_name): the_result = None for item in results: maybe_result = cls.existing(**item) id_value, name_value = None, None if the_id is not None: id_value = getattr(maybe_result, the_id, None) if the_name is not None: name_value = getattr(maybe_result, the_name, None) if (id_value == name_or_id) or (name_value == name_or_id): # Only allow one resource to be found. If we already # found a match, raise an exception to show it. if the_result is None: the_result = maybe_result else: msg = "More than one %s exists with the name '%s'." msg = (msg % (cls.get_resource_name(), name_or_id)) raise exceptions.DuplicateResource(msg) return the_result