#!/usr/bin/python2
# coding=utf-8

import MySQLdb

dbconn = {'host': '10.1.11.26',
          'user': 'LDAP2018',
          'pass': 'Pr4t7UEZITwW1AYG',
          'base': 'sitemanager0'}

sql = 'select * from LDAP'

ad_groups = ['vpn-users', 'gogs']


def mysql_reader(dbconn, sql):
    try:
        conn = MySQLdb.connect(host=dbconn['host'], user=dbconn['user'],
                               passwd=dbconn['pass'], db=dbconn['base'], charset="utf8")
        cur = conn.cursor(MySQLdb.cursors.DictCursor)
        cur.execute(sql)
        data = cur.fetchall()
        cur.close()
    except MySQLdb.Error as err:
        print("Connection error: {}".format(err))
        conn.close()
    else:
        conn.close()
    return data


def users_make_zayavka(users, memberOf):
    res = {}
    for i in users:
        usrName = str(i['LoginEmail']).split('@')[0]
        enabled = False
        pwChange = False
        toRemove = False
        if i['Activ'] == 'Y':
            enabled = True
        if i['Changes'] == 'Y':
            pwChange = True
        if i['Delete'] == 'Y':
            toRemove = True
        res[usrName] = {'samAccountName': usrName,
                        'memberOf': memberOf,
                        'usrPass': str(i['Pass']),
                        'groupSet': str(i['Otdel']),
                        'enabled': enabled,
                        'pwChange': pwChange,
                        'toRemove': toRemove
                        }
    return res


def main():
    users = users_make_zayavka(mysql_reader(dbconn, sql), ad_groups)
    for user in users:
        print user, users[user]


if __name__ == '__main__':
    main()
