Esempio n. 1
0
def upload(request):
    if request.method == "POST":
        form = UploadImageForm(request.POST)
        if form.is_valid():
            image = form.clean()
            metadata = {'is_public': image['is_public'],
                        'disk_format': 'ami',
                        'container_format': 'ami',
                        'name': image['name']}
            try:
                messages.success(request, "Image was successfully uploaded.")
            except:
                # TODO add better error management
                messages.error(request, "Image could not be uploaded, "
                        "please try again.")

            try:
                api.image_create(request, metadata, image['image_file'])
            except glance_exception.ClientConnectionError, e:
                LOG.error('Error connecting to glance while trying to upload'
                          ' image', exc_info=True)
                messages.error(request,
                               "Error connecting to glance: %s" % e.message)
            except glance_exception.Error, e:
                LOG.error('Glance exception while uploading image',
                          exc_info=True)
                messages.error(request, "Error adding image: %s" % e.message)
Esempio n. 2
0
def upload(request):
    if request.method == "POST":
        form = UploadImageForm(request.POST)
        if form.is_valid():
            image = form.clean()
            metadata = {
                'is_public': image['is_public'],
                'disk_format': 'ami',
                'container_format': 'ami',
                'name': image['name']
            }
            try:
                messages.success(request, "Image was successfully uploaded.")
            except:
                # TODO add better error management
                messages.error(
                    request, "Image could not be uploaded, "
                    "please try again.")

            try:
                api.image_create(request, metadata, image['image_file'])
            except glance_exception.ClientConnectionError, e:
                LOG.error(
                    'Error connecting to glance while trying to upload'
                    ' image',
                    exc_info=True)
                messages.error(request,
                               "Error connecting to glance: %s" % e.message)
            except glance_exception.Error, e:
                LOG.error('Glance exception while uploading image',
                          exc_info=True)
                messages.error(request, "Error adding image: %s" % e.message)
Esempio n. 3
0
class UploadMetadata(forms.SelfHandlingForm):
    image_meta_file = forms.FileField(label="Image Metadata File", required=True)

    def handle(self, request, data):
        data = self.files['image_meta_file'].read()

        try:
            root = etree.XML(data)
        except etree.XMLSyntaxError, e:
            messages.error(request, 'Metadata content is invalid')
            return

        image = {}

        image['name'] = root.findtext("name")
        image['location'] = root.findtext("location")
        image['is_public'] = True
        image['owner'] = request.user.username
        disk_format = root.findtext("format/disk")
        container_format = root.findtext("format/container")
        min_disk = root.findtext("info/min_disk")
        min_ram = root.findtext("info/min_ram")

        try:
            api.image_create(request, image, None)
            messages.success(request, "Image Metadata was successfully registerd")
        except glance_exception.BadStoreUri as e:
            messages.error(request, 'hogehoge %s' % str(e))
        
        return shortcuts.redirect(request.build_absolute_uri())
    def test_image_create(self):
        IMAGE_FILE = "someData"
        IMAGE_META = {"metadata": "foo"}

        glance_api = self.stub_glance_api()
        glance_api.add_image(IMAGE_META, IMAGE_FILE).AndReturn(TEST_RETURN)

        self.mox.ReplayAll()

        ret_val = api.image_create(self.request, IMAGE_META, IMAGE_FILE)

        self.assertIsInstance(ret_val, api.Image)
        self.assertEqual(ret_val._apidict, TEST_RETURN)

        self.mox.VerifyAll()
Esempio n. 5
0
        if properties is not None:
            for property in properties.getchildren():
                name = property.findtext('name')
                value = property.findtext('value')
                prop[name] = value
        prop['user_id'] = request.user.username
        meta['properties'] = prop

        meta_send = {}
        for key, value in meta.iteritems():
            if not value:
                continue
            meta_send[key] = value
        LOG.info('sending meta %s' % meta_send)
        try:
            upload_image = api.image_create(request, image, None)
            try:
                update_image = api.image_update(request, upload_image.id,
                                                meta_send)
            except glance_exception.Invalid as e:
                messages.error(request,
                               'Image Metadata has invalid data %s' % str(e))
                update_image = api.image_update(request, upload_image.id,
                                                {'location': ''})
                api.image_delete(request, upload_image.id)
                return shortcuts.redirect(request.build_absolute_uri())
            messages.success(request,
                             "Image Metadata was successfully registerd")
        except glance_exception.BadStoreUri as e:
            messages.error(request, 'Metadata has bad location %s' % str(e))
Esempio n. 6
0
    def test_metadata_upload_post(self):
        IMAGE_ID = u'1'
        METADATA_DATA = """
<image type="openstack-glance">
    <name>visibleImage</name>
    <location>swift://localhost/AUTH_test/Cont</location>
    <format>
      <disk />
      <container />
    </format>
    <size>0</size>
    <info>
       <min_disk>0</min_disk>
       <min_ram>1G</min_ram>
       <properties />
    </info>
</image>

"""
        METADATA_FILE = tempfile.TemporaryFile()
        METADATA_FILE.write(METADATA_DATA)
        METADATA_FILE.flush()
        METADATA_FILE.seek(0)

        image_dict = {
            'name': 'visibleImage',
            'container_format': 'novaImage',
            'location': 'swift://localhost/',
            'container_format': '',
            'disk_format': '',
            'image.size': '',
            'image.min_disk': '',
            'image.min_ram': ''
        }
        form_data = {
            'method': 'UploadMetadata',
            'image_meta_file': METADATA_FILE
        }

        image = api.Image(image_dict)

        self.visibleImage = api.Image(image_dict)

        #self.mox.StubOutWithMock(api, 'image_get_meta')
        #api.image_get_meta(IsA(http.HttpRequest), IMAGE_ID).AndReturn(image)
        self.mox.StubOutWithMock(api, 'token_get_tenant')
        api.token_get_tenant(IsA(http.HttpRequest), self.TEST_TENANT)
        self.mox.StubOutWithMock(api, 'image_list_detailed')
        api.image_list_detailed(IsA(http.HttpRequest)).AndReturn(self.images)
        self.mox.StubOutWithMock(api, 'image_create')
        api.image_create(IsA(http.HttpRequest), image, None)

        self.mox.ReplayAll()

        res = self.client.post(
            reverse('dash_images_metadata', args=[self.TEST_TENANT]),
            form_data)
        self.assertTemplateUsed(
            res, 'django_openstack/dash/images_metadata/index.html')

        #self.assertIn('images', res.context)
        #images = res.context['images']
        #self.assertEqual(len(images), 1)
        #self.assertEqual(images[0].name, 'visibleImage')

        self.mox.VerifyAll()
Esempio n. 7
0
        if properties is not None:
            for property in properties.getchildren():
                name = property.findtext('name')
                value = property.findtext('value')
                prop[name] = value
        prop['user_id'] = request.user.username
        meta['properties'] = prop

        meta_send = {}
        for key,value in meta.iteritems():
            if not value:
               continue
            meta_send[key] = value
        LOG.info('sending meta %s' % meta_send) 
        try:
            upload_image = api.image_create(request, image, None)
            try:
                update_image = api.image_update(request, upload_image.id , meta_send)
            except glance_exception.Invalid as e:
                messages.error(request, 'Image Metadata has invalid data %s' % str(e))
                update_image = api.image_update(request, upload_image.id, { 'location' : '' })
                api.image_delete(request, upload_image.id)
                return shortcuts.redirect(request.build_absolute_uri())
            messages.success(request, "Image Metadata was successfully registerd")
        except glance_exception.BadStoreUri as e:
            messages.error(request, 'Metadata has bad location %s' % str(e))

        return shortcuts.redirect(request.build_absolute_uri())

# utility
def _parse_location(url):