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

Fix inconsistency in interface creation

The string representation of a Network is the vlan_name.
This is what we should allow to pass as string to create an interface.

Note that if we pass network_id instead of network when creating
and interface, the validate_interfaces method from the Network class
is not called.
parent 0efae3a3
No related branches found
No related tags found
No related merge requests found
......@@ -62,6 +62,9 @@ def get_interfaces():
@jwt_groups_accepted('admin', 'create')
def create_interface():
"""Create a new interface"""
# The validate_interfaces method from the Network class is called when
# setting interface.network. This is why we don't pass network_id here
# but network (as vlan_name string)
return create_generic_model(models.Interface, mandatory_fields=('network', 'ip', 'name'))
......
......@@ -554,9 +554,9 @@ class Interface(CreatedMixin, db.Model):
def __init__(self, **kwargs):
# Automatically convert network to an instance of Network if it was passed
# as an address string
# as a string
if 'network' in kwargs:
kwargs['network'] = utils.convert_to_model(kwargs['network'], Network, 'address')
kwargs['network'] = utils.convert_to_model(kwargs['network'], Network, 'vlan_name')
# WARNING! Setting self.network will call validates_interfaces in the Network class
# For the validation to work, self.ip must be set before!
# Ensure that ip is passed before network
......
......@@ -606,7 +606,7 @@ def test_create_interface(client, network_factory, user_token):
response = post(client, f'{API_URL}/network/interfaces', data={'network': network.address}, token=user_token)
check_response_message(response, "Missing mandatory field 'ip'", 422)
data = {'network': network.address,
data = {'network': network.vlan_name,
'ip': '192.168.1.20',
'name': 'interface1'}
response = post(client, f'{API_URL}/network/interfaces', data=data, token=user_token)
......@@ -623,7 +623,7 @@ def test_create_interface(client, network_factory, user_token):
check_response_message(response, '(psycopg2.IntegrityError) duplicate key value violates unique constraint', 422)
# Check that all parameters can be passed
data2 = {'network': network.address,
data2 = {'network': network.vlan_name,
'ip': '192.168.1.21',
'name': 'myhostname'}
response = post(client, f'{API_URL}/network/interfaces', data=data2, token=user_token)
......@@ -637,7 +637,7 @@ def test_create_interface(client, network_factory, user_token):
def test_create_interface_invalid_ip(ip, client, network_factory, user_token):
network = network_factory(address='192.168.1.0/24', first_ip='192.168.1.10', last_ip='192.168.1.250')
# invalid IP address
data = {'network': network.address,
data = {'network': network.vlan_name,
'ip': ip,
'name': 'hostname'}
response = post(client, f'{API_URL}/network/interfaces', data=data, token=user_token)
......@@ -647,7 +647,7 @@ def test_create_interface_invalid_ip(ip, client, network_factory, user_token):
def test_create_interface_ip_not_in_network(client, network_factory, user_token):
network = network_factory(address='192.168.1.0/24', first_ip='192.168.1.10', last_ip='192.168.1.250')
# IP address not in range
data = {'network': network.address,
data = {'network': network.vlan_name,
'ip': '192.168.2.4',
'name': 'hostname'}
response = post(client, f'{API_URL}/network/interfaces', data=data, token=user_token)
......
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