Exemplo n.º 1
0
    def _assert_response(
            self, response, allowed_status_codes=(200,), stream=False):
        """Assert properties of the response.

        LXD's API clearly defines specific responses. If the API
        response is something unexpected (i.e. an error), then
        we need to raise an exception and have the call points
        handle the errors or just let the issue be raised to the
        user.
        """
        if response.status_code not in allowed_status_codes:
            if response.status_code == 404:
                raise exceptions.NotFound(response)
            raise exceptions.LXDAPIException(response)

        # In the case of streaming, we can't validate the json the way we
        # would with normal HTTP responses, so just ignore that entirely.
        if stream:
            return

        try:
            data = response.json()
        except ValueError:
            # Not a JSON response
            return

        if response.status_code == 200:
            # Synchronous request
            try:
                if data['type'] != 'sync':
                    raise exceptions.LXDAPIException(response)
            except KeyError:
                # Missing 'type' in response
                raise exceptions.LXDAPIException(response)
Exemplo n.º 2
0
    def _assert_response(self, response, allowed_status_codes=(200, )):
        """Assert properties of the response.

        LXD's API clearly defines specific responses. If the API
        response is something unexpected (i.e. an error), then
        we need to raise an exception and have the call points
        handle the errors or just let the issue be raised to the
        user.
        """
        if response.status_code not in allowed_status_codes:
            raise exceptions.LXDAPIException(response)

        try:
            data = response.json()
        except ValueError:
            # Not a JSON response
            return

        if response.status_code == 200:
            # Synchronous request
            try:
                if data['type'] != 'sync':
                    raise exceptions.LXDAPIException(response)
            except KeyError:
                # Missing 'type' in response
                raise exceptions.LXDAPIException(response)
Exemplo n.º 3
0
    def wait(self):
        """Wait for the operation to complete and return."""
        response = self._client.api.operations[self.id].wait.get()

        try:
            if response.json()['metadata']['status'] == 'Failure':
                raise exceptions.LXDAPIException(response)
        except KeyError:
            # Support for legacy LXD
            pass
Exemplo n.º 4
0
 def side_effect(*args, **kwargs):
     raise lxdcore_exceptions.LXDAPIException(MockResponse(404))
Exemplo n.º 5
0
 def container_get(*args, **kwargs):
     raise lxdcore_exceptions.LXDAPIException(MockResponse(404))
Exemplo n.º 6
0
 def container_create(*args, **kwargs):
     raise lxdcore_exceptions.LXDAPIException(MockResponse(500))
Exemplo n.º 7
0
 def profile_create(*args, **kwargs):
     raise lxdcore_exceptions.LXDAPIException(MockResponse(500))