Skip to content

Commit 29e5676

Browse files
author
Johannes Erwerle
committed
changed net to fix issue #1
- get_net can now be queried with networks which are not in the database - get_net returns a dictionary with the "network_in_database" key set to False. (If it is in the DB this key is True) - get_net does no longer raise a Fault when a Network is not in the DB but returns a dictionary with the "network_in_database" key set to False and an empty list of children.
1 parent 3a801de commit 29e5676

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

minipam/server.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def close_database_connection():
5959
def get_net(net, depth=-1):
6060
"""
6161
returns networks within the given network.
62-
This network does have to exist in the database
62+
This network does not have to exist in the database
63+
if the network does not exist in the database the "network_in_database"
64+
variable in the returned dict is False. It is true otherwise.
6365
:param net: the network (ip/subnet maks) which should be returned
6466
:param depth: how many levels of children should be added. Default is -1 (return everything)
6567
means add all children.
@@ -75,13 +77,25 @@ def get_net(net, depth=-1):
7577
network_address_to_int(network),
7678
network_broadcast_to_int(network)))
7779
results = c.fetchall()
78-
if len(results) == 0 or results[0]["net"] != str(network):
79-
raise_fault("NetworkNotInDatabase")
80+
#if len(results) == 0:
81+
# raise_fault("NetworkNotInDatabase")
8082

81-
ret = { "address" : results[0]["net"].split("/")[0],
82-
"cidr" : results[0]["net"],
83-
"netmask" : results[0]["netmask"],
84-
"children" : list()}
83+
if len(results) == 0 or results[0]["net"] != str(network):
84+
ret = { "address": network.network_address,
85+
"cidr" : str(network),
86+
"netmask": network.prefixlen,
87+
"children": list(),
88+
"network_in_database": False}
89+
start = 0
90+
if len(results) == 0:
91+
return ret
92+
else:
93+
ret = { "address" : results[0]["net"].split("/")[0],
94+
"cidr" : results[0]["net"],
95+
"netmask" : results[0]["netmask"],
96+
"children" : list(),
97+
"network_in_database": True}
98+
start = 1
8599

86100
def insert_in_netlist(ip_net, netlist, remaining_depth):
87101
"""
@@ -99,10 +113,11 @@ def insert_in_netlist(ip_net, netlist, remaining_depth):
99113
netlist.append({"address" : n["net"].split("/")[0],
100114
"cidr": n["net"],
101115
"netmask" : n["netmask"],
102-
"children" : list()})
116+
"children" : list(),
117+
"network_in_database" : True})
103118

104119
if depth != 0:
105-
for n in results[1:]:
120+
for n in results[start:]:
106121
n_net = ip_network(n["net"])
107122
insert_in_netlist(n_net, ret["children"], depth-1)
108123

test/net_methods.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,21 @@ def test_get_net_host_bits_set(self):
7676
self.assertEqual(cm.exception.faultString, "InvalidNetworkDescription")
7777

7878
def test_get_net_not_in_db(self):
79-
with self.assertRaises(Fault) as cm:
80-
get_net("10.0.0.0/8")
81-
self.assertEqual(cm.exception.faultString, "NetworkNotInDatabase")
79+
result = get_net("0.0.0.0/0")
80+
self.assertEqual(result["cidr"] , "0.0.0.0/0")
81+
self.assertEqual(result["network_in_database"] , False)
82+
self.assertEqual(len(result["children"]), 1)
83+
self.assertEqual(result["children"][0]["address"], "127.0.0.0")
84+
self.assertEqual(result["children"][0]["netmask"], 8)
85+
self.assertEqual(len(result["children"][0]["children"]), 1)
86+
87+
def test_get_net_not_in_db_and_empty(self):
88+
result = get_net("128.0.0.0/8")
89+
self.assertEqual(result["cidr"], "128.0.0.0/8")
90+
self.assertEqual(result["network_in_database"], False)
91+
self.assertEqual(len(result["children"]), 0)
92+
93+
8294

8395
class TestDeleteNetMethods(unittest.TestCase):
8496
def setUp(self):
@@ -107,9 +119,8 @@ def test_delete_net_normal(self):
107119

108120
def test_delete_net_recursive(self):
109121
delete_net("127.0.0.0/8", recursive=True)
110-
with self.assertRaises(Fault) as cm:
111-
get_net("127.0.0.0/8")
112-
self.assertEqual(cm.exception.faultString, "NetworkNotInDatabase")
122+
result = get_net("127.0.0.0/8")
123+
self.assertFalse(result["network_in_database"])
113124

114125
def test_delete_net_inner(self):
115126
delete_net("127.0.0.0/16")

0 commit comments

Comments
 (0)