Prevent caller from specifying id during Service.create()

The patch is to remove id when it was set before creating a service
in objects, and check if the service exists before it is created.

Related to blueprint icehouse-objects.

Change-Id: I688bfc98234f4bc27fb15474e05c7f25753efaf7
This commit is contained in:
Shane Wang 2014-01-07 14:38:02 +08:00
parent c2d9a6fdfd
commit 7b66e8ecef
2 changed files with 15 additions and 0 deletions

View File

@ -107,7 +107,11 @@ class Service(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')
updates = self.obj_get_changes()
updates.pop('id', None)
db_service = db.service_create(context, updates)
self._from_db_object(context, self, db_service)

View File

@ -85,6 +85,17 @@ class _TestServiceObject(object):
service_obj.create(self.context)
self.assertEqual(fake_service['id'], service_obj.id)
def test_recreate_fails(self):
self.mox.StubOutWithMock(db, 'service_create')
db.service_create(self.context, {'host': 'fake-host'}).AndReturn(
fake_service)
self.mox.ReplayAll()
service_obj = service.Service()
service_obj.host = 'fake-host'
service_obj.create(self.context)
self.assertRaises(exception.ObjectActionError, service_obj.create,
self.context)
def test_save(self):
self.mox.StubOutWithMock(db, 'service_update')
db.service_update(self.context, 123, {'host': 'fake-host'}).AndReturn(