from phi.logging import get_logger log = get_logger(__name__) def flatten_attribute(attr): if isinstance(attr, list) and len(attr)==1: return attr[0] else: return attr def flatten_entry(entry): return {k: flatten_attribute(attr) for k, attr in entry['attributes'].items()} def make_user_dict(client, entry): user = flatten_entry(entry) dn = 'uid={},ou=Hackers,{}'.format(user['uid'], client.base_dn) user['dn'] = dn return user def make_group_dict(client, entry): group = flatten_entry(entry) dn = 'cn={},ou=Groups,{}'.format(group['cn'], client.base_dn) group['dn'] = dn # unflatten members, they have to be a list even is there is a single one group['member'] = entry['attributes']['member'] return group def get_response(client, response_id): response, result, request = client.connection.get_response( response_id, get_request=True ) log.debug("Request: {}".format(request)) log.debug("Response: {}".format(response)) log.debug("Result: {}".format(result)) if result['description'] is not 'success': raise Exception(result['description']) return response def add_entry(client, dn, attributes): log.info('Adding entry with distinguiscet name: {}' 'and attributes {}'.format(dn, attributes)) response_id = client.connection.add(dn, attributes=attributes) response = get_response(client, response_id) return response def delete_entry(client, dn): log.info('Deleting entry with distinguiscet name: {}') response_id = client.connection.delete(dn) response = get_response(client, response_id) return response