[
https://issues.apache.org/jira/browse/AIRFLOW-3051?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16626766#comment-16626766
]
ASF GitHub Bot commented on AIRFLOW-3051:
-
r39132 closed pull request #3936: [AIRFLOW-3051] Change CLI to make users ops
similar to connections
URL: https://github.com/apache/incubator-airflow/pull/3936
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/airflow/bin/cli.py b/airflow/bin/cli.py
index dbab8b38d8..1c5494ead1 100644
--- a/airflow/bin/cli.py
+++ b/airflow/bin/cli.py
@@ -1347,74 +1347,75 @@ def kerberos(args): # noqa
@cli_utils.action_logging
-def create_user(args):
-fields = {
-'role': args.role,
-'username': args.username,
-'email': args.email,
-'firstname': args.firstname,
-'lastname': args.lastname,
-}
-empty_fields = [k for k, v in fields.items() if not v]
-if empty_fields:
-raise SystemExit('Required arguments are missing: {}.'.format(
-', '.join(empty_fields)))
-
-appbuilder = cached_appbuilder()
-role = appbuilder.sm.find_role(args.role)
-if not role:
-raise SystemExit('{} is not a valid role.'.format(args.role))
-
-if args.use_random_password:
-password = ''.join(random.choice(string.printable) for _ in range(16))
-elif args.password:
-password = args.password
-else:
-password = getpass.getpass('Password:')
-password_confirmation = getpass.getpass('Repeat for confirmation:')
-if password != password_confirmation:
-raise SystemExit('Passwords did not match!')
+def users(args):
+if args.list:
+
+appbuilder = cached_appbuilder()
+users = appbuilder.sm.get_all_users()
+fields = ['id', 'username', 'email', 'first_name', 'last_name',
'roles']
+users = [[user.__getattribute__(field) for field in fields] for user
in users]
+msg = tabulate(users, [field.capitalize().replace('_', ' ') for field
in fields],
+ tablefmt="fancy_grid")
+if sys.version_info[0] < 3:
+msg = msg.encode('utf-8')
+print(msg)
-if appbuilder.sm.find_user(args.username):
-print('{} already exist in the db'.format(args.username))
return
-user = appbuilder.sm.add_user(args.username, args.firstname, args.lastname,
- args.email, role, password)
-if user:
-print('{} user {} created.'.format(args.role, args.username))
-else:
-raise SystemExit('Failed to create user.')
+if args.create:
+fields = {
+'role': args.role,
+'username': args.username,
+'email': args.email,
+'firstname': args.firstname,
+'lastname': args.lastname,
+}
+empty_fields = [k for k, v in fields.items() if not v]
+if empty_fields:
+raise SystemExit('Required arguments are missing: {}.'.format(
+', '.join(empty_fields)))
-@cli_utils.action_logging
-def delete_user(args):
-if not args.username:
-raise SystemExit('Required arguments are missing: username')
+appbuilder = cached_appbuilder()
+role = appbuilder.sm.find_role(args.role)
+if not role:
+raise SystemExit('{} is not a valid role.'.format(args.role))
+
+if args.use_random_password:
+password = ''.join(random.choice(string.printable) for _ in
range(16))
+elif args.password:
+password = args.password
+else:
+password = getpass.getpass('Password:')
+password_confirmation = getpass.getpass('Repeat for confirmation:')
+if password != password_confirmation:
+raise SystemExit('Passwords did not match!')
-appbuilder = cached_appbuilder()
+if appbuilder.sm.find_user(args.username):
+print('{} already exist in the db'.format(args.username))
+return
+user = appbuilder.sm.add_user(args.username, args.firstname,
args.lastname,
+ args.email, role, password)
+if user:
+print('{} user {} created.'.format(args.role, args.username))
+else:
+raise SystemExit('Failed to create user.')
-try:
-u = next(u for u in appbuilder.sm.get_all_users() if u.username ==
args.username)
-except StopIteration:
-raise SystemExit('{} is not a valid user.'.format(args.username))
+if args.delete:
+if not args.username:
+raise SystemExit('Required arguments are missing: username')
-if appbuilder.sm.del_register_user(u):
-