Skip to content

Commit 213f8a2

Browse files
authored
Enable faster lookups on Netbox version < 3.0.6 (#645)
* Enable faster lookups on Netbox version < 3.0.6 This creates a new code path where we attempt to lookup the interfaces, and then trace them back via the cable attribute to find the exact match. This is to help performance on older versions of NetBox where the GET api does not take termination_*_id parameters to provide an exact match. The old behavior of looping through ALL the cables is preserved in the case where an exact match is not found, however it probably should be removed.
1 parent 22781ce commit 213f8a2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,18 @@ def run(self):
160160
)
161161
]
162162
else:
163-
cables = [
164-
cable
165-
for cable in nb_endpoint.all()
166-
if cable.termination_a_type == data["termination_a_type"]
167-
and cable.termination_a_id == data["termination_a_id"]
168-
and cable.termination_b_type == data["termination_b_type"]
169-
and cable.termination_b_id == data["termination_b_id"]
170-
]
163+
# Attempt to find the exact cable via the interface
164+
# relationship
165+
interface_a = self.nb.dcim.interfaces.get(data["termination_a_id"])
166+
interface_b = self.nb.dcim.interfaces.get(data["termination_b_id"])
167+
if (
168+
interface_a.cable
169+
and interface_b.cable
170+
and interface_a.cable.id == interface_b.cable.id
171+
):
172+
cables = [self.nb.dcim.cables.get(interface_a.cable.id)]
173+
else:
174+
cables = []
171175
if len(cables) == 0:
172176
self.nb_object = None
173177
elif len(cables) == 1:

0 commit comments

Comments
 (0)