From fb10449e5521163a4d7282d09d110a0bd8f3ecf2 Mon Sep 17 00:00:00 2001 From: Roopa Prabhu Date: Thu, 17 Sep 2015 21:57:06 -0700 Subject: [PATCH] Add support to display status (pass, fail) in ifquery --check json output Ticket: CM-7464 Reviewed By: CCR-3507 Testing Done: Tested ifquery check sanity ifquery --check non-json output displays 'pass' and 'fail' for each attribute on the same line (see below). This output is not json friendly. For json, include status in 'config_status' a dictionary whose keys are similar to the 'config' dictionary but values are status for the corresponding keys in the 'config' dictionary (see example below) auto bond4 iface bond4 inet static [pass] bond-mode 802.3ad [pass] bond-miimon 100 [pass] bond-use-carrier 1 [pass] bond-lacp-rate 1 [pass] bond-min-links 1 [pass] bond-xmit-hash-policy layer3+4 [pass] bond-slaves swp3 swp4 [pass] [ { "name": "bond4", "addr_method": "static", "addr_family": "inet", "auto": true, "config": { "bond-use-carrier": "1", "bond-miimon": "100", "bond-lacp-rate": "1", "bond-min-links": "1", "bond-slaves": "swp3 swp4", "bond-mode": "802.3ad", "bond-xmit-hash-policy": "layer3+4" }, "config_status": { "bond-use-carrier": "pass", "bond-miimon": "pass", "bond-lacp-rate": "pass", "bond-min-links": "pass", "bond-slaves": "pass", "bond-mode": "pass", "bond-xmit-hash-policy": "pass" }, "status": "pass" } ] --- config/ifupdown2.conf | 4 +-- ifupdown/iface.py | 65 +++++++++++++++++++++++++++++----------- ifupdown/ifupdownmain.py | 13 ++++---- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/config/ifupdown2.conf b/config/ifupdown2.conf index 55bfa55..8ffdc8f 100644 --- a/config/ifupdown2.conf +++ b/config/ifupdown2.conf @@ -29,8 +29,8 @@ multiple_vlan_aware_bridge_support=0 # cross marks against interface attributes. # Use the below strings to modify the default behaviour. # -ifquery_check_success_str=[pass] -ifquery_check_error_str=[fail] +ifquery_check_success_str=pass +ifquery_check_error_str=fail ifquery_check_unknown_str= # diff --git a/ifupdown/iface.py b/ifupdown/iface.py index e047476..272a480 100644 --- a/ifupdown/iface.py +++ b/ifupdown/iface.py @@ -17,6 +17,11 @@ from collections import OrderedDict import logging import json +class ifaceStatusStrs(): + SUCCESS = "success", + FAILURE = "error", + UNKNOWN = "unknown" + class ifaceType(): UNKNOWN = 0x0 IFACE = 0x1 @@ -175,16 +180,44 @@ class ifaceState(): return cls.QUERY_RUNNING class ifaceJsonEncoder(json.JSONEncoder): - def default(self, o): + def default(self, o, with_status=False): retconfig = {} - if o.config: - retconfig = dict((k, (v[0] if len(v) == 1 else v)) - for k,v in o.config.items()) - return OrderedDict({'name' : o.name, - 'addr_method' : o.addr_method, - 'addr_family' : o.addr_family, - 'auto' : o.auto, - 'config' : retconfig}) + retconfig_status = {} + retifacedict = OrderedDict([]) + if o.config: + for k,v in o.config.items(): + idx = 0 + vitem_status = [] + for vitem in v: + s = o.get_config_attr_status(k, idx) + if s == -1: + status_str = ifaceStatusStrs.UNKNOWN + elif s == 1: + status_str = ifaceStatusStrs.ERROR + elif s == 0: + status_str = ifaceStatusStrs.SUCCESS + vitem_status.append('%s' %status_str) + idx += 1 + retconfig[k] = v[0] if len(v) == 1 else v + retconfig_status[k] = vitem_status[0] if len(vitem_status) == 1 else vitem_status + + if (o.status == ifaceStatus.NOTFOUND or + o.status == ifaceStatus.ERROR): + status = ifaceStatusStrs.ERROR + else: + status = ifaceStatusStrs.SUCCESS + + retifacedict['name'] = o.name + if o.addr_method: + retifacedict['addr_method'] = o.addr_method + if o.addr_family: + retifacedict['addr_family'] = o.addr_family + retifacedict['auto'] = o.auto + retifacedict['config'] = retconfig + retifacedict['config_status'] = retconfig_status + retifacedict['status'] = status + + return retifacedict class ifaceJsonDecoder(): @classmethod @@ -535,9 +568,7 @@ class iface(): logger.info(indent + indent + str(config)) logger.info('}') - def dump_pretty(self, with_status=False, - successstr='success', errorstr='error', - unknownstr='unknown', use_realname=False): + def dump_pretty(self, with_status=False, use_realname=False): indent = '\t' outbuf = '' if use_realname and self.realname: @@ -561,9 +592,9 @@ class iface(): self.status == ifaceStatus.NOTFOUND): if self.status_str: ifaceline += ' (%s)' %self.status_str - status_str = errorstr + status_str = '[%s]' %ifaceStatusStrs.ERROR elif self.status == ifaceStatus.SUCCESS: - status_str = successstr + status_str = '[%s]' %ifaceStatusStrs.SUCCESS if status_str: outbuf += '{0:65} {1:>8}'.format(ifaceline, status_str) + '\n' else: @@ -583,11 +614,11 @@ class iface(): if with_status: s = self.get_config_attr_status(cname, idx) if s == -1: - status_str = unknownstr + status_str = '[%s]' %ifaceStatusStrs.UNKNOWN elif s == 1: - status_str = errorstr + status_str = '[%s]' %ifaceStatusStrs.ERROR elif s == 0: - status_str = successstr + status_str = '[%s]' %ifaceStatusStrs.SUCCESS outbuf += (indent + '{0:55} {1:>10}'.format( '%s %s' %(cname, cv), status_str)) + '\n' else: diff --git a/ifupdown/ifupdownmain.py b/ifupdown/ifupdownmain.py index 02386df..3adf075 100644 --- a/ifupdown/ifupdownmain.py +++ b/ifupdown/ifupdownmain.py @@ -1402,16 +1402,17 @@ class ifupdownMain(ifupdownBase): ifaceobjs = [] ret = self._get_ifaceobjscurr_pretty(ifacenames, ifaceobjs) if not ifaceobjs: return + ifaceStatusStrs.SUCCESS = self.config.get('ifquery_check_success_str', + _success_sym) + ifaceStatusStrs.ERROR = self.config.get('ifquery_check_error_str', + _error_sym) + ifaceStatusStrs.UNKNOWN = self.config.get('ifquery_check_unknown_str', + '') if format == 'json': print json.dumps(ifaceobjs, cls=ifaceJsonEncoder, indent=2, separators=(',', ': ')) else: - map(lambda i: i.dump_pretty(with_status=True, - successstr=self.config.get('ifquery_check_success_str', - _success_sym), - errorstr=self.config.get('ifquery_check_error_str', _error_sym), - unknownstr=self.config.get('ifquery_check_unknown_str', '')), - ifaceobjs) + map(lambda i: i.dump_pretty(with_status=True), ifaceobjs) return ret def print_ifaceobjsrunning_pretty(self, ifacenames, format='native'): -- 2.39.2