Skip to content

Commit 7b14561

Browse files
authored
Merge pull request #22 from peterbaumert/enhancements
- added regex provided by @cybarox THANKS - use device "position" by default or 1 for non virtual chassis members not autodetection - add fallback name to all lower case + [^.a-zA-Z\d] changed to "-"
2 parents 5f0c54b + bef892c commit 7b14561

4 files changed

Lines changed: 21 additions & 35 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ For each Device Type you need to add a DeviceView.
4343

4444
It is based on a CSS grid view with 32 columns and 2 rows.
4545
You need to specify the grid-template-areas.
46-
- Interface positions will use the following format: {interfacename}{module}-{port}
46+
- Interface positions will use the following format: {interfacename}{module}-{port}
47+
or fallback to all lower case + [^.a-zA-Z\d] changed to "-"
4748
- leading "empties" can be specified as x
4849
- trailing "empties" can be specified as z
4950
- between "empties" can be named s{0-99}

examples/C9500-24Y4C.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
.deviceview.area {
33
grid-template-areas:
44
"gigabitethernet-0 x twentyfivegige0-1 twentyfivegige0-3 twentyfivegige0-5 twentyfivegige0-7 twentyfivegige0-9 twentyfivegige0-11 twentyfivegige0-13 twentyfivegige0-15 twentyfivegige0-17 twentyfivegige0-19 twentyfivegige0-21 twentyfivegige0-23 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 hundredgige0-25 hundredgige0-25 hundredgige0-27 hundredgige0-27"
5-
"con-0 x twentyfivegige0-2 twentyfivegige0-4 twentyfivegige0-6 twentyfivegige0-8 twentyfivegige0-10 twentyfivegige0-12 twentyfivegige0-14 twentyfivegige0-16 twentyfivegige0-18 twentyfivegige0-20 twentyfivegige0-22 twentyfivegige0-24 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 hundredgige0-26 hundredgige0-26 hundredgige0-28 hundredgige0-28";
6-
}
5+
"con0 x twentyfivegige0-2 twentyfivegige0-4 twentyfivegige0-6 twentyfivegige0-8 twentyfivegige0-10 twentyfivegige0-12 twentyfivegige0-14 twentyfivegige0-16 twentyfivegige0-18 twentyfivegige0-20 twentyfivegige0-22 twentyfivegige0-24 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 s1 hundredgige0-26 hundredgige0-26 hundredgige0-28 hundredgige0-28";
6+
}

netbox_device_view/templates/netbox_device_view/deviceview.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ <h4>Options</h4>
9292
<input onChange="this.form.submit()" type="checkbox" name="cable_colors" class="form-check-input" {% if cable_colors == "on" %}checked{% endif %} />
9393
<label for="cable_colors" class="form-check-label">Cable Colors</label>
9494
</div>
95-
<div class="form-check">
96-
<input onChange="this.form.submit()" type="checkbox" name="something_else" class="form-check-input" {% if something_else == "on" %}checked{% endif %} />
97-
<label for="something_else" class="form-check-label">something_else</label>
98-
</div>
9995
</form>
10096
</div>
10197
</div>

netbox_device_view/utils.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import re
66

77

8-
def process_interfaces(interfaces, ports_chassis, dev=1):
8+
def process_interfaces(interfaces, ports_chassis, dev):
99
if interfaces is not None:
1010
for itf in interfaces:
11-
regex = r"^(?P<type>([a-z]+))((?P<dev>[0-9]+)\/)?((?P<module>[0-9]+)\/)?((?P<port>[0-9]+))$"
11+
if itf.type == "virtual" or itf.type == "lag":
12+
continue
13+
regex = r"^(?P<type>([a-zA-Z\-_]*))(\/|(?P<dev>[0-9]+).|\s)?((?P<module>[0-9]+).|\s)?((?P<port>[0-9]+))$"
1214
matches = re.search(regex, itf.name.lower())
1315
if matches:
1416
itf.stylename = (
@@ -17,37 +19,24 @@ def process_interfaces(interfaces, ports_chassis, dev=1):
1719
+ "-"
1820
+ matches["port"]
1921
)
20-
sw = int(matches["dev"] or 999)
21-
if (
22-
hasattr(itf, "mgmt_only")
23-
and itf.mgmt_only
24-
and itf.type != "virtual"
25-
) or hasattr(itf, "mgmt_only") == False:
26-
sw = dev
27-
if sw not in ports_chassis and sw != 999:
28-
ports_chassis[sw] = []
29-
if sw != 999:
30-
ports_chassis[sw].append(itf)
22+
else:
23+
itf.stylename = re.sub(r"[^.a-zA-Z\d]", "-", itf.name.lower())
24+
if dev not in ports_chassis:
25+
ports_chassis[dev] = []
26+
ports_chassis[dev].append(itf)
3127
return ports_chassis
3228

3329

34-
def process_ports(ports, ports_chassis, where):
30+
def process_ports(ports, ports_chassis, dev):
3531
if ports is not None:
3632
for port in ports:
37-
regex = r"^(?P<type>([A-Za-z]+))[\s]?((?P<port>[0-9]+))$"
38-
matches = re.search(regex, port.name.lower())
39-
port.is_port = True
40-
if matches:
41-
port.stylename = (matches["type"] or "") + "-" + matches["port"]
42-
else:
43-
port.stylename = re.sub(r"[^.a-zA-Z\d]", "-", port.name.lower())
44-
sw = where
4533
if port.type == "virtual":
46-
sw = 999
47-
if sw not in ports_chassis and sw != 999:
48-
ports_chassis[sw] = []
49-
if sw != 999:
50-
ports_chassis[sw].append(port)
34+
continue
35+
port.is_port = True
36+
port.stylename = re.sub(r"[^.a-zA-Z\d]", "-", port.name.lower())
37+
if dev not in ports_chassis:
38+
ports_chassis[dev] = []
39+
ports_chassis[dev].append(port)
5140
return ports_chassis
5241

5342

@@ -62,7 +51,7 @@ def prepare(obj):
6251
device_type=obj.device_type
6352
).grid_template_area
6453
modules[1] = obj.modules.all()
65-
ports_chassis = process_interfaces(obj.interfaces.all(), ports_chassis)
54+
ports_chassis = process_interfaces(obj.interfaces.all(), ports_chassis, 1)
6655
ports_chassis = process_ports(obj.frontports.all(), ports_chassis, "Front")
6756
ports_chassis = process_ports(obj.rearports.all(), ports_chassis, "Rear")
6857
ports_chassis = process_ports(

0 commit comments

Comments
 (0)