Prevent caller from specifying id during Aggregate.create()
The patch is to remove id when it was set before creating an aggregate in objects, and check if the aggregate exists before it is created. Related to blueprint icehouse-objects. Change-Id: I0d7c5c5da9a93e52c7fee9eee7df80d2f460593e
This commit is contained in:
committed by
Gerrit Code Review
parent
24a9cd1ba3
commit
7cda04dd70
@@ -58,6 +58,9 @@ class Aggregate(base.NovaPersistentObject, base.NovaObject):
|
||||
|
||||
@base.remotable
|
||||
def create(self, context):
|
||||
if self.obj_attr_is_set('id'):
|
||||
raise exception.ObjectActionError(action='create',
|
||||
reason='already created')
|
||||
self._assert_no_hosts('create')
|
||||
updates = self.obj_get_changes()
|
||||
payload = dict(updates)
|
||||
@@ -68,6 +71,7 @@ class Aggregate(base.NovaPersistentObject, base.NovaObject):
|
||||
"create.start",
|
||||
payload)
|
||||
metadata = updates.pop('metadata', None)
|
||||
updates.pop('id', None)
|
||||
db_aggregate = db.aggregate_create(context, updates, metadata=metadata)
|
||||
self._from_db_object(context, self, db_aggregate)
|
||||
payload['aggregate_id'] = self.id
|
||||
|
||||
@@ -53,6 +53,18 @@ class _TestAggregateObject(object):
|
||||
agg.create(self.context)
|
||||
self.compare_obj(agg, fake_aggregate, subs=SUBS)
|
||||
|
||||
def test_recreate_fails(self):
|
||||
self.mox.StubOutWithMock(db, 'aggregate_create')
|
||||
db.aggregate_create(self.context, {'name': 'foo'},
|
||||
metadata={'one': 'two'}).AndReturn(fake_aggregate)
|
||||
self.mox.ReplayAll()
|
||||
agg = aggregate.Aggregate()
|
||||
agg.name = 'foo'
|
||||
agg.metadata = {'one': 'two'}
|
||||
agg.create(self.context)
|
||||
self.assertRaises(exception.ObjectActionError, agg.create,
|
||||
self.context)
|
||||
|
||||
def test_save(self):
|
||||
self.mox.StubOutWithMock(db, 'aggregate_update')
|
||||
db.aggregate_update(self.context, 123, {'name': 'baz'}).AndReturn(
|
||||
|
||||
Reference in New Issue
Block a user