You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/01/30 19:10:05 UTC

[GitHub] astahlman commented on a change in pull request #4624: [AIRFLOW-3787] Import/export users from JSON file

astahlman 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_r252396877
 
 

 ##########
 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
 
 Review comment:
   Yep, it's a SyntaxError:
   
   ```
   ERROR: Failure: SyntaxError (invalid syntax (cli.py, line 1498))
   ```

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services