diff --git a/app/api/network.py b/app/api/network.py index 0a0b7bf43bc36e2b595e095141686c99ffdc39c1..10902ad63dcf795aa913b7742b8687e4aa40fc0d 100644 --- a/app/api/network.py +++ b/app/api/network.py @@ -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')) diff --git a/app/models.py b/app/models.py index fa52ac24a0c84f865d4920b396e5ad17bcc926c0..c6f16ba08264c9ebb16132be54421259ef8a3bc1 100644 --- a/app/models.py +++ b/app/models.py @@ -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 diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 708fc7bacb896f9d1cb50492e4540a710e029f8f..889a113924d4afe2e9c92ad9a94faea3e9c1872d 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -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)