From 22348cdbfc2b3b6ea200100704c03e3b82a3b63c Mon Sep 17 00:00:00 2001 From: Arzhel Younsi Date: Mon, 8 Jul 2024 18:04:17 +0200 Subject: [PATCH] Extend STORAGE_BACKEND config to support Swift (#16319) * Extend STORAGE_BACKEND config to support Swift Requires django-storage-swift >= 1.4.0 when used. Bug: T310717 Change-Id: I67cf439e9152608cbba3a3de4173d54ba5fbddc2 * Update system.md from suggestions Co-authored-by: Jeremy Stretch * Update settings.py from suggestions Co-authored-by: Jeremy Stretch * Update system.md from suggestions 2 Co-authored-by: Jeremy Stretch * Remove SWIFT storage from configuration_example.py * Load swift config as global instead of monkey path --------- Co-authored-by: Jeremy Stretch --- docs/configuration/system.md | 4 ++-- netbox/netbox/settings.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/configuration/system.md b/docs/configuration/system.md index a1e0ebb17..f3c68db1b 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -177,7 +177,7 @@ The dotted path to the desired search backend class. `CachedValueSearchBackend` Default: None (local storage) -The backend storage engine for handling uploaded files (e.g. image attachments). NetBox supports integration with the [`django-storages`](https://django-storages.readthedocs.io/en/stable/) package, which provides backends for several popular file storage services. If not configured, local filesystem storage will be used. +The backend storage engine for handling uploaded files (e.g. image attachments). NetBox supports integration with the [`django-storages`](https://django-storages.readthedocs.io/en/stable/) and [`django-storage-swift`](https://github.com/dennisv/django-storage-swift) packages, which provide backends for several popular file storage services. If not configured, local filesystem storage will be used. The configuration parameters for the specified storage backend are defined under the `STORAGE_CONFIG` setting. @@ -187,7 +187,7 @@ The configuration parameters for the specified storage backend are defined under Default: Empty -A dictionary of configuration parameters for the storage backend configured as `STORAGE_BACKEND`. The specific parameters to be used here are specific to each backend; see the [`django-storages` documentation](https://django-storages.readthedocs.io/en/stable/) for more detail. +A dictionary of configuration parameters for the storage backend configured as `STORAGE_BACKEND`. The specific parameters to be used here are specific to each backend; see the documentation for your selected backend ([`django-storages`](https://django-storages.readthedocs.io/en/stable/) or [`django-storage-swift`](https://github.com/dennisv/django-storage-swift)) for more detail. If `STORAGE_BACKEND` is not defined, this setting will be ignored. diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 3a8e51e05..7a248c875 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -225,6 +225,23 @@ if STORAGE_BACKEND is not None: return globals().get(name, default) storages.utils.setting = _setting + # django-storage-swift + elif STORAGE_BACKEND == 'swift.storage.SwiftStorage': + try: + import swift.utils # type: ignore + except ModuleNotFoundError as e: + if getattr(e, 'name') == 'swift': + raise ImproperlyConfigured( + f"STORAGE_BACKEND is set to {STORAGE_BACKEND} but django-storage-swift is not present. " + "It can be installed by running 'pip install django-storage-swift'." + ) + raise e + + # Load all SWIFT_* settings from the user configuration + for param, value in STORAGE_CONFIG.items(): + if param.startswith('SWIFT_'): + globals()[param] = value + if STORAGE_CONFIG and STORAGE_BACKEND is None: warnings.warn( "STORAGE_CONFIG has been set in configuration.py but STORAGE_BACKEND is not defined. STORAGE_CONFIG will be "