Example #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)
Example #2
0
    def get(cls, client, name):
        """Get a profile."""
        response = client.api.profiles[name].get()

        if response.status_code == 404:
            raise exceptions.NotFound(response.json())
        return cls(_client=client, **response.json()['metadata'])
Example #3
0
    def get(cls, client, name):
        """Get a container by name."""
        response = client.api.containers[name].get()

        if response.status_code == 404:
            raise exceptions.NotFound(response.json())
        container = cls(_client=client, **response.json()['metadata'])
        return container
Example #4
0
    def get(cls, client, fingerprint):
        """Get an image."""
        response = client.api.images[fingerprint].get()

        if response.status_code == 404:
            raise exceptions.NotFound(response.json())
        image = Image(_client=client, **response.json()['metadata'])
        return image
Example #5
0
    def fetch(self):
        """Fetch the object from LXD, populating attributes."""
        response = self._client.api.profiles[self.name].get()

        if response.status_code == 404:
            raise exceptions.NotFound(response.json())

        for key, val in six.iteritems(response.json()['metadata']):
            setattr(self, key, val)
Example #6
0
    def get(cls, client, name):
        """Get a profile."""
        try:
            response = client.api.profiles[name].get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        return cls(_client=client, **response.json()['metadata'])
Example #7
0
    def export(self):
        """Export the image."""
        try:
            response = self._client.api.images[self.fingerprint].export.get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        return response.content
Example #8
0
 def get(self, filepath):
     try:
         response = self._client.api.containers[
             self._container.name].files.get(params={'path': filepath})
     except exceptions.LXDAPIException as e:
         # LXD 2.0.3+ return 404, not 500,
         if e.response.status_code in (500, 404):
             raise exceptions.NotFound()
         raise
     return response.content
Example #9
0
 def fetch(self):
     """Reload the container information."""
     try:
         response = self._client.api.containers[self.name].get()
     except exceptions.LXDAPIException as e:
         if e.response.status_code == 404:
             raise exceptions.NotFound()
         raise
     for key, value in six.iteritems(response.json()['metadata']):
         setattr(self, key, value)
Example #10
0
    def fetch(self):
        """Fetch the object from LXD, populating attributes."""
        try:
            response = self._client.api.images[self.fingerprint].get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        for key, val in six.iteritems(response.json()['metadata']):
            setattr(self, key, val)
Example #11
0
    def get(cls, client, fingerprint):
        """Get an image."""
        try:
            response = client.api.images[fingerprint].get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        image = cls(_client=client, **response.json()['metadata'])
        return image
Example #12
0
    def get(cls, client, name):
        """Get a container by name."""
        try:
            response = client.api.containers[name].get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        container = cls(client, **response.json()['metadata'])
        return container
Example #13
0
    def get(cls, client, container, name):
        try:
            response = client.api.containers[
                container.name].snapshots[name].get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise

        snapshot = cls(client,
                       container=container,
                       **response.json()['metadata'])
        # Snapshot names are namespaced in LXD, as
        # container-name/snapshot-name. We hide that implementation
        # detail.
        snapshot.name = snapshot.name.split('/')[-1]
        return snapshot
Example #14
0
    def sync(self, rollback=False):
        """Sync from the server.

        When collections of objects are retrieved from the server, they
        are often partial objects. The full object must be retrieved before
        it can modified. This method is called when getattr is called on
        a non-initaliazed object.
        """
        # XXX: rockstar (25 Jun 2016) - This has the potential to step
        # on existing attributes.
        try:
            response = self.api.get()
        except exceptions.LXDAPIException as e:
            if e.response.status_code == 404:
                raise exceptions.NotFound()
            raise
        for key, val in response.json()['metadata'].items():
            if key not in self.__dirty__ or rollback:
                setattr(self, key, val)
        if rollback:
            del self.__dirty__[:]