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

Use the last IP as network gateway

"gateway" tag removed. It could be re-added if needed to overwrite the
default computed gateway for example.

JIRA INFRA-339
parent 9e8fe489
No related branches found
No related tags found
No related merge requests found
...@@ -85,9 +85,3 @@ class NetworkAdmin(AdminModelView): ...@@ -85,9 +85,3 @@ class NetworkAdmin(AdminModelView):
form_overrides = { form_overrides = {
'vlan_name': fields.StringField, 'vlan_name': fields.StringField,
} }
form_args = {
'gateway': {
'filters': [lambda x: x or None],
},
}
...@@ -28,6 +28,5 @@ defaults = [ ...@@ -28,6 +28,5 @@ defaults = [
models.DeviceType(name='VME'), models.DeviceType(name='VME'),
models.DeviceType(name='PLC'), models.DeviceType(name='PLC'),
models.Tag(name='gateway', admin_only=True),
models.Tag(name='IOC', admin_only=False), models.Tag(name='IOC', admin_only=False),
] ]
...@@ -511,12 +511,10 @@ class Network(CreatedMixin, db.Model): ...@@ -511,12 +511,10 @@ class Network(CreatedMixin, db.Model):
return [addr for addr in self.ip_range() return [addr for addr in self.ip_range()
if addr not in self.used_ips()] if addr not in self.used_ips()]
@property
def gateway(self): def gateway(self):
"""Return the network gateway""" """Return the network gateway IP"""
for interface in self.interfaces: return list(self.network_ip.hosts())[-1]
if 'gateway' in [tag.name for tag in interface.tags]:
return interface
return None
@staticmethod @staticmethod
def ip_in_network(ip, address): def ip_in_network(ip, address):
......
...@@ -34,17 +34,6 @@ def starts_with_hostname(form, field): ...@@ -34,17 +34,6 @@ def starts_with_hostname(form, field):
raise validators.ValidationError(f'Interface name shall start with the hostname "{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:
tag_name = choices.get(tag_id)
if tag_name == 'gateway':
network = models.Network.query.get(form.network_id.data)
existing_gateway = network.gateway()
if existing_gateway is not None:
raise validators.ValidationError(f'A gateway is already defined for network {network}: {existing_gateway}')
def ip_in_network(form, field): def ip_in_network(form, field):
"""Check that the IP is in the network""" """Check that the IP is in the network"""
network_id_field = form['network_id'] network_id_field = form['network_id']
...@@ -149,8 +138,7 @@ class InterfaceForm(CSEntryForm): ...@@ -149,8 +138,7 @@ class InterfaceForm(CSEntryForm):
description='space separated list of cnames (must be 2-20 characters long and contain only letters, numbers and dash)', description='space separated list of cnames (must be 2-20 characters long and contain only letters, numbers and dash)',
validators=[validators.Optional(), validators=[validators.Optional(),
RegexpList(HOST_NAME_RE)]) RegexpList(HOST_NAME_RE)])
tags = SelectMultipleField('Tags', coerce=utils.coerce_to_str_or_none, tags = SelectMultipleField('Tags', coerce=utils.coerce_to_str_or_none)
validators=[validate_tags])
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
......
...@@ -94,6 +94,13 @@ def test_network_available_and_used_ips(network_factory, interface_factory): ...@@ -94,6 +94,13 @@ def test_network_available_and_used_ips(network_factory, interface_factory):
assert list(network2.available_ips()) == [] assert list(network2.available_ips()) == []
def test_network_gateway(network_factory):
network = network_factory(address='192.168.0.0/24')
assert str(network.gateway) == '192.168.0.254'
network = network_factory(address='172.16.110.0/23')
assert str(network.gateway) == '172.16.111.254'
def test_mac_address_validation(mac_factory): def test_mac_address_validation(mac_factory):
mac = mac_factory(address='F4:A7:39:15:DA:01') mac = mac_factory(address='F4:A7:39:15:DA:01')
assert mac.address == 'f4:a7:39:15:da:01' assert mac.address == 'f4:a7:39:15:da:01'
......
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