Cabling script minor updates

This commit is contained in:
kr3ator 2022-04-08 15:51:27 +02:00
parent 302c0fed59
commit 57da852af6
3 changed files with 62 additions and 46 deletions

View File

@ -1,26 +1,44 @@
## Required parameters for termination X ('a' or 'b'): # # 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 # # termination_x_name -> name of interface
## Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort # # termination_x_device -> name of the device interface belongs to
## # # termination_x_class -> required if different than 'Interface' which is the default
## If termination is a circuit then the required parameter is termination_x_circuit. # # ```
## Required parameters for a circuit termination: # #
## termination_x_circuit: # # Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort, PowerPort, PowerOutlet
## 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 circuit then the required parameter is termination_x_circuit.
## # # Required parameters for a circuit termination:
## Any other Cable parameters supported by Netbox are supported as the top level keys, e.g. 'type', 'status', etc. # #
## # # ```
## - termination_a_name: console # # termination_x_circuit:
## termination_a_device: spine # # term_side -> termination side of a circuit. Must be A or B
## termination_a_class: ConsolePort # # cid -> circuit ID value
## termination_b_name: tty9 # # site OR provider_network -> name of Site or ProviderNetwork respectively. If both provided, Site takes precedence
## termination_b_device: console-server # # ```
## termination_b_class: ConsoleServerPort # #
## type: cat6 # # 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_name: to-server02
# termination_a_device: server01 # termination_a_device: server01
# termination_b_name: to-server01 # termination_b_name: to-server01

View File

@ -20,3 +20,7 @@
# enabled: true # enabled: true
# type: 1000base-t # type: 1000base-t
# name: eth0 # name: eth0
# - device: server02
# enabled: true
# type: virtual
# name: loopback

View File

@ -22,7 +22,7 @@ from startup_script_utils import load_yaml
CONSOLE_PORT_TERMINATION = ContentType.objects.get_for_model(ConsolePort) CONSOLE_PORT_TERMINATION = ContentType.objects.get_for_model(ConsolePort)
CONSOLE_SERVER_PORT_TERMINATION = ContentType.objects.get_for_model(ConsoleServerPort) CONSOLE_SERVER_PORT_TERMINATION = ContentType.objects.get_for_model(ConsoleServerPort)
FRONT_PORT_TERMINATION = ContentType.objects.get_for_model(FrontPort) 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] FRONT_AND_REAR = [FRONT_PORT_TERMINATION, REAR_PORT_TERMINATION]
POWER_PORT_TERMINATION = ContentType.objects.get_for_model(PowerPort) POWER_PORT_TERMINATION = ContentType.objects.get_for_model(PowerPort)
POWER_OUTLET_TERMINATION = ContentType.objects.get_for_model(PowerOutlet) 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) termination = klass.objects.get(name=name, device__name=device)
return termination return termination
elif feed_params: 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) power_panel = PowerPanel.objects.get(**q)
termination = PowerFeed.objects.get(name=feed_params["name"], power_panel=power_panel) termination = PowerFeed.objects.get(name=feed_params["name"], power_panel=power_panel)
return termination 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: if not port_class:
return Interface return Interface
klass = globals()[port_class] return 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
def cable_in_cables(term_a: tuple, term_b: tuple) -> bool: 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}" f"Termination {termination.device} {termination} {termination.type}"
) )
except AttributeError: except AttributeError:
# CircuitTermination dosn't have a type field # CircuitTermination doesn't have a type field
pass pass
def check_terminations_are_free(*args): def check_terminations_are_free(*args):
any_failed = False any_failed = False
for termination in args: for termination in args:
if termination.cable_id: if termination.cable_id:
any_failed = True 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: 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") cables = load_yaml("/opt/netbox/initializers/cables.yml")
@ -200,8 +194,8 @@ if cables is None:
sys.exit() sys.exit()
for params in cables: for params in cables:
params["termination_a_class"] = get_termination_class(params.get("termination_a_class")) params["termination_a_class"] = get_termination_class_by_name(params.get("termination_a_class"))
params["termination_b_class"] = get_termination_class(params.get("termination_b_class")) params["termination_b_class"] = get_termination_class_by_name(params.get("termination_b_class"))
term_a = get_termination_object(params, side="a") term_a = get_termination_object(params, side="a")
term_b = get_termination_object(params, side="b") term_b = get_termination_object(params, side="b")