From c52e26805d76d0050e7cc80f25e91d6eae588ad9 Mon Sep 17 00:00:00 2001 From: bsun-sudo <56011247+bsun-sudo@users.noreply.github.com> Date: Thu, 14 May 2020 14:01:28 -0700 Subject: [PATCH] Fix is_mgmt_vrf_enabled when MGMT_VRF_CONFIG is not present the config DB (#885) Fix is_mgmt_vrf_enabled for the case where MGMT_VRF_CONFIG is not at all in the config DB. This is the case where mgmt vrf is never configured. The function throws error at File "/usr/lib/python2.7/dist-packages/show/main.py", line 651, in is_mgmt_vrf_enabled mvrf_dict = json.loads(p.stdout.read()) Two show commands uses is_mgmt_vrf_enabled. "show mgmt-vrf" and "show ntp" Both commands throw error if mgmt vrf is never configured Co-authored-by: Bing Sun --- show/main.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/show/main.py b/show/main.py index 363895fce1..de57cf3229 100755 --- a/show/main.py +++ b/show/main.py @@ -636,16 +636,19 @@ def is_mgmt_vrf_enabled(ctx): cmd = 'sonic-cfggen -d --var-json "MGMT_VRF_CONFIG"' p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout = p.communicate()[0] - if p.returncode == 0: - mvrf_dict = json.loads(stdout) - - # if the mgmtVrfEnabled attribute is configured, check the value - # and return True accordingly. - if 'mgmtVrfEnabled' in mvrf_dict['vrf_global']: - if (mvrf_dict['vrf_global']['mgmtVrfEnabled'] == "true"): - #ManagementVRF is enabled. Return True. - return True + try : + mvrf_dict = json.loads(p.stdout.read()) + except ValueError: + print("MGMT_VRF_CONFIG is not present.") + return False + + # if the mgmtVrfEnabled attribute is configured, check the value + # and return True accordingly. + if 'mgmtVrfEnabled' in mvrf_dict['vrf_global']: + if (mvrf_dict['vrf_global']['mgmtVrfEnabled'] == "true"): + #ManagementVRF is enabled. Return True. + return True + return False #