Ejemplo n.º 1
0
    def render_POST(self, request):  # pylint: disable=R0201
        """Update image attributes: public/private"""

        # image_id required for all requests
        image_id = get_argument(request, 'image_id', u'')
        image_object = image.Image(image_id)
        if not image_object.is_authorized(request.context):
            LOG.audit(_("Not authorized to update attributes of image %s"),
                      image_id,
                      context=request.context)
            raise exception.NotAuthorized()

        operation = get_argument(request, 'operation', u'')
        if operation:
            # operation implies publicity toggle
            newstatus = (operation == 'add')
            LOG.audit(_("Toggling publicity flag of image %(image_id)s"
                        " %(newstatus)r") % locals(),
                      context=request.context)
            image_object.set_public(newstatus)
        else:
            # other attributes imply update
            LOG.audit(_("Updating user fields on image %s"),
                      image_id,
                      context=request.context)
            clean_args = {}
            for arg in request.args.keys():
                clean_args[arg] = request.args[arg][0]
            image_object.update_user_editable_fields(clean_args)
        return ''
Ejemplo n.º 2
0
    def delete(self):
        """ delete a registered image """
        image_id = self.get_argument("image_id", u"")
        image_object = image.Image(image_id)

        if not image.is_authorized(self.context):
            raise web.HTTPError(403)

        image_object.delete()

        self.set_status(204)
Ejemplo n.º 3
0
    def post(self):
        """ update image attributes: public/private """

        image_id = self.get_argument('image_id', u'')
        operation = self.get_argument('operation', u'')

        image_object = image.Image(image_id)

        if not image.is_authorized(self.context):
            raise web.HTTPError(403)

        image_object.set_public(operation == 'add')

        self.finish()
Ejemplo n.º 4
0
    def render_DELETE(self, request):  # pylint: disable=R0201
        """Delete a registered image"""
        image_id = get_argument(request, "image_id", u"")
        image_object = image.Image(image_id)

        if not image_object.is_authorized(request.context):
            LOG.audit(_("Unauthorized attempt to delete image %s"),
                      image_id,
                      context=request.context)
            raise exception.NotAuthorized()

        image_object.delete()
        LOG.audit(_("Deleted image: %s"), image_id, context=request.context)

        request.setResponseCode(204)
        return ''
Ejemplo n.º 5
0
    def do_test_images(self, manifest_file, expect_kernel_and_ramdisk,
                       image_bucket, image_name):
        "Test the image API."

        # create a bucket for our bundle
        objectstore.bucket.Bucket.create(image_bucket, self.context)
        bucket = objectstore.bucket.Bucket(image_bucket)

        # upload an image manifest/parts
        bundle_path = os.path.join(os.path.dirname(__file__), 'bundle')
        for path in glob.glob(bundle_path + '/*'):
            bucket[os.path.basename(path)] = open(path, 'rb').read()

        # register an image
        image.Image.register_aws_image(image_name,
                                       '%s/%s' % (image_bucket, manifest_file),
                                       self.context)

        # verify image
        my_img = image.Image(image_name)
        result_image_file = os.path.join(my_img.path, 'image')
        self.assertEqual(os.stat(result_image_file).st_size, 1048576)

        sha = hashlib.sha1(open(result_image_file).read()).hexdigest()
        self.assertEqual(sha, '3b71f43ff30f4b15b5cd85dd9e95ebc7e84eb5a3')

        if expect_kernel_and_ramdisk:
            # Verify the default kernel and ramdisk are set
            self.assertEqual(my_img.metadata['kernelId'], 'aki-test')
            self.assertEqual(my_img.metadata['ramdiskId'], 'ari-test')
        else:
            # Verify that the default kernel and ramdisk (the one from FLAGS)
            # doesn't get embedded in the metadata
            self.assertFalse('kernelId' in my_img.metadata)
            self.assertFalse('ramdiskId' in my_img.metadata)

        # verify image permissions
        context2 = context.RequestContext('user2', 'proj2')
        self.assertFalse(my_img.is_authorized(context2))

        # change user-editable fields
        my_img.update_user_editable_fields({'display_name': 'my cool image'})
        self.assertEqual('my cool image', my_img.metadata['displayName'])
        my_img.update_user_editable_fields({'display_name': ''})
        self.assert_(not my_img.metadata['displayName'])
Ejemplo n.º 6
0
 def test_user_editable_image_endpoint(self):
     pathdir = os.path.join(FLAGS.images_path, 'ami-testing')
     os.mkdir(pathdir)
     info = {'isPublic': False}
     with open(os.path.join(pathdir, 'info.json'), 'w') as f:
         json.dump(info, f)
     img = image.Image('ami-testing')
     # self.cloud.set_image_description(self.context, 'ami-testing',
     #                                  'Foo Img')
     # NOTE(vish): Above won't work unless we start objectstore or create
     #             a fake version of api/ec2/images.py conn that can
     #             call methods directly instead of going through boto.
     #             for now, just cheat and call the method directly
     self._fake_set_image_description(self.context, 'ami-testing',
                                      'Foo Img')
     self.assertEqual('Foo Img', img.metadata['description'])
     self._fake_set_image_description(self.context, 'ami-testing', '')
     self.assertEqual('', img.metadata['description'])
     shutil.rmtree(pathdir)
Ejemplo n.º 7
0
    def get(self, image_id):
        """ send the decrypted image file

        streaming content through python is slow and should only be used
        in development mode.  You should serve files via a web server
        in production.
        """

        self.set_header("Content-Type", "application/octet-stream")

        READ_SIZE = 64*1024

        img = image.Image(image_id)
        with open(img.image_path, 'rb') as fp:
            s = fp.read(READ_SIZE)
            while s:
                self.write(s)
                s = fp.read(READ_SIZE)

        self.finish()
Ejemplo n.º 8
0
 def __init__(self, name):
     resource.Resource.__init__(self)
     self.img = image.Image(name)