CONF = cfg.CONF
# NOTE(thangp): OPTIONAL_FIELDS are fields that would be lazy-loaded. They are
# typically the relationship in the sqlalchemy object.
-OPTIONAL_FIELDS = ['volume', 'metadata']
+OPTIONAL_FIELDS = ['volume', 'metadata', 'cgsnapshot']
LOG = logging.getLogger(__name__)
'provider_auth': fields.StringField(nullable=True),
'volume': fields.ObjectField('Volume', nullable=True),
+ 'cgsnapshot': fields.ObjectField('CGSnapshot', nullable=True),
}
# NOTE(thangp): obj_extra_fields is used to hold properties that are not
volume = objects.Volume(context)
volume._from_db_object(context, volume, db_snapshot['volume'])
snapshot.volume = volume
+ if 'cgsnapshot' in expected_attrs:
+ cgsnapshot = objects.CGSnapshot(context)
+ cgsnapshot._from_db_object(context, cgsnapshot,
+ db_snapshot['cgsnapshot'])
+ snapshot.cgsnapshot = cgsnapshot
if 'metadata' in expected_attrs:
metadata = db_snapshot.get('snapshot_metadata')
if metadata is None:
if 'volume' in updates:
raise exception.ObjectActionError(action='create',
reason=_('volume assigned'))
+ if 'cgsnapshot' in updates:
+ raise exception.ObjectActionError(action='create',
+ reason=_('cgsnapshot assigned'))
db_snapshot = db.snapshot_create(self._context, updates)
self._from_db_object(self._context, self, db_snapshot)
if 'volume' in updates:
raise exception.ObjectActionError(action='save',
reason=_('volume changed'))
+ if 'cgsnapshot' in updates:
+ raise exception.ObjectActionError(
+ action='save', reason=_('cgsnapshot changed'))
if 'metadata' in updates:
# Metadata items that are not specified in the
self.volume = objects.Volume.get_by_id(self._context,
self.volume_id)
+ if attrname == 'cgsnapshot':
+ self.cgsnapshot = objects.CGSnapshot.get_by_id(self._context,
+ self.cgsnapshot_id)
+
self.obj_reset_changes(fields=[attrname])
def delete_metadata_key(self, context, key):
LOG = logging.getLogger(__name__)
-fake_db_snapshot = fake_snapshot.fake_db_snapshot()
+fake_db_snapshot = fake_snapshot.fake_db_snapshot(
+ cgsnapshot_id='fake_cgsnap_id')
del fake_db_snapshot['metadata']
del fake_db_snapshot['volume']
self.assertEqual('volume-2', snapshot.volume_name)
@mock.patch('cinder.objects.volume.Volume.get_by_id')
- def test_obj_load_attr(self, volume_get_by_id):
+ @mock.patch('cinder.objects.cgsnapshot.CGSnapshot.get_by_id')
+ def test_obj_load_attr(self, cgsnapshot_get_by_id, volume_get_by_id):
snapshot = objects.Snapshot._from_db_object(
self.context, objects.Snapshot(), fake_db_snapshot)
+ # Test volume lazy-loaded field
volume = objects.Volume(context=self.context, id=2)
volume_get_by_id.return_value = volume
self.assertEqual(volume, snapshot.volume)
volume_get_by_id.assert_called_once_with(self.context,
snapshot.volume_id)
+ # Test cgsnapshot lazy-loaded field
+ cgsnapshot = objects.CGSnapshot(context=self.context, id=2)
+ cgsnapshot_get_by_id.return_value = cgsnapshot
+ self.assertEqual(cgsnapshot, snapshot.cgsnapshot)
+ cgsnapshot_get_by_id.assert_called_once_with(self.context,
+ snapshot.cgsnapshot_id)
@mock.patch('cinder.db.snapshot_data_get_for_project')
def test_snapshot_data_get_for_project(self, snapshot_data_get):