Example #1
0
    def show(self, context, image_id):
        """Returns a dict with image data for the given opaque image id."""
        try:
            image_meta = self._get_client(context).get_image_meta(image_id)
        except tank_exception.NotFound:
            raise exception.ImageNotFound(image_id=image_id)

        if not self._is_image_available(context, image_meta):
            raise exception.ImageNotFound(image_id=image_id)

        base_image_meta = self._translate_from_tank(image_meta)
        return base_image_meta
Example #2
0
    def delete(self, context, image_id):
        """Delete the given image.

        :raises: ImageNotFound if the image does not exist.
        :raises: NotAuthorized if the user is not an owner.

        """
        # NOTE(vish): show is to check if image is available
        image_meta = self.show(context, image_id)

        if FLAGS.use_deprecated_auth:
            # NOTE(parthi): only allow image deletions if the user
            # is a member of the project owning the image, in case of
            # setup without keystone
            # TODO Currently this access control breaks if
            # 1. Image is not owned by a project
            # 2. Deleting user is not bound a project
            properties = image_meta['properties']
            if (context.project_id and ('project_id' in properties)
                    and (context.project_id != properties['project_id'])):
                raise exception.NotAuthorized(_("Not the image owner"))

            if (context.project_id and ('owner_id' in properties)
                    and (context.project_id != properties['owner_id'])):
                raise exception.NotAuthorized(_("Not the image owner"))

        try:
            result = self._get_client(context).delete_image(image_id)
        except tank_exception.NotFound:
            raise exception.ImageNotFound(image_id=image_id)
        return result
Example #3
0
 def show_by_name(self, context, name):
     """Returns a dict containing image data for the given name."""
     images = copy.deepcopy(self.images.values())
     for image in images:
         if name == image.get('name'):
             return image
     raise exception.ImageNotFound(image_id=name)
Example #4
0
 def update_image(self, image_id, metadata, data):
     for i, image in enumerate(self.images):
         if image['id'] == str(image_id):
             if 'id' in metadata:
                 metadata['id'] = str(metadata['id'])
             self.images[i].update(metadata)
             return self.images[i]
     raise exception.ImageNotFound(image_id=image_id)
Example #5
0
 def show_by_name(self, context, name):
     """Returns a dict containing image data for the given name."""
     # TODO(vish): replace this with more efficient call when tank
     #             supports it.
     image_metas = self.detail(context)
     for image_meta in image_metas:
         if name == image_meta.get('name'):
             return image_meta
     raise exception.ImageNotFound(image_id=name)
Example #6
0
    def delete(self, context, image_id):
        """Delete the given image.

        :raises: ImageNotFound if the image does not exist.

        """
        removed = self.images.pop(image_id, None)
        if not removed:
            raise exception.ImageNotFound(image_id=image_id)
Example #7
0
    def update(self, context, image_id, metadata, data=None):
        """Replace the contents of the given image with the new data.

        :raises: ImageNotFound if the image does not exist.

        """
        if not self.images.get(image_id):
            raise exception.ImageNotFound(image_id=image_id)
        self.images[image_id] = copy.deepcopy(metadata)
Example #8
0
    def show(self, context, image_id):
        """Get data about specified image.

        Returns a dict containing image data for the given opaque image id.

        """
        image = self.images.get(str(image_id))
        if image:
            return copy.deepcopy(image)
        LOG.warn('Unable to find image id %s.  Have images: %s',
                 image_id, self.images)
        raise exception.ImageNotFound(image_id=image_id)
Example #9
0
    def update(self, context, image_id, image_meta, data=None):
        """Replace the contents of the given image with the new data.

        :raises: ImageNotFound if the image does not exist.

        """
        # NOTE(vish): show is to check if image is available
        self.show(context, image_id)
        image_meta = self._translate_to_tank(image_meta)
        try:
            client = self._get_client(context)
            image_meta = client.update_image(image_id, image_meta, data)
        except tank_exception.NotFound:
            raise exception.ImageNotFound(image_id=image_id)

        base_image_meta = self._translate_from_tank(image_meta)
        return base_image_meta
Example #10
0
    def get(self, context, image_id, data):
        """Calls out to Tank for metadata and data and writes data."""
        num_retries = FLAGS.tank_num_retries
        for count in xrange(1 + num_retries):
            client = self._get_client(context)
            try:
                image_meta, image_chunks = client.get_image(image_id)
                break
            except tank_exception.NotFound:
                raise exception.ImageNotFound(image_id=image_id)
            except Exception:
                if count == num_retries:
                    raise
            time.sleep(1)

        for chunk in image_chunks:
            data.write(chunk)

        base_image_meta = self._translate_from_tank(image_meta)
        return base_image_meta
Example #11
0
 def delete_image(self, image_id):
     for i, image in enumerate(self.images):
         if image['id'] == image_id:
             del self.images[i]
             return
     raise exception.ImageNotFound(image_id=image_id)
Example #12
0
 def get_image_meta(self, image_id):
     for image in self.images:
         if image['id'] == str(image_id):
             return image
     raise exception.ImageNotFound(image_id=image_id)