Skip to content

Commit

Permalink
make ip state compatible with newer PHPIPAM versions (#35)
Browse files Browse the repository at this point in the history
* `state` field in ipaddresses table is default to 2 (instead of 1) in newer versions
  • Loading branch information
fdomain authored and dclaisse committed Oct 7, 2019
1 parent a7e9dd2 commit e97956f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
22 changes: 14 additions & 8 deletions ipam/client/backends/phpipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, params):
dbtype = DEFAULT_IPAM_DB_TYPE
subnet_options = DEFAULT_SUBNET_OPTIONS.copy()
self.hostname_db_field = 'hostname'
self.used_ip_state = 2
for (option, value) in DEFAULT_SUBNET_OPTIONS.items():
param_name = 'subnet_{}'.format(option)
if params.get(param_name):
Expand Down Expand Up @@ -84,6 +85,7 @@ def __init__(self, params):
if self._get_version() < 1.32:
# Older PHPIPAM version use `dns_name` field instead of `hostname`
self.hostname_db_field = 'dns_name'
self.used_ip_state = 1

def set_section_id(self, section_id):
self.section_id = section_id
Expand Down Expand Up @@ -495,8 +497,8 @@ def get_ip(self, ip):
LEFT JOIN vlans v ON\
s.vlanId = v.vlanId\
WHERE ip.ip_addr = '%d'\
AND ip.state = 1"
% (self.hostname_db_field, ip))
AND ip.state = %d"
% (self.hostname_db_field, ip, self.used_ip_state))
row = self.cur.fetchone()
if row is not None:
item = {}
Expand Down Expand Up @@ -552,8 +554,10 @@ def get_ip_interface_list_by_desc(self, description):
LEFT JOIN vlans v ON\
s.vlanId = v.vlanId\
WHERE ip.description LIKE '%s'\
AND ip.state = 1"
% (self.hostname_db_field, description))
AND ip.state = %d"
% (self.hostname_db_field,
description,
self.used_ip_state))
iplist = list()
for row in self.cur:
item = {}
Expand Down Expand Up @@ -624,8 +628,10 @@ def get_ip_interface_list_by_subnet_name(self, subnet_name):
LEFT JOIN subnets s ON\
ip.subnetId = s.id\
WHERE s.description LIKE '%s'\
AND ip.state = 1"
% (self.hostname_db_field, subnet_name))
AND ip.state = %d"
% (self.hostname_db_field,
subnet_name,
self.used_ip_state))
iplist = list()
for row in self.cur:
item = {}
Expand Down Expand Up @@ -753,8 +759,8 @@ def get_subnet_by_id(self, subnetid):
def get_num_ips_by_desc(self, description):
self.cur.execute("SELECT COUNT(ip_addr) FROM ipaddresses \
WHERE description LIKE '%s'\
AND state = '1'"
% (description))
AND state = %d"
% (description, self.used_ip_state))
row = self.cur.fetchone()
return int(row[0])

Expand Down
32 changes: 16 additions & 16 deletions ipam/client/tests/data/db-recent-version.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ CREATE TABLE "ipaddresses" (
"hostname" varchar(64) NOT NULL,
"mac" varchar(20) DEFAULT NULL,
"owner" varchar(32) DEFAULT NULL,
"state" varchar(1) DEFAULT '1',
"state" varchar(1) DEFAULT '2',
"switch" int(11) DEFAULT NULL,
"port" varchar(32) DEFAULT NULL,
"note" text,
"lastSeen" datetime DEFAULT '0000-00-00 00:00:00',
"excludePing" binary(1) DEFAULT '0',
"editDate" timestamp DEFAULT NULL
);
INSERT INTO "ipaddresses" VALUES (1,1,'167837697','test ip #1','test-ip-1','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (2,1,'167837698','test ip #2','test-ip-2','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (3,1,'167837699','test ip #3','test-ip-3','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (4,1,'167837703','test ip #4','test-ip-4','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (5,1,'167837704','test ip #5','test-ip-5','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (6,1,'167837705','test ip #6','test-ip-6','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (7,1,'167837706','test ip #7','test-ip-7','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (8,2,'167903233','test ip group 1','test-ip-8','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (9,2,'167903234','test ip group 1','test-ip-9','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (10,2,'167903235','test ip #10','test-ip-10','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (11,2,'167903236','test ip #11','test-ip-11','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (12,2,'167903237','test ip #12','test-ip-12','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (13,2,'167903238','test ip #13','test-ip-13','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (14,3,'167968770','test ip #14','test-ip-14','','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (15,5,'168099840','test ip #15','test-ip-15','52:24:10:00:00:02','','1',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (1,1,'167837697','test ip #1','test-ip-1','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (2,1,'167837698','test ip #2','test-ip-2','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (3,1,'167837699','test ip #3','test-ip-3','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (4,1,'167837703','test ip #4','test-ip-4','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (5,1,'167837704','test ip #5','test-ip-5','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (6,1,'167837705','test ip #6','test-ip-6','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (7,1,'167837706','test ip #7','test-ip-7','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (8,2,'167903233','test ip group 1','test-ip-8','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (9,2,'167903234','test ip group 1','test-ip-9','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (10,2,'167903235','test ip #10','test-ip-10','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (11,2,'167903236','test ip #11','test-ip-11','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (12,2,'167903237','test ip #12','test-ip-12','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (13,2,'167903238','test ip #13','test-ip-13','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (14,3,'167968770','test ip #14','test-ip-14','','','2',0,'','','NULL','0','NULL');
INSERT INTO "ipaddresses" VALUES (15,5,'168099840','test ip #15','test-ip-15','52:24:10:00:00:02','','2',0,'','','NULL','0','NULL');
CREATE TABLE "sections" (
"id" int(11) NOT NULL ,
"name" varchar(128) NOT NULL DEFAULT '',
Expand Down
16 changes: 8 additions & 8 deletions ipam/client/tests/test_phpipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def test_delete_ip(testphpipam):
assert iplist == [{'ip': ip_address('10.1.0.2'),
'description': 'test ip #2',
'dnsname': 'test-ip-2',
'state': 1,
'state': testphpipam.used_ip_state,
'mac': ''}]
testphpipam.delete_ip(ip_interface('10.1.0.2/28'))
iplist = testphpipam.get_ip_list_by_desc('test ip #2')
Expand Down Expand Up @@ -578,13 +578,13 @@ def test_get_ip_by_desc(testphpipam):
assert testip['ip'] == ip_address('10.1.0.2')
assert testip['description'] == 'test ip #2'
assert testip['dnsname'] == 'test-ip-2'
assert testip['state'] == 1
assert testip['state'] == testphpipam.used_ip_state

testip = testphpipam.get_ip_by_desc('test ip group 1')
assert testip['ip'] == ip_address('10.2.0.1')
assert testip['description'] == 'test ip group 1'
assert testip['dnsname'] == 'test-ip-8'
assert testip['state'] == 1
assert testip['state'] == testphpipam.used_ip_state


def test_get_ip_interface_by_desc(testphpipam):
Expand Down Expand Up @@ -615,19 +615,19 @@ def test_get_ip_list_by_desc(testphpipam):
assert iplist == [{'ip': ip_address('10.1.0.2'),
'description': 'test ip #2',
'dnsname': 'test-ip-2',
'state': 1,
'state': testphpipam.used_ip_state,
'mac': ''}]

iplist = testphpipam.get_ip_list_by_desc('test ip group 1')
assert iplist == [{'ip': ip_address('10.2.0.1'),
'description': 'test ip group 1',
'dnsname': 'test-ip-8',
'state': 1,
'state': testphpipam.used_ip_state,
'mac': ''},
{'ip': ip_address('10.2.0.2'),
'description': 'test ip group 1',
'dnsname': 'test-ip-9',
'state': 1,
'state': testphpipam.used_ip_state,
'mac': ''}]


Expand All @@ -644,7 +644,7 @@ def test_get_ip_list_by_mac(testphpipam):
assert iplist == [{'ip': ip_address('10.5.0.0'),
'description': 'test ip #15',
'dnsname': 'test-ip-15',
'state': 1,
'state': testphpipam.used_ip_state,
'mac': '52:24:10:00:00:02'}]


Expand Down Expand Up @@ -896,7 +896,7 @@ def test_get_subnet_with_ips(testphpipam):
assert subnetlist == {'subnet': ip_network('10.3.0.0/30'),
'description': 'TST /30 SUBNET', 'vlan_id': 10,
'ips': [{'ip': ip_address('10.3.0.2'),
'state': '1',
'state': str(testphpipam.used_ip_state),
'dnsname': 'test-ip-14',
'description': 'test ip #14',
'mac': ''}]}
Expand Down

0 comments on commit e97956f

Please sign in to comment.