Skip to content

Commit

Permalink
Add support for the LDAP gorups API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Jun 13, 2018
1 parent 5183069 commit ebf822c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
15 changes: 13 additions & 2 deletions docs/gl_objects/groups.rst
Expand Up @@ -177,12 +177,23 @@ LDAP group links

Add an LDAP group link to an existing GitLab group::

group.add_ldap_group_link(ldap_group_cn, gitlab.DEVELOPER_ACCESS, 'main')
group.add_ldap_group_link(ldap_group_cn, gitlab.DEVELOPER_ACCESS, 'ldapmain')

Remove a link::

group.delete_ldap_group_link(ldap_group_cn, 'main')
group.delete_ldap_group_link(ldap_group_cn, 'ldapmain')

Sync the LDAP groups::

group.ldap_sync()

You can use the ``ldapgroups`` manager to list available LDAP groups::

# listing (supports pagination)
ldap_groups = gl.ldapgroups.list()

# filter using a group name
ldap_groups = gl.ldapgroups.list(search='foo')

# list the groups for a specific LDAP provider
ldap_groups = gl.ldapgroups.list(search='foo', provider='ldapmain')
1 change: 1 addition & 0 deletions gitlab/__init__.py
Expand Up @@ -111,6 +111,7 @@ def __init__(self, url, private_token=None, oauth_token=None, email=None,
self.groups = objects.GroupManager(self)
self.hooks = objects.HookManager(self)
self.issues = objects.IssueManager(self)
self.ldapgroups = objects.LDAPGroupManager(self)
self.licenses = objects.LicenseManager(self)
self.namespaces = objects.NamespaceManager(self)
self.notificationsettings = objects.NotificationSettingsManager(self)
Expand Down
44 changes: 44 additions & 0 deletions gitlab/v4/objects.py
Expand Up @@ -907,6 +907,50 @@ class IssueManager(ListMixin, RESTManager):
_types = {'labels': types.ListAttribute}


class LDAPGroup(RESTObject):
_id_attr = None


class LDAPGroupManager(RESTManager):
_path = '/ldap/groups'
_obj_cls = LDAPGroup
_list_filters = ('search', 'provider')

@exc.on_http_error(exc.GitlabListError)
def list(self, **kwargs):
"""Retrieve a list of objects.
Args:
all (bool): If True, return all the items, without pagination
per_page (int): Number of items to retrieve per request
page (int): ID of the page to return (starts with page 1)
as_list (bool): If set to False and no pagination option is
defined, return a generator instead of a list
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
Returns:
list: The list of objects, or a generator if `as_list` is False
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the server cannot perform the request
"""
data = kwargs.copy()
if self.gitlab.per_page:
data.setdefault('per_page', self.gitlab.per_page)

if 'provider' in data:
path = '/ldap/%s/groups' % data['provider']
else:
path = self._path

obj = self.gitlab.http_list(path, **data)
if isinstance(obj, list):
return [self._obj_cls(self, item) for item in obj]
else:
return base.RESTObjectList(self, self._obj_cls, obj)


class License(RESTObject):
_id_attr = 'key'

Expand Down
1 change: 1 addition & 0 deletions tools/ee-test.py
Expand Up @@ -77,6 +77,7 @@ def end_log():
if hasattr(group1, 'ldap_group_links'):
for link in group1.ldap_group_links:
group1.delete_ldap_group_link(link['cn'], link['provider'])
assert(gl.ldapgroups.list())
group1.add_ldap_group_link(LDAP_CN, 30, LDAP_PROVIDER)
group1.ldap_sync()
group1.delete_ldap_group_link(LDAP_CN)
Expand Down

0 comments on commit ebf822c

Please sign in to comment.