Raise database exception instead of returning None

In case of database errors when creating a new compute host, the host
plugin would not raise the database exception after removing the host
from the freepool, but would instead return None. This is not helpful
for operators who get the following message from the CLI:

    'NoneType' object has no attribute 'items'

This patch raises the database exception again, which results in error
messages such as:

    ERROR: BlazarDBDuplicateEntry: A database error occurred

The code returning None is removed as it is now unneeded.

Change-Id: If4240b5a191e2a4e3acee120c53517af029d96b7
This commit is contained in:
Pierre Riteau 2018-07-24 16:45:53 +01:00
parent 0429ac8210
commit 29d28fe82f
2 changed files with 15 additions and 17 deletions

View File

@ -355,30 +355,27 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
if trust_id:
host_details.update({'trust_id': trust_id})
host = db_api.host_create(host_details)
except db_ex.BlazarDBException:
except db_ex.BlazarDBException as e:
# We need to rollback
# TODO(sbauza): Investigate use of Taskflow for atomic
# transactions
pool.remove_computehost(self.freepool_name,
host_details['service_name'])
if host:
for key in extra_capabilities:
values = {'computehost_id': host['id'],
'capability_name': key,
'capability_value': extra_capabilities[key],
}
try:
db_api.host_extra_capability_create(values)
except db_ex.BlazarDBException:
cantaddextracapability.append(key)
raise e
for key in extra_capabilities:
values = {'computehost_id': host['id'],
'capability_name': key,
'capability_value': extra_capabilities[key],
}
try:
db_api.host_extra_capability_create(values)
except db_ex.BlazarDBException:
cantaddextracapability.append(key)
if cantaddextracapability:
raise manager_ex.CantAddExtraCapability(
keys=cantaddextracapability,
host=host['id'])
if host:
return self.get_computehost(host['id'])
else:
return None
return self.get_computehost(host['id'])
def is_updatable_extra_capability(self, capability):
reservations = db_utils.get_reservations_by_host_id(

View File

@ -253,8 +253,9 @@ class PhysicalHostPluginTestCase(tests.TestCase):
def fake_db_host_create(*args, **kwargs):
raise db_exceptions.BlazarDBException
self.db_host_create.side_effect = fake_db_host_create
host = self.fake_phys_plugin.create_computehost(self.fake_host)
self.assertIsNone(host)
self.assertRaises(db_exceptions.BlazarDBException,
self.fake_phys_plugin.create_computehost,
self.fake_host)
def test_create_host_having_issue_when_storing_extra_capability(self):
def fake_db_host_extra_capability_create(*args, **kwargs):