def _get(self, resource_type, value=None, path_args=None, args=None): """Get a resource :param resource_type: The type of resource to get. :type resource_type: :class:`~ecl.resource.Resource` :param value: The value to get. Can be either the ID of a resource or a :class:`~ecl.resource.Resource` subclass. :param path_args: A dict containing arguments for forming the request URL, if needed. :param args: A optional dict containing arguments that will be translated into query strings when forming the request URL. :returns: The result of the ``get`` :rtype: :class:`~ecl.resource.Resource` """ res = self._get_resource(resource_type, value, path_args) try: return res.get(self.session, args=args) except exceptions.NotFoundException as e: raise exceptions.ResourceNotFound(message="No %s found for %s" % (resource_type.__name__, value), details=e.details, response=e.response, request_id=e.request_id, url=e.url, method=e.method, http_status=e.http_status, cause=e.cause)
def abandon_stack(self, stack, ignore_missing=False): """Abandon a stack :param stack: The value can be either the ID of a stack or a :class:`~ecl.orchestration.v1.stack.Stack` instance. :param bool ignore_missing: When set to ``False`` :class:`~ecl.exceptions.ResourceNotFound` will be raised when the stack does not exist. When set to ``True``, no exception will be set when attempting to abandon a nonexistent stack. :return: ``None`` """ try: if not isinstance(stack, _stack.Stack): stack = self.get_stack(stack) stack.abandon(self.session) except exceptions.NotFoundException as e: if ignore_missing: return None else: # Reraise with a more specific type and message raise exceptions.ResourceNotFound( message="No %s found for %s" % (_stack.Stack.__name__, str(stack)), details=e.details, response=e.response, request_id=e.request_id, url=e.url, method=e.method, http_status=e.http_status, cause=e.cause)
def _get(self, resource_type, value=None, requires_id=True, **attrs): """Get a resource :param resource_type: The type of resource to get. :type resource_type: :class:`~ecl.resource2.Resource` :param value: The value to get. Can be either the ID of a resource or a :class:`~ecl.resource2.Resource` subclass. :param dict attrs: Attributes to be passed onto the :meth:`~ecl.resource2.Resource.get` method. These should correspond to either :class:`~ecl.resource2.Body` or :class:`~ecl.resource2.Header` values on this resource. :returns: The result of the ``get`` :rtype: :class:`~ecl.resource2.Resource` """ res = self._get_resource(resource_type, value, **attrs) try: return res.get(self.session, requires_id=requires_id) except exceptions.NotFoundException as e: raise exceptions.ResourceNotFound(message="No %s found for %s" % (resource_type.__name__, value), details=e.details, response=e.response, request_id=e.request_id, url=e.url, method=e.method, http_status=e.http_status, cause=e.cause)
def _get(self, resource_type, value=None, requires_id=True, **params): """Get a resource :param resource_type: The type of resource to get. :type resource_type: :class:`~ecl.resource2.Resource` :param value: The value to get. Can be either the ID of a resource or a :class:`~ecl.resource2.Resource` subclass. :param dict params: Parameters to be passed onto the :meth:`~ecl.resource2.Resource.get` method. These are used as query parameters with GET request. :returns: The result of the ``get`` :rtype: :class:`~ecl.resource2.Resource` """ res = self._get_resource(resource_type, value) if not res.allow_get: raise exceptions.MethodNotSupported(res, "get") try: request = res._prepare_request(requires_id=requires_id) query_params = res._query_mapping._transpose(params) response = self.session.get(request.uri, endpoint_filter=res.service, params=query_params) res._translate_response(response) return res except exceptions.NotFoundException as e: raise exceptions.ResourceNotFound( message="No %s found for %s" % (resource_type.__name__, value), details=e.details, response=e.response, request_id=e.request_id, url=e.url, method=e.method, http_status=e.http_status, cause=e.cause)
def find(cls, session, name_or_id, ignore_missing=False, **params): """Find a resource by its name or id. :param session: The session to use for making this request. :type session: :class:`~ecl.session.Session` :param name_or_id: This resource's identifier, if needed by the request. The default is ``None``. :param bool ignore_missing: When set to ``False`` :class:`~ecl.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. :param dict params: Any additional parameters to be passed into underlying methods, such as to :meth:`~ecl.resource2.Resource.existing` in order to pass on URI parameters. :return: The :class:`Resource` object matching the given name or id or None if nothing matches. :raises: :class:`ecl.exceptions.DuplicateResource` if more than one resource is found for this request. :raises: :class:`ecl.exceptions.ResourceNotFound` if nothing is found and ignore_missing is ``False``. """ # Try to short-circuit by looking directly for a matching ID. data = cls.list(session, **params) result = cls._get_one_match(name_or_id, data) 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_resources_stack_not_found(self, mock_list, mock_find): stack_name = 'test_stack' mock_find.side_effect = exceptions.ResourceNotFound( 'No stack found for test_stack') ex = self.assertRaises(exceptions.ResourceNotFound, self.proxy.resources, stack_name) self.assertEqual('ResourceNotFound: No stack found for test_stack', six.text_type(ex))
def _delete(self, resource_type, value, path_args=None, ignore_missing=False): """Delete a resource :param resource_type: The type of resource to delete. This should be a :class:`~ecl.resource.Resource` subclass with a ``from_id`` method. :param value: The value to delete. Can be either the ID of a resource or a :class:`~ecl.resource.Resource` subclass. :param path_args: A dict containing arguments for forming the request URL, if needed. :param bool ignore_missing: When set to ``False`` :class:`~ecl.exceptions.ResourceNotFound` will be raised when the resource does not exist. When set to ``True``, no exception will be set when attempting to delete a nonexistent resource. :returns: The result of the ``delete`` :raises: ``ValueError`` if ``value`` is a :class:`~ecl.resource.Resource` that doesn't match the ``resource_type``. :class:`~ecl.exceptions.ResourceNotFound` when ignore_missing if ``False`` and a nonexistent resource is attempted to be deleted. """ res = self._get_resource(resource_type, value, path_args) try: rv = res.delete(self.session) except exceptions.NotFoundException as e: if ignore_missing: return None else: # Reraise with a more specific type and message raise exceptions.ResourceNotFound( message="No %s found for %s" % (resource_type.__name__, value), details=e.details, response=e.response, request_id=e.request_id, url=e.url, method=e.method, http_status=e.http_status, cause=e.cause) return rv
def delete_resource(self, session, resource_id, x_mvna_request_id=None, ignore_missing=False): uri = self.base_path + '/%s' % resource_id try: if x_mvna_request_id: resp = session.delete( uri, endpoint_filter=self.service, headers={"X-MVNA-Request-Id": x_mvna_request_id}) else: resp = session.delete(uri, endpoint_filter=self.service) except exceptions.NotFoundException: if ignore_missing: return None raise exceptions.ResourceNotFound( message="No %s found for %s" % (self.__class__.__name__, resource_id)) self._translate_response(resp, has_body=False) return self
def find(cls, session, name_or_id, path_args=None, ignore_missing=False): """Find a resource by its name or id. :param session: The session to use for making this request. :type session: :class:`~ecl.session.Session` :param name_or_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. :param bool ignore_missing: When set to ``False`` :class:`~ecl.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:`ecl.exceptions.DuplicateResource` if more than one resource is found for this request. :raises: :class:`ecl.exceptions.ResourceNotFound` if nothing is found and ignore_missing is ``False``. """ # Only return one matching resource. 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 # Try to short-circuit by looking directly for a matching ID. try: if cls.allow_retrieve: return cls.get_by_id(session, name_or_id, path_args=path_args) except exceptions.NotFoundException: pass data = cls.list(session, path_args=path_args) result = get_one_match(data, cls.id_attribute, cls.name_attribute) 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))