Skip to content

Commit 57da852

Browse files
committed
Cabling script minor updates
1 parent 302c0fe commit 57da852

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

initializers/cables.yml

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
1-
## Required parameters for termination X ('a' or 'b'):
2-
## termination_X_name - name of interface
3-
## termination_X_device - name of the device interface belongs to
4-
## termination_X_class - required if different than Interface which is the default
5-
## Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort
6-
##
7-
## If termination is a circuit then the required parameter is termination_x_circuit.
8-
## Required parameters for a circuit termination:
9-
## termination_x_circuit:
10-
## term_side - termination side of a circuit. Must be A or B
11-
## cid - circuit ID value
12-
## site OR provider_network - name of Site or ProviderNetwork respectively. If both provided, Site takes precedence
13-
##
14-
## Any other Cable parameters supported by Netbox are supported as the top level keys, e.g. 'type', 'status', etc.
15-
##
16-
## - termination_a_name: console
17-
## termination_a_device: spine
18-
## termination_a_class: ConsolePort
19-
## termination_b_name: tty9
20-
## termination_b_device: console-server
21-
## termination_b_class: ConsoleServerPort
22-
## type: cat6
23-
##
1+
# # Required parameters for termination X ('a' or 'b'):
2+
# #
3+
# # ```
4+
# # termination_x_name -> name of interface
5+
# # termination_x_device -> name of the device interface belongs to
6+
# # termination_x_class -> required if different than 'Interface' which is the default
7+
# # ```
8+
# #
9+
# # Supported termination classes: Interface, ConsolePort, ConsoleServerPort, FrontPort, RearPort, PowerPort, PowerOutlet
10+
# #
11+
# #
12+
# # If a termination is a circuit then the required parameter is termination_x_circuit.
13+
# # Required parameters for a circuit termination:
14+
# #
15+
# # ```
16+
# # termination_x_circuit:
17+
# # term_side -> termination side of a circuit. Must be A or B
18+
# # cid -> circuit ID value
19+
# # site OR provider_network -> name of Site or ProviderNetwork respectively. If both provided, Site takes precedence
20+
# # ```
21+
# #
22+
# # If a termination is a power feed then the required parameter is termination_x_feed.
23+
# #
24+
# # ```
25+
# # termination_x_feed:
26+
# # name -> name of the PowerFeed object
27+
# # power_panel:
28+
# # name -> name of the PowerPanel the PowerFeed is attached to
29+
# # site -> name of the Site in which the PowerPanel is present
30+
# # ```
31+
# #
32+
# # Any other Cable parameters supported by Netbox are supported as the top level keys, e.g. 'type', 'status', etc.
33+
# #
34+
# # - termination_a_name: console
35+
# # termination_a_device: spine
36+
# # termination_a_class: ConsolePort
37+
# # termination_b_name: tty9
38+
# # termination_b_device: console-server
39+
# # termination_b_class: ConsoleServerPort
40+
# # type: cat6
41+
# #
2442
# - termination_a_name: to-server02
2543
# termination_a_device: server01
2644
# termination_b_name: to-server01

initializers/dcim_interfaces.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
# enabled: true
2121
# type: 1000base-t
2222
# name: eth0
23+
# - device: server02
24+
# enabled: true
25+
# type: virtual
26+
# name: loopback

startup_scripts/460_cables.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
CONSOLE_PORT_TERMINATION = ContentType.objects.get_for_model(ConsolePort)
2323
CONSOLE_SERVER_PORT_TERMINATION = ContentType.objects.get_for_model(ConsoleServerPort)
2424
FRONT_PORT_TERMINATION = ContentType.objects.get_for_model(FrontPort)
25-
REAR_PORT_TERMINATION = ContentType.objects.get_for_model(Interface)
25+
REAR_PORT_TERMINATION = ContentType.objects.get_for_model(RearPort)
2626
FRONT_AND_REAR = [FRONT_PORT_TERMINATION, REAR_PORT_TERMINATION]
2727
POWER_PORT_TERMINATION = ContentType.objects.get_for_model(PowerPort)
2828
POWER_OUTLET_TERMINATION = ContentType.objects.get_for_model(PowerOutlet)
@@ -43,7 +43,10 @@ def get_termination_object(params: dict, side: str):
4343
termination = klass.objects.get(name=name, device__name=device)
4444
return termination
4545
elif feed_params:
46-
q = {"name": feed_params["power_panel"]["name"], "site__name": feed_params["power_panel"]["site"]}
46+
q = {
47+
"name": feed_params["power_panel"]["name"],
48+
"site__name": feed_params["power_panel"]["site"],
49+
}
4750
power_panel = PowerPanel.objects.get(**q)
4851
termination = PowerFeed.objects.get(name=feed_params["name"], power_panel=power_panel)
4952
return termination
@@ -78,25 +81,11 @@ def get_termination_object(params: dict, side: str):
7881
)
7982

8083

81-
def get_termination_class(port_class: str):
84+
def get_termination_class_by_name(port_class: str):
8285
if not port_class:
8386
return Interface
8487

85-
klass = globals()[port_class]
86-
if klass not in [
87-
Interface,
88-
FrontPort,
89-
RearPort,
90-
CircuitTermination,
91-
ConsolePort,
92-
ConsoleServerPort,
93-
PowerPort,
94-
PowerOutlet,
95-
PowerFeed,
96-
]:
97-
raise Exception(f"⚠️ Requested {port_class} is not supported as a cable termination!")
98-
99-
return klass
88+
return globals()[port_class]
10089

10190

10291
def cable_in_cables(term_a: tuple, term_b: tuple) -> bool:
@@ -182,26 +171,31 @@ def check_interface_types(*args):
182171
f"Termination {termination.device} {termination} {termination.type}"
183172
)
184173
except AttributeError:
185-
# CircuitTermination dosn't have a type field
174+
# CircuitTermination doesn't have a type field
186175
pass
187176

177+
188178
def check_terminations_are_free(*args):
189179
any_failed = False
190180
for termination in args:
191181
if termination.cable_id:
192182
any_failed = True
193-
print(f"⚠️ Termination {termination} is already occupied with cable #{termination.cable_id}")
183+
print(
184+
f"⚠️ Termination {termination} is already occupied "
185+
f"with cable #{termination.cable_id}"
186+
)
194187
if any_failed:
195-
raise Exception(f"⚠️ At least one end of the cable is already occupied.")
188+
raise Exception("⚠️ At least one end of the cable is already occupied.")
189+
196190

197191
cables = load_yaml("/opt/netbox/initializers/cables.yml")
198192

199193
if cables is None:
200194
sys.exit()
201195

202196
for params in cables:
203-
params["termination_a_class"] = get_termination_class(params.get("termination_a_class"))
204-
params["termination_b_class"] = get_termination_class(params.get("termination_b_class"))
197+
params["termination_a_class"] = get_termination_class_by_name(params.get("termination_a_class"))
198+
params["termination_b_class"] = get_termination_class_by_name(params.get("termination_b_class"))
205199

206200
term_a = get_termination_object(params, side="a")
207201
term_b = get_termination_object(params, side="b")

0 commit comments

Comments
 (0)