From bae27c758b49ffde7dd6705080fc4af0bc8693a0 Mon Sep 17 00:00:00 2001 From: lasseedfast Date: Fri, 19 Sep 2025 08:40:41 +0200 Subject: [PATCH] Added terminal function for admin --- README.md | 35 +++++++++++++++++++++++++++++++++ arango.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 33a8fe1..09f42b7 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,41 @@ Features: - Create and delete users - Set user permissions for databases and collections + +## Interactive Admin Terminal Guide + +You can use the built-in terminal guide to perform common admin tasks interactively: + +- **Create a database** +- **Create a user and set permissions** + +To start the guide, you have two options: + +**Option 1: Run from Python code** +```python +from _arango import admin + +admin() +``` + +**Option 2: Run directly from the terminal** +```bash +python -m _arango.arango admin +``` +Or, if your Python path is set up, you can also run: +```bash +python -m _arango.arango +``` +This will start the guide if you add logic to handle command-line arguments (see below). + +Permission options are explained in the guide: +- `rw` – read and write access +- `ro` – read-only access +- `none` – no access + +For more details on permissions, see: [ArangoDB User Permissions](https://docs.python-arango.com/en/main/specs.html#user-permissions) + + ## Using as a Base for Your Own Project If you want to use this package as a starting point for your own ArangoDB utilities, you can clone the repository: diff --git a/arango.py b/arango.py index d866ed2..99802ec 100644 --- a/arango.py +++ b/arango.py @@ -230,8 +230,63 @@ class AdminArango: + (f", collection '{collection}'" if collection else "") ) + +def admin(): + """ + Interactive terminal guide for ArangoDB admin tasks: + 1) Create a database + 2) Create a user and set permissions + """ + GREY = "\033[90m" + RESET = "\033[0m" + + admin = AdminArango() + print("\nArangoDB Admin Guide") + print("1) Create a database") + print("2) Create user and set permissions") + choice = input("Choose an option (1/2): ").strip() + + if choice == "1": + db_name = input("Enter new database name: ").strip() + add_user = input("Add a user to this database? (y/n): ").strip().lower() + users = [] + if add_user == "y": + username = input("Enter username: ").strip() + password = input("Enter password: ").strip() + users.append({"username": username, "password": password}) + admin.create_database(db_name, users=users if users else None) + + elif choice == "2": + username = input("Enter new username: ").strip() + password = input("Enter password: ").strip() + active = input("Should the user be active? (y/n): ").strip().lower() == "y" + admin.create_user(username, password=password, active=active) + db_name = input("Set permissions for which database?: ").strip() + print( + f"{GREY}Permission options:\n" + " rw - read and write access\n" + " ro - read-only access\n" + " none - no access\n" + "See: https://docs.python-arango.com/en/main/specs.html#user-permissions" + f"{RESET}" + ) + permission = input("Permission (rw/ro/none): ").strip() + print( + f"{GREY}Leave collection blank to set permissions for the whole database.{RESET}" + ) + collection = input("Collection (leave blank for database-level): ").strip() + collection = collection if collection else None + admin.set_user_permission(username, permission, db_name, collection) + else: + print("Invalid choice.") + +# You can call admin_terminal_guide() from __main__ or elsewhere as needed. arango = Arango() if __name__ == "__main__": - arango = Arango() - print("Connected to ArangoDB") + import sys + if len(sys.argv) > 1 and sys.argv[1] == "admin": + admin() + else: + arango = Arango() + print("Connected to ArangoDB")