Exemplo n.º 1
0
    def _from_db_object(context,
                        cgsnapshot,
                        db_cgsnapshots,
                        expected_attrs=None):
        expected_attrs = expected_attrs or []
        for name, field in cgsnapshot.fields.items():
            if name in OPTIONAL_FIELDS:
                continue
            value = db_cgsnapshots.get(name)
            setattr(cgsnapshot, name, value)

        if 'consistencygroup' in expected_attrs:
            consistencygroup = objects.ConsistencyGroup(context)
            consistencygroup._from_db_object(
                context, consistencygroup, db_cgsnapshots['consistencygroup'])
            cgsnapshot.consistencygroup = consistencygroup

        if 'snapshots' in expected_attrs:
            snapshots = base.obj_make_list(context,
                                           objects.SnapshotsList(context),
                                           objects.Snapshots,
                                           db_cgsnapshots['snapshots'])
            cgsnapshot.snapshots = snapshots

        cgsnapshot._context = context
        cgsnapshot.obj_reset_changes()
        return cgsnapshot
Exemplo n.º 2
0
def create_consistencygroup(ctxt,
                            host='test_host@fakedrv#fakepool',
                            name='test_cg',
                            description='this is a test cg',
                            status=fields.ConsistencyGroupStatus.AVAILABLE,
                            availability_zone='fake_az',
                            volume_type_id=None,
                            cgsnapshot_id=None,
                            source_cgid=None,
                            **kwargs):
    """Create a consistencygroup object in the DB."""

    cg = objects.ConsistencyGroup(ctxt)
    cg.host = host
    cg.user_id = ctxt.user_id or fake.USER_ID
    cg.project_id = ctxt.project_id or fake.PROJECT_ID
    cg.status = status
    cg.name = name
    cg.description = description
    cg.availability_zone = availability_zone

    if volume_type_id:
        cg.volume_type_id = volume_type_id
    cg.cgsnapshot_id = cgsnapshot_id
    cg.source_cgid = source_cgid
    new_id = kwargs.pop('id', None)
    cg.update(kwargs)
    cg.create()
    if new_id and new_id != cg.id:
        db.consistencygroup_update(ctxt, cg.id, {'id': new_id})
        cg = objects.ConsistencyGroup.get_by_id(ctxt, new_id)
    return cg
Exemplo n.º 3
0
 def test_destroy(self, consistencygroup_destroy):
     consistencygroup = objects.ConsistencyGroup(context=self.context,
                                                 id='1')
     consistencygroup.destroy()
     self.assertTrue(consistencygroup_destroy.called)
     admin_context = consistencygroup_destroy.call_args[0][0]
     self.assertTrue(admin_context.is_admin)
Exemplo n.º 4
0
def create_consistencygroup(ctxt,
                            host='test_host@fakedrv#fakepool',
                            name='test_cg',
                            description='this is a test cg',
                            status=fields.ConsistencyGroupStatus.AVAILABLE,
                            availability_zone='fake_az',
                            volume_type_id=None,
                            cgsnapshot_id=None,
                            source_cgid=None,
                            **kwargs):
    """Create a consistencygroup object in the DB."""

    cg = objects.ConsistencyGroup(ctxt)
    cg.host = host
    cg.user_id = ctxt.user_id or 'fake_user_id'
    cg.project_id = ctxt.project_id or 'fake_project_id'
    cg.status = status
    cg.name = name
    cg.description = description
    cg.availability_zone = availability_zone

    if volume_type_id:
        cg.volume_type_id = volume_type_id
    cg.cgsnapshot_id = cgsnapshot_id
    cg.source_cgid = source_cgid
    for key in kwargs:
        setattr(cg, key, kwargs[key])
    cg.create()
    return cg
Exemplo n.º 5
0
    def create(self,
               context,
               name,
               description,
               cg_volume_types,
               availability_zone=None):
        check_policy(context, 'create')

        volume_type_list = None
        volume_type_list = cg_volume_types.split(',')

        req_volume_types = []
        req_volume_types = (self.db.volume_types_get_by_name_or_id(
            context, volume_type_list))

        req_volume_type_ids = ""
        for voltype in req_volume_types:
            req_volume_type_ids = (req_volume_type_ids + voltype.get('id') +
                                   ",")
        if len(req_volume_type_ids) == 0:
            req_volume_type_ids = None

        availability_zone = self._extract_availability_zone(availability_zone)
        kwargs = {
            'user_id': context.user_id,
            'project_id': context.project_id,
            'availability_zone': availability_zone,
            'status': "creating",
            'name': name,
            'description': description,
            'volume_type_id': req_volume_type_ids
        }
        group = None
        try:
            group = objects.ConsistencyGroup(context=context, **kwargs)
            group.create()
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE("Error occurred when creating consistency group"
                        " %s."), name)

        request_spec_list = []
        filter_properties_list = []
        for req_volume_type in req_volume_types:
            request_spec = {
                'volume_type': req_volume_type.copy(),
                'consistencygroup_id': group.id
            }
            filter_properties = {}
            request_spec_list.append(request_spec)
            filter_properties_list.append(filter_properties)

        # Update quota for consistencygroups
        self.update_quota(context, group, 1)

        self._cast_create_consistencygroup(context, group, request_spec_list,
                                           filter_properties_list)

        return group
Exemplo n.º 6
0
    def test_obj_load_attr(self, mock_vol_get_all_by_group,
                           mock_cgsnap_get_all_by_group):
        consistencygroup = objects.ConsistencyGroup._from_db_object(
            self.context, objects.ConsistencyGroup(), fake_consistencygroup)
        # Test cgsnapshots lazy-loaded field
        cgsnapshots_objs = [
            objects.CGSnapshot(context=self.context, id=i) for i in
            [fake.CGSNAPSHOT_ID, fake.CGSNAPSHOT2_ID, fake.CGSNAPSHOT3_ID]
        ]
        cgsnapshots = objects.CGSnapshotList(context=self.context,
                                             objects=cgsnapshots_objs)
        mock_cgsnap_get_all_by_group.return_value = cgsnapshots
        self.assertEqual(cgsnapshots, consistencygroup.cgsnapshots)
        mock_cgsnap_get_all_by_group.assert_called_once_with(
            self.context, consistencygroup.id)

        # Test volumes lazy-loaded field
        volume_objs = [
            objects.Volume(context=self.context, id=i)
            for i in [fake.VOLUME_ID, fake.VOLUME2_ID, fake.VOLUME3_ID]
        ]
        volumes = objects.VolumeList(context=self.context, objects=volume_objs)
        mock_vol_get_all_by_group.return_value = volumes
        self.assertEqual(volumes, consistencygroup.volumes)
        mock_vol_get_all_by_group.assert_called_once_with(
            self.context, consistencygroup.id)
Exemplo n.º 7
0
 def test_create(self, consistencygroup_create):
     fake_cg = fake_consistencygroup.copy()
     del fake_cg['id']
     consistencygroup = objects.ConsistencyGroup(context=self.context,
                                                 **fake_cg)
     consistencygroup.create()
     self._compare(self, fake_consistencygroup, consistencygroup)
Exemplo n.º 8
0
 def test_save_with_consistencygroup(self):
     db_volume = fake_volume.fake_db_volume()
     volume = objects.Volume._from_db_object(self.context, objects.Volume(),
                                             db_volume)
     volume.display_name = 'foobar'
     volume.consistencygroup = objects.ConsistencyGroup()
     self.assertRaises(exception.ObjectActionError, volume.save)
Exemplo n.º 9
0
    def test_create_volume_with_consistencygroup_invalid_type(self):
        """Test volume creation with ConsistencyGroup & invalid volume type."""
        vol_type = db.volume_type_create(
            context.get_admin_context(),
            dict(name=conf_fixture.def_vol_type, extra_specs={}))
        vol_type = objects.VolumeType.get_by_id(self.context, vol_type.id)
        cg = objects.ConsistencyGroup(self.context,
                                      id=fake.CONSISTENCY_GROUP_ID,
                                      name='cg1',
                                      volume_type_id=vol_type.id)
        fake_type = fake_volume.fake_volume_type_obj(self.context,
                                                     id=fake.VOLUME_TYPE_ID,
                                                     name='fake')
        vol_api = cinder.volume.api.API()

        # Volume type must be provided when creating a volume in a
        # consistency group.
        self.assertRaises(exception.InvalidInput,
                          vol_api.create,
                          self.context,
                          1,
                          'vol1',
                          'volume 1',
                          consistencygroup=cg)

        # Volume type must be valid.
        self.assertRaises(exception.InvalidInput,
                          vol_api.create,
                          self.context,
                          1,
                          'vol1',
                          'volume 1',
                          volume_type=fake_type,
                          consistencygroup=cg)
Exemplo n.º 10
0
 def test_save(self, consistencygroup_update):
     consistencygroup = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), fake_consistencygroup)
     consistencygroup.status = 'active'
     consistencygroup.save()
     consistencygroup_update.assert_called_once_with(
         self.context, consistencygroup.id, {'status': 'active'})
Exemplo n.º 11
0
    def _from_db_object(context, volume, db_volume, expected_attrs=None):
        if expected_attrs is None:
            expected_attrs = []
        for name, field in volume.fields.items():
            if name in Volume.OPTIONAL_FIELDS:
                continue
            value = db_volume.get(name)
            if isinstance(field, fields.IntegerField):
                value = value or 0
            volume[name] = value

        # Get data from db_volume object that was queried by joined query
        # from DB
        if 'metadata' in expected_attrs:
            metadata = db_volume.get('volume_metadata', [])
            volume.metadata = {item['key']: item['value'] for item in metadata}
        if 'admin_metadata' in expected_attrs:
            metadata = db_volume.get('volume_admin_metadata', [])
            volume.admin_metadata = {
                item['key']: item['value']
                for item in metadata
            }
        if 'glance_metadata' in expected_attrs:
            metadata = db_volume.get('volume_glance_metadata', [])
            volume.glance_metadata = {
                item['key']: item['value']
                for item in metadata
            }
        if 'volume_type' in expected_attrs:
            db_volume_type = db_volume.get('volume_type')
            if db_volume_type:
                vt_expected_attrs = []
                if 'volume_type.extra_specs' in expected_attrs:
                    vt_expected_attrs.append('extra_specs')
                volume.volume_type = objects.VolumeType._from_db_object(
                    context,
                    objects.VolumeType(),
                    db_volume_type,
                    expected_attrs=vt_expected_attrs)
        if 'volume_attachment' in expected_attrs:
            attachments = base.obj_make_list(
                context, objects.VolumeAttachmentList(context),
                objects.VolumeAttachment, db_volume.get('volume_attachment'))
            volume.volume_attachment = attachments
        if 'consistencygroup' in expected_attrs:
            consistencygroup = objects.ConsistencyGroup(context)
            consistencygroup._from_db_object(context, consistencygroup,
                                             db_volume['consistencygroup'])
            volume.consistencygroup = consistencygroup
        if 'snapshots' in expected_attrs:
            snapshots = base.obj_make_list(context,
                                           objects.SnapshotList(context),
                                           objects.Snapshot,
                                           db_volume['snapshots'])
            volume.snapshots = snapshots

        volume._context = context
        volume.obj_reset_changes()
        return volume
Exemplo n.º 12
0
 def test_save(self, consistencygroup_update):
     consistencygroup = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), fake_consistencygroup)
     consistencygroup.status = fields.ConsistencyGroupStatus.AVAILABLE
     consistencygroup.save()
     consistencygroup_update.assert_called_once_with(
         self.context, consistencygroup.id,
         {'status': fields.ConsistencyGroupStatus.AVAILABLE})
Exemplo n.º 13
0
 def test_save_with_cgsnapshots(self):
     consistencygroup = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), fake_consistencygroup)
     cgsnapshots_objs = [objects.CGSnapshot(context=self.context, id=i)
                         for i in [3, 4, 5]]
     cgsnapshots = objects.CGSnapshotList(objects=cgsnapshots_objs)
     consistencygroup.name = 'foobar'
     consistencygroup.cgsnapshots = cgsnapshots
     self.assertEqual({'name': 'foobar',
                       'cgsnapshots': cgsnapshots},
                      consistencygroup.obj_get_changes())
     self.assertRaises(exception.ObjectActionError, consistencygroup.save)
 def test_save_with_consistencygroup(self, cgsnapshot_update,
                                     cgsnapshot_cg_update):
     consistencygroup = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), fake_consistencygroup)
     cgsnapshot = objects.CGSnapshot._from_db_object(
         self.context, objects.CGSnapshot(), fake_cgsnapshot)
     cgsnapshot.name = 'foobar'
     cgsnapshot.consistencygroup = consistencygroup
     self.assertEqual({'name': 'foobar',
                       'consistencygroup': consistencygroup},
                      cgsnapshot.obj_get_changes())
     self.assertRaises(exception.ObjectActionError, cgsnapshot.save)
Exemplo n.º 15
0
 def test_save_with_volumes(self):
     consistencygroup = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), fake_consistencygroup)
     volumes_objs = [objects.Volume(context=self.context, id=i)
                     for i in [3, 4, 5]]
     volumes = objects.VolumeList(objects=volumes_objs)
     consistencygroup.name = 'foobar'
     consistencygroup.volumes = volumes
     self.assertEqual({'name': 'foobar',
                       'volumes': volumes},
                      consistencygroup.obj_get_changes())
     self.assertRaises(exception.ObjectActionError, consistencygroup.save)
Exemplo n.º 16
0
    def create_from_src(self, context, name, description=None,
                        cgsnapshot_id=None, source_cgid=None):
        check_policy(context, 'create')

        kwargs = {
            'user_id': context.user_id,
            'project_id': context.project_id,
            'status': c_fields.ConsistencyGroupStatus.CREATING,
            'name': name,
            'description': description,
            'cgsnapshot_id': cgsnapshot_id,
            'source_cgid': source_cgid,
        }

        group = None
        try:
            group = objects.ConsistencyGroup(context=context, **kwargs)
            group.create(cg_snap_id=cgsnapshot_id, cg_id=source_cgid)
        except exception.ConsistencyGroupNotFound:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Source CG %(source_cg)s not found when "
                              "creating consistency group %(cg)s from "
                              "source."),
                          {'cg': name, 'source_cg': source_cgid})
        except exception.CgSnapshotNotFound:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("CG snapshot %(cgsnap)s not found when creating "
                              "consistency group %(cg)s from source."),
                          {'cg': name, 'cgsnap': cgsnapshot_id})
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Error occurred when creating consistency group"
                              " %(cg)s from cgsnapshot %(cgsnap)s."),
                          {'cg': name, 'cgsnap': cgsnapshot_id})

        # Update quota for consistencygroups
        self.update_quota(context, group, 1)

        if not group.host:
            msg = _("No host to create consistency group %s.") % group.id
            LOG.error(msg)
            raise exception.InvalidConsistencyGroup(reason=msg)

        if cgsnapshot_id:
            self._create_cg_from_cgsnapshot(context, group, cgsnapshot_id)
        elif source_cgid:
            self._create_cg_from_source_cg(context, group, source_cgid)

        return group
Exemplo n.º 17
0
 def test_create_from_group(self, group_create):
     fake_grp = fake_group.copy()
     del fake_grp['id']
     group = objects.Group(context=self.context, **fake_grp)
     group.create()
     volumes_objs = [
         objects.Volume(context=self.context, id=i)
         for i in [fake.VOLUME_ID, fake.VOLUME2_ID, fake.VOLUME3_ID]
     ]
     volumes = objects.VolumeList(objects=volumes_objs)
     group.volumes = volumes
     consistencygroup = objects.ConsistencyGroup()
     consistencygroup.from_group(group)
     self.assertEqual(group.id, consistencygroup.id)
     self.assertEqual(group.name, consistencygroup.name)
Exemplo n.º 18
0
 def test_from_db_object_with_all_expected_attributes(self):
     expected_attrs = ['volumes', 'cgsnapshots']
     db_volumes = [
         fake_volume.fake_db_volume(admin_metadata={}, volume_metadata={})
     ]
     db_cgsnaps = [fake_cgsnapshot.copy()]
     db_cg = fake_consistencygroup.copy()
     db_cg['volumes'] = db_volumes
     db_cg['cgsnapshots'] = db_cgsnaps
     cg = objects.ConsistencyGroup._from_db_object(
         self.context, objects.ConsistencyGroup(), db_cg, expected_attrs)
     self.assertEqual(len(db_volumes), len(cg.volumes))
     self._compare(self, db_volumes[0], cg.volumes[0])
     self.assertEqual(len(db_cgsnaps), len(cg.cgsnapshots))
     self._compare(self, db_cgsnaps[0], cg.cgsnapshots[0])
Exemplo n.º 19
0
 def test_destroy(self, consistencygroup_destroy, utcnow_mock):
     consistencygroup_destroy.return_value = {
         'status': fields.ConsistencyGroupStatus.DELETED,
         'deleted': True,
         'deleted_at': utcnow_mock.return_value
     }
     consistencygroup = objects.ConsistencyGroup(
         context=self.context, id=fake.CONSISTENCY_GROUP_ID)
     consistencygroup.destroy()
     self.assertTrue(consistencygroup_destroy.called)
     admin_context = consistencygroup_destroy.call_args[0][0]
     self.assertTrue(admin_context.is_admin)
     self.assertTrue(consistencygroup.deleted)
     self.assertEqual(fields.ConsistencyGroupStatus.DELETED,
                      consistencygroup.status)
     self.assertEqual(utcnow_mock.return_value.replace(tzinfo=pytz.UTC),
                      consistencygroup.deleted_at)
 def test_obj_load_attr(self, snapshotlist_get_for_cgs,
                        consistencygroup_get_by_id):
     cgsnapshot = objects.CGSnapshot._from_db_object(
         self.context, objects.CGSnapshot(), fake_cgsnapshot)
     # Test consistencygroup lazy-loaded field
     consistencygroup = objects.ConsistencyGroup(context=self.context, id=2)
     consistencygroup_get_by_id.return_value = consistencygroup
     self.assertEqual(consistencygroup, cgsnapshot.consistencygroup)
     consistencygroup_get_by_id.assert_called_once_with(
         self.context, cgsnapshot.consistencygroup_id)
     # Test snapshots lazy-loaded field
     snapshots_objs = [objects.Snapshot(context=self.context, id=i)
                       for i in [3, 4, 5]]
     snapshots = objects.SnapshotList(context=self.context,
                                      objects=snapshots_objs)
     snapshotlist_get_for_cgs.return_value = snapshots
     self.assertEqual(snapshots, cgsnapshot.snapshots)
     snapshotlist_get_for_cgs.assert_called_once_with(
         self.context, cgsnapshot.id)
Exemplo n.º 21
0
 def _create_consistencygroup(
         self,
         ctxt=None,
         name='test_consistencygroup',
         description='this is a test consistency group',
         volume_type_id='123456',
         availability_zone='az1',
         host='fakehost',
         status=fields.ConsistencyGroupStatus.CREATING):
     """Create a consistency group object."""
     ctxt = ctxt or self.ctxt
     consistencygroup = objects.ConsistencyGroup(ctxt)
     consistencygroup.user_id = 'fake'
     consistencygroup.project_id = 'fake'
     consistencygroup.availability_zone = availability_zone
     consistencygroup.name = name
     consistencygroup.description = description
     consistencygroup.volume_type_id = volume_type_id
     consistencygroup.host = host
     consistencygroup.status = status
     consistencygroup.create()
     return consistencygroup
Exemplo n.º 22
0
 def _create_consistencygroup(
         self,
         ctxt=None,
         name='test_consistencygroup',
         description='this is a test consistency group',
         volume_type_id=fake.VOLUME_TYPE_ID,
         availability_zone='az1',
         host='fakehost',
         status=fields.ConsistencyGroupStatus.CREATING,
         **kwargs):
     """Create a consistency group object."""
     ctxt = ctxt or self.ctxt
     consistencygroup = objects.ConsistencyGroup(ctxt)
     consistencygroup.user_id = fake.USER_ID
     consistencygroup.project_id = fake.PROJECT_ID
     consistencygroup.availability_zone = availability_zone
     consistencygroup.name = name
     consistencygroup.description = description
     consistencygroup.volume_type_id = volume_type_id
     consistencygroup.host = host
     consistencygroup.status = status
     consistencygroup.update(kwargs)
     consistencygroup.create()
     return consistencygroup
Exemplo n.º 23
0
    def test_obj_load_attr(self, mock_sl_get_all_for_volume, mock_cg_get_by_id,
                           mock_va_get_all_by_vol, mock_vt_get_by_id,
                           mock_admin_metadata_get, mock_glance_metadata_get,
                           mock_metadata_get):
        volume = objects.Volume._from_db_object(self.context, objects.Volume(),
                                                fake_volume.fake_db_volume())

        # Test metadata lazy-loaded field
        metadata = {'foo': 'bar'}
        mock_metadata_get.return_value = metadata
        self.assertEqual(metadata, volume.metadata)
        mock_metadata_get.assert_called_once_with(self.context, volume.id)

        # Test glance_metadata lazy-loaded field
        glance_metadata = [{'key': 'foo', 'value': 'bar'}]
        mock_glance_metadata_get.return_value = glance_metadata
        self.assertEqual({'foo': 'bar'}, volume.glance_metadata)
        mock_glance_metadata_get.assert_called_once_with(
            self.context, volume.id)

        # Test volume_type lazy-loaded field
        # Case1. volume.volume_type_id = None
        self.assertIsNone(volume.volume_type)

        # Case2. volume2.volume_type_id = 1
        fake2 = fake_volume.fake_db_volume()
        fake2.update({'volume_type_id': fake.volume_id})
        volume2 = objects.Volume._from_db_object(self.context,
                                                 objects.Volume(), fake2)
        volume_type = objects.VolumeType(context=self.context,
                                         id=fake.volume_type_id)
        mock_vt_get_by_id.return_value = volume_type
        self.assertEqual(volume_type, volume2.volume_type)
        mock_vt_get_by_id.assert_called_once_with(self.context,
                                                  volume2.volume_type_id)

        # Test consistencygroup lazy-loaded field
        consistencygroup = objects.ConsistencyGroup(
            context=self.context, id=fake.consistency_group_id)
        mock_cg_get_by_id.return_value = consistencygroup
        self.assertEqual(consistencygroup, volume.consistencygroup)
        mock_cg_get_by_id.assert_called_once_with(self.context,
                                                  volume.consistencygroup_id)

        # Test snapshots lazy-loaded field
        snapshots = objects.SnapshotList(context=self.context,
                                         id=fake.snapshot_id)
        mock_sl_get_all_for_volume.return_value = snapshots
        self.assertEqual(snapshots, volume.snapshots)
        mock_sl_get_all_for_volume.assert_called_once_with(
            self.context, volume.id)

        # Test volume_attachment lazy-loaded field
        va_objs = [
            objects.VolumeAttachment(context=self.context, id=i)
            for i in [fake.object_id, fake.object2_id, fake.object3_id]
        ]
        va_list = objects.VolumeAttachmentList(context=self.context,
                                               objects=va_objs)
        mock_va_get_all_by_vol.return_value = va_list
        self.assertEqual(va_list, volume.volume_attachment)
        mock_va_get_all_by_vol.assert_called_once_with(self.context, volume.id)

        # Test admin_metadata lazy-loaded field - user context
        adm_metadata = {'bar': 'foo'}
        mock_admin_metadata_get.return_value = adm_metadata
        self.assertEqual({}, volume.admin_metadata)
        self.assertFalse(mock_admin_metadata_get.called)

        # Test admin_metadata lazy-loaded field - admin context
        adm_context = self.context.elevated()
        volume = objects.Volume._from_db_object(adm_context, objects.Volume(),
                                                fake_volume.fake_db_volume())
        adm_metadata = {'bar': 'foo'}
        mock_admin_metadata_get.return_value = adm_metadata
        self.assertEqual(adm_metadata, volume.admin_metadata)
        mock_admin_metadata_get.assert_called_once_with(adm_context, volume.id)
Exemplo n.º 24
0
 def test_create_with_id_except_exception(self, ):
     consistencygroup = objects.ConsistencyGroup(
         context=self.context, **{'id': fake.CONSISTENCY_GROUP_ID})
     self.assertRaises(exception.ObjectActionError, consistencygroup.create)
Exemplo n.º 25
0
    def _from_db_object(cls, context, volume, db_volume, expected_attrs=None):
        if expected_attrs is None:
            expected_attrs = []
        for name, field in volume.fields.items():
            if name in cls.OPTIONAL_FIELDS:
                continue
            value = db_volume.get(name)
            if isinstance(field, fields.IntegerField):
                value = value or 0
            volume[name] = value

        # Get data from db_volume object that was queried by joined query
        # from DB
        if 'metadata' in expected_attrs:
            metadata = db_volume.get('volume_metadata', [])
            volume.metadata = {item['key']: item['value'] for item in metadata}
        if 'admin_metadata' in expected_attrs:
            metadata = db_volume.get('volume_admin_metadata', [])
            volume.admin_metadata = {item['key']: item['value']
                                     for item in metadata}
        if 'glance_metadata' in expected_attrs:
            metadata = db_volume.get('volume_glance_metadata', [])
            volume.glance_metadata = {item['key']: item['value']
                                      for item in metadata}
        if 'volume_type' in expected_attrs:
            db_volume_type = db_volume.get('volume_type')
            if db_volume_type:
                vt_expected_attrs = []
                if 'volume_type.extra_specs' in expected_attrs:
                    vt_expected_attrs.append('extra_specs')
                volume.volume_type = objects.VolumeType._from_db_object(
                    context, objects.VolumeType(), db_volume_type,
                    expected_attrs=vt_expected_attrs)
        if 'volume_attachment' in expected_attrs:
            attachments = base.obj_make_list(
                context, objects.VolumeAttachmentList(context),
                objects.VolumeAttachment,
                db_volume.get('volume_attachment'))
            volume.volume_attachment = attachments
        if volume.consistencygroup_id and 'consistencygroup' in expected_attrs:
            consistencygroup = objects.ConsistencyGroup(context)
            consistencygroup._from_db_object(context,
                                             consistencygroup,
                                             db_volume['consistencygroup'])
            volume.consistencygroup = consistencygroup
        if 'snapshots' in expected_attrs:
            snapshots = base.obj_make_list(
                context, objects.SnapshotList(context),
                objects.Snapshot,
                db_volume['snapshots'])
            volume.snapshots = snapshots
        if 'cluster' in expected_attrs:
            db_cluster = db_volume.get('cluster')
            # If this volume doesn't belong to a cluster the cluster field in
            # the ORM instance will have value of None.
            if db_cluster:
                volume.cluster = objects.Cluster(context)
                objects.Cluster._from_db_object(context, volume.cluster,
                                                db_cluster)
            else:
                volume.cluster = None
        if volume.group_id and 'group' in expected_attrs:
            group = objects.Group(context)
            group._from_db_object(context,
                                  group,
                                  db_volume['group'])
            volume.group = group

        volume._context = context
        volume.obj_reset_changes()
        return volume
Exemplo n.º 26
0
def fake_consistencyobject_obj(context, **updates):
    return objects.ConsistencyGroup._from_db_object(context,
                                                    objects.ConsistencyGroup(),
                                                    fake_db_consistencygroup(
                                                        **updates))
Exemplo n.º 27
0
    def create_from_src(self,
                        context,
                        name,
                        description=None,
                        cgsnapshot_id=None,
                        source_cgid=None):
        check_policy(context, 'create')
        cgsnapshot = None
        orig_cg = None
        if cgsnapshot_id:
            try:
                cgsnapshot = objects.CGSnapshot.get_by_id(
                    context, cgsnapshot_id)
            except exception.CgSnapshotNotFound:
                with excutils.save_and_reraise_exception():
                    LOG.error(
                        _LE("CG snapshot %(cgsnap)s not found when "
                            "creating consistency group %(cg)s from "
                            "source."), {
                                'cg': name,
                                'cgsnap': cgsnapshot_id
                            })
            else:
                orig_cg = cgsnapshot.consistencygroup

        source_cg = None
        if source_cgid:
            try:
                source_cg = objects.ConsistencyGroup.get_by_id(
                    context, source_cgid)
            except exception.ConsistencyGroupNotFound:
                with excutils.save_and_reraise_exception():
                    LOG.error(
                        _LE("Source CG %(source_cg)s not found when "
                            "creating consistency group %(cg)s from "
                            "source."), {
                                'cg': name,
                                'source_cg': source_cgid
                            })

        kwargs = {
            'user_id': context.user_id,
            'project_id': context.project_id,
            'status': c_fields.ConsistencyGroupStatus.CREATING,
            'name': name,
            'description': description,
            'cgsnapshot_id': cgsnapshot_id,
            'source_cgid': source_cgid,
        }

        if orig_cg:
            kwargs['volume_type_id'] = orig_cg.volume_type_id
            kwargs['availability_zone'] = orig_cg.availability_zone
            kwargs['host'] = orig_cg.host

        if source_cg:
            kwargs['volume_type_id'] = source_cg.volume_type_id
            kwargs['availability_zone'] = source_cg.availability_zone
            kwargs['host'] = source_cg.host

        group = None
        try:
            group = objects.ConsistencyGroup(context=context, **kwargs)
            group.create()
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE("Error occurred when creating consistency group"
                        " %(cg)s from cgsnapshot %(cgsnap)s."), {
                            'cg': name,
                            'cgsnap': cgsnapshot_id
                        })

        # Update quota for consistencygroups
        self.update_quota(context, group, 1)

        if not group.host:
            msg = _("No host to create consistency group %s.") % group.id
            LOG.error(msg)
            raise exception.InvalidConsistencyGroup(reason=msg)

        if cgsnapshot:
            self._create_cg_from_cgsnapshot(context, group, cgsnapshot)
        elif source_cg:
            self._create_cg_from_source_cg(context, group, source_cg)

        return group