Skip to content

Commit 9e9de39

Browse files
authored
Fix #608 - faster cabling (#624)
* Implement #608 to improve cabling speed in 3.0.6+
1 parent 8ef978f commit 9e9de39

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
NB_SITE_GROUPS = "site_groups"
4747
NB_VIRTUAL_CHASSIS = "virtual_chassis"
4848

49+
try:
50+
from packaging.version import Version
51+
except ImportError as imp_exc:
52+
PACKAGING_IMPORT_ERROR = imp_exc
53+
else:
54+
PACKAGING_IMPORT_ERROR = None
55+
4956

5057
class NetboxDcimModule(NetboxModule):
5158
def __init__(self, module, endpoint):
@@ -141,14 +148,24 @@ def run(self):
141148
data["color"] = data["color"].lower()
142149

143150
if self.endpoint == "cables":
144-
cables = [
145-
cable
146-
for cable in nb_endpoint.all()
147-
if cable.termination_a_type == data["termination_a_type"]
148-
and cable.termination_a_id == data["termination_a_id"]
149-
and cable.termination_b_type == data["termination_b_type"]
150-
and cable.termination_b_id == data["termination_b_id"]
151-
]
151+
if Version(self.full_version) >= Version("3.0.6"):
152+
cables = [
153+
nb_endpoint.get(
154+
termination_a_type=data["termination_a_type"],
155+
termination_a_id=data["termination_a_id"],
156+
termination_b_type=data["termination_b_type"],
157+
termination_b_id=data["termination_b_id"],
158+
)
159+
]
160+
else:
161+
cables = [
162+
cable
163+
for cable in nb_endpoint.all()
164+
if cable.termination_a_type == data["termination_a_type"]
165+
and cable.termination_a_id == data["termination_a_id"]
166+
and cable.termination_b_type == data["termination_b_type"]
167+
and cable.termination_b_id == data["termination_b_id"]
168+
]
152169
if len(cables) == 0:
153170
self.nb_object = None
154171
elif len(cables) == 1:

plugins/module_utils/netbox_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import traceback
1313
import re
1414
import json
15-
1615
from itertools import chain
1716

1817
from ansible.module_utils.common.text.converters import to_text
@@ -538,6 +537,7 @@ def __init__(self, module, endpoint, nb_client=None):
538537
self.nb = nb_client
539538
try:
540539
self.version = self.nb.version
540+
self.full_version = self.nb.status().get("netbox-version")
541541
except AttributeError:
542542
self.module.fail_json(msg="Must have pynetbox >=4.1.0")
543543

@@ -586,6 +586,7 @@ def _connect_netbox_api(self, url, token, ssl_verify, cert):
586586
nb.http_session = session
587587
try:
588588
self.version = nb.version
589+
self.full_version = nb.status().get("netbox-version")
589590
except AttributeError:
590591
self.module.fail_json(msg="Must have pynetbox >=4.1.0")
591592
except Exception:

0 commit comments

Comments
 (0)