'Constructor initializes the LDAP connection'

def __init__(self, ldap_config, ldap_url, bind_user=None, bind_password=None, cert_file=None):

    self.ldap_config = ldap_config
    self._ldap_url = ldap_url
    self._username = bind_user
    self._ldap_cert = cert_file
    if (cert_file is not None):
        ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
        ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, cert_file)
    if self.ldap_config.FOLLOW_REFERRALS.get():
        ldap.set_option(ldap.OPT_REFERRALS, 1)
    else:
        ldap.set_option(ldap.OPT_REFERRALS, 0)
    if ldap_config.DEBUG.get():
        ldap.set_option(ldap.OPT_DEBUG_LEVEL, ldap_config.DEBUG_LEVEL.get())
    self.ldap_handle = ldap.initialize(uri=ldap_url, trace_level=ldap_config.TRACE_LEVEL.get())
    if bind_user:
        try:
            self.ldap_handle.simple_bind_s(bind_user, bind_password)
        except:
            msg = ('Failed    to    bind    to    LDAP    server    as    user    %s' % bind_user)
            LOG.exception(msg)
            raise LdapBindException(msg)
    else:
        try:
            self.ldap_handle.simple_bind_s('', '')
        except:
            msg = 'Failed    to    bind    to    LDAP    server    anonymously'
            LOG.exception(msg)
            raise LdapBindException(msg)
