Merge "[Admin-Utils] delete all backup edges"

This commit is contained in:
Jenkins 2017-01-26 18:51:04 +00:00 committed by Gerrit Code Review
commit b43ca796d5
4 changed files with 64 additions and 21 deletions

View File

@ -93,6 +93,10 @@ Backup Edges
nsxadmin -r backup-edges -o clean --property edge-id=edge-9
- Delete all backup edges::
nsxadmin -r backup-edges -o clean-all
- List Edge name mismatches between DB and backend, and backup edges that are missing from the backend::
nsxadmin -r backup-edges -o list-mismatches

View File

@ -76,8 +76,30 @@ def _delete_edge_from_nsx_and_neutron(edge_id, router_id):
nsxv.delete_edge(edge_id)
# Remove bindings from Neutron DB
_delete_backup_from_neutron_db(edge_id, router_id)
return True
except Exception as expt:
LOG.error(_LE("%s"), str(expt))
return False
def _nsx_delete_backup_edge(edge_id, all_backup_edges):
"""Delete a specific backup edge"""
try:
edge_result = nsxv.get_edge(edge_id)
except exceptions.NeutronException as x:
LOG.error(_LE("%s"), str(x))
else:
# edge_result[0] is response status code
# edge_result[1] is response body
edge = edge_result[1]
backup_edges = [e['id'] for e in all_backup_edges]
if (not edge['name'].startswith('backup-')
or edge['id'] not in backup_edges):
LOG.error(
_LE('Edge: %s is not a backup edge; aborting delete'),
edge_id)
else:
return _delete_edge_from_nsx_and_neutron(edge_id, edge['name'])
def nsx_clean_backup_edge(resource, event, trigger, **kwargs):
@ -92,26 +114,35 @@ def nsx_clean_backup_edge(resource, event, trigger, **kwargs):
if not edge_id:
LOG.error(_LE("%s"), errmsg)
return
try:
edge = nsxv.get_edge(edge_id)
except exceptions.NeutronException as x:
LOG.error(_LE("%s"), str(x))
else:
# edge[0] is response status code
# edge[1] is response body
backup_edges = [e['id'] for e in get_nsxv_backup_edges()]
if (not edge[1]['name'].startswith('backup-')
or edge[1]['id'] not in backup_edges):
LOG.error(
_LE('Edge: %s is not a backup edge; aborting delete'), edge_id)
return
#ask for the user confirmation
confirm = admin_utils.query_yes_no(
"Do you want to delete edge: %s" % edge_id, default="no")
if not confirm:
LOG.info(_LI("Backup edge deletion aborted by user"))
return
# delete the backup edge
_nsx_delete_backup_edge(edge_id, get_nsxv_backup_edges())
confirm = admin_utils.query_yes_no(
"Do you want to delete edge: %s" % edge_id, default="no")
if not confirm:
LOG.info(_LI("Backup edge deletion aborted by user"))
return
_delete_edge_from_nsx_and_neutron(edge_id, edge[1]['name'])
def nsx_clean_all_backup_edges(resource, event, trigger, **kwargs):
"""Delete all backup edges"""
backup_edges = get_nsxv_backup_edges()
#ask for the user confirmation
confirm = admin_utils.query_yes_no(
"Do you want to delete %s backup edges?" % len(backup_edges),
default="no")
if not confirm:
LOG.info(_LI("Backup edges deletion aborted by user"))
return
deleted_cnt = 0
for edge in backup_edges:
# delete the backup edge
if _nsx_delete_backup_edge(edge['id'], backup_edges):
deleted_cnt = deleted_cnt + 1
LOG.info(_LI('Done Deleting %s backup edges'), deleted_cnt)
@admin_utils.output_header
@ -287,6 +318,9 @@ registry.subscribe(nsx_list_backup_edges,
registry.subscribe(nsx_clean_backup_edge,
constants.BACKUP_EDGES,
shell.Operations.CLEAN.value)
registry.subscribe(nsx_clean_all_backup_edges,
constants.BACKUP_EDGES,
shell.Operations.CLEAN_ALL.value)
registry.subscribe(nsx_list_name_mismatches,
constants.BACKUP_EDGES,
shell.Operations.LIST_MISMATCHES.value)

View File

@ -33,6 +33,7 @@ LOG = logging.getLogger(__name__)
class Operations(enum.Enum):
LIST = 'list'
CLEAN = 'clean'
CLEAN_ALL = 'clean-all'
LIST_MISMATCHES = 'list-mismatches'
FIX_MISMATCH = 'fix-mismatch'
@ -101,6 +102,7 @@ nsxv_resources = {
constants.BACKUP_EDGES: Resource(constants.BACKUP_EDGES,
[Operations.LIST.value,
Operations.CLEAN.value,
Operations.CLEAN_ALL.value,
Operations.LIST_MISMATCHES.value,
Operations.FIX_MISMATCH.value,
Operations.NEUTRON_CLEAN.value]),

View File

@ -61,9 +61,10 @@ class AbstractTestAdminUtils(base.BaseTestCase):
self._init_resource_plugin()
self.addCleanup(resource_registry.unregister_all_resources)
@abc.abstractmethod
def _init_mock_plugin(self):
pass
mock_query = mock.patch(
"vmware_nsx.shell.admin.plugins.common.utils.query_yes_no")
mock_query.start()
@abc.abstractmethod
def _get_plugin_name(self):
@ -106,6 +107,7 @@ class TestNsxvAdminUtils(AbstractTestAdminUtils,
mock_vcns_instance.return_value = self.fc
self.addCleanup(self.fc.reset_all)
super(TestNsxvAdminUtils, self)._init_mock_plugin()
def _get_plugin_name(self):
return 'nsxv'
@ -152,6 +154,7 @@ class TestNsxv3AdminUtils(AbstractTestAdminUtils,
return_value=[{'id': uuidutils.generate_uuid()}])
self._patch_object(nsx_v3_resources.LogicalRouterPort,
'__init__', return_value=None)
super(TestNsxv3AdminUtils, self)._init_mock_plugin()
def _get_plugin_name(self):
return 'nsxv3'