From 23448e022cd287dcfac32acc09657ee0901c615c Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Fri, 23 Feb 2018 10:42:55 +0100
Subject: [PATCH] Add netmask field in network json representation

---
 app/models.py                | 5 +++++
 tests/functional/test_api.py | 9 +++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/app/models.py b/app/models.py
index 4478aa1..53e008f 100644
--- a/app/models.py
+++ b/app/models.py
@@ -402,6 +402,10 @@ class Network(CreatedMixin, db.Model):
     def network_ip(self):
         return ipaddress.ip_network(self.address)
 
+    @property
+    def netmask(self):
+        return self.network_ip.netmask
+
     @property
     def first(self):
         return ipaddress.ip_address(self.first_ip)
@@ -490,6 +494,7 @@ class Network(CreatedMixin, db.Model):
             'vlan_name': self.vlan_name,
             'vlan_id': self.vlan_id,
             'address': self.address,
+            'netmask': str(self.netmask),
             'first_ip': self.first_ip,
             'last_ip': self.last_ip,
             'description': self.description,
diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py
index e81cd86..6954f28 100644
--- a/tests/functional/test_api.py
+++ b/tests/functional/test_api.py
@@ -463,15 +463,16 @@ def test_create_network(client, admin_token, network_scope_factory):
             'scope': scope.name}
     response = post(client, f'{API_URL}/network/networks', data=data, token=admin_token)
     assert response.status_code == 201
-    assert {'id', 'vlan_name', 'vlan_id', 'address', 'first_ip',
-            'last_ip', 'description', 'admin_only', 'scope',
-            'domain', 'interfaces', 'created_at', 'updated_at',
-            'user'} == set(response.json.keys())
+    assert {'id', 'vlan_name', 'vlan_id', 'address', 'netmask',
+            'first_ip', 'last_ip', 'description', 'admin_only',
+            'scope', 'domain', 'interfaces', 'created_at',
+            'updated_at', 'user'} == set(response.json.keys())
     assert response.json['vlan_name'] == 'network1'
     assert response.json['vlan_id'] == 1600
     assert response.json['address'] == '172.16.1.0/24'
     assert response.json['first_ip'] == '172.16.1.10'
     assert response.json['last_ip'] == '172.16.1.250'
+    assert response.json['netmask'] == '255.255.255.0'
 
     # Check that address and name shall be unique
     response = post(client, f'{API_URL}/network/networks', data=data, token=admin_token)
-- 
GitLab