Example #1
0
    def modify_meta_by_user(self, user, image, format="json"):
        params = validate_modify_image(request.params)

        image_q = meta.Session.query(Image)
        image = image_q.filter(Image.name == image).filter(Image.owner.has(User.user_name == user)).first()

        if image:
            inline_auth(AnyOf(OwnsImage(image), HasPermission("image_modify")), auth_403)
            for k, v in params.iteritems():
                if v:
                    setattr(image, k, v)
            image.modified = datetime.utcfromtimestamp(time())
            meta.Session.commit()
        else:
            abort(404, "404 Not Found")
Example #2
0
    def modify_meta_by_user(self, user, image, format='json'):
        params = validate_modify_image(request.params)

        image_q = meta.Session.query(Image)
        image = image_q.filter(Image.name==image)\
                       .filter(Image.owner.has(User.user_name==user))\
                       .first()

        if image:
            inline_auth(AnyOf(OwnsImage(image), HasPermission('image_modify')),
                        auth_403)
            for k, v in params.iteritems():
                if v:
                    setattr(image, k, v)
            image.modified = datetime.utcfromtimestamp(time())
            meta.Session.commit()
        else:
            abort(404, '404 Not Found')
Example #3
0
    def modify_meta_by_user(self, user, image, format='json'):
        params = validate_modify_image(request.params)

        image_q = meta.Session.query(Image)
        image = image_q.filter(Image.name==image)\
                       .filter(Image.owner.has(User.user_name==user))\
                       .first()

        if image:
            inline_auth(AnyOf(OwnsImage(image), HasPermission('image_modify')), auth_403)
            # Make sure all given attributes exist in the image object.
            for k,v in request.params.iteritems():
                if not hasattr(image, k):
                    abort(400, 'The "%s" image metadata does not exist.  Please check your syntax and try again.' % (k))

            # Do a check here to make sure we do not overwrite
            # any existing image. (Andre)
            if ('name' in params) and (params['name'] != image.name):
                image2 = image_q.filter(Image.name==params['name'])\
                    .filter(Image.owner.has(User.user_name==user))\
                    .first()
                if image2:
                    log.debug('Conflict detected in image renaming: %s -> %s.  Operation aborted.' % (image.name, params['name']))
                    abort(409, 'Cannot rename an image to an existing image.  Operation aborted.')

            # Here we must have some smarts to check if the new metadata has less hypervisors
            # than the previous one.  If this is true, then we must cleanup the images for the
            # hypervisors that are not listed anymore, else we will end up with stale image
            # files on the server.             
            if image.hypervisor != None and params['hypervisor'] and params['hypervisor'] != None:
                previous_hypervisors = image.hypervisor.split(',')
                new_hypervisors = params['hypervisor'].split(',')
                for previous_hypervisor in previous_hypervisors:
                    if previous_hypervisor not in new_hypervisors:
                        # Cleanup
                        image.delete_image_file_for_hypervisor(previous_hypervisor)

            # Check to see if the user wants to assign the image to a new owner.
            # If that is the case, then we need to rename the image file because
            # it has the owner's username hardcoded in its filename.
            if ('owner' in params) and (params['owner'] != None) and (params['owner'] != image.owner):
                # Verify if target user exist
                user_q = meta.Session.query(User)
                target_user = user_q.filter(User.user_name==params['owner']).first()
                if not target_user:
                    abort(400, 'The new image owner %s does not exist.' % (params['owner']))

                log.debug('Changing ownership of image %s from user %s to user %s.' % 
                          (image.name, image.owner.user_name, target_user.user_name))
                if not image.change_image_files_to_new_owner(target_user):
                    # Could not change owner because of conflict.  Abort operation.
                    abort(409, 'Could not change ownership of the image because it conflicts with an image already owned by the target user.  Operation aborted.')
                # Don't forget to delete the 'owner' parameter (it is a special case) 
                # else the setattr call below will not like it.
                del params['owner']

            # If we rename the image slot, don't forget to rename the associated image files accordingly.
            # Calling the image.rename() method will take care of this.
            if ('name' in params) and (params['name'] != image.name):
                if not image.rename(params['name']):
                    abort(409, 'Could not rename the image because it conflicts with an image already owned by the target user.  Operation aborted.')

            for k,v in params.iteritems():
                if v != None:
                    setattr(image, k, v)
            image.modified = datetime.utcfromtimestamp(time())
            meta.Session.commit()
        else:
            abort(404, '404 Not Found')