Benjamin Renard commited on 2014-06-25 17:16:18
Showing 1 changed files, with 15 additions and 9 deletions.
... | ... |
@@ -1,6 +1,5 @@ |
1 | 1 |
#!/usr/bin/python |
2 | 2 |
|
3 |
-import sys |
|
4 | 3 |
import ldap |
5 | 4 |
import ldap.modlist as modlist |
6 | 5 |
import logging |
... | ... |
@@ -14,13 +13,20 @@ class LdapServer(object): |
14 | 13 |
|
15 | 14 |
con = 0 |
16 | 15 |
|
17 |
- def __init__(self,uri,dn=None,pwd=None,v2=None): |
|
16 |
+ def __init__(self,uri,dn=None,pwd=None,v2=None,raiseOnError=False): |
|
18 | 17 |
self.uri = uri |
19 | 18 |
self.dn = dn |
20 | 19 |
self.pwd = pwd |
20 |
+ self.raiseOnError = raiseOnError |
|
21 | 21 |
if v2: |
22 | 22 |
self.v2=True |
23 | 23 |
|
24 |
+ def _error(self,error,level=logging.WARNING): |
|
25 |
+ if self.raiseOnError: |
|
26 |
+ raise 'LdapServer - Error connecting and binding to LDAP server : %s' % e |
|
27 |
+ else: |
|
28 |
+ logging.log(level,error) |
|
29 |
+ |
|
24 | 30 |
def connect(self): |
25 | 31 |
if self.con == 0: |
26 | 32 |
try: |
... | ... |
@@ -34,9 +40,11 @@ class LdapServer(object): |
34 | 40 |
con.simple_bind_s(self.dn,self.pwd) |
35 | 41 |
|
36 | 42 |
self.con = con |
43 |
+ return True |
|
37 | 44 |
except ldap.LDAPError, e: |
38 |
- logging.critical('LdapServer - Error connecting and binding to LDAP server : %s' % e) |
|
39 |
- sys.exit(1) |
|
45 |
+ self._error('LdapServer - Error connecting and binding to LDAP server : %s' % e,logging.CRITICAL) |
|
46 |
+ return False |
|
47 |
+ return True |
|
40 | 48 |
|
41 | 49 |
def search(self,basedn,filter,attrs,sizelimit=0): |
42 | 50 |
res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs) |
... | ... |
@@ -59,21 +67,19 @@ class LdapServer(object): |
59 | 67 |
self.con.add_s(dn,ldif) |
60 | 68 |
return True |
61 | 69 |
except ldap.LDAPError, e: |
62 |
- logging.warning("LdapServer - Error adding %s : %s" % (dn,e)) |
|
70 |
+ self._error("LdapServer - Error adding %s : %s" % (dn,e)) |
|
63 | 71 |
|
64 | 72 |
return False |
65 | 73 |
|
66 | 74 |
def update_object(self,dn,old,new): |
67 | 75 |
ldif = modlist.modifyModlist(old,new) |
68 | 76 |
if ldif == []: |
69 |
- #logging.debug("LdapServer - No change for %s" % dn) |
|
70 | 77 |
return True |
71 | 78 |
try: |
72 |
- #logging.debug("LdapServer - Update %s" % dn) |
|
73 | 79 |
self.con.modify_s(dn,ldif) |
74 | 80 |
return True |
75 | 81 |
except ldap.LDAPError, e: |
76 |
- logging.warning("LdapServer - Error updating %s : %s" % (dn,e)) |
|
82 |
+ self._error("LdapServer - Error updating %s : %s" % (dn,e)) |
|
77 | 83 |
return False |
78 | 84 |
|
79 | 85 |
def drop_object(self,dn): |
... | ... |
@@ -82,7 +88,7 @@ class LdapServer(object): |
82 | 88 |
self.con.delete_s(dn) |
83 | 89 |
return True |
84 | 90 |
except ldap.LDAPError, e: |
85 |
- logging.warning("LdapServer - Error deleting %s : %s" % (dn,e)) |
|
91 |
+ self._error("LdapServer - Error deleting %s : %s" % (dn,e)) |
|
86 | 92 |
|
87 | 93 |
return False |
88 | 94 |
|
89 | 95 |