|
|
|
@ -25,13 +25,19 @@
|
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
#
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils._text import to_text, to_native
|
|
|
|
|
from ansible.module_utils.connection import Connection, ConnectionError
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from lxml.etree import Element, fromstring
|
|
|
|
|
from lxml.etree import Element, fromstring, XMLSyntaxError
|
|
|
|
|
except ImportError:
|
|
|
|
|
from xml.etree.ElementTree import Element, fromstring
|
|
|
|
|
if sys.version_info < (2, 7):
|
|
|
|
|
from xml.parsers.expat import ExpatError as XMLSyntaxError
|
|
|
|
|
else:
|
|
|
|
|
from xml.etree.ElementTree import ParseError as XMLSyntaxError
|
|
|
|
|
|
|
|
|
|
NS_MAP = {'nc': "urn:ietf:params:xml:ns:netconf:base:1.0"}
|
|
|
|
|
|
|
|
|
@ -67,25 +73,28 @@ class NetconfConnection(Connection):
|
|
|
|
|
|
|
|
|
|
def parse_rpc_error(self, rpc_error):
|
|
|
|
|
if self.check_rc:
|
|
|
|
|
error_root = fromstring(rpc_error)
|
|
|
|
|
root = Element('root')
|
|
|
|
|
root.append(error_root)
|
|
|
|
|
|
|
|
|
|
error_list = root.findall('.//nc:rpc-error', NS_MAP)
|
|
|
|
|
if not error_list:
|
|
|
|
|
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))
|
|
|
|
|
|
|
|
|
|
warnings = []
|
|
|
|
|
for error in error_list:
|
|
|
|
|
try:
|
|
|
|
|
message = error.find('./nc:error-message', NS_MAP).text
|
|
|
|
|
except Exception:
|
|
|
|
|
message = error.find('./nc:error-info', NS_MAP).text
|
|
|
|
|
|
|
|
|
|
severity = error.find('./nc:error-severity', NS_MAP).text
|
|
|
|
|
|
|
|
|
|
if severity == 'warning' and self.ignore_warning:
|
|
|
|
|
warnings.append(message)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
error_root = fromstring(rpc_error)
|
|
|
|
|
root = Element('root')
|
|
|
|
|
root.append(error_root)
|
|
|
|
|
|
|
|
|
|
error_list = root.findall('.//nc:rpc-error', NS_MAP)
|
|
|
|
|
if not error_list:
|
|
|
|
|
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))
|
|
|
|
|
return warnings
|
|
|
|
|
|
|
|
|
|
warnings = []
|
|
|
|
|
for error in error_list:
|
|
|
|
|
try:
|
|
|
|
|
message = error.find('./nc:error-message', NS_MAP).text
|
|
|
|
|
except Exception:
|
|
|
|
|
message = error.find('./nc:error-info', NS_MAP).text
|
|
|
|
|
|
|
|
|
|
severity = error.find('./nc:error-severity', NS_MAP).text
|
|
|
|
|
|
|
|
|
|
if severity == 'warning' and self.ignore_warning:
|
|
|
|
|
warnings.append(message)
|
|
|
|
|
else:
|
|
|
|
|
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))
|
|
|
|
|
return warnings
|
|
|
|
|
except XMLSyntaxError:
|
|
|
|
|
raise ConnectionError(rpc_error)
|
|
|
|
|