From 551e9e0ddf11b0faa2b4360bcbd41c04a85e256f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thorsten=20Ro=C3=9Fner?= <thorsten.rossner.extern@zendis.de>
Date: Thu, 19 Sep 2024 09:08:40 +0200
Subject: [PATCH] fix: Improve import filehandling and log output.

---
 user_import_udm_rest_api.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/user_import_udm_rest_api.py b/user_import_udm_rest_api.py
index d7eef3e..089a36a 100755
--- a/user_import_udm_rest_api.py
+++ b/user_import_udm_rest_api.py
@@ -28,7 +28,7 @@ p = configargparse.ArgParser()
 p.add('--import_domain', env_var='IMPORT_DOMAIN', required=True, help='The domain name of your openDesk instance - omit the "portal." or other service specific hostnames.')
 p.add('--udm_api_username', env_var='UDM_API_USERNAME', default='default.admin', help='User to authentication against the UDM REST API with.')
 p.add('--udm_api_password', env_var='UDM_API_PASSWORD', required=True, help='Password for the UDM REST API user.')
-p.add('--import_filename', env_var='IMPORT_FILENAME', required=False, default='', help='The filename containing the user account details for the import - see template.ods for reference. If filename is not provided or related file is not found random users will be imported.')
+p.add('--import_filename', env_var='IMPORT_FILENAME', required=False, default=None, help='The filename containing the user account details for the import - see template.ods for reference. If filename is not provided or related file is not found random users will be imported.')
 p.add('--import_random_amount', env_var='IMPORT_RANDOM_AMOUNT', default=10, help='The number of random accounts to import if the "import_filename" was not set or found.')
 p.add('--import_maildomain', env_var='IMPORT_MAILDOMAIN', required=False, help='Optional: If you are using a different maildomain please specify it, otherwise `IMPORT_DOMAIN` is used.')
 p.add('--create_maildomains', env_var='CREATE_MAILDOMAINS', default=False, help='Optional: Set to "True" to get non existing mail domains auto-created. Only relevant when importing a files in which mailPrimaryAddresses are provided that differ from the default (mail)domain.')
@@ -84,11 +84,14 @@ import_maildomain = options.import_domain if not options.import_maildomain else
 
 ucs = Ucs(adm_username=options.udm_api_username, adm_password=options.udm_api_password, base_url=options.import_domain, maildomain=import_maildomain, options_object=options)
 
-if os.path.isfile(options.import_filename):
-    ImportUser(import_callback, import_filename=options.import_filename, create_admin_accounts=options.create_admin_accounts)
-else:
-    print(f"! File '{options.import_filename}' for import not found, starting random user import...")
+if not options.import_filename:
+    logging.info(f"Starting random user import, as no file for import was defined.")
     RandomUser(import_callback, create_admins=options.create_admin_accounts, amount=int(options.import_random_amount), password_reset_mail=options.password_recovery_email)
     logging.info(f"Accounts that have been created:\n{ucs.return_accounts_from_file()}")
+elif os.path.isfile(options.import_filename):
+    logging.info(f"Importing users from '{options.import_filename}'")
+    ImportUser(import_callback, import_filename=options.import_filename, create_admin_accounts=options.create_admin_accounts)
+else:
+    logging.error(f"File to import from '{options.import_filename}' was not found.")
 
-ucs.summary()
\ No newline at end of file
+ucs.summary()
-- 
GitLab