diff --git a/ipam/client/abstractipam.py b/ipam/client/abstractipam.py index d3425bb..aff6b1d 100644 --- a/ipam/client/abstractipam.py +++ b/ipam/client/abstractipam.py @@ -27,19 +27,19 @@ def get_description_by_ip(self, ip): raise NotImplementedError() @abstractmethod - def get_ipnetwork_list_by_desc(self, description): + def get_ip_interface_list_by_desc(self, description): raise NotImplementedError() @abstractmethod - def get_ipnetwork_list_by_subnet_name(self, subnet_name): + def get_ip_interface_list_by_subnet_name(self, subnet_name): raise NotImplementedError() @abstractmethod - def get_ipnetwork_by_subnet_name(self, subnet_name): + def get_ip_interface_by_subnet_name(self, subnet_name): raise NotImplementedError() @abstractmethod - def get_ipnetwork_by_desc(self, description): + def get_ip_interface_by_desc(self, description): raise NotImplementedError() @abstractmethod diff --git a/ipam/client/backends/phpipam.py b/ipam/client/backends/phpipam.py index 8f90c6f..ef01483 100644 --- a/ipam/client/backends/phpipam.py +++ b/ipam/client/backends/phpipam.py @@ -1,9 +1,8 @@ -#!/usr/bin/env python - +from __future__ import unicode_literals import mysql.connector import sqlite3 from ipam.client.abstractipam import AbstractIPAM -from netaddr import IPAddress, IPNetwork +from ipaddress import ip_address, ip_interface, ip_network DEFAULT_IPAM_DB_TYPE = 'mysql' @@ -50,9 +49,20 @@ def get_section_id(self): return self.section_id def find_subnet_id(self, subnet): + """ + Return subnet id from database + """ + if hasattr(subnet, 'network'): + # This is an interface + network = subnet.network + else: + # This is a subnet + network = subnet + self.cur.execute("SELECT id FROM subnets WHERE subnet='%d' \ AND mask='%d'" - % (subnet.network, subnet.prefixlen)) + % (network.network_address, + network.prefixlen)) row = self.cur.fetchone() if row is not None: return int(row[0]) @@ -60,12 +70,13 @@ def find_subnet_id(self, subnet): def add_ip(self, ipaddress, dnsname, description): """ Adds an IP address in IPAM. ipaddress must be an - instance of IPNetwork. Returns True """ + instance of ip_interface. Returns True """ subnetid = self.find_subnet_id(ipaddress) if subnetid is None: raise ValueError("Unable to get subnet id from database " "for subnet %s/%s" - % (ipaddress.network, ipaddress.prefixlen)) + % (ipaddress.network.network_address, + ipaddress.network.prefixlen)) self.cur.execute("SELECT ip_addr FROM ipaddresses \ WHERE ip_addr='%d' AND subnetId=%d" % (ipaddress.ip, subnetid)) @@ -83,7 +94,7 @@ def add_ip(self, ipaddress, dnsname, description): def add_next_ip(self, subnet, dnsname, description): """ Finds next free ip in subnet, and adds it in IPAM. - Returns IP address as IPNetwork """ + Returns IP address as ip_interface """ try: ipaddress = self.get_next_free_ip(subnet) self.add_ip(ipaddress, dnsname, description) @@ -93,24 +104,21 @@ def add_next_ip(self, subnet, dnsname, description): subnet, str(e))) def get_next_free_ip(self, subnet): - """ Finds next free ip in subnet. Returns IP address as IPNetwork """ + """ + Finds next free ip in subnet. Returns IP address as ip_interface + """ # Find PHPIPAM subnet id subnetid = self.find_subnet_id(subnet) if subnetid is None: raise ValueError("Unable to get subnet id from database " "for subnet %s/%s" - % (subnet.network, subnet.prefixlen)) + % (subnet.network_address, + subnet.prefixlen)) # Create hosts list in subnet - subnetips = subnet.iter_hosts() + subnetips = subnet.hosts() # Get allocated ip addresses from database usedips = self.get_allocated_ips_by_subnet_id(subnetid) - # Dirty hack, as netaddr has no support for RFC 6164 /127 subnets - # https://github.com/drkjam/netaddr/pull/168 - if subnet.prefixlen == 127: - subnetips = list(subnetips) - subnetips.append(subnet.network) - subnetips = set(subnetips) usedips = set(usedips) # Compute the difference between sets, aka available ip addresses set @@ -120,26 +128,29 @@ def get_next_free_ip(self, subnet): availableips.sort() if len(availableips) <= 0: raise ValueError("Subnet %s/%s is full" - % (subnet.network, subnet.prefixlen)) + % (subnet.network_address, + subnet.prefixlen)) # Return first available ip address in the list - return IPNetwork("%s/%d" % (availableips[0], subnet.prefixlen)) + return ip_interface("%s/%d" % (availableips[0], + subnet.prefixlen)) def get_allocated_ips_by_subnet_id(self, subnetid): self.cur.execute("SELECT ip_addr FROM ipaddresses \ WHERE subnetId=%d ORDER BY ip_addr ASC" % (subnetid)) - iplist = [IPAddress(int(ip[0])) for ip in self.cur] + iplist = [ip_address(int(ip[0])) for ip in self.cur] return iplist def delete_ip(self, ipaddress): """Delete an IP address in IPAM. ipaddress must be an - instance of IPNetwork with correct prefix length. + instance of ip_interface with correct prefix length. """ subnetid = self.find_subnet_id(ipaddress) if subnetid is None: raise ValueError("Unable to get subnet id from database " "for subnet %s/%s" - % (ipaddress.network, ipaddress.prefixlen)) + % (ipaddress.network.network_address, + ipaddress.network.prefixlen)) self.cur.execute("SELECT ip_addr FROM ipaddresses \ WHERE ip_addr='%d' AND subnetId=%d" % (ipaddress.ip, subnetid)) @@ -172,6 +183,12 @@ def get_description_by_ip(self, ip): return None def get_ipnetwork_list_by_desc(self, description): + """ + Wrapper for backward compatibility + """ + return self.get_ip_interface_list_by_desc(description) + + def get_ip_interface_list_by_desc(self, description): self.cur.execute("SELECT ip.ip_addr,ip.description,ip.dns_name,\ s.mask,s.description,v.number\ FROM ipaddresses ip\ @@ -185,8 +202,8 @@ def get_ipnetwork_list_by_desc(self, description): iplist = list() for row in self.cur: item = {} - ip_address = IPAddress(int(row[0])) - item['ip'] = IPNetwork(str(ip_address) + "/" + row[3]) + net_ip_address = ip_address(int(row[0])) + item['ip'] = ip_interface(str(net_ip_address) + "/" + row[3]) item['description'] = row[1] item['dnsname'] = row[2] item['subnet_name'] = row[4] @@ -195,13 +212,25 @@ def get_ipnetwork_list_by_desc(self, description): return iplist def get_ipnetwork_by_desc(self, description): - iplist = self.get_ipnetwork_list_by_desc(description) + """ + Wrapper for backward compatibility + """ + return self.get_ip_interface_by_desc(description) + + def get_ip_interface_by_desc(self, description): + iplist = self.get_ip_interface_list_by_desc(description) if iplist == []: return None else: return iplist[0] def get_ipnetwork_list_by_subnet_name(self, subnet_name): + """ + Wrapper for backward compatibility + """ + return self.get_ip_interface_list_by_subnet_name(subnet_name) + + def get_ip_interface_list_by_subnet_name(self, subnet_name): self.cur.execute("SELECT ip.ip_addr,ip.description,ip.dns_name,\ s.mask,s.description\ FROM ipaddresses ip\ @@ -213,8 +242,8 @@ def get_ipnetwork_list_by_subnet_name(self, subnet_name): iplist = list() for row in self.cur: item = {} - ip_address = IPAddress(int(row[0])) - item['ip'] = IPNetwork(str(ip_address) + "/" + row[3]) + net_ip_address = ip_address(int(row[0])) + item['ip'] = ip_interface(str(net_ip_address) + "/" + row[3]) item['description'] = row[1] item['dnsname'] = row[2] item['subnet_name'] = row[4] @@ -222,7 +251,13 @@ def get_ipnetwork_list_by_subnet_name(self, subnet_name): return iplist def get_ipnetwork_by_subnet_name(self, subnet_name): - iplist = self.get_ipnetwork_list_by_subnet_name(subnet_name) + """ + Wrapper for backward compatibility + """ + return self.get_ip_interface_by_subnet_name(subnet_name) + + def get_ip_interface_by_subnet_name(self, subnet_name): + iplist = self.get_ip_interface_list_by_subnet_name(subnet_name) if iplist == []: return None else: @@ -237,7 +272,7 @@ def get_ip_list_by_desc(self, description): iplist = list() for row in self.cur: item = {} - item['ip'] = IPAddress(int(row[0])) + item['ip'] = ip_address(int(row[0])) item['description'] = row[1] item['dnsname'] = row[2] iplist.append(item) @@ -257,8 +292,8 @@ def get_subnet_list_by_desc(self, description): netlist = list() for row in self.cur: item = {} - subnet = str(IPAddress(int(row[0]))) - item['subnet'] = IPNetwork("%s/%s" % (subnet, row[1])) + subnet = str(ip_address(int(row[0]))) + item['subnet'] = ip_network("%s/%s" % (subnet, row[1])) item['description'] = row[2] netlist.append(item) return netlist @@ -277,8 +312,8 @@ def get_subnet_by_id(self, subnetid): row = self.cur.fetchone() if row is not None: item = {} - subnet = str(IPAddress(int(row[0]))) - item['subnet'] = IPNetwork("%s/%s" % (subnet, row[1])) + subnet = str(ip_address(int(row[0]))) + item['subnet'] = ip_network("%s/%s" % (subnet, row[1])) item['description'] = row[2] return item return None diff --git a/ipam/client/tests/test_phpipam.py b/ipam/client/tests/test_phpipam.py index 857441a..05b2800 100644 --- a/ipam/client/tests/test_phpipam.py +++ b/ipam/client/tests/test_phpipam.py @@ -1,10 +1,11 @@ +from __future__ import unicode_literals import mysql.connector import os import pytest import tempfile import sqlite3 from ipam.client.backends.phpipam import PHPIPAM -from netaddr import IPAddress, IPNetwork +from ipaddress import ip_address, ip_interface, ip_network @pytest.fixture @@ -72,37 +73,37 @@ def test_get_section_id(testphpipam): def test_find_subnet_id(testphpipam): - assert testphpipam.find_subnet_id(IPNetwork('10.42.0.0/16')) is None - assert testphpipam.find_subnet_id(IPNetwork('10.2.0.0/29')) == 2 + assert testphpipam.find_subnet_id(ip_interface('10.42.0.0/16')) is None + assert testphpipam.find_subnet_id(ip_interface('10.2.0.0/29')) == 2 def test_add_ip(testphpipam): - ip = IPNetwork('10.1.0.1/28') + ip = ip_interface('10.1.0.1/28') with pytest.raises(ValueError) as excinfo: testphpipam.add_ip(ip, 'err', 'err') assert "already registered" in str(excinfo.value) - ip = IPNetwork('10.1.0.4/28') + ip = ip_interface('10.1.0.4/28') description = 'add_ip generated ip 1' dnsname = 'add_ip generated-ip-1' assert testphpipam.add_ip(ip, dnsname, description) is True - ip = IPNetwork('10.42.0.1/28') + ip = ip_interface('10.42.0.1/28') with pytest.raises(ValueError) as excinfo: testphpipam.add_ip(ip, 'err', 'err') assert "Unable to get subnet id" in str(excinfo.value) def test_add_next_ip(testphpipam): - subnet = IPNetwork('10.1.0.0/28') + subnet = ip_network('10.1.0.0/28') for i in list(range(4, 7)) + list(range(11, 15)): description = 'add_next_ip generated ip %d' % i dnsname = 'add_next_ip generated-ip-%d' % i - ipaddr = IPAddress('10.1.0.%d' % i) + ipaddr = ip_address('10.1.0.%d' % i) ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 28 + assert ip.network.prefixlen == 28 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -112,42 +113,42 @@ def test_add_next_ip(testphpipam): testphpipam.add_next_ip(subnet, 'err', 'err') assert "is full" in str(excinfo.value) - subnet = IPNetwork('10.2.0.0/29') + subnet = ip_network('10.2.0.0/29') with pytest.raises(ValueError) as excinfo: testphpipam.add_next_ip(subnet, 'err', 'err') assert "is full" in str(excinfo.value) - subnet = IPNetwork('10.3.0.0/30') + subnet = ip_network('10.3.0.0/30') description = 'add_next_ip generated ip 16' dnsname = 'add_next_ip generated-ip-16' - ipaddr = IPAddress('10.3.0.1') + ipaddr = ip_address('10.3.0.1') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 30 + assert ip.network.prefixlen == 30 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname assert testip['description'] == description - subnet = IPNetwork('10.4.0.0/31') + subnet = ip_network('10.4.0.0/31') description = 'add_next_ip generated ip 17' dnsname = 'add_next_ip generated-ip-17' - ipaddr = IPAddress('10.4.0.0') + ipaddr = ip_address('10.4.0.0') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 31 + assert ip.network.prefixlen == 31 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname assert testip['description'] == description - subnet = IPNetwork('10.5.0.0/31') + subnet = ip_network('10.5.0.0/31') description = 'add_next_ip generated ip 18' dnsname = 'add_next_ip generated-ip-18' - ipaddr = IPAddress('10.5.0.1') + ipaddr = ip_address('10.5.0.1') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 31 + assert ip.network.prefixlen == 31 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -157,13 +158,13 @@ def test_add_next_ip(testphpipam): testphpipam.add_next_ip(subnet, 'err', 'err') assert "is full" in str(excinfo.value) - subnet = IPNetwork('2001::40/125') + subnet = ip_network('2001::40/125') description = 'add_next_ip generated ip 19' dnsname = 'add_next_ip generated-ip-19' - ipaddr = IPAddress('2001::41') + ipaddr = ip_address('2001::41') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -171,10 +172,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 20' dnsname = 'add_next_ip generated-ip-20' - ipaddr = IPAddress('2001::42') + ipaddr = ip_address('2001::42') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -182,10 +183,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 21' dnsname = 'add_next_ip generated-ip-21' - ipaddr = IPAddress('2001::43') + ipaddr = ip_address('2001::43') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -193,10 +194,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 22' dnsname = 'add_next_ip generated-ip-22' - ipaddr = IPAddress('2001::44') + ipaddr = ip_address('2001::44') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -204,10 +205,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 23' dnsname = 'add_next_ip generated-ip-21' - ipaddr = IPAddress('2001::45') + ipaddr = ip_address('2001::45') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -215,10 +216,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 24' dnsname = 'add_next_ip generated-ip-22' - ipaddr = IPAddress('2001::46') + ipaddr = ip_address('2001::46') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -226,10 +227,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 25' dnsname = 'add_next_ip generated-ip-22' - ipaddr = IPAddress('2001::47') + ipaddr = ip_address('2001::47') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 125 + assert ip.network.prefixlen == 125 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -239,13 +240,13 @@ def test_add_next_ip(testphpipam): testphpipam.add_next_ip(subnet, 'err', 'err') assert "is full" in str(excinfo.value) - subnet = IPNetwork('2001::50/127') + subnet = ip_network('2001::50/127') description = 'add_next_ip generated ip 26' dnsname = 'add_next_ip generated-ip-23' - ipaddr = IPAddress('2001::50') + ipaddr = ip_address('2001::50') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 127 + assert ip.network.prefixlen == 127 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -253,10 +254,10 @@ def test_add_next_ip(testphpipam): description = 'add_next_ip generated ip 27' dnsname = 'add_next_ip generated-ip-24' - ipaddr = IPAddress('2001::51') + ipaddr = ip_address('2001::51') ip = testphpipam.add_next_ip(subnet, dnsname, description) assert ip.ip == ipaddr - assert ip.prefixlen == 127 + assert ip.network.prefixlen == 127 testip = testphpipam.get_ip_by_desc(description) assert testip['ip'] == ipaddr assert testip['dnsname'] == dnsname @@ -269,62 +270,62 @@ def test_add_next_ip(testphpipam): def test_delete_ip(testphpipam): iplist = testphpipam.get_ip_list_by_desc('test ip #2') - assert iplist == [{'ip': IPAddress('10.1.0.2'), + assert iplist == [{'ip': ip_address('10.1.0.2'), 'description': 'test ip #2', 'dnsname': 'test-ip-2'}] - testphpipam.delete_ip(IPNetwork('10.1.0.2/28')) + testphpipam.delete_ip(ip_interface('10.1.0.2/28')) iplist = testphpipam.get_ip_list_by_desc('test ip #2') assert iplist == [] with pytest.raises(ValueError): - testphpipam.delete_ip(IPNetwork('10.1.0.2/28')) + testphpipam.delete_ip(ip_interface('10.1.0.2/28')) with pytest.raises(ValueError): - testphpipam.delete_ip(IPNetwork('10.1.0.42/28')) + testphpipam.delete_ip(ip_interface('10.1.0.42/28')) def test_get_next_free_ip(testphpipam): - ip = testphpipam.get_next_free_ip(IPNetwork('10.1.0.0/28')) - assert ip.ip == IPAddress('10.1.0.4') - assert ip.prefixlen == 28 + ip = testphpipam.get_next_free_ip(ip_network('10.1.0.0/28')) + assert ip.ip == ip_address('10.1.0.4') + assert ip.network.prefixlen == 28 with pytest.raises(ValueError) as excinfo: - testphpipam.get_next_free_ip(IPNetwork('10.2.0.0/29')) + testphpipam.get_next_free_ip(ip_network('10.2.0.0/29')) assert "is full" in str(excinfo.value) - ip = testphpipam.get_next_free_ip(IPNetwork('10.3.0.0/30')) - assert ip.ip == IPAddress('10.3.0.1') - assert ip.prefixlen == 30 + ip = testphpipam.get_next_free_ip(ip_network('10.3.0.0/30')) + assert ip.ip == ip_address('10.3.0.1') + assert ip.network.prefixlen == 30 - ip = testphpipam.get_next_free_ip(IPNetwork('10.4.0.0/31')) - assert ip.ip == IPAddress('10.4.0.0') - assert ip.prefixlen == 31 + ip = testphpipam.get_next_free_ip(ip_network('10.4.0.0/31')) + assert ip.ip == ip_address('10.4.0.0') + assert ip.network.prefixlen == 31 - ip = testphpipam.get_next_free_ip(IPNetwork('10.5.0.0/31')) - assert ip.ip == IPAddress('10.5.0.1') - assert ip.prefixlen == 31 + ip = testphpipam.get_next_free_ip(ip_network('10.5.0.0/31')) + assert ip.ip == ip_address('10.5.0.1') + assert ip.network.prefixlen == 31 with pytest.raises(ValueError) as excinfo: - testphpipam.get_next_free_ip(IPNetwork('10.42.0.0/29')) + testphpipam.get_next_free_ip(ip_network('10.42.0.0/29')) assert "Unable to get subnet id from database" in str(excinfo.value) def test_get_subnet_by_id(testphpipam): assert testphpipam.get_subnet_by_id(42) is None testsubnet = testphpipam.get_subnet_by_id(3) - assert testsubnet['subnet'] == IPNetwork('10.3.0.0/30') + assert testsubnet['subnet'] == ip_network('10.3.0.0/30') assert testsubnet['description'] == 'TST /30 SUBNET' def test_get_allocated_ips_by_subnet_id(testphpipam): assert testphpipam.get_allocated_ips_by_subnet_id(4) == [] - iplist = [IPAddress('10.1.0.1'), - IPAddress('10.1.0.2'), - IPAddress('10.1.0.3'), - IPAddress('10.1.0.7'), - IPAddress('10.1.0.8'), - IPAddress('10.1.0.9'), - IPAddress('10.1.0.10')] + iplist = [ip_address('10.1.0.1'), + ip_address('10.1.0.2'), + ip_address('10.1.0.3'), + ip_address('10.1.0.7'), + ip_address('10.1.0.8'), + ip_address('10.1.0.9'), + ip_address('10.1.0.10')] assert testphpipam.get_allocated_ips_by_subnet_id(1) == iplist @@ -332,68 +333,72 @@ def test_get_ip_by_desc(testphpipam): assert testphpipam.get_ip_by_desc('unknown ip') is None testip = testphpipam.get_ip_by_desc('test ip #2') - assert testip['ip'] == IPAddress('10.1.0.2') + assert testip['ip'] == ip_address('10.1.0.2') assert testip['description'] == 'test ip #2' assert testip['dnsname'] == 'test-ip-2' testip = testphpipam.get_ip_by_desc('test ip group 1') - assert testip['ip'] == IPAddress('10.2.0.1') + assert testip['ip'] == ip_address('10.2.0.1') assert testip['description'] == 'test ip group 1' assert testip['dnsname'] == 'test-ip-8' -def test_get_ipnetwork_by_desc(testphpipam): +def test_get_ip_interface_by_desc(testphpipam): assert testphpipam.get_ip_by_desc('unknown ip') is None - testip = testphpipam.get_ipnetwork_by_desc('test ip #2') - assert testip['ip'] == IPNetwork('10.1.0.2/28') + testip = testphpipam.get_ip_interface_by_desc('test ip #2') + assert testip['ip'] == ip_interface('10.1.0.2/28') assert testip['description'] == 'test ip #2' assert testip['dnsname'] == 'test-ip-2' - testip = testphpipam.get_ipnetwork_by_desc('test ip group 1') - assert testip['ip'] == IPNetwork('10.2.0.1/29') + testip = testphpipam.get_ip_interface_by_desc('test ip group 1') + assert testip['ip'] == ip_interface('10.2.0.1/29') assert testip['description'] == 'test ip group 1' assert testip['dnsname'] == 'test-ip-8' - testip = testphpipam.get_ipnetwork_by_desc('non-existent ip') + testip = testphpipam.get_ip_interface_by_desc('non-existent ip') assert testip is None +def test_get_ipnetwork_by_desc(testphpipam): + assert testphpipam.get_ipnetwork_by_desc('unknown ip') is None + + def test_get_ip_list_by_desc(testphpipam): assert testphpipam.get_ip_list_by_desc('unknown ip') == [] iplist = testphpipam.get_ip_list_by_desc('test ip #2') - assert iplist == [{'ip': IPAddress('10.1.0.2'), + assert iplist == [{'ip': ip_address('10.1.0.2'), 'description': 'test ip #2', 'dnsname': 'test-ip-2'}] iplist = testphpipam.get_ip_list_by_desc('test ip group 1') - assert iplist == [{'ip': IPAddress('10.2.0.1'), + assert iplist == [{'ip': ip_address('10.2.0.1'), 'description': 'test ip group 1', 'dnsname': 'test-ip-8'}, - {'ip': IPAddress('10.2.0.2'), + {'ip': ip_address('10.2.0.2'), 'description': 'test ip group 1', 'dnsname': 'test-ip-9'}] -def test_get_ipnetwork_list_by_desc(testphpipam): - assert testphpipam.get_ipnetwork_list_by_desc('unknown ip') == [] +def test_get_ip_interface_list_by_desc(testphpipam): + assert testphpipam.get_ip_interface_list_by_desc('unknown ip') == [] - iplist = testphpipam.get_ipnetwork_list_by_desc('test ip #2') - assert iplist == [{'ip': IPNetwork('10.1.0.2/28'), + iplist = testphpipam.get_ip_interface_list_by_desc('test ip #2') + assert iplist == [{'ip': ip_interface('10.1.0.2/28'), 'description': 'test ip #2', 'dnsname': 'test-ip-2', 'subnet_name': 'TEST /28 SUBNET', 'vlan_id': 42 }] - iplist = testphpipam.get_ipnetwork_list_by_desc('test ip group 1') - assert iplist == [{'ip': IPNetwork('10.2.0.1/29'), + iplist = testphpipam.get_ip_interface_list_by_desc('test ip group 1') + assert iplist == [{'ip': ip_interface('10.2.0.1/29'), 'description': 'test ip group 1', 'dnsname': 'test-ip-8', 'subnet_name': 'TEST FULL /29 SUBNET', 'vlan_id': None }, - {'ip': IPNetwork('10.2.0.2/29'), + {'ip': ip_interface('10.2.0.2/29'), 'description': 'test ip group 1', 'dnsname': 'test-ip-9', 'subnet_name': 'TEST FULL /29 SUBNET', @@ -401,73 +406,143 @@ def test_get_ipnetwork_list_by_desc(testphpipam): ] -def test_get_ipnetwork_by_subnet_name(testphpipam): - ip = testphpipam.get_ipnetwork_by_subnet_name('TEST%') +def test_get_ipnetwork_list_by_desc(testphpipam): + assert testphpipam.get_ipnetwork_list_by_desc('unknown ip') == [] + + +def test_get_ip_interface_by_subnet_name(testphpipam): + ip = testphpipam.get_ip_interface_by_subnet_name('TEST%') assert ip == {'description': u'test ip #1', 'dnsname': u'test-ip-1', - 'ip': IPNetwork('10.1.0.1/28'), + 'ip': ip_interface('10.1.0.1/28'), 'subnet_name': u'TEST /28 SUBNET'} + ip = testphpipam.get_ip_interface_by_subnet_name('non-existent ip') + assert ip is None + + +def test_get_ipnetwork_by_subnet_name(testphpipam): ip = testphpipam.get_ipnetwork_by_subnet_name('non-existent ip') assert ip is None +def test_get_ip_interface_list_by_subnet_name(testphpipam): + iplist = testphpipam.get_ip_interface_list_by_subnet_name('TEST%') + assert iplist == [{'description': u'test ip #1', + 'dnsname': u'test-ip-1', + 'ip': ip_interface('10.1.0.1/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #2', + 'dnsname': u'test-ip-2', + 'ip': ip_interface('10.1.0.2/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #3', + 'dnsname': u'test-ip-3', + 'ip': ip_interface('10.1.0.3/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #4', + 'dnsname': u'test-ip-4', + 'ip': ip_interface('10.1.0.7/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #5', + 'dnsname': u'test-ip-5', + 'ip': ip_interface('10.1.0.8/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #6', + 'dnsname': u'test-ip-6', + 'ip': ip_interface('10.1.0.9/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip #7', + 'dnsname': u'test-ip-7', + 'ip': ip_interface('10.1.0.10/28'), + 'subnet_name': u'TEST /28 SUBNET'}, + {'description': u'test ip group 1', + 'dnsname': u'test-ip-8', + 'ip': ip_interface('10.2.0.1/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip group 1', + 'dnsname': u'test-ip-9', + 'ip': ip_interface('10.2.0.2/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip #10', + 'dnsname': u'test-ip-10', + 'ip': ip_interface('10.2.0.3/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip #11', + 'dnsname': u'test-ip-11', + 'ip': ip_interface('10.2.0.4/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip #12', + 'dnsname': u'test-ip-12', + 'ip': ip_interface('10.2.0.5/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip #13', + 'dnsname': u'test-ip-13', + 'ip': ip_interface('10.2.0.6/29'), + 'subnet_name': u'TEST FULL /29 SUBNET'}, + {'description': u'test ip #15', + 'dnsname': u'test-ip-15', + 'ip': ip_interface('10.5.0.0/31'), + 'subnet_name': u'TEST /31 SUBNET GROUP'}, + ] + + def test_get_ipnetwork_list_by_subnet_name(testphpipam): iplist = testphpipam.get_ipnetwork_list_by_subnet_name('TEST%') assert iplist == [{'description': u'test ip #1', 'dnsname': u'test-ip-1', - 'ip': IPNetwork('10.1.0.1/28'), + 'ip': ip_interface('10.1.0.1/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #2', 'dnsname': u'test-ip-2', - 'ip': IPNetwork('10.1.0.2/28'), + 'ip': ip_interface('10.1.0.2/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #3', 'dnsname': u'test-ip-3', - 'ip': IPNetwork('10.1.0.3/28'), + 'ip': ip_interface('10.1.0.3/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #4', 'dnsname': u'test-ip-4', - 'ip': IPNetwork('10.1.0.7/28'), + 'ip': ip_interface('10.1.0.7/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #5', 'dnsname': u'test-ip-5', - 'ip': IPNetwork('10.1.0.8/28'), + 'ip': ip_interface('10.1.0.8/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #6', 'dnsname': u'test-ip-6', - 'ip': IPNetwork('10.1.0.9/28'), + 'ip': ip_interface('10.1.0.9/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip #7', 'dnsname': u'test-ip-7', - 'ip': IPNetwork('10.1.0.10/28'), + 'ip': ip_interface('10.1.0.10/28'), 'subnet_name': u'TEST /28 SUBNET'}, {'description': u'test ip group 1', 'dnsname': u'test-ip-8', - 'ip': IPNetwork('10.2.0.1/29'), + 'ip': ip_interface('10.2.0.1/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip group 1', 'dnsname': u'test-ip-9', - 'ip': IPNetwork('10.2.0.2/29'), + 'ip': ip_interface('10.2.0.2/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip #10', 'dnsname': u'test-ip-10', - 'ip': IPNetwork('10.2.0.3/29'), + 'ip': ip_interface('10.2.0.3/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip #11', 'dnsname': u'test-ip-11', - 'ip': IPNetwork('10.2.0.4/29'), + 'ip': ip_interface('10.2.0.4/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip #12', 'dnsname': u'test-ip-12', - 'ip': IPNetwork('10.2.0.5/29'), + 'ip': ip_interface('10.2.0.5/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip #13', 'dnsname': u'test-ip-13', - 'ip': IPNetwork('10.2.0.6/29'), + 'ip': ip_interface('10.2.0.6/29'), 'subnet_name': u'TEST FULL /29 SUBNET'}, {'description': u'test ip #15', 'dnsname': u'test-ip-15', - 'ip': IPNetwork('10.5.0.0/31'), + 'ip': ip_interface('10.5.0.0/31'), 'subnet_name': u'TEST /31 SUBNET GROUP'}, ] @@ -475,22 +550,22 @@ def test_get_ipnetwork_list_by_subnet_name(testphpipam): def test_get_subnet_by_desc(testphpipam): assert testphpipam.get_subnet_by_desc('unknown subnet') is None subnet = testphpipam.get_subnet_by_desc('TEST /28 SUBNET') - assert subnet['subnet'] == IPNetwork('10.1.0.0/28') + assert subnet['subnet'] == ip_network('10.1.0.0/28') assert subnet['description'] == 'TEST /28 SUBNET' subnet = testphpipam.get_subnet_by_desc('TEST /31 SUBNET GROUP') - assert subnet['subnet'] == IPNetwork('10.4.0.0/31') + assert subnet['subnet'] == ip_network('10.4.0.0/31') assert subnet['description'] == 'TEST /31 SUBNET GROUP' def test_get_subnet_list_by_desc(testphpipam): assert testphpipam.get_subnet_list_by_desc('unknown subnet') == [] subnetlist = testphpipam.get_subnet_list_by_desc('TEST /28 SUBNET') - assert subnetlist == [{'subnet': IPNetwork('10.1.0.0/28'), + assert subnetlist == [{'subnet': ip_network('10.1.0.0/28'), 'description': 'TEST /28 SUBNET'}] subnetlist = testphpipam.get_subnet_list_by_desc('TEST /31 SUBNET GROUP') - assert subnetlist == [{'subnet': IPNetwork('10.4.0.0/31'), + assert subnetlist == [{'subnet': ip_network('10.4.0.0/31'), 'description': 'TEST /31 SUBNET GROUP'}, - {'subnet': IPNetwork('10.5.0.0/31'), + {'subnet': ip_network('10.5.0.0/31'), 'description': 'TEST /31 SUBNET GROUP'}] @@ -507,11 +582,14 @@ def test_get_num_subnets_by_desc(testphpipam): def test_get_hostname_by_ip(testphpipam): - assert testphpipam.get_hostname_by_ip(IPAddress('1.1.1.1')) is None - assert testphpipam.get_hostname_by_ip(IPAddress('10.2.0.2')) == 'test-ip-9' + assert testphpipam.get_hostname_by_ip( + ip_address('1.1.1.1')) is None + assert testphpipam.get_hostname_by_ip( + ip_address('10.2.0.2')) == 'test-ip-9' def test_get_description_by_ip(testphpipam): - assert testphpipam.get_description_by_ip(IPAddress('1.1.1.1')) is None - assert testphpipam.get_description_by_ip(IPAddress('10.2.0.2')) == 'test' \ - + ' ip group 1' + assert testphpipam.get_description_by_ip( + ip_address('1.1.1.1')) is None + assert testphpipam.get_description_by_ip( + ip_address('10.2.0.2')) == 'test ip group 1' diff --git a/requirements.txt b/requirements.txt index f2c5df9..80b87ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -netaddr==0.7.19 +ipaddress==1.0.19 mysql-connector-python==8.0.5