From e02353026d1497a527c3db1535753aa9c2411042 Mon Sep 17 00:00:00 2001 From: YuandongXu <786018072@qq.com> Date: Sat, 29 Jun 2019 03:52:40 +0800 Subject: [PATCH] update ce_interface to fix bugs (#57907) * update ce_interface * update ce_interface * add a changelog fragment. --- .../57907-update-ce_interface-to-fix-bugs.yml | 3 + .../network/cloudengine/ce_interface.py | 93 ++++++++++++------- 2 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 changelogs/fragments/57907-update-ce_interface-to-fix-bugs.yml diff --git a/changelogs/fragments/57907-update-ce_interface-to-fix-bugs.yml b/changelogs/fragments/57907-update-ce_interface-to-fix-bugs.yml new file mode 100644 index 00000000000..2c065464f27 --- /dev/null +++ b/changelogs/fragments/57907-update-ce_interface-to-fix-bugs.yml @@ -0,0 +1,3 @@ +bugfixes: + - ce_interface - It is not a good way to find data from a xml tree by regular. lin379 line405. + - ce_interface - line 750,779 Some attributes of interfaces are missing, 'ifAdminStatus', 'ifDescr', 'isL2SwitchPort'.So add them when get interface status. diff --git a/lib/ansible/modules/network/cloudengine/ce_interface.py b/lib/ansible/modules/network/cloudengine/ce_interface.py index e3bbbfc5cec..ff0f77c4813 100644 --- a/lib/ansible/modules/network/cloudengine/ce_interface.py +++ b/lib/ansible/modules/network/cloudengine/ce_interface.py @@ -149,6 +149,7 @@ changed: import re +from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.cloudengine.ce import get_nc_config, set_nc_config, ce_argument_spec @@ -193,7 +194,7 @@ CE_NC_GET_INTF = """ CE_NC_XML_CREATE_INTF = """ - + %s %s @@ -204,7 +205,7 @@ CE_NC_XML_CREATE_INTF = """ CE_NC_XML_CREATE_INTF_L2SUB = """ - + %s %s true @@ -376,21 +377,22 @@ class Interface(object): if "" in recv_xml: return intfs_info - intf = re.findall( - r'.*(.*).*\s*(.*).*\s*' - r'(.*).*\s*(.*).*\s*' - r'(.*).*\s*' - r'(.*).*\s*(.*).*', recv_xml) - - for tmp in intf: - if tmp[1]: - if not intfs_info.get(tmp[1].lower()): - # new interface type list - intfs_info[tmp[1].lower()] = list() - intfs_info[tmp[1].lower()].append(dict(ifName=tmp[0], ifPhyType=tmp[1], ifNumber=tmp[2], - ifDescr=tmp[3], isL2SwitchPort=tmp[4], - ifAdminStatus=tmp[5], ifMtu=tmp[6])) - + xml_str = recv_xml.replace('\r', '').replace('\n', '').\ + replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ + replace('xmlns="http://www.huawei.com/netconf/vrp"', "") + + root = ElementTree.fromstring(xml_str) + intfs = root.findall("ifm/interfaces/") + if intfs: + for intf in intfs: + intf_type = intf.find("ifPhyType").text.lower() + if intf_type: + if not intfs_info.get(intf_type): + intfs_info[intf_type] = list() + intf_info = dict() + for tmp in intf: + intf_info[tmp.tag] = tmp.text + intfs_info[intf_type].append(intf_info) return intfs_info def get_interface_dict(self, ifname): @@ -402,22 +404,15 @@ class Interface(object): if "" in recv_xml: return intf_info - - intf = re.findall( - r'.*(.*).*\s*' - r'(.*).*\s*' - r'(.*).*\s*' - r'(.*).*\s*' - r'(.*).*\s*' - r'(.*).*\s*' - r'(.*).*', recv_xml) - - if intf: - intf_info = dict(ifName=intf[0][0], ifPhyType=intf[0][1], - ifNumber=intf[0][2], ifDescr=intf[0][3], - isL2SwitchPort=intf[0][4], - ifAdminStatus=intf[0][5], ifMtu=intf[0][6]) - + xml_str = recv_xml.replace('\r', '').replace('\n', '').\ + replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ + replace('xmlns="http://www.huawei.com/netconf/vrp"', "") + + root = ElementTree.fromstring(xml_str) + intfs = root.findall("ifm/interfaces/interface/") + if intfs: + for intf in intfs: + intf_info[intf.tag] = intf.text return intf_info def create_interface(self, ifname, description, admin_state, mode, l2sub): @@ -752,10 +747,23 @@ class Interface(object): else: self.existing["mode"] = "layer3" + if self.intfs_info: + intfs = self.intfs_info.get(self.intf_type.lower()) + for intf in intfs: + intf_para = dict() + if intf["ifAdminStatus"]: + intf_para["admin_state"] = intf["ifAdminStatus"] + intf_para["description"] = intf["ifDescr"] + + if intf["isL2SwitchPort"] == "true": + intf_para["mode"] = "layer2" + else: + intf_para["mode"] = "layer3" + self.existing[intf["ifName"]] = intf_para + def get_end_state(self): """get_end_state""" - - if self.intf_info: + if self.interface: end_info = self.get_interface_dict(self.interface) if end_info: self.end_state["interface"] = end_info["ifName"] @@ -768,6 +776,21 @@ class Interface(object): else: self.end_state["mode"] = "layer3" + if self.interface_type: + end_info = self.get_interfaces_dict() + intfs = end_info.get(self.intf_type.lower()) + for intf in intfs: + intf_para = dict() + if intf["ifAdminStatus"]: + intf_para["admin_state"] = intf["ifAdminStatus"] + intf_para["description"] = intf["ifDescr"] + + if intf["isL2SwitchPort"] == "true": + intf_para["mode"] = "layer2" + else: + intf_para["mode"] = "layer3" + self.end_state[intf["ifName"]] = intf_para + def work(self): """worker"""