From e04d0c6d59f165a4ed7c9b2a4e13b7c9b9c70034 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Tue, 7 May 2024 13:54:27 +0200 Subject: [PATCH] feat(network): Batch requests when filtering on interfaces This avoids an issue where the requested URI is too long when many interfaces are present --- netbox_agent/network.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 673dfc1..ae32e31 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -1,7 +1,7 @@ import logging import os import re -from itertools import chain +from itertools import chain, islice import netifaces from netaddr import IPAddress @@ -413,11 +413,16 @@ class Network(object): # delete IP on netbox that are not known on this server if len(nb_nics): - netbox_ips = nb.ipam.ip_addresses.filter( - **{self.intf_type: [x.id for x in nb_nics]} - ) + def batched(it, n): + while batch := tuple(islice(it, n)): + yield batch + + netbox_ips = [] + for ids in batched((x.id for x in nb_nics), 25): + netbox_ips += list( + nb.ipam.ip_addresses.filter(**{self.intf_type: ids}) + ) - netbox_ips = list(netbox_ips) all_local_ips = list(chain.from_iterable([ x['ip'] for x in self.nics if x['ip'] is not None ]))