From 2ca5f17dd73c02f7f782e2aaed03d6ab0f4777ad Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Thu, 23 Jun 2016 19:11:43 +0300 Subject: [PATCH] [Admin-Util] cleanup to avoid crashing in extreme cases As a preparation to creating unit test for the admin utils, we need some cleanup in order to avoid crashing on missing inputs Change-Id: I59c2e01110c90222c8d6069013ea58e13c37f114 --- .../shell/admin/plugins/nsxv/resources/dhcp_binding.py | 6 ++++-- vmware_nsx/shell/admin/plugins/nsxv/resources/edges.py | 2 +- vmware_nsx/shell/admin/plugins/nsxv/resources/metadata.py | 3 ++- .../shell/admin/plugins/nsxv3/resources/securitygroups.py | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py index 9c2963e399..2a5a35419b 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/dhcp_binding.py @@ -95,13 +95,15 @@ def list_missing_dhcp_bindings(resource, event, trigger, **kwargs): @admin_utils.output_header def nsx_update_dhcp_edge_binding(resource, event, trigger, **kwargs): """Resync DHCP bindings on NSXv Edge""" - - if not kwargs['property']: + if not kwargs.get('property'): LOG.error(_LE("Need to specify edge-id parameter")) return else: properties = admin_utils.parse_multi_keyval_opt(kwargs['property']) edge_id = properties.get('edge-id') + if not edge_id: + LOG.error(_LE("Need to specify edge-id parameter")) + return LOG.info(_LI("Updating NSXv Edge: %s"), edge_id) # Need to create a NeutronDbPlugin object; so that we are able to # do neutron list-ports. diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/edges.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/edges.py index f06df40b39..c5faa205ad 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/edges.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/edges.py @@ -100,7 +100,7 @@ def nsx_delete_orphaned_edges(resource, event, trigger, **kwargs): orphaned_edges = get_orphaned_edges() LOG.info(_LI("Before delete; Orphaned Edges: %s"), orphaned_edges) - if not kwargs['force']: + if not kwargs.get('force'): if len(orphaned_edges): user_confirm = admin_utils.query_yes_no("Do you want to delete " "orphaned edges", diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/metadata.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/metadata.py index 3e26562151..944dcea42d 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/metadata.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/metadata.py @@ -108,7 +108,8 @@ def nsx_redo_metadata_cfg(resource, event, trigger, **kwargs): lb.submit_to_backend(nsxv, edge_id, False) -def update_shared_secret(): +@admin_utils.output_header +def update_shared_secret(resource, event, trigger, **kwargs): edgeapi = utils.NeutronDbClient() edge_list = nsxv_db.get_nsxv_internal_edges_by_purpose( edgeapi.context.session, diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/securitygroups.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/securitygroups.py index 496b5cc68f..979f3a7775 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/securitygroups.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/securitygroups.py @@ -60,7 +60,7 @@ def nsx_list_security_groups(resource, event, trigger, **kwargs): @admin_utils.output_header def nsx_delete_security_groups(resource, event, trigger, **kwargs): - if kwargs['force'] is False: + if 'force' in kwargs and kwargs['force'] is False: if nsx_list_security_groups(resource, event, trigger, **kwargs): msg = ('Do you want to delete the following NSX firewall ' 'sections/nsgroups?') @@ -103,7 +103,7 @@ def neutron_list_security_groups(resource, event, trigger, **kwargs): @admin_utils.output_header def neutron_delete_security_groups(resource, event, trigger, **kwargs): - if kwargs['force'] is False: + if 'force' in kwargs and kwargs['force'] is False: if neutron_list_security_groups(resource, event, trigger, **kwargs): msg = ('Do you want to delete the following neutron ' 'security groups?')