module_utils-network-cloudengine: fix get_nc_next. (#62587)

* fix get_nc_next.

* add a changelog fragment.

* upadte for  changelgo fragment.

* merge two prs, one depens another.

* merge two prs, one depens another.

* update changelog.
This commit is contained in:
Xu Yuandong 2019-09-23 21:13:42 +08:00 committed by Felix Fontein
parent bd8097ea91
commit d6ef7c8530
3 changed files with 30 additions and 5 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- "Cloudengine module_utils - the ``set-id`` (RPC-REPLY XML attribute) may change over the time althougth ``set-id`` is the identity of the next RPC packet."
- "Cloudengine netconf plugin - add a dispatch RPC function,just return original RPC-REPLY, the function is used by ``Cloudengine module_utils``."

View file

@ -357,12 +357,13 @@ def get_nc_next(module, xml_str):
response = conn.get(xml_str, if_rpc_reply=True) response = conn.get(xml_str, if_rpc_reply=True)
result = response.find('./*') result = response.find('./*')
set_id = response.get('set-id') set_id = response.get('set-id')
fetch_node = new_ele_ns('get-next', 'http://www.huawei.com/netconf/capability/base/1.0', {'set-id': set_id}) while True and set_id is not None:
while True:
try: try:
next = conn.dispatch(etree.tostring(fetch_node)) fetch_node = new_ele_ns('get-next', 'http://www.huawei.com/netconf/capability/base/1.0', {'set-id': set_id})
if next is not None: next_xml = conn.dispatch_rpc(etree.tostring(fetch_node))
result.extend(next) if next_xml is not None:
result.extend(next_xml.find('./*'))
set_id = next_xml.get('set-id')
except ConnectionError: except ConnectionError:
break break
if result is not None: if result is not None:

View file

@ -36,6 +36,11 @@ try:
except (ImportError, AttributeError): # paramiko and gssapi are incompatible and raise AttributeError not ImportError except (ImportError, AttributeError): # paramiko and gssapi are incompatible and raise AttributeError not ImportError
HAS_NCCLIENT = False HAS_NCCLIENT = False
try:
from lxml.etree import fromstring
except ImportError:
from xml.etree.ElementTree import fromstring
class Netconf(NetconfBase): class Netconf(NetconfBase):
@ -213,3 +218,19 @@ class Netconf(NetconfBase):
@ensure_connected @ensure_connected
def discard_changes(self, *args, **kwargs): def discard_changes(self, *args, **kwargs):
return self.m.discard_changes(*args, **kwargs).data_xml return self.m.discard_changes(*args, **kwargs).data_xml
@ensure_ncclient
@ensure_connected
def dispatch_rpc(self, rpc_command=None, source=None, filter=None):
"""
Execute rpc on the remote device eg. dispatch('get-next')
:param rpc_command: specifies rpc command to be dispatched either in plain text or in xml element format (depending on command)
:param source: name of the configuration datastore being queried
:param filter: specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
:return: Returns xml string containing the rpc-reply response received from remote host
"""
if rpc_command is None:
raise ValueError('rpc_command value must be provided')
resp = self.m.dispatch(fromstring(rpc_command), source=source, filter=filter)
# just return rpc-reply xml
return resp.xml