Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

core.py 2.21 KB
Newer Older
1
from time import sleep
Rob Carleski's avatar
V2    
Rob Carleski committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from urllib.parse import quote


def validate_name(client, name):
    """Validate a given name against the MCommunity API

    Parameters
    ----------
    client : obj
        An instance of mcommunity.MCommClient

    name : str
        The name to validate

    Returns
    -------
    boolean
        true or false, depending on validity status.
    """

    r = client.get('/isValidName/{}'.format(name)).json()

    if r['valid']:
        return True
    elif not r['valid']:
        raise MCommError(r['error'][0]['message'])


def get_entity_dn(client, name):
    """Fetch the DN for a specified entity in MCommunity

    Parameters
    ---------
    client : obj
        An instance of mcommunity.MCommClient

    name : str
        The name of the target entity

    Returns
    -------
    str
        The DN of the given entity
    """
    if '=' in name or '@' in name:
        return name

49
50
51
52
53
54
55
56
57
58
59
60
    for i in range(3):
        data = client.get('/find/both/{}'.format(name)).json()
        if data:
            try:
                assert type(data[0]) == dict
                break
            except AssertionError:
                sleep(2)
                continue
            else:
                raise MCommError('Searching MCommunity failed.')
        else:
Rob Carleski's avatar
Rob Carleski committed
61
62
63
            raise MCommEntityNotFound(
                'Unable to find "{}" in MCommunity'.format(name)
            )
Rob Carleski's avatar
V2    
Rob Carleski committed
64
65
66
67
68
69
70

    if data:
        for item in data:
            if item['person']:
                if item['naming'].casefold() == name.casefold():
                    return item['dn']
            elif item['group']:
71
                if item['displayName'].casefold() == name.casefold():
Rob Carleski's avatar
V2    
Rob Carleski committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
                    return item['dn']
                else:
                    r = client.get('/profile/dn/{}'.format(
                        quote(item['dn'])
                    ))
                    if r.ok:
                        group = r.json()['group'][0]
                        aliases = group['aliases']
                        aliases.append(group['name'])
                        if name in [x.casefold() for x in aliases]:
                            return item['dn']


class MCommError(Exception):
    pass
Rob Carleski's avatar
Rob Carleski committed
87
88
89
90


class MCommEntityNotFound(Exception):
    pass