Move resource provider staticmethods to proxies

Because of bugs in stub_out(), if we use staticmethods combined
with the db_api context managers, we'll break those methods when
the stubs are removed. There is a fix in the queue, but it's going
to be weeks before we can use it. This adds staticmethod proxy
jumps to work around the problem like we're doing for the flavor
code right now. That keeps the tests and other code the way we want
it to look, and we can move the meat back into the staticmethods once
the bug is fixed.

The following patch will be stub_out-ing relevant bits, so fix this
first to keep that diff smaller.

Change-Id: I2015ac25500f7f6960f4916a4249bb711b157b9b
This commit is contained in:
Dan Smith 2016-03-30 09:29:46 -07:00 committed by Jay Pipes
parent 11cb56a224
commit d1a8ffc408

View File

@ -20,6 +20,23 @@ from nova.objects import base
from nova.objects import fields
@db_api.main_context_manager.writer
def _create_rp_in_db(context, updates):
db_rp = models.ResourceProvider()
db_rp.update(updates)
context.session.add(db_rp)
return db_rp
@db_api.main_context_manager.reader
def _get_rp_by_uuid_from_db(context, uuid):
result = context.session.query(models.ResourceProvider).filter_by(
uuid=uuid).first()
if not result:
raise exception.NotFound()
return result
@base.NovaObjectRegistry.register
class ResourceProvider(base.NovaObject):
# Version 1.0: Initial version
@ -48,12 +65,8 @@ class ResourceProvider(base.NovaObject):
return cls._from_db_object(context, cls(), db_resource_provider)
@staticmethod
@db_api.main_context_manager.writer
def _create_in_db(context, updates):
db_rp = models.ResourceProvider()
db_rp.update(updates)
context.session.add(db_rp)
return db_rp
return _create_rp_in_db(context, updates)
@staticmethod
def _from_db_object(context, resource_provider, db_resource_provider):
@ -64,13 +77,8 @@ class ResourceProvider(base.NovaObject):
return resource_provider
@staticmethod
@db_api.main_context_manager.reader
def _get_by_uuid_from_db(context, uuid):
result = context.session.query(models.ResourceProvider).filter_by(
uuid=uuid).first()
if not result:
raise exception.NotFound()
return result
return _get_rp_by_uuid_from_db(context, uuid)
class _HasAResourceProvider(base.NovaObject):
@ -121,6 +129,22 @@ class _HasAResourceProvider(base.NovaObject):
return target
@db_api.main_context_manager.writer
def _create_inventory_in_db(context, updates):
db_inventory = models.Inventory()
db_inventory.update(updates)
context.session.add(db_inventory)
return db_inventory
@db_api.main_context_manager.writer
def _update_inventory_in_db(context, id_, updates):
result = context.session.query(
models.Inventory).filter_by(id=id_).update(updates)
if not result:
raise exception.NotFound()
@base.NovaObjectRegistry.register
class Inventory(_HasAResourceProvider):
# Version 1.0: Initial version
@ -157,20 +181,12 @@ class Inventory(_HasAResourceProvider):
self._update_in_db(self._context, self.id, updates)
@staticmethod
@db_api.main_context_manager.writer
def _create_in_db(context, updates):
db_inventory = models.Inventory()
db_inventory.update(updates)
context.session.add(db_inventory)
return db_inventory
return _create_inventory_in_db(context, updates)
@staticmethod
@db_api.main_context_manager.writer
def _update_in_db(context, id_, updates):
result = context.session.query(
models.Inventory).filter_by(id=id_).update(updates)
if not result:
raise exception.NotFound()
return _update_inventory_in_db(context, id_, updates)
@base.NovaObjectRegistry.register