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 create(self, session): # This overrides the default behavior of resource creation because # heat doesn't accept resource_key in its request. # & Will use self.stack_prepare_request to get proper headers. if not self.allow_create: raise exceptions.MethodNotSupported(self, "create") if self.put_create: request = self.stack_prepare_request(session, requires_id=True, prepend_key=False) response = session.put(request.uri, endpoint_filter=self.service, json=request.body, headers=request.headers) else: request = self.stack_prepare_request(session, requires_id=False, prepend_key=False) response = session.post(request.uri, endpoint_filter=self.service, json=request.body, headers=request.headers) self._translate_response(response) return self
def abandon(self, session): self.base_path = '/stacks/%s/%s/abandon' % (self.name, self.id) if not self.allow_delete: raise exceptions.MethodNotSupported(self, "delete") request = self._prepare_request(requires_id=False) response = session.delete(request.uri, endpoint_filter=self.service, headers={"Accept": ""}) self._translate_response(response, has_body=False) return self
def create_by_id(cls, session, attrs, resource_id=None, path_args=None): """Create a remote resource from its attributes. :param session: The session to use for making this request. :type session: :class:`~ecl.session.Session` :param dict attrs: The attributes to be sent in the body of the request. :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: A ``dict`` representing the response body. :raises: :exc:`~ecl.exceptions.MethodNotSupported` if :data:`Resource.allow_create` is not set to ``True``. """ if not cls.allow_create: raise exceptions.MethodNotSupported(cls, 'create') # Convert attributes from Resource types into their ids. attrs = cls.convert_ids(attrs) headers = attrs.pop(HEADERS, None) body = cls._get_create_body(attrs) url = cls._get_url(path_args, resource_id) args = {'json': body} if headers: args[HEADERS] = headers if resource_id: resp = session.put(url, endpoint_filter=cls.service, **args) else: resp = session.post(url, endpoint_filter=cls.service, **args) resp_headers = resp.headers resp = resp.json() if cls.resource_key: resp = resp[cls.resource_key] if resp_headers: resp[HEADERS] = copy.deepcopy(resp_headers) return resp
def create(self, session, prepend_key=True): """ Recordset allow creating several records once. Thus return a list. """ if not self.allow_create: raise exceptions.MethodNotSupported(self, "create") if self.put_create: request = self._prepare_request(requires_id=True, prepend_key=prepend_key) response = session.put(request.uri, endpoint_filter=self.service, json=request.body, headers=request.headers) else: request = self._prepare_request(requires_id=False, prepend_key=prepend_key) response = session.post(request.uri, endpoint_filter=self.service, json=request.body, headers=request.headers) return list(self._translate_recordsets(response, has_body=True))
def delete(self, session): """Delete the remote resource based on this instance. :param session: The session to use for making this request. :type session: :class:`~ecl.session.Session` :return: This :class:`Resource` instance. :raises: :exc:`~ecl.exceptions.MethodNotSupported` if :data:`Resource.allow_update` is not set to ``True``. """ if not self.allow_delete: raise exceptions.MethodNotSupported(self, "delete") request = self._prepare_request() response = session.delete(request.uri, endpoint_filter=self.service, headers={"Accept": ""}) self._translate_response(response, has_body=False) return self
def test_method_not_supported(self): exc = exceptions.MethodNotSupported(self.__class__, 'list') expected = ('The list method is not supported for ' + 'ecl.tests.unit.test_exceptions.Test_Exception') self.assertEqual(expected, str(exc))