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

new filters for netbox 4.1.0 #155

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
45 changes: 26 additions & 19 deletions netbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def __init__(self, settings):
self.netbox = None
self.ignore_ssl = settings.IGNORE_SSL_ERRORS
self.modules = False
self.new_filters = False
self.connect_api()
self.verify_compatibility()
self.existing_manufacturers = self.get_manufacturers()
self.device_types = DeviceTypes(self.netbox, self.handle, self.counter, self.ignore_ssl)
self.device_types = DeviceTypes(self.netbox, self.handle, self.counter, self.ignore_ssl, self.new_filters)

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

# check if version >= 4.1 in order to use new filter names (https://github.com/netbox-community/netbox/issues/15410)
if version_split[0] >= 4 and version_split[1] >= 1:
self.new_filters = True
self.handle.log(f'Netbox version {self.netbox.version} found. Using new filters.')

def get_manufacturers(self):
return {str(item): item for item in self.netbox.dcim.manufacturers.all()}

Expand Down Expand Up @@ -183,27 +189,28 @@ class DeviceTypes:
def __new__(cls, *args, **kwargs):
return super().__new__(cls)

def __init__(self, netbox, handle, counter, ignore_ssl):
def __init__(self, netbox, handle, counter, ignore_ssl, new_filters):
self.netbox = netbox
self.handle = handle
self.counter = counter
self.existing_device_types = self.get_device_types()
self.ignore_ssl = ignore_ssl
self.new_filters = new_filters

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

def get_power_ports(self, device_type):
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(devicetype_id=device_type)}

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})}
def get_rear_ports(self, device_type):
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(devicetype_id=device_type)}
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})}

def get_module_power_ports(self, module_type):
return {str(item): item for item in self.netbox.dcim.power_port_templates.filter(moduletype_id=module_type)}
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})}

def get_module_rear_ports(self, module_type):
return {str(item): item for item in self.netbox.dcim.rear_port_templates.filter(moduletype_id=module_type)}
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})}

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

def create_interfaces(self, interfaces, device_type):
existing_interfaces = {str(item): item for item in self.netbox.dcim.interface_templates.filter(
devicetype_id=device_type)}
**{'device_type_id' if self.new_filters else 'devicetype_id': device_type})}
to_create = self.get_device_type_ports_to_create(
interfaces, device_type, existing_interfaces)

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

def create_console_ports(self, console_ports, device_type):
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(console_ports, device_type, existing_console_ports)

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

def create_power_outlets(self, power_outlets, device_type):
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(power_outlets, device_type, existing_power_outlets)

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

def create_console_server_ports(self, console_server_ports, device_type):
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(console_server_ports, device_type, existing_console_server_ports)

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

def create_front_ports(self, front_ports, device_type):
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(front_ports, device_type, existing_front_ports)

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

def create_device_bays(self, device_bays, device_type):
existing_device_bays = {str(item): item for item in self.netbox.dcim.device_bay_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(device_bays, device_type, existing_device_bays)

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

def create_module_bays(self, module_bays, device_type):
existing_module_bays = {str(item): item for item in self.netbox.dcim.module_bay_templates.filter(devicetype_id=device_type)}
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})}
to_create = self.get_device_type_ports_to_create(module_bays, device_type, existing_module_bays)

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

def create_module_interfaces(self, module_interfaces, module_type):
existing_interfaces = {str(item): item for item in self.netbox.dcim.interface_templates.filter(moduletype_id=module_type)}
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})}
to_create = self.get_module_type_ports_to_create(module_interfaces, module_type, existing_interfaces)

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

def create_module_console_ports(self, console_ports, module_type):
existing_console_ports = {str(item): item for item in self.netbox.dcim.console_port_templates.filter(moduletype_id=module_type)}
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})}
to_create = self.get_module_type_ports_to_create(console_ports, module_type, existing_console_ports)

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

def create_module_power_outlets(self, power_outlets, module_type):
existing_power_outlets = {str(item): item for item in self.netbox.dcim.power_outlet_templates.filter(moduletype_id=module_type)}
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})}
to_create = self.get_module_type_ports_to_create(power_outlets, module_type, existing_power_outlets)

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

def create_module_console_server_ports(self, console_server_ports, module_type):
existing_console_server_ports = {str(item): item for item in self.netbox.dcim.console_server_port_templates.filter(moduletype_id=module_type)}
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})}
to_create = self.get_module_type_ports_to_create(console_server_ports, module_type, existing_console_server_ports)

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

def create_module_front_ports(self, front_ports, module_type):
existing_front_ports = {str(item): item for item in self.netbox.dcim.front_port_templates.filter(moduletype_id=module_type)}
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})}
to_create = self.get_module_type_ports_to_create(front_ports, module_type, existing_front_ports)

if to_create:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GitPython==3.1.32
pynetbox==7.0.1
pynetbox==7.4.0
python-dotenv==1.0.0
PyYAML==6.0.1
PyYAML==6.0.1