Add a warning in 'nova-manage cell_v2 delete_cell'

When deleting a cell, if there are instance mappings to the cell,
the command fails with the following message.

* There are existing instances mapped to cell with uuid UUID.

But even if all instances have been deleted in the cell,
the same message is shown.

So in that case, add a warning that the instance mappings have to
be deleted by 'nova-manage db archive_deleted_rows'
before deleting the cell.

Change-Id: I2a163fb50a7e71ce9f463bc9ddeffe2ea47d1588
Closes-Bug: #1725331
This commit is contained in:
Takashi NATSUME 2017-10-20 23:13:28 +09:00
parent 802378ae55
commit 52e7e6e3e4
4 changed files with 54 additions and 5 deletions

View File

@ -213,7 +213,9 @@ Nova Cells v2
the cell and the hosts are deleted successfully with ``--force`` option,
1 if a cell with that uuid could not be found, 2 if host mappings were
found for the cell (cell not empty) without ``--force`` option, and 3
if there are instances mapped to the cell (cell not empty).
if there are instances mapped to the cell (cell not empty), 4 if there are
instance mappings to the cell but all instances have been deleted
in the cell.
``nova-manage cell_v2 list_hosts [--cell_uuid <cell_uuid>]``

View File

@ -1393,9 +1393,21 @@ class CellV2Commands(object):
instance_mappings = objects.InstanceMappingList.get_by_cell_id(
ctxt, cell_mapping.id)
if instance_mappings:
print(_('There are existing instances mapped to cell with '
'uuid %s.') % cell_uuid)
return 3
with context.target_cell(ctxt, cell_mapping) as cctxt:
instances = objects.InstanceList.get_all(cctxt)
if instances:
# There are instances in the cell.
print(_('There are existing instances mapped to cell with '
'uuid %s.') % cell_uuid)
return 3
# There are no instances in the cell but the records remains
# in the 'instance_mappings' table.
print(_("There are instance mappings to cell with uuid %s, "
"but all instances have been deleted "
"in the cell.") % cell_uuid)
print(_("So execute 'nova-manage db archive_deleted_rows' to "
"delete the instance mappings."))
return 4
# Delete hosts mapped to the cell.
for host_mapping in host_mappings:

View File

@ -1691,10 +1691,14 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
output = self.output.getvalue().strip()
self.assertIn('There are existing hosts mapped to cell', output)
def test_delete_cell_instance_mappings_exist(self):
@mock.patch.object(objects.InstanceList, 'get_all')
def test_delete_cell_instance_mappings_exist_with_instances(
self, mock_get_all):
"""Tests trying to delete a cell which has instance mappings."""
cell_uuid = uuidutils.generate_uuid()
ctxt = context.get_admin_context()
mock_get_all.return_value = [objects.Instance(
ctxt, uuid=uuidsentinel.instance)]
# create the cell mapping
cm = objects.CellMapping(
context=ctxt, uuid=cell_uuid, database_connection='fake:///db',
@ -1709,6 +1713,31 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
output = self.output.getvalue().strip()
self.assertIn('There are existing instances mapped to cell', output)
@mock.patch.object(objects.InstanceList, 'get_all',
return_value=[])
def test_delete_cell_instance_mappings_exist_without_instances(
self, mock_get_all):
"""Tests trying to delete a cell which has instance mappings."""
cell_uuid = uuidutils.generate_uuid()
ctxt = context.get_admin_context()
# create the cell mapping
cm = objects.CellMapping(
context=ctxt, uuid=cell_uuid, database_connection='fake:///db',
transport_url='fake:///mq')
cm.create()
# create an instance mapping in this cell
im = objects.InstanceMapping(
context=ctxt, instance_uuid=uuidutils.generate_uuid(),
cell_mapping=cm, project_id=uuidutils.generate_uuid())
im.create()
self.assertEqual(4, self.commands.delete_cell(cell_uuid))
output = self.output.getvalue().strip()
self.assertIn('There are instance mappings to cell with uuid', output)
self.assertIn('but all instances have been deleted in the cell.',
output)
self.assertIn("So execute 'nova-manage db archive_deleted_rows' to "
"delete the instance mappings.", output)
def test_delete_cell_success_without_host_mappings(self):
"""Tests trying to delete an empty cell."""
cell_uuid = uuidutils.generate_uuid()

View File

@ -0,0 +1,6 @@
---
features:
- |
The ``nova-manage cell_v2 delete_cell`` command returns an exit code 4
when there are instance mappings to a cell to delete but all instances
have been deleted in the cell.