2020-10-10 18:45:16 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from pprint import pformat as pp
|
2020-10-10 21:14:21 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
2020-10-11 01:11:56 +02:00
|
|
|
import inspect
|
2020-10-10 18:45:16 +02:00
|
|
|
|
|
|
|
import phi.ldap.client
|
|
|
|
from phi.config import get_config
|
|
|
|
from phi.logging import setup_logging, get_logger
|
|
|
|
|
2020-10-10 23:27:08 +02:00
|
|
|
from phi.ldap.user import get_user_by_uid
|
|
|
|
|
2020-10-10 18:45:16 +02:00
|
|
|
log = get_logger(__name__)
|
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'--config', metavar='config.yml', help='custom configuration file')
|
|
|
|
subparses = parser.add_subparsers(title='actions', dest='action')
|
2020-10-10 18:45:16 +02:00
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
cli_callbacks = {}
|
|
|
|
|
|
|
|
|
|
|
|
def cli_register(action_info='', param_infos=[]):
|
|
|
|
def decorator(action):
|
|
|
|
# Get function name and arguments
|
|
|
|
action_name = action.__name__
|
|
|
|
param_names = inspect.getfullargspec(action)[0]
|
|
|
|
|
|
|
|
# Create subparser for specific action
|
|
|
|
subparser = subparses.add_parser(action_name, help=action_info)
|
|
|
|
|
|
|
|
for name, info in zip(param_names, param_infos):
|
|
|
|
subparser.add_argument(dest=name, help=info)
|
|
|
|
|
|
|
|
# Register action
|
|
|
|
cli_callbacks[action_name] = action, param_names
|
|
|
|
return action
|
|
|
|
|
|
|
|
return decorator
|
2020-10-10 18:45:16 +02:00
|
|
|
|
2020-10-10 23:27:08 +02:00
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
def cli_run(args):
|
|
|
|
for action_name, (action, param_names) in cli_callbacks.items():
|
|
|
|
if args['action'] == action_name:
|
|
|
|
action(**{pname: args[pname] for pname in param_names})
|
2020-10-10 21:14:21 +02:00
|
|
|
|
2020-10-10 23:27:08 +02:00
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
def cli_get_args():
|
2020-10-10 23:27:08 +02:00
|
|
|
namespace = parser.parse_args(sys.argv[1:])
|
2020-10-11 01:11:56 +02:00
|
|
|
args = namespace.__dict__
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
@cli_register('dispaly user field', ['user identifier'])
|
|
|
|
def showuser(uid):
|
|
|
|
user = get_user_by_uid(client, uid)
|
|
|
|
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = cli_get_args()
|
|
|
|
|
|
|
|
config_file = args['config']
|
2020-10-10 18:45:16 +02:00
|
|
|
|
2020-10-10 21:14:21 +02:00
|
|
|
config_file, config = get_config(config_file)
|
|
|
|
setup_logging(config.get('logging', {}))
|
2020-10-10 23:27:08 +02:00
|
|
|
log.info("Using configuration at '{}':\n{}"
|
|
|
|
.format(config_file, pp(config)))
|
2020-10-10 18:45:16 +02:00
|
|
|
|
2020-10-10 23:27:08 +02:00
|
|
|
# TODO: check fields in config
|
2020-10-10 18:45:16 +02:00
|
|
|
client = phi.ldap.client.Client(**config['ldap'])
|
2020-10-10 23:27:08 +02:00
|
|
|
|
|
|
|
log.info('Opening LDAP client')
|
2020-10-10 18:45:16 +02:00
|
|
|
client.open()
|
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
log.info('Arguments: {}'.format(pp(args)))
|
2020-10-10 23:27:08 +02:00
|
|
|
|
2020-10-11 01:11:56 +02:00
|
|
|
cli_run(args)
|
2020-10-10 18:45:16 +02:00
|
|
|
|
|
|
|
log.info('Closing LDAP client')
|
|
|
|
client.close()
|