Commit ec8e142a authored by Adrian's avatar Adrian
Browse files

add patch script

parent e8c72492
#!/usr/bin/env python3
from dcim.models import *
from ipam.models import *
from extras.scripts import *
class EquivPatch(Script):
class Meta:
name = "Equivalent Rear Port Bulk Patch"
description = "Patch two Patchpanels' Rear Ports equivalently, "
"i.e. patch port i to port i"
patchpanel_a = ObjectVar(
label = "Patchpanel A",
queryset = Device.objects.filter(
device_role__in = DeviceRole.objects.filter(name__contains =
"Patchpanel")
)
)
patchpanel_b = ObjectVar(
label = "Patchpanel B",
queryset = Device.objects.filter(
device_role__in = DeviceRole.objects.filter(name__contains =
"Patchpanel")
)
)
def run(self, data):
patchpanel_a = data["patchpanel_a"]
patchpanel_b = data["patchpanel_b"]
if (patchpanel_a == patchpanel_b):
self.log_failure("Cannot connect patchpanel to itself")
return
patchpanel_a_rear_ports = RearPort.objects.filter(device =
patchpanel_a)
patchpanel_b_rear_ports = RearPort.objects.filter(device =
patchpanel_b)
if (len(patchpanel_a_rear_ports) != len(patchpanel_b_rear_ports)):
self.log_failure("Number of rear ports not matching")
return
for i in range(0, len(patchpanel_a_rear_ports)):
try:
conn = Cable(termination_a = patchpanel_a_rear_ports[i],
termination_b = patchpanel_b_rear_ports[i])
conn.save()
self.log_info("Connected {} to {}".format(
patchpanel_a_rear_ports[i].name,
patchpanel_b_rear_ports[i].name))
except:
self.log_failure("Failed to connect {} to {}".format(
patchpanel_a_rear_ports[i].name,
patchpanel_b_rear_ports[i].name))
self.log_success("Connected rear ports")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment