update ce_interface to fix bugs (#57907)
* update ce_interface * update ce_interface * add a changelog fragment.
This commit is contained in:
parent
fdfab01a2e
commit
e02353026d
2 changed files with 59 additions and 33 deletions
|
@ -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.
|
|
@ -149,6 +149,7 @@ changed:
|
||||||
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from xml.etree import ElementTree
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.network.cloudengine.ce import get_nc_config, set_nc_config, ce_argument_spec
|
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 = """
|
CE_NC_XML_CREATE_INTF = """
|
||||||
<ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
<ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
||||||
<interfaces>
|
<interfaces>
|
||||||
<interface operation="create">
|
<interface operation="merge">
|
||||||
<ifName>%s</ifName>
|
<ifName>%s</ifName>
|
||||||
<ifDescr>%s</ifDescr>
|
<ifDescr>%s</ifDescr>
|
||||||
</interface>
|
</interface>
|
||||||
|
@ -204,7 +205,7 @@ CE_NC_XML_CREATE_INTF = """
|
||||||
CE_NC_XML_CREATE_INTF_L2SUB = """
|
CE_NC_XML_CREATE_INTF_L2SUB = """
|
||||||
<ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
<ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
||||||
<interfaces>
|
<interfaces>
|
||||||
<interface operation="create">
|
<interface operation="merge">
|
||||||
<ifName>%s</ifName>
|
<ifName>%s</ifName>
|
||||||
<ifDescr>%s</ifDescr>
|
<ifDescr>%s</ifDescr>
|
||||||
<l2SubIfFlag>true</l2SubIfFlag>
|
<l2SubIfFlag>true</l2SubIfFlag>
|
||||||
|
@ -376,21 +377,22 @@ class Interface(object):
|
||||||
if "<data/>" in recv_xml:
|
if "<data/>" in recv_xml:
|
||||||
return intfs_info
|
return intfs_info
|
||||||
|
|
||||||
intf = re.findall(
|
xml_str = recv_xml.replace('\r', '').replace('\n', '').\
|
||||||
r'.*<ifName>(.*)</ifName>.*\s*<ifPhyType>(.*)</ifPhyType>.*\s*'
|
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
|
||||||
r'<ifNumber>(.*)</ifNumber>.*\s*<ifDescr>(.*)</ifDescr>.*\s*'
|
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||||
r'<isL2SwitchPort>(.*)</isL2SwitchPort>.*\s*<ifAdminStatus>'
|
|
||||||
r'(.*)</ifAdminStatus>.*\s*<ifMtu>(.*)</ifMtu>.*', 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]))
|
|
||||||
|
|
||||||
|
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
|
return intfs_info
|
||||||
|
|
||||||
def get_interface_dict(self, ifname):
|
def get_interface_dict(self, ifname):
|
||||||
|
@ -402,22 +404,15 @@ class Interface(object):
|
||||||
|
|
||||||
if "<data/>" in recv_xml:
|
if "<data/>" in recv_xml:
|
||||||
return intf_info
|
return intf_info
|
||||||
|
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"', "")
|
||||||
|
|
||||||
intf = re.findall(
|
root = ElementTree.fromstring(xml_str)
|
||||||
r'.*<ifName>(.*)</ifName>.*\s*'
|
intfs = root.findall("ifm/interfaces/interface/")
|
||||||
r'<ifPhyType>(.*)</ifPhyType>.*\s*'
|
if intfs:
|
||||||
r'<ifNumber>(.*)</ifNumber>.*\s*'
|
for intf in intfs:
|
||||||
r'<ifDescr>(.*)</ifDescr>.*\s*'
|
intf_info[intf.tag] = intf.text
|
||||||
r'<isL2SwitchPort>(.*)</isL2SwitchPort>.*\s*'
|
|
||||||
r'<ifAdminStatus>(.*)</ifAdminStatus>.*\s*'
|
|
||||||
r'<ifMtu>(.*)</ifMtu>.*', 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])
|
|
||||||
|
|
||||||
return intf_info
|
return intf_info
|
||||||
|
|
||||||
def create_interface(self, ifname, description, admin_state, mode, l2sub):
|
def create_interface(self, ifname, description, admin_state, mode, l2sub):
|
||||||
|
@ -752,10 +747,23 @@ class Interface(object):
|
||||||
else:
|
else:
|
||||||
self.existing["mode"] = "layer3"
|
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):
|
def get_end_state(self):
|
||||||
"""get_end_state"""
|
"""get_end_state"""
|
||||||
|
if self.interface:
|
||||||
if self.intf_info:
|
|
||||||
end_info = self.get_interface_dict(self.interface)
|
end_info = self.get_interface_dict(self.interface)
|
||||||
if end_info:
|
if end_info:
|
||||||
self.end_state["interface"] = end_info["ifName"]
|
self.end_state["interface"] = end_info["ifName"]
|
||||||
|
@ -768,6 +776,21 @@ class Interface(object):
|
||||||
else:
|
else:
|
||||||
self.end_state["mode"] = "layer3"
|
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):
|
def work(self):
|
||||||
"""worker"""
|
"""worker"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue