From 7cda04dd706bee4e7b511d9db9a224b64470d767 Mon Sep 17 00:00:00 2001 From: Shane Wang Date: Tue, 7 Jan 2014 15:07:50 +0800 Subject: [PATCH] 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 --- nova/objects/aggregate.py | 4 ++++ nova/tests/objects/test_aggregate.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/nova/objects/aggregate.py b/nova/objects/aggregate.py index 50b8dbd64..6e75a20dd 100644 --- a/nova/objects/aggregate.py +++ b/nova/objects/aggregate.py @@ -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 diff --git a/nova/tests/objects/test_aggregate.py b/nova/tests/objects/test_aggregate.py index 7b2c0157a..c27fadb84 100644 --- a/nova/tests/objects/test_aggregate.py +++ b/nova/tests/objects/test_aggregate.py @@ -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(