|
14 | 14 |
|
15 | 15 | import ldap
|
16 | 16 |
|
| 17 | +from ldap import modlist |
| 18 | + |
17 | 19 | ldap_server = "ldap://localhost"
|
18 | 20 | ldap_domain = "DC=osdemo,DC=local"
|
19 | 21 | ldap_user = "OSDEMO\\Administrator"
|
20 | 22 | ldap_password = "Passw0rd"
|
21 | 23 |
|
22 |
| -cn = "CN=Organizational-Role,CN=Schema,CN=Configuration,%s" % ldap_domain |
| 24 | +ldap_base_ou_dn = "OU=OpenStack,%s" % ldap_domain |
| 25 | +ldap_users_ou_dn = "OU=Users,OU=OpenStack,%s" % ldap_domain |
| 26 | +ldap_tenants_ou_dn = "OU=Tenants,OU=OpenStack,%s" % ldap_domain |
| 27 | +ldap_roles_ou_dn = "OU=Roles,OU=OpenStack,%s" % ldap_domain |
| 28 | + |
| 29 | + |
| 30 | +def update_schema(l, ldap_domain): |
| 31 | + dn = "CN=Organizational-Role,CN=Schema,CN=Configuration,%s" % ldap_domain |
| 32 | + org_role = l.search_s(dn, ldap.SCOPE_BASE)[0] |
| 33 | + |
| 34 | + if "groupOfNames" not in org_role[1].get("possSuperiors", []): |
| 35 | + l.modify_s(dn, [(ldap.MOD_ADD, 'possSuperiors', 'groupOfNames')]) |
| 36 | + |
| 37 | + |
| 38 | +def create_organizational_unit(l, ou_dn): |
| 39 | + try: |
| 40 | + ou = l.search_s(ou_dn, ldap.SCOPE_BASE) |
| 41 | + # ou exists |
| 42 | + return |
| 43 | + except ldap.NO_SUCH_OBJECT: |
| 44 | + pass |
| 45 | + |
| 46 | + attrs = {} |
| 47 | + attrs['objectclass'] = ['top', 'organizationalUnit'] |
| 48 | + ldif = modlist.addModlist(attrs) |
| 49 | + l.add_s(ou_dn, ldif) |
| 50 | + |
23 | 51 |
|
24 | 52 | l = ldap.initialize(ldap_server)
|
25 | 53 | l.simple_bind_s(ldap_user, ldap_password)
|
26 |
| -org_role = l.search_s(cn, ldap.SCOPE_BASE)[0] |
27 | 54 |
|
28 |
| -if not "groupOfNames" in org_role[1].get("possSuperiors", []): |
29 |
| - l.modify_s(cn, [(ldap.MOD_ADD, 'possSuperiors', 'groupOfNames')]) |
| 55 | +update_schema(l, ldap_domain) |
| 56 | + |
| 57 | +create_organizational_unit(l, ldap_base_ou_dn) |
| 58 | +create_organizational_unit(l, ldap_users_ou_dn) |
| 59 | +create_organizational_unit(l, ldap_tenants_ou_dn) |
| 60 | +create_organizational_unit(l, ldap_roles_ou_dn) |
30 | 61 |
|
31 |
| -l.unbind() |
| 62 | +l.unbind_s() |
0 commit comments