From 317548d05720cdcb6778d850e9f19e1b28b2ad68 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Tue, 19 Apr 2016 12:04:44 +0300 Subject: [PATCH] [Admin-Util] Add error handling to nsxv update_switch admin utility 'nsxadmin -r networks -o nsx-update' can fail if given the wrong dvs-id or teamingpolicy. Now we expect those errors and log proper messages. Output Exmaple for unknown policy: NSX Plugin in use: nsxv ==== [NSX] Update Switch ==== Updating NSXv switch dvs-22 teaming policy to FAILOVE Unknown teaming policy FAILOVE Output example for unkown DVS: NSX Plugin in use: nsxv ==== [NSX] Update Switch ==== DVS dvs-2222 not found Change-Id: Id0ae060ae8f82faa5d09a4fb8fedc5e3bc3b5984 --- .../admin/plugins/nsxv/resources/networks.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py b/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py index b717baf4e3..461fce2e98 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py +++ b/vmware_nsx/shell/admin/plugins/nsxv/resources/networks.py @@ -14,12 +14,13 @@ import logging +from oslo_serialization import jsonutils import xml.etree.ElementTree as et from vmware_nsx._i18n import _LE, _LI +from vmware_nsx.plugins.nsx_v.vshield.common import exceptions from vmware_nsx.shell.admin.plugins.common import constants from vmware_nsx.shell.admin.plugins.common import formatters - from vmware_nsx.shell.admin.plugins.common import utils as admin_utils from vmware_nsx.shell.admin.plugins.nsxv.resources import utils as utils from vmware_nsx.shell import nsxadmin as shell @@ -66,7 +67,11 @@ def nsx_update_switch(resource, event, trigger, **kwargs): LOG.error(_LE("Need to specify dvs-id. " "Add --property dvs-id=")) return - h, switch = nsxv.get_vdn_switch(dvs_id) + try: + h, switch = nsxv.get_vdn_switch(dvs_id) + except exceptions.ResourceNotFound: + LOG.error(_LE("DVS %s not found"), dvs_id) + return policy = properties.get('teamingpolicy') if policy: if switch['teamingPolicy'] == policy: @@ -75,7 +80,17 @@ def nsx_update_switch(resource, event, trigger, **kwargs): LOG.info(_LI("Updating NSXv switch %(dvs)s teaming policy to " "%(policy)s"), {'dvs': dvs_id, 'policy': policy}) switch['teamingPolicy'] = policy - switch = nsxv.update_vdn_switch(switch) + try: + switch = nsxv.update_vdn_switch(switch) + except exceptions.VcnsApiException as e: + desc = jsonutils.loads(e.response) + details = desc.get('details') + if details.startswith("No enum constant"): + LOG.error(_LE("Unknown teaming policy %s"), policy) + else: + LOG.error(_LE("Unexpected error occurred: %s"), details) + return + LOG.info(_LI("Switch value after update: %s"), switch) else: LOG.error(_LE("No teaming policy set. "