Merge "Add nova-manage cell_v2 delete_cell command"
This commit is contained in:
@@ -1461,6 +1461,44 @@ class CellV2Commands(object):
|
||||
print(t)
|
||||
return 0
|
||||
|
||||
@args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
|
||||
required=True, help=_('The uuid of the cell to delete.'))
|
||||
def delete_cell(self, cell_uuid):
|
||||
"""Delete an empty cell by the given uuid.
|
||||
|
||||
If the cell is not found by uuid or it is not empty (it has host or
|
||||
instance mappings) this command will return a non-zero exit code.
|
||||
|
||||
Returns 0 if the empty cell is found and deleted successfully.
|
||||
"""
|
||||
ctxt = context.get_admin_context()
|
||||
# Find the CellMapping given the uuid.
|
||||
try:
|
||||
cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
|
||||
except exception.CellMappingNotFound:
|
||||
print(_('Cell with uuid %s was not found.') % cell_uuid)
|
||||
return 1
|
||||
|
||||
# Check to see if there are any HostMappings for this cell.
|
||||
host_mappings = objects.HostMappingList.get_by_cell_id(
|
||||
ctxt, cell_mapping.id)
|
||||
if host_mappings:
|
||||
print(_('There are existing hosts mapped to cell with uuid %s.') %
|
||||
cell_uuid)
|
||||
return 2
|
||||
|
||||
# Check to see if there are any InstanceMappings for this cell.
|
||||
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
|
||||
|
||||
# There are no hosts or instances mapped to the cell so delete it.
|
||||
cell_mapping.destroy()
|
||||
return 0
|
||||
|
||||
|
||||
CATEGORIES = {
|
||||
'account': AccountCommands,
|
||||
|
||||
@@ -1462,6 +1462,62 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
|
||||
+--------+--------------------+---------------+---------------------+''',
|
||||
output)
|
||||
|
||||
def test_delete_cell_not_found(self):
|
||||
"""Tests trying to delete a cell that is not found by uuid."""
|
||||
cell_uuid = uuidutils.generate_uuid()
|
||||
self.assertEqual(1, self.commands.delete_cell(cell_uuid))
|
||||
output = self.output.getvalue().strip()
|
||||
self.assertEqual('Cell with uuid %s was not found.' % cell_uuid,
|
||||
output)
|
||||
|
||||
def test_delete_cell_host_mappings_exist(self):
|
||||
"""Tests trying to delete a cell which has host 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 a host mapping in this cell
|
||||
hm = objects.HostMapping(
|
||||
context=ctxt, host='fake-host', cell_mapping=cm)
|
||||
hm.create()
|
||||
self.assertEqual(2, self.commands.delete_cell(cell_uuid))
|
||||
output = self.output.getvalue().strip()
|
||||
self.assertIn('There are existing hosts mapped to cell', output)
|
||||
|
||||
def test_delete_cell_instance_mappings_exist(self):
|
||||
"""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(3, self.commands.delete_cell(cell_uuid))
|
||||
output = self.output.getvalue().strip()
|
||||
self.assertIn('There are existing instances mapped to cell', output)
|
||||
|
||||
def test_delete_cell_success(self):
|
||||
"""Tests trying to delete an empty cell."""
|
||||
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()
|
||||
self.assertEqual(0, self.commands.delete_cell(cell_uuid))
|
||||
output = self.output.getvalue().strip()
|
||||
self.assertEqual('', output)
|
||||
|
||||
|
||||
class TestNovaManageMain(test.NoDBTestCase):
|
||||
"""Tests the nova-manage:main() setup code."""
|
||||
|
||||
Reference in New Issue
Block a user