From edf9da60e0fc46b06169302b800a73bb1a1877b3 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Mon, 4 Feb 2019 14:24:30 +0100 Subject: [PATCH] return list of device structures --- inventory_provider/juniper.py | 17 ++++++++++++++++- test/test_junosspace_io.py | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/inventory_provider/juniper.py b/inventory_provider/juniper.py index 67f10a02..d5d5a3c9 100644 --- a/inventory_provider/juniper.py +++ b/inventory_provider/juniper.py @@ -1,4 +1,5 @@ import logging +import re from jnpr.junos import Device from lxml import etree @@ -294,4 +295,18 @@ def load_routers_from_junosspace(config): juniper_logger.error("%d.%d: %s" % (e.line, e.column, e.message)) assert False - return True + for d in devices.xpath('//devices/device'): + name = d.xpath('./name/text()')[0] + # TODO: ask ops if this name->hostname operation is valid + m = re.match(r'^(.*)\.re\d+$', name) + if m: + hostname = m.group(1) + '.geant.net' + else: + hostname = None + yield { + "OSVersion": d.xpath('./OSVersion/text()')[0], + "platform": d.xpath('./platform/text()')[0], + "address": d.xpath('./ipAddr/text()')[0], + "name": name, + "hostname": hostname + } diff --git a/test/test_junosspace_io.py b/test/test_junosspace_io.py index 39d82889..7f7ad8ee 100644 --- a/test/test_junosspace_io.py +++ b/test/test_junosspace_io.py @@ -22,5 +22,5 @@ def test_junosspace_devices_parsing(data_config): url = data_config['junosspace']['api'] + '/device-management/devices', body=f.read()) - doc = juniper.load_routers_from_junosspace(data_config['junosspace']) - assert doc + routers = juniper.load_routers_from_junosspace(data_config['junosspace']) + print(list(routers)) -- GitLab