Skip to content
Snippets Groups Projects
Commit ab95b1aa authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add extra validator on interface name

The interface name shall start with the hostname of the host it is
associated to.

Note that this is only enforced via the UI (from which most users will interact),
not from the API.

Fixes INFRA-195
parent f627e2c9
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,21 @@ from ..validators import (Unique, RegexpList, IPNetwork, HOST_NAME_RE,
from .. import utils, models
def starts_with_hostname(form, field):
"""Check that interface name starts with hostname"""
try:
# Create / Edit interface form
host_id_field = form['host_id']
except KeyError:
# Create host form
hostname = form['name'].data
else:
host = models.Host.query.get(host_id_field.data)
hostname = host.name
if not field.data.startswith(hostname):
raise validators.ValidationError(f'Interface name shall start with the hostname "{hostname}"')
def validate_tags(form, field):
choices = dict(field.choices)
for tag_id in field.data:
......@@ -115,7 +130,8 @@ class InterfaceForm(CSEntryForm):
description='name must be 2-20 characters long and contain only letters, numbers and dash',
validators=[validators.InputRequired(),
validators.Regexp(HOST_NAME_RE),
Unique(models.Interface)],
Unique(models.Interface),
starts_with_hostname],
filters=[utils.lowercase_field])
mac_address = StringField(
'MAC',
......
......@@ -48,6 +48,10 @@ $(document).ready(function() {
$("#interface_name").val(hostname);
});
// Force the first interface to have the hostname as name
// This only applies to the hostForm (not when editing or adding interfaces)
$("#hostForm input[name=interface_name]").prop("readonly", true);
var hosts_table = $("#hosts_table").DataTable({
"ajax": function(data, callback, settings) {
$.getJSON(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment