feat(network): Batch requests when filtering on interfaces #297

Merged
Tom-Hubrecht merged 1 commit from Tom-Hubrecht/master into master 2024-10-21 10:01:35 +02:00

View file

@ -1,7 +1,7 @@
import logging import logging
import os import os
import re import re
from itertools import chain from itertools import chain, islice
import netifaces import netifaces
from netaddr import IPAddress from netaddr import IPAddress
@ -413,11 +413,16 @@ class Network(object):
# delete IP on netbox that are not known on this server # delete IP on netbox that are not known on this server
if len(nb_nics): if len(nb_nics):
netbox_ips = nb.ipam.ip_addresses.filter( def batched(it, n):
**{self.intf_type: [x.id for x in nb_nics]} 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([ all_local_ips = list(chain.from_iterable([
x['ip'] for x in self.nics if x['ip'] is not None x['ip'] for x in self.nics if x['ip'] is not None
])) ]))