Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions netbox_agent/network.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -413,11 +413,16 @@ def create_or_update_netbox_network_cards(self):

# 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
]))
Expand Down