Skip to content

Commit

Permalink
Add get_ip_by_desc_and_subnet function (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
djothi authored Oct 17, 2019
1 parent e97956f commit 885b039
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ipam/client/abstractipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def get_ip_list_by_desc(self, description):
def get_ip_by_desc(self, description):
raise NotImplementedError()

@abstractmethod
def get_ip_by_desc_and_subnet(self, description, subnet_description):
raise NotImplementedError()

@abstractmethod
def get_ip_list_by_mac(self, mac):
raise NotImplementedError()
Expand Down
27 changes: 27 additions & 0 deletions ipam/client/backends/phpipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,33 @@ def get_ip_by_desc(self, description):
else:
return iplist[0]

def get_ip_by_desc_and_subnet(self, description, subnet):
self.cur.execute("SELECT id \
FROM subnets \
WHERE subnet = '%i'" % int(ip_address((subnet))))
row = self.cur.fetchone()
if row is not None:
subnet_id = int(row[0])
else:
raise ValueError(
"Unable to get subnet id from database "
"for this subnet: {}".format(subnet)
)

self.cur.execute("SELECT ip_addr \
FROM ipaddresses \
WHERE description LIKE '%s' AND subnetId = '%i'"
% (description, subnet_id))

row = self.cur.fetchone()
if row is not None:
return ip_address(int(row[0]))

raise ValueError(
"Unable to get {} in the subnet {} from database".format(
description, subnet)
)

def get_ip_list_by_mac(self, mac):
self.cur.execute("SELECT ip_addr,description,%s,state,mac \
FROM ipaddresses \
Expand Down
20 changes: 20 additions & 0 deletions ipam/client/tests/test_phpipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,26 @@ def test_get_ip_by_desc(testphpipam):
assert testip['state'] == testphpipam.used_ip_state


def test_get_ip_by_desc_and_subnet(testphpipam):
with pytest.raises(
ValueError,
match='Unable to get subnet id from database for this subnet: '
'10.0.0.0'
):
testphpipam.get_ip_by_desc_and_subnet('unknown ip', '10.0.0.0')

testip = testphpipam.get_ip_by_desc_and_subnet(
'test ip #2', '10.1.0.0'
)
assert testip == ip_address('10.1.0.2')

with pytest.raises(
ValueError,
match='Unable to get unknown ip in the subnet 10.1.0.0'
):
testphpipam.get_ip_by_desc_and_subnet('unknown ip', '10.1.0.0')


def test_get_ip_interface_by_desc(testphpipam):
assert testphpipam.get_ip_by_desc('unknown ip') is None

Expand Down

0 comments on commit 885b039

Please sign in to comment.