Merge "Fix and cleanup baymodel dbapi"

This commit is contained in:
Jenkins 2015-01-01 23:52:32 +00:00 committed by Gerrit Code Review
commit fc44bc4aa8
4 changed files with 16 additions and 73 deletions

View File

@ -331,6 +331,14 @@ class FileSystemNotSupported(MagnumException):
"File system %(fs)s is not supported.")
class BayModelNotFound(ResourceNotFound):
message = _("Baymodel %(baymodel)s could not be found.")
class BayModelAlreadyExists(Conflict):
message = _("A baymodel with UUID %(uuid)s already exists.")
class BayNotFound(ResourceNotFound):
message = _("Bay %(bay)s could not be found.")

View File

@ -169,32 +169,6 @@ class Connection(object):
:returns: A list of tuples of the specified columns.
"""
@abc.abstractmethod
def reserve_baymodel(self, tag, baymodel_id):
"""Reserve a baymodel.
To prevent other ManagerServices from manipulating the given
BayModel while a Task is performed, mark it reserved by this host.
:param tag: A string uniquely identifying the reservation holder.
:param baymodel_id: A baymodel id or uuid.
:returns: A BayModel object.
:raises: BayModelNotFound if the baymodel is not found.
:raises: BayModelLocked if the baymodel is already reserved.
"""
@abc.abstractmethod
def release_baymodel(self, tag, baymodel_id):
"""Release the reservation on a baymodel.
:param tag: A string uniquely identifying the reservation holder.
:param baymodel_id: A baymodel id or uuid.
:raises: BayModelNotFound if the baymodel is not found.
:raises: BayModelLocked if the baymodel is reserved by another host.
:raises: BayModelNotLocked if the baymodel was found to not have a
reservation at all.
"""
@abc.abstractmethod
def create_baymodel(self, values):
"""Create a new baymodel.
@ -251,7 +225,6 @@ class Connection(object):
:param baymodel_id: The id or uuid of a baymodel.
:returns: A baymodel.
:raises: BayModelAssociated
:raises: BayModelNotFound
"""

View File

@ -340,44 +340,6 @@ class Connection(api.Connection):
return _paginate_query(models.BayModel, limit, marker,
sort_key, sort_dir, query)
def reserve_baymodel(self, tag, baymodel_id):
session = get_session()
with session.begin():
query = model_query(models.BayModel, session=session)
query = add_identity_filter(query, baymodel_id)
# be optimistic and assume we usually create a reservation
count = query.filter_by(reservation=None).update(
{'reservation': tag}, synchronize_session=False)
try:
baymodel = query.one()
if count != 1:
# Nothing updated and baymodel exists. Must already be
# locked.
raise exception.BayModelLocked(baymodel=baymodel_id,
host=baymodel['reservation'])
return baymodel
except NoResultFound:
raise exception.BayModelNotFound(baymodel_id)
def release_baymodel(self, tag, baymodel_id):
session = get_session()
with session.begin():
query = model_query(models.BayModel, session=session)
query = add_identity_filter(query, baymodel_id)
# be optimistic and assume we usually release a reservation
count = query.filter_by(reservation=tag).update(
{'reservation': None}, synchronize_session=False)
try:
if count != 1:
baymodel = query.one()
if baymodel['reservation'] is None:
raise exception.BayModelNotLocked(baymodel=baymodel_id)
else:
raise exception.BayModelLocked(baymodel=baymodel_id,
host=baymodel['reservation'])
except NoResultFound:
raise exception.BayModelNotFound(baymodel_id)
def create_baymodel(self, values):
# ensure defaults are present for new baymodels
if not values.get('uuid'):
@ -453,14 +415,6 @@ class Connection(api.Connection):
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_id)
# Prevent instance_uuid overwriting
if values.get("instance_uuid") and ref.instance_uuid:
raise exception.BayModelAssociated(baymodel=baymodel_id,
instance=ref.instance_uuid)
if 'provision_state' in values:
values['provision_updated_at'] = timeutils.utcnow()
ref.update(values)
return ref

View File

@ -143,6 +143,14 @@ class TestException(base.BaseTestCase):
self.assertRaises(exception.BayNotLocked,
lambda: self.raise_(exception.BayNotLocked()))
def test_BayModelNotFound(self):
self.assertRaises(exception.BayModelNotFound,
lambda: self.raise_(exception.BayModelNotFound()))
def test_BayModelAlreadyExists(self):
self.assertRaises(exception.BayModelAlreadyExists,
lambda: self.raise_(exception.BayModelAlreadyExists()))
def test_ContainerNotFound(self):
self.assertRaises(exception.ContainerNotFound,
lambda: self.raise_(exception.ContainerNotFound()))