Example #1
0
 def _get_size(self, context, image_meta, location):
     # retrieve the image size from remote store (if not provided)
     try:
         return image_meta.get("size", 0) or get_size_from_backend(context, location)
     except (exception.NotFound, exception.BadStoreUri) as e:
         LOG.debug(e)
         raise HTTPBadRequest(explanation=e.msg, content_type="text/plain")
Example #2
0
 def _handle_source(self, req, image_id, image_meta, image_data):
     copy_from = self._copy_from(req)
     location = image_meta.get("location")
     sources = filter(lambda x: x, (copy_from, location, image_data))
     if len(sources) >= 2:
         msg = _("It's invalid to provide multiple image sources.")
         LOG.debug(msg)
         raise HTTPBadRequest(explanation=msg, request=req, content_type="text/plain")
     if image_data:
         image_meta = self._validate_image_for_activation(req, image_id, image_meta)
         image_meta = self._upload_and_activate(req, image_meta)
     elif copy_from:
         msg = _("Triggering asynchronous copy from external source")
         LOG.info(msg)
         self.pool.spawn_n(self._upload_and_activate, req, image_meta)
     else:
         if location:
             self._validate_image_for_activation(req, image_id, image_meta)
             image_size_meta = image_meta.get("size")
             if image_size_meta:
                 image_size_store = get_size_from_backend(req.context, location)
                 # NOTE(zhiyan): A returned size of zero usually means
                 # the driver encountered an error. In this case the
                 # size provided by the client will be used as-is.
                 if image_size_store and image_size_store != image_size_meta:
                     msg = _(
                         "Provided image size must match the stored "
                         "image size. (provided size: %(ps)d, "
                         "stored size: %(ss)d)"
                     ) % {"ps": image_size_meta, "ss": image_size_store}
                     LOG.debug(msg)
                     raise HTTPConflict(explanation=msg, request=req, content_type="text/plain")
             image_meta = self._activate(req, image_id, location)
     return image_meta
Example #3
0
 def _get_size(self, context, image_meta, location):
     # retrieve the image size from remote store (if not provided)
     try:
         return (image_meta.get('size', 0)
                 or get_size_from_backend(context, location))
     except (exception.NotFound, exception.BadStoreUri) as e:
         LOG.debug(e)
         raise HTTPBadRequest(explanation=e.msg, content_type="text/plain")
Example #4
0
def _set_image_size(context, image, locations):
    if not image.size:
        for location in locations:
            size_from_backend = store.get_size_from_backend(
                context, location['url'])
            if size_from_backend:
                # NOTE(flwang): This assumes all locations have the same size
                image.size = size_from_backend
                break
Example #5
0
def _set_image_size(context, image, locations):
    if not image.size:
        for location in locations:
            size_from_backend = store.get_size_from_backend(
                context, location['url'])
            if size_from_backend:
                # NOTE(flwang): This assumes all locations have the same size
                image.size = size_from_backend
                break
Example #6
0
    def _handle_source(self, req, image_id, image_meta, image_data):
        copy_from = self._copy_from(req)
        location = image_meta.get('location')
        sources = filter(lambda x: x, (copy_from, location, image_data))
        if len(sources) >= 2:
            msg = "It's invalid to provide multiple image sources."
            LOG.debug(msg)
            raise HTTPBadRequest(explanation=msg,
                                 request=req,
                                 content_type="text/plain")
        if image_data:
            image_meta = self._validate_image_for_activation(req,
                                                             image_id,
                                                             image_meta)
            image_meta = self._upload_and_activate(req, image_meta)
        elif copy_from:
            msg = _('Triggering asynchronous copy from external source')
            LOG.info(msg)
            self.pool.spawn_n(self._upload_and_activate, req, image_meta)
        else:
            if location:
                try:
                    validate_location(req.context, location)
                except exception.BadStoreUri as bse:
                    raise HTTPBadRequest(explanation=bse.msg,
                                         request=req)

                self._validate_image_for_activation(req, image_id, image_meta)
                image_size_meta = image_meta.get('size')
                if image_size_meta:
                    image_size_store = get_size_from_backend(req.context,
                                                             location)
                    # NOTE(zhiyan): A returned size of zero usually means
                    # the driver encountered an error. In this case the
                    # size provided by the client will be used as-is.
                    if (image_size_store and
                            image_size_store != image_size_meta):
                        msg = ("Provided image size must match the stored "
                               "image size. (provided size: %(ps)d, "
                               "stored size: %(ss)d)" % {
                                   "ps": image_size_meta,
                                   "ss": image_size_store})
                        LOG.debug(msg)
                        raise HTTPConflict(explanation=msg,
                                           request=req,
                                           content_type="text/plain")
                location_data = {'url': location, 'metadata': {},
                                 'status': 'active'}
                image_meta = self._activate(req, image_id, location_data)
        return image_meta
Example #7
0
    def _reserve(self, req, image_meta):
        """
        Adds the image metadata to the registry and assigns
        an image identifier if one is not supplied in the request
        headers. Sets the image's status to `queued`.

        :param req: The WSGI/Webob Request object
        :param id: The opaque image identifier

        :raises HTTPConflict if image already exists
        :raises HTTPBadRequest if image metadata is not valid
        """
        location = image_meta.get('location')
        if location:
            store = get_store_from_location(location)
            # check the store exists before we hit the registry, but we
            # don't actually care what it is at this point
            self.get_store_or_400(req, store)

            # retrieve the image size from remote store (if not provided)
            image_meta['size'] = image_meta.get('size', 0) \
                                 or get_size_from_backend(location)
        else:
            # Ensure that the size attribute is set to zero for uploadable
            # images (if not provided). The size will be set to a non-zero
            # value during upload
            image_meta['size'] = image_meta.get('size', 0)

        image_meta['status'] = 'queued'

        try:
            image_meta = registry.add_image_metadata(req.context, image_meta)
            return image_meta
        except exception.Duplicate:
            msg = (_("An image with identifier %s already exists")
                  % image_meta['id'])
            logger.error(msg)
            raise HTTPConflict(msg, request=req, content_type="text/plain")
        except exception.Invalid, e:
            msg = (_("Failed to reserve image. Got error: %(e)s") % locals())
            for line in msg.split('\n'):
                logger.error(line)
            raise HTTPBadRequest(msg, request=req, content_type="text/plain")
Example #8
0
    def _reserve(self, req, image_meta):
        """
        Adds the image metadata to the registry and assigns
        an image identifier if one is not supplied in the request
        headers. Sets the image's status to `queued`.

        :param req: The WSGI/Webob Request object
        :param id: The opaque image identifier

        :raises HTTPConflict if image already exists
        :raises HTTPBadRequest if image metadata is not valid
        """
        location = image_meta.get('location')
        if location:
            store = get_store_from_location(location)
            # check the store exists before we hit the registry, but we
            # don't actually care what it is at this point
            self.get_store_or_400(req, store)

            # retrieve the image size from remote store (if not provided)
            image_meta['size'] = image_meta.get('size', 0) \
                                 or get_size_from_backend(location)
        else:
            # Ensure that the size attribute is set to zero for uploadable
            # images (if not provided). The size will be set to a non-zero
            # value during upload
            image_meta['size'] = image_meta.get('size', 0)

        image_meta['status'] = 'queued'

        try:
            image_meta = registry.add_image_metadata(req.context, image_meta)
            return image_meta
        except exception.Duplicate:
            msg = (_("An image with identifier %s already exists")
                  % image_meta['id'])
            logger.error(msg)
            raise HTTPConflict(msg, request=req, content_type="text/plain")
        except exception.Invalid, e:
            msg = (_("Failed to reserve image. Got error: %(e)s") % locals())
            for line in msg.split('\n'):
                logger.error(line)
            raise HTTPBadRequest(msg, request=req, content_type="text/plain")
Example #9
0
 def _get_size(self, context, image_meta, location):
     # retrieve the image size from remote store (if not provided)
     return image_meta.get('size', 0) or get_size_from_backend(context,
                                                               location)
Example #10
0
 def _get_size(self, context, image_meta, location):
     # retrieve the image size from remote store (if not provided)
     return image_meta.get('size', 0) or get_size_from_backend(context,
                                                               location)