diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index 6afd12de3899b25d36cc895a0cba50633c41f4db..04ca6a6b296cf16e297e6d983d53923b577a8163 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -410,7 +410,7 @@ SERVICES_LIST_SCHEMA = { 'additionalProperties': False }, 'snmp-info': { - 'type': 'object', + 'type': ['object', 'null'], 'properties': { 'ifIndex': {'type': 'integer'}, 'community': {'type': 'string'}, @@ -1558,22 +1558,28 @@ def _get_services_internal(service_type=None): return return_all or (s["status"].lower() == "operational") def _format_services(s): + hostname = common.ims_equipment_to_hostname(s["here"]["equipment"]) return { "id": s["id"], "name": s["name"], "customer": s["project"], "speed": s["speed_value"], "pop": s["here"]["pop"]["name"], - "hostname": common.ims_equipment_to_hostname(s["here"]["equipment"]), + "hostname": hostname, "interface": s["here"]["port"].lower(), "type": s["type"], "status": s["status"], "original": s, + "vendor": get_vendor(hostname), } - def _add_snmp(s, all_snmp_info): - snmp_interfaces = all_snmp_info.get(s['hostname'], {}) - interface_info = snmp_interfaces.get(s['interface'], None) + def _juniper_add_snmp(s, all_snmp_info): + if s["vendor"] != "juniper": + s['snmp'] = None + return s + + snmp_interfaces = all_snmp_info.get(s["hostname"], {}) + interface_info = snmp_interfaces.get(s["interface"], None) if interface_info: s['snmp'] = { 'ifIndex': interface_info['index'], @@ -1596,12 +1602,6 @@ def _get_services_internal(service_type=None): get_vendor = vendor_getter(config) - def _add_vendor(s): - if not include_vendor: - return s - s["vendor"] = get_vendor(s["hostname"]) - return s - def _add_ip_filter(s, all_ip_filters): s["ip_filter"] = all_ip_filters.get((s["hostname"], s["interface"])) return s @@ -1610,19 +1610,23 @@ def _get_services_internal(service_type=None): s.pop("original", None) return s + def _strip_vendor(s): + s.pop("vendor", None) + return s + result = _services() result = filter(_wanted_in_output, result) result = map(_format_services, result) if include_snmp: all_snmp_info = common.load_snmp_indexes(config) - result = map(partial(_add_snmp, all_snmp_info=all_snmp_info), result) - if include_vendor: - result = map(_add_vendor, result) + result = map(partial(_juniper_add_snmp, all_snmp_info=all_snmp_info), result) if include_ip_filter: all_ip_filters = common.load_gws_indirect_ip_filters(config) result = map( partial(_add_ip_filter, all_ip_filters=all_ip_filters), result ) + if not include_vendor: + result = map(_strip_vendor, result) result = map(_strip_original, result) result = list(result) diff --git a/test/test_general_poller_routes.py b/test/test_general_poller_routes.py index e1b2589397f306aa4e99e9821c2467eb4bdf1c24..b3c12078d0b2075aab6cba2c945d3b5acb36d7de 100644 --- a/test/test_general_poller_routes.py +++ b/test/test_general_poller_routes.py @@ -188,20 +188,39 @@ def test_gws_indirect_nokia(client, mocked_redis): } ), ) - mocked_redis.set("nokia-ip-filters:rt0.ams.nl.geant.net", json.dumps({ - "lag-11.333": "IAS_IP_FITLER" - })) + mocked_redis.set( + "nokia-ip-filters:rt0.ams.nl.geant.net", + json.dumps({"lag-11.333": "IAS_IP_FITLER"}), + ) + mocked_redis.set( + "snmp-interfaces:rt0.ams.nl.geant.net", + json.dumps( + [ + { + "name": "lag-11.333", + "index": 1234, + "communities": { + "inventory-provider": "test-inprov", + "dashboard": "test-dashboard", + "brian": "test-brian", + }, + }, + ] + ), + ) rv = client.get( - "/poller/gws/indirect?vendor=1&ip_filter=1", headers=DEFAULT_REQUEST_HEADERS + "/poller/gws/indirect?snmp=1&vendor=1&ip_filter=1", + headers=DEFAULT_REQUEST_HEADERS, ) assert rv.status_code == 200 response_data = rv.json jsonschema.validate(response_data, poller.SERVICES_LIST_SCHEMA) - services = { - (svc['hostname'], svc['interface']): svc for svc in response_data - } - assert services[('rt0.ams.nl.geant.net', 'lag-11.333')]['ip_filter'] == "IAS_IP_FITLER" + services = {(svc["hostname"], svc["interface"]): svc for svc in response_data} + assert ( + services[("rt0.ams.nl.geant.net", "lag-11.333")]["ip_filter"] == "IAS_IP_FITLER" + ) + assert services[("rt0.ams.nl.geant.net", "lag-11.333")]["snmp"] == None def test_gws_indirect_snmp(client): @@ -222,6 +241,7 @@ def test_gws_indirect_snmp(client): assert operational # sanity: test data contains operational services assert all('snmp' in s for s in operational) assert all('counters' in s['snmp'] for s in operational) + assert not any('vendor' in s for s in operational) def test_get_services_default(client):