Improve Interface validation

This commit is contained in:
Jeremy Stretch 2021-03-29 10:34:31 -04:00
parent 7396975500
commit 2c9b791b85

View File

@ -141,7 +141,9 @@ class CableTermination(models.Model):
super().clean() super().clean()
if self.mark_connected and self.cable_id: if self.mark_connected and self.cable_id:
raise ValidationError("Cannot set mark_connected with a cable connected.") raise ValidationError({
"mark_connected": "Cannot mark as connected with a cable attached."
})
def get_cable_peer(self): def get_cable_peer(self):
return self._cable_peer return self._cable_peer
@ -596,12 +598,15 @@ class Interface(ComponentModel, BaseInterface, CableTermination, PathEndpoint):
super().clean() super().clean()
# Virtual interfaces cannot be connected # Virtual interfaces cannot be connected
if self.type in NONCONNECTABLE_IFACE_TYPES and ( if not self.is_connectable and self.cable:
self.cable or getattr(self, 'circuit_termination', False)
):
raise ValidationError({ raise ValidationError({
'type': "Virtual and wireless interfaces cannot be connected to another interface or circuit. " 'type': f"{self.get_type_display()} interfaces cannot have a cable attached."
"Disconnect the interface or choose a suitable type." })
# Non-connectable interfaces cannot be marked as connected
if not self.is_connectable and self.mark_connected:
raise ValidationError({
'mark_connected': f"{self.get_type_display()} interfaces cannot be marked as connected."
}) })
# An interface's parent must belong to the same device or virtual chassis # An interface's parent must belong to the same device or virtual chassis