From 009ab253828fa4bf016bd3b0df318c1d83057dd0 Mon Sep 17 00:00:00 2001 From: "Tagawa, Masaki" Date: Thu, 15 Aug 2024 15:45:40 +0900 Subject: [PATCH] Do not filter IP addresses by VRF as default By default, we don't filter IP address information from Netbox by VRF. Previously, we used `None` as the default for `vrf_id` to avoid this filtering. However, from pynetbox v7.4.0, `None` will be replaced by `'null'`. Using `'null'` will only retrieve addresses that do not have a VRF configured, so the default behavior has changed unintentionally. This commit will remove `vrf_id` from the query parameter by default. --- octodns_netbox/__init__.py | 42 +++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/octodns_netbox/__init__.py b/octodns_netbox/__init__.py index 93061de..8e1e8e0 100644 --- a/octodns_netbox/__init__.py +++ b/octodns_netbox/__init__.py @@ -158,14 +158,22 @@ def _populate_PTR(self, zone: Zone, family: Literal[4, 6]) -> typing.List[Rr]: ret = [] network = octodns_netbox.reversename.to_network(zone) - kw = {f"{self.field_name}__empty": "false"} - ipam_records = self._nb_client.ipam.ip_addresses.filter( - parent=network.compressed, - family=family, - vrf_id=self.populate_vrf_id, - tag=self.populate_tags, - **kw, - ) + kw = { + f"{self.field_name}__empty": "false", + "parent": network.compressed, + "family": family, + "vrf_id": self.populate_vrf_id, + "tag": self.populate_tags, + } + + # https://github.com/netbox-community/pynetbox/pull/545 + # From pynetbox v7.4.0, None will be mapped to null. + # When vrf_id is null, it does not mean that it is not filtered by vrf_id, + # but it would be an intention that VRF is not set. + if kw["vrf_id"] is None: + del kw["vrf_id"] + + ipam_records = self._nb_client.ipam.ip_addresses.filter(**kw) for ipam_record in ipam_records: ip_address = ip_interface(ipam_record.address).ip @@ -188,10 +196,20 @@ def _populate_PTR(self, zone: Zone, family: Literal[4, 6]) -> typing.List[Rr]: def _populate_normal(self, zone: Zone) -> typing.List[Rr]: ret = [] - kw = {f"{self.field_name}__ic": zone.name[:-1]} - ipam_records = self._nb_client.ipam.ip_addresses.filter( - vrf_id=self.populate_vrf_id, tag=self.populate_tags, **kw - ) + kw = { + f"{self.field_name}__ic": zone.name[:-1], + "vrf_id": self.populate_vrf_id, + "tag": self.populate_tags, + } + + # https://github.com/netbox-community/pynetbox/pull/545 + # From pynetbox v7.4.0, None will be mapped to null. + # When vrf_id is null, it does not mean that it is not filtered by vrf_id, + # but it would be an intention that VRF is not set. + if kw["vrf_id"] is None: + del kw["vrf_id"] + + ipam_records = self._nb_client.ipam.ip_addresses.filter(**kw) for ipam_record in ipam_records: ip_address = ip_interface(ipam_record.address).ip