diff --git a/inventory_provider/juniper.py b/inventory_provider/juniper.py index 4a08c5233edb47e3e88ae168bc88641d3e4d97f9..a878645893c1ff8ede8371f0fce09d3f943eb216 100644 --- a/inventory_provider/juniper.py +++ b/inventory_provider/juniper.py @@ -108,6 +108,10 @@ UNIT_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?> """ # noqa: E501 +class NetconfHandlingError(Exception): + pass + + def _rpc(hostname, ssh): dev = Device( host=hostname, @@ -121,14 +125,22 @@ def _rpc(hostname, ssh): def validate_netconf_config(config_doc): + """ + :param config_doc: + :return: + :raises: NetconfHandlingError in case of validation errors + """ logger = logging.getLogger(__name__) def _validate(schema, doc): if schema.validate(doc): return + messages = [] for e in schema.error_log: - logger.error("%d.%d: %s" % (e.line, e.column, e.message)) - assert False + msg = f'{e.line}.{e.column}: {e.message}' + messages.append(msg) + logger.error(msg) + raise NetconfHandlingError('\n'.join(messages)) schema_doc = etree.XML(CONFIG_SCHEMA.encode('utf-8')) config_schema = etree.XMLSchema(schema_doc) @@ -151,6 +163,7 @@ def load_config(hostname, ssh_params): :param hostname: router hostname :param ssh_params: 'ssh' config element(cf. config.py:CONFIG_SCHEMA) :return: + :raises: NetconfHandlingError from validate_netconf_config """ logger = logging.getLogger(__name__) logger.info("capturing netconf data for '%s'" % hostname) diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index f8b491e264a39ed4cdb8637339adb3722a8d7eb4..c596e80b73dab131263b8e42304293cfd9512e26 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -157,7 +157,7 @@ def netconf_refresh_config(self, hostname): netconf_doc = juniper.load_config( hostname, InventoryTask.config["ssh"]) netconf_str = etree.tostring(netconf_doc, encoding='unicode') - except ConnectionError: + except (ConnectionError, juniper.NetconfHandlingError): msg = f'error loading netconf data from {hostname}' logger.exception(msg) self.log_warning(msg)