Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/add_rpc_huge_tree_support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- "Add `huge_tree` support for RPC calls to support large RPC responses with lxml."
13 changes: 12 additions & 1 deletion plugins/module_utils/network/common/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
HAS_NCCLIENT = False

try:
from lxml.etree import Element, XMLSyntaxError, fromstring
from lxml.etree import Element, XMLParser, XMLSyntaxError, fromstring
except ImportError:
from xml.etree.ElementTree import Element, fromstring

Expand Down Expand Up @@ -59,12 +59,23 @@ def __rpc__(self, name, *args, **kwargs):
"""
self.check_rc = kwargs.pop("check_rc", True)
self.ignore_warning = kwargs.pop("ignore_warning", True)
self.huge_tree = kwargs.pop("huge_tree", False)

response = self._exec_jsonrpc(name, *args, **kwargs)
if "error" in response:
rpc_error = response["error"].get("data")
return self.parse_rpc_error(to_bytes(rpc_error, errors="surrogate_then_replace"))

if self.huge_tree:
try:
_parser = XMLParser(encoding="utf-8", recover=True, huge_tree=True)
except NameError as exc:
raise RuntimeError("XML huge_tree requires lxml.") from exc
return fromstring(
to_bytes(response["result"], errors="surrogate_then_replace"),
_parser,
)

return fromstring(to_bytes(response["result"], errors="surrogate_then_replace"))

def parse_rpc_error(self, rpc_error):
Expand Down