Skip to content

Commit da61087

Browse files
authored
new filters for netbox 4.1.0 (#155)
Update for NetBox 4.1.0+. Changed the following filters: * devicetype_id => device_type_id * moduletype_id => module_type_id netbox-community/netbox#15410
1 parent dda8ed8 commit da61087

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

netbox_api.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def __init__(self, settings):
2424
self.netbox = None
2525
self.ignore_ssl = settings.IGNORE_SSL_ERRORS
2626
self.modules = False
27+
self.new_filters = False
2728
self.connect_api()
2829
self.verify_compatibility()
2930
self.existing_manufacturers = self.get_manufacturers()
30-
self.device_types = DeviceTypes(self.netbox, self.handle, self.counter, self.ignore_ssl)
31+
self.device_types = DeviceTypes(self.netbox, self.handle, self.counter, self.ignore_ssl, self.new_filters)
3132

3233
def connect_api(self):
3334
try:
@@ -54,6 +55,11 @@ def verify_compatibility(self):
5455
if version_split[0] > 3 or (version_split[0] == 3 and version_split[1] >= 2):
5556
self.modules = True
5657

58+
# check if version >= 4.1 in order to use new filter names (https://github.com/netbox-community/netbox/issues/15410)
59+
if version_split[0] >= 4 and version_split[1] >= 1:
60+
self.new_filters = True
61+
self.handle.log(f'Netbox version {self.netbox.version} found. Using new filters.')
62+
5763
def get_manufacturers(self):
5864
return {str(item): item for item in self.netbox.dcim.manufacturers.all()}
5965

@@ -183,27 +189,28 @@ class DeviceTypes:
183189
def __new__(cls, *args, **kwargs):
184190
return super().__new__(cls)
185191

186-
def __init__(self, netbox, handle, counter, ignore_ssl):
192+
def __init__(self, netbox, handle, counter, ignore_ssl, new_filters):
187193
self.netbox = netbox
188194
self.handle = handle
189195
self.counter = counter
190196
self.existing_device_types = self.get_device_types()
191197
self.ignore_ssl = ignore_ssl
198+
self.new_filters = new_filters
192199

193200
def get_device_types(self):
194201
return {str(item): item for item in self.netbox.dcim.device_types.all()}
195202

196203
def get_power_ports(self, device_type):
197-
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(devicetype_id=device_type)}
198-
204+
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
205+
199206
def get_rear_ports(self, device_type):
200-
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(devicetype_id=device_type)}
207+
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
201208

202209
def get_module_power_ports(self, module_type):
203-
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(moduletype_id=module_type)}
210+
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
204211

205212
def get_module_rear_ports(self, module_type):
206-
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(moduletype_id=module_type)}
213+
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
207214

208215
def get_device_type_ports_to_create(self, dcim_ports, device_type, existing_ports):
209216
to_create = [port for port in dcim_ports if port['name'] not in existing_ports]
@@ -221,7 +228,7 @@ def get_module_type_ports_to_create(self, module_ports, module_type, existing_po
221228

222229
def create_interfaces(self, interfaces, device_type):
223230
existing_interfaces = {str(item): item for item in self.netbox.dcim.interface_templates.filter(
224-
devicetype_id=device_type)}
231+
**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
225232
to_create = self.get_device_type_ports_to_create(
226233
interfaces, device_type, existing_interfaces)
227234

@@ -248,7 +255,7 @@ def create_power_ports(self, power_ports, device_type):
248255
self.handle.log(f"Error '{excep.error}' creating Power Port")
249256

250257
def create_console_ports(self, console_ports, device_type):
251-
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(devicetype_id=device_type)}
258+
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
252259
to_create = self.get_device_type_ports_to_create(console_ports, device_type, existing_console_ports)
253260

254261
if to_create:
@@ -261,7 +268,7 @@ def create_console_ports(self, console_ports, device_type):
261268
self.handle.log(f"Error '{excep.error}' creating Console Port")
262269

263270
def create_power_outlets(self, power_outlets, device_type):
264-
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(devicetype_id=device_type)}
271+
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
265272
to_create = self.get_device_type_ports_to_create(power_outlets, device_type, existing_power_outlets)
266273

267274
if to_create:
@@ -282,7 +289,7 @@ def create_power_outlets(self, power_outlets, device_type):
282289
self.handle.log(f"Error '{excep.error}' creating Power Outlet")
283290

284291
def create_console_server_ports(self, console_server_ports, device_type):
285-
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(devicetype_id=device_type)}
292+
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
286293
to_create = self.get_device_type_ports_to_create(console_server_ports, device_type, existing_console_server_ports)
287294

288295
if to_create:
@@ -308,7 +315,7 @@ def create_rear_ports(self, rear_ports, device_type):
308315
self.handle.log(f"Error '{excep.error}' creating Rear Port")
309316

310317
def create_front_ports(self, front_ports, device_type):
311-
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(devicetype_id=device_type)}
318+
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
312319
to_create = self.get_device_type_ports_to_create(front_ports, device_type, existing_front_ports)
313320

314321
if to_create:
@@ -330,7 +337,7 @@ def create_front_ports(self, front_ports, device_type):
330337
self.handle.log(f"Error '{excep.error}' creating Front Port")
331338

332339
def create_device_bays(self, device_bays, device_type):
333-
existing_device_bays = {str(item): item for item in self.netbox.dcim.device_bay_templates.filter(devicetype_id=device_type)}
340+
existing_device_bays = {str(item): item for item in self.netbox.dcim.device_bay_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
334341
to_create = self.get_device_type_ports_to_create(device_bays, device_type, existing_device_bays)
335342

336343
if to_create:
@@ -343,7 +350,7 @@ def create_device_bays(self, device_bays, device_type):
343350
self.handle.log(f"Error '{excep.error}' creating Device Bay")
344351

345352
def create_module_bays(self, module_bays, device_type):
346-
existing_module_bays = {str(item): item for item in self.netbox.dcim.module_bay_templates.filter(devicetype_id=device_type)}
353+
existing_module_bays = {str(item): item for item in self.netbox.dcim.module_bay_templates.filter(**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
347354
to_create = self.get_device_type_ports_to_create(module_bays, device_type, existing_module_bays)
348355

349356
if to_create:
@@ -356,7 +363,7 @@ def create_module_bays(self, module_bays, device_type):
356363
self.handle.log(f"Error '{excep.error}' creating Module Bay")
357364

358365
def create_module_interfaces(self, module_interfaces, module_type):
359-
existing_interfaces = {str(item): item for item in self.netbox.dcim.interface_templates.filter(moduletype_id=module_type)}
366+
existing_interfaces = {str(item): item for item in self.netbox.dcim.interface_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
360367
to_create = self.get_module_type_ports_to_create(module_interfaces, module_type, existing_interfaces)
361368

362369
if to_create:
@@ -382,7 +389,7 @@ def create_module_power_ports(self, power_ports, module_type):
382389
self.handle.log(f"Error '{excep.error}' creating Module Power Port")
383390

384391
def create_module_console_ports(self, console_ports, module_type):
385-
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(moduletype_id=module_type)}
392+
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
386393
to_create = self.get_module_type_ports_to_create(console_ports, module_type, existing_console_ports)
387394

388395
if to_create:
@@ -395,7 +402,7 @@ def create_module_console_ports(self, console_ports, module_type):
395402
self.handle.log(f"Error '{excep.error}' creating Module Console Port")
396403

397404
def create_module_power_outlets(self, power_outlets, module_type):
398-
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(moduletype_id=module_type)}
405+
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
399406
to_create = self.get_module_type_ports_to_create(power_outlets, module_type, existing_power_outlets)
400407

401408
if to_create:
@@ -416,7 +423,7 @@ def create_module_power_outlets(self, power_outlets, module_type):
416423
self.handle.log(f"Error '{excep.error}' creating Module Power Outlet")
417424

418425
def create_module_console_server_ports(self, console_server_ports, module_type):
419-
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(moduletype_id=module_type)}
426+
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
420427
to_create = self.get_module_type_ports_to_create(console_server_ports, module_type, existing_console_server_ports)
421428

422429
if to_create:
@@ -442,7 +449,7 @@ def create_module_rear_ports(self, rear_ports, module_type):
442449
self.handle.log(f"Error '{excep.error}' creating Module Rear Port")
443450

444451
def create_module_front_ports(self, front_ports, module_type):
445-
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(moduletype_id=module_type)}
452+
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(**{'module_type_id' if self.new_filters else 'moduletype_id': module_type})}
446453
to_create = self.get_module_type_ports_to_create(front_ports, module_type, existing_front_ports)
447454

448455
if to_create:

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
GitPython==3.1.32
2-
pynetbox==7.0.1
2+
pynetbox==7.4.0
33
python-dotenv==1.0.0
4-
PyYAML==6.0.1
4+
PyYAML==6.0.1

0 commit comments

Comments
 (0)