From 57da852af610936003ca635ff13ad3b60e97de78 Mon Sep 17 00:00:00 2001 From: kr3ator <48438188+kr3ator@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:51:27 +0200 Subject: [PATCH] Cabling script minor updates --- initializers/cables.yml | 64 ++++++++++++++++++++------------ initializers/dcim_interfaces.yml | 4 ++ startup_scripts/460_cables.py | 40 +++++++++----------- 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/initializers/cables.yml b/initializers/cables.yml index 30e9869..3257643 100644 --- a/initializers/cables.yml +++ b/initializers/cables.yml @@ -1,26 +1,44 @@ -## Required parameters for termination X ('a' or 'b'): -## termination_X_name - name of interface -## termination_X_device - name of the device interface belongs to -## termination_X_class - required if different than Interface which is the default -## Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort -## -## If termination is a circuit then the required parameter is termination_x_circuit. -## Required parameters for a circuit termination: -## termination_x_circuit: -## term_side - termination side of a circuit. Must be A or B -## cid - circuit ID value -## site OR provider_network - name of Site or ProviderNetwork respectively. If both provided, Site takes precedence -## -## Any other Cable parameters supported by Netbox are supported as the top level keys, e.g. 'type', 'status', etc. -## -## - termination_a_name: console -## termination_a_device: spine -## termination_a_class: ConsolePort -## termination_b_name: tty9 -## termination_b_device: console-server -## termination_b_class: ConsoleServerPort -## type: cat6 -## +# # Required parameters for termination X ('a' or 'b'): +# # +# # ``` +# # termination_x_name -> name of interface +# # termination_x_device -> name of the device interface belongs to +# # termination_x_class -> required if different than 'Interface' which is the default +# # ``` +# # +# # Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort, PowerPort, PowerOutlet +# # +# # +# # If a termination is a circuit then the required parameter is termination_x_circuit. +# # Required parameters for a circuit termination: +# # +# # ``` +# # termination_x_circuit: +# # term_side -> termination side of a circuit. Must be A or B +# # cid -> circuit ID value +# # site OR provider_network -> name of Site or ProviderNetwork respectively. If both provided, Site takes precedence +# # ``` +# # +# # If a termination is a power feed then the required parameter is termination_x_feed. +# # +# # ``` +# # termination_x_feed: +# # name -> name of the PowerFeed object +# # power_panel: +# # name -> name of the PowerPanel the PowerFeed is attached to +# # site -> name of the Site in which the PowerPanel is present +# # ``` +# # +# # Any other Cable parameters supported by Netbox are supported as the top level keys, e.g. 'type', 'status', etc. +# # +# # - termination_a_name: console +# # termination_a_device: spine +# # termination_a_class: ConsolePort +# # termination_b_name: tty9 +# # termination_b_device: console-server +# # termination_b_class: ConsoleServerPort +# # type: cat6 +# # # - termination_a_name: to-server02 # termination_a_device: server01 # termination_b_name: to-server01 diff --git a/initializers/dcim_interfaces.yml b/initializers/dcim_interfaces.yml index ef1ea8c..67d5e44 100644 --- a/initializers/dcim_interfaces.yml +++ b/initializers/dcim_interfaces.yml @@ -20,3 +20,7 @@ # enabled: true # type: 1000base-t # name: eth0 +# - device: server02 +# enabled: true +# type: virtual +# name: loopback diff --git a/startup_scripts/460_cables.py b/startup_scripts/460_cables.py index b39d40a..6e4cec2 100644 --- a/startup_scripts/460_cables.py +++ b/startup_scripts/460_cables.py @@ -22,7 +22,7 @@ from startup_script_utils import load_yaml CONSOLE_PORT_TERMINATION = ContentType.objects.get_for_model(ConsolePort) CONSOLE_SERVER_PORT_TERMINATION = ContentType.objects.get_for_model(ConsoleServerPort) FRONT_PORT_TERMINATION = ContentType.objects.get_for_model(FrontPort) -REAR_PORT_TERMINATION = ContentType.objects.get_for_model(Interface) +REAR_PORT_TERMINATION = ContentType.objects.get_for_model(RearPort) FRONT_AND_REAR = [FRONT_PORT_TERMINATION, REAR_PORT_TERMINATION] POWER_PORT_TERMINATION = ContentType.objects.get_for_model(PowerPort) POWER_OUTLET_TERMINATION = ContentType.objects.get_for_model(PowerOutlet) @@ -43,7 +43,10 @@ def get_termination_object(params: dict, side: str): termination = klass.objects.get(name=name, device__name=device) return termination elif feed_params: - q = {"name": feed_params["power_panel"]["name"], "site__name": feed_params["power_panel"]["site"]} + q = { + "name": feed_params["power_panel"]["name"], + "site__name": feed_params["power_panel"]["site"], + } power_panel = PowerPanel.objects.get(**q) termination = PowerFeed.objects.get(name=feed_params["name"], power_panel=power_panel) return termination @@ -78,25 +81,11 @@ def get_termination_object(params: dict, side: str): ) -def get_termination_class(port_class: str): +def get_termination_class_by_name(port_class: str): if not port_class: return Interface - klass = globals()[port_class] - if klass not in [ - Interface, - FrontPort, - RearPort, - CircuitTermination, - ConsolePort, - ConsoleServerPort, - PowerPort, - PowerOutlet, - PowerFeed, - ]: - raise Exception(f"⚠️ Requested {port_class} is not supported as a cable termination!") - - return klass + return globals()[port_class] def cable_in_cables(term_a: tuple, term_b: tuple) -> bool: @@ -182,17 +171,22 @@ def check_interface_types(*args): f"Termination {termination.device} {termination} {termination.type}" ) except AttributeError: - # CircuitTermination dosn't have a type field + # CircuitTermination doesn't have a type field pass + def check_terminations_are_free(*args): any_failed = False for termination in args: if termination.cable_id: any_failed = True - print(f"⚠️ Termination {termination} is already occupied with cable #{termination.cable_id}") + print( + f"⚠️ Termination {termination} is already occupied " + f"with cable #{termination.cable_id}" + ) if any_failed: - raise Exception(f"⚠️ At least one end of the cable is already occupied.") + raise Exception("⚠️ At least one end of the cable is already occupied.") + cables = load_yaml("/opt/netbox/initializers/cables.yml") @@ -200,8 +194,8 @@ if cables is None: sys.exit() for params in cables: - params["termination_a_class"] = get_termination_class(params.get("termination_a_class")) - params["termination_b_class"] = get_termination_class(params.get("termination_b_class")) + params["termination_a_class"] = get_termination_class_by_name(params.get("termination_a_class")) + params["termination_b_class"] = get_termination_class_by_name(params.get("termination_b_class")) term_a = get_termination_object(params, side="a") term_b = get_termination_object(params, side="b")