def test_vol_glance_metadata_copy_from_volume_to_volume(self):
        ctxt = context.get_admin_context()
        db.volume_create(ctxt, {"id": fake.VOLUME_ID})
        db.volume_create(ctxt, {"id": fake.VOLUME2_ID, "source_volid": fake.VOLUME_ID})
        db.volume_glance_metadata_create(ctxt, fake.VOLUME_ID, "key1", "value1")
        db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, fake.VOLUME_ID, fake.VOLUME2_ID)

        expected_meta = {"key": "key1", "value": "value1"}

        for meta in db.volume_glance_metadata_get(ctxt, fake.VOLUME2_ID):
            for (key, value) in expected_meta.items():
                self.assertEqual(value, meta[key])
    def test_vol_glance_metadata_copy_from_volume_to_volume(self):
        ctxt = context.get_admin_context()
        db.volume_create(ctxt, {'id': 1})
        db.volume_create(ctxt, {'id': 100, 'source_volid': 1})
        vol_meta = db.volume_glance_metadata_create(ctxt, 1, 'key1', 'value1')
        db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 1, 100)

        expected_meta = {'key': 'key1', 'value': 'value1'}

        for meta in db.volume_glance_metadata_get(ctxt, 100):
            for (key, value) in expected_meta.items():
                self.assertEqual(meta[key], value)
    def test_vol_glance_metadata_copy_from_volume_to_volume(self):
        ctxt = context.get_admin_context()
        db.volume_create(ctxt, {"id": 1})
        db.volume_create(ctxt, {"id": 100, "source_volid": 1})
        db.volume_glance_metadata_create(ctxt, 1, "key1", "value1")
        db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 1, 100)

        expected_meta = {"key": "key1", "value": "value1"}

        for meta in db.volume_glance_metadata_get(ctxt, 100):
            for (key, value) in expected_meta.items():
                self.assertEqual(meta[key], value)
    def test_vol_glance_metadata_copy_from_volume_to_volume(self):
        ctxt = context.get_admin_context()
        db.volume_create(ctxt, {'id': 1})
        db.volume_create(ctxt, {'id': 100, 'source_volid': 1})
        db.volume_glance_metadata_create(ctxt, 1, 'key1', 'value1')
        db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 1, 100)

        expected_meta = {'key': 'key1',
                         'value': 'value1'}

        for meta in db.volume_glance_metadata_get(ctxt, 100):
            for (key, value) in expected_meta.items():
                self.assertEqual(value, meta[key])
    def test_vol_glance_metadata_copy_from_volume_to_volume(self):
        ctxt = context.get_admin_context()
        db.volume_create(ctxt, {'id': fake.VOLUME_ID})
        db.volume_create(ctxt, {
            'id': fake.VOLUME2_ID,
            'source_volid': fake.VOLUME_ID
        })
        db.volume_glance_metadata_create(ctxt, fake.VOLUME_ID, 'key1',
                                         'value1')
        db.volume_glance_metadata_copy_from_volume_to_volume(
            ctxt, fake.VOLUME_ID, fake.VOLUME2_ID)

        expected_meta = {'key': 'key1', 'value': 'value1'}

        for meta in db.volume_glance_metadata_get(ctxt, fake.VOLUME2_ID):
            for (key, value) in expected_meta.items():
                self.assertEqual(value, meta[key])
예제 #6
0
    def create_volume(self, context, volume_id, snapshot_id=None,
                      image_id=None, source_volid=None, **kwargs):
        context = context.elevated()
        volume = db.volume_get(context, volume_id)
        LOG.info(_("volume %s: creating"), volume['name'])
        model_update = {'host': 'lunr'}
        volume['host'] = 'lunr'
        try:
            # Try to get the volume type name, else use the default volume type
            volume_type_name = volume['volume_type']['name']
        except (KeyError, TypeError):
            volume_type_name = CONF.lunr_default_volume_type
            # Using the default volume type name,
            # ask the db for the volume type id
            vtype_id = self._get_volume_type_id(volume_type_name)
            model_update['volume_type_id'] = vtype_id
            volume['volume_type_id'] = vtype_id

        db.volume_update(context, volume['id'], model_update)

        params = {
            'name': volume['id'],
            'size': volume['size'],
            'volume_type_name': volume_type_name,
        }

        # Copy image to volume!
        if image_id:
            params['image_id'] = image_id
            image_service, image_id = glance.get_remote_image_service(context,
                                                                      image_id)
            image_meta = image_service.show(context, image_id)
            if image_meta:
                db.volume_glance_metadata_create(context, volume['id'],
                                                 'image_id', image_id)
                name = image_meta.get('name', None)
                if name:
                    db.volume_glance_metadata_create(context, volume['id'],
                                                     'image_name', name)
                image_properties = image_meta.get('properties', {})
                for key, value in image_properties.items():
                    db.volume_glance_metadata_create(context, volume['id'],
                                                     key, value)

        # If this is a snapshot request, add the backup param
        if snapshot_id:
            params['backup'] = snapshot_id
            snapshot_ref = db.snapshot_get(context, snapshot_id)
            original_vref = db.volume_get(context, snapshot_ref['volume_id'])
            if original_vref['bootable']:
                db.volume_glance_metadata_copy_to_volume(
                    context, volume_id, snapshot_id)
                db.volume_update(context, volume_id, {'bootable': True})

        # If this is a clone request, add the source_volume_id param
        if source_volid:
            params['source_volume'] = source_volid
            source_vref = db.volume_get(context, source_volid)
            if source_vref['bootable']:
                db.volume_glance_metadata_copy_from_volume_to_volume(
                    context, source_volid, volume_id)
                db.volume_update(context, volume_id, {'bootable': True})

        try:
            resp = LunrClient(volume, logger=LOG).volumes.create(
                volume['id'], **params)
        except LunrError, e:
            LOG.debug('error creating volume %s', volume['id'])
            # Don't leave an error'd volume around, the raise here
            # will notify the caller of the error (See Github Issue #343)
            # Also, in Havana, TaskFlow will revert the quota increase.
            db.volume_destroy(context, volume['id'])
            raise e
예제 #7
0
    def create_volume(self,
                      context,
                      volume_id,
                      snapshot_id=None,
                      image_id=None,
                      source_volid=None,
                      **kwargs):
        context = context.elevated()
        volume = db.volume_get(context, volume_id)
        LOG.info(_("volume %s: creating"), volume['name'])
        model_update = {'host': 'lunr'}
        volume['host'] = 'lunr'
        try:
            # Try to get the volume type name, else use the default volume type
            volume_type_name = volume['volume_type']['name']
        except (KeyError, TypeError):
            volume_type_name = CONF.lunr_default_volume_type
            # Using the default volume type name,
            # ask the db for the volume type id
            vtype_id = self._get_volume_type_id(volume_type_name)
            model_update['volume_type_id'] = vtype_id
            volume['volume_type_id'] = vtype_id

        db.volume_update(context, volume['id'], model_update)

        params = {
            'name': volume['id'],
            'size': volume['size'],
            'volume_type_name': volume_type_name,
        }

        # Copy image to volume!
        if image_id:
            params['image_id'] = image_id
            image_service, image_id = glance.get_remote_image_service(
                context, image_id)
            image_meta = image_service.show(context, image_id)
            if image_meta:
                db.volume_glance_metadata_create(context, volume['id'],
                                                 'image_id', image_id)
                name = image_meta.get('name', None)
                if name:
                    db.volume_glance_metadata_create(context, volume['id'],
                                                     'image_name', name)
                image_properties = image_meta.get('properties', {})
                for key, value in image_properties.items():
                    db.volume_glance_metadata_create(context, volume['id'],
                                                     key, value)

        # If this is a snapshot request, add the backup param
        if snapshot_id:
            params['backup'] = snapshot_id
            snapshot_ref = db.snapshot_get(context, snapshot_id)
            original_vref = db.volume_get(context, snapshot_ref['volume_id'])
            if original_vref['bootable']:
                db.volume_glance_metadata_copy_to_volume(
                    context, volume_id, snapshot_id)
                db.volume_update(context, volume_id, {'bootable': True})

        # If this is a clone request, add the source_volume_id param
        if source_volid:
            params['source_volume'] = source_volid
            source_vref = db.volume_get(context, source_volid)
            if source_vref['bootable']:
                db.volume_glance_metadata_copy_from_volume_to_volume(
                    context, source_volid, volume_id)
                db.volume_update(context, volume_id, {'bootable': True})

        try:
            resp = LunrClient(volume, logger=LOG).volumes.create(
                volume['id'], **params)
        except LunrError, e:
            LOG.debug('error creating volume %s', volume['id'])
            # Don't leave an error'd volume around, the raise here
            # will notify the caller of the error (See Github Issue #343)
            # Also, in Havana, TaskFlow will revert the quota increase.
            db.volume_destroy(context, volume['id'])
            raise e