feng-tao commented on a change in pull request #4624: [AIRFLOW-3787] 
Import/export users from JSON file
URL: https://github.com/apache/airflow/pull/4624#discussion_r252391949
 
 

 ##########
 File path: airflow/bin/cli.py
 ##########
 @@ -1474,6 +1474,107 @@ def users(args):
                 print('User "{}" added to role "{}".'.format(
                     user,
                     args.role))
+    elif args.export:
+        appbuilder = cached_appbuilder()
+        users = appbuilder.sm.get_all_users()
+        fields = ['id', 'username', 'email', 'first_name', 'last_name', 
'roles']
+
+        # In the User model the first and last name fields have underscores,
+        # but the corresponding parameters in the CLI don't
+        def remove_underscores(s):
+            return re.sub("_", "", s)
+
+        users = [
+            {remove_underscores(field): user.__getattribute__(field)
+             if field != 'roles' else [r.name for r in user.roles]
+             for field in fields}
+            for user in users
+        ]
+
+        with open(args.export, 'w') as f:
+            f.write(json.dumps(users, sort_keys=True, indent=4))
+            print("{} users successfully exported to {}".format(len(users), 
f.name))
+
+    elif getattr(args, 'import'):  # "import" is a reserved word
+        json_file = getattr(args, 'import')
+        if not os.path.exists(json_file):
+            print("File '{}' does not exist")
+            exit(1)
+
+        users_list = None
+        try:
+            with open(json_file, 'r') as f:
+                users_list = json.loads(f.read())
+        except ValueError as e:
+            print("File '{}' is not valid JSON. Error: {}".format(json_file, 
e))
+            exit(1)
+
+        users_created, users_updated = _import_users(users_list)
+        if users_created:
+            print("Created the following users:\n\t{}".format(
+                "\n\t".join(users_created)))
+
+        if users_updated:
+            print("Updated the following users:\n\t{}".format(
+                "\n\t".join(users_updated)))
+
+
+def _import_users(users_list):
+    appbuilder = cached_appbuilder()
+    users_created = []
+    users_updated = []
+
+    for user in users_list:
+        roles = []
+        for rolename in user['roles']:
+            role = appbuilder.sm.find_role(rolename)
+            if not role:
+                print("Error: '{}' is not a valid role".format(rolename))
+                exit(1)
+            else:
+                roles.append(role)
+
+        required_fields = ['username', 'firstname', 'lastname',
 
 Review comment:
   ah, I see now. is there any reason we don't use first_name, last_name for 
the field name when import from a file?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to