Check if a host is reserved before deleting it

Currently, if you try to delete a host which is reserved, it is removed
from the freepool but remains in the computehosts table. This patch
checks if the host is reserved before removing it.

Change-Id: I3daaba338809375e16018ae5e03107d938e89b9c
Closes-Bug: #1721456
This commit is contained in:
Hiroaki Kobayashi 2017-10-06 10:23:18 +09:00 committed by Pierre Riteau
parent d4a1e54e49
commit 86101b022f
3 changed files with 46 additions and 8 deletions

View File

@ -30,6 +30,11 @@ class CantRemoveHost(exceptions.BlazarException):
msg_fmt = _("Can't remove host(s) %(host)s from Aggregate %(pool)s")
class CantDeleteHost(exceptions.BlazarException):
code = 409
msg_fmt = _("Can't delete host %(host)s. %(msg)s")
class CantAddHost(exceptions.BlazarException):
code = 409
msg_fmt = _("Can't add host(s) %(host)s to Aggregate %(pool)s")

View File

@ -321,8 +321,13 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
raise manager_ex.HostNotFound(host=host_id)
with trusts.create_ctx_from_trust(host['trust_id']):
# TODO(sbauza):
# - Check if no leases having this host scheduled
if db_api.host_allocation_get_all_by_values(
compute_host_id=host_id):
raise manager_ex.CantDeleteHost(
host=host_id,
msg='The host is reserved.'
)
inventory = nova.NovaInventory()
servers = inventory.get_servers_per_host(
host['hypervisor_hostname'])
@ -337,11 +342,12 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
# NOTE(sbauza): Extracapabilities will be destroyed thanks to
# the DB FK.
db_api.host_destroy(host_id)
except db_ex.BlazarDBException:
# Nothing so bad, but we need to advert the admin
# he has to rerun
raise manager_ex.CantRemoveHost(host=host_id,
pool=self.freepool_name)
except db_ex.BlazarDBException as e:
# Nothing so bad, but we need to alert admins
# they have to rerun
raise manager_ex.CantDeleteHost(
host=host_id,
msg=e.message)
def _matching_hosts(self, hypervisor_properties, resource_properties,
count_range, start_date, end_date):

View File

@ -280,13 +280,36 @@ class PhysicalHostPluginTestCase(tests.TestCase):
})
def test_delete_host(self):
host_allocation_get_all = self.patch(
self.db_api,
'host_allocation_get_all_by_values')
host_allocation_get_all.return_value = []
self.fake_phys_plugin.delete_computehost(self.fake_host_id)
self.db_host_destroy.assert_called_once_with(self.fake_host_id)
self.get_servers_per_host.assert_called_once_with(
self.fake_host["hypervisor_hostname"])
def test_delete_host_reserved(self):
host_allocation_get_all = self.patch(
self.db_api,
'host_allocation_get_all_by_values')
host_allocation_get_all.return_value = [
{
'id': u'dd305477-4df8-4547-87f6-69069ee546a6',
'compute_host_id': self.fake_host_id
}
]
self.assertRaises(manager_exceptions.CantDeleteHost,
self.fake_phys_plugin.delete_computehost,
self.fake_host_id)
def test_delete_host_having_vms(self):
host_allocation_get_all = self.patch(
self.db_api,
'host_allocation_get_all_by_values')
host_allocation_get_all.return_value = []
self.get_servers_per_host.return_value = ['server1', 'server2']
self.assertRaises(manager_exceptions.HostHavingServers,
self.fake_phys_plugin.delete_computehost,
@ -303,8 +326,12 @@ class PhysicalHostPluginTestCase(tests.TestCase):
def test_delete_host_issuing_rollback(self):
def fake_db_host_destroy(*args, **kwargs):
raise db_exceptions.BlazarDBException
host_allocation_get_all = self.patch(
self.db_api,
'host_allocation_get_all_by_values')
host_allocation_get_all.return_value = []
self.db_host_destroy.side_effect = fake_db_host_destroy
self.assertRaises(manager_exceptions.CantRemoveHost,
self.assertRaises(manager_exceptions.CantDeleteHost,
self.fake_phys_plugin.delete_computehost,
self.fake_host_id)