From 448201b4676160c3ee40761a321bf0a969a42123 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 10 Sep 2024 15:39:49 -0400 Subject: [PATCH 1/2] Closes #97: Introduce the exempt_models config parameter to disable branching support for specific plugin models --- docs/configuration.md | 25 +++++++++++++++++++++++++ netbox_branching/__init__.py | 24 ++++++++++++++++++------ netbox_branching/constants.py | 8 ++++---- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index f70b842..4bba0ce 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1,5 +1,30 @@ # Configuration Parameters +## `exempt_models` + +Default: `[]` (empty list) + +A list of models provided by other plugins which should be exempt from branching support. (Only models which support change logging need be listed; all other models are ineligible for branching support.) + +Models must be specified by app label and model name, as such: + +```python +exempt_models = ( + 'my_plugin.foo', + 'my_plugin.bar', +) +``` + +It is also possible to exclude _all_ models from within a plugin by substituting an asterisk (`*`) for the model name: + +```python +exempt_models = ( + 'my_plugin.*', +) +``` + +--- + ## `max_working_branches` Default: None diff --git a/netbox_branching/__init__.py b/netbox_branching/__init__.py index a17aa6c..4aeb70c 100644 --- a/netbox_branching/__init__.py +++ b/netbox_branching/__init__.py @@ -1,7 +1,7 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured -from netbox.plugins import PluginConfig +from netbox.plugins import PluginConfig, get_plugin_config from netbox.registry import registry @@ -22,6 +22,9 @@ class AppConfig(PluginConfig): # The maximum number of branches which can be provisioned simultaneously 'max_branches': None, + # Models from other plugins which should be excluded from branching support + 'exempt_models': [], + # This string is prefixed to the name of each new branch schema during provisioning 'schema_prefix': 'branch_', } @@ -42,11 +45,20 @@ def ready(self): ) # Record all object types which support branching in the NetBox registry - if 'branching' not in registry['model_features']: - registry['model_features']['branching'] = { - k: v for k, v in registry['model_features']['change_logging'].items() - if k not in constants.EXCLUDED_APPS - } + exempt_models = ( + *constants.EXEMPT_MODELS, + *get_plugin_config('netbox_branching', 'exempt_models'), + ) + branching_models = {} + for app_label, models in registry['model_features']['change_logging'].items(): + # Wildcard exclusion for all models in this app + if f'{app_label}.*' in exempt_models: + continue + branching_models[app_label] = [ + model for model in models + if f'{app_label}.{model}' not in exempt_models + ] + registry['model_features']['branching'] = branching_models config = AppConfig diff --git a/netbox_branching/constants.py b/netbox_branching/constants.py index c7743b5..d641b63 100644 --- a/netbox_branching/constants.py +++ b/netbox_branching/constants.py @@ -10,8 +10,8 @@ # URL query parameter name QUERY_PARAM = '_branch' -# Apps which are explicitly excluded from branching -EXCLUDED_APPS = ( - 'netbox_branching', - 'netbox_changes', +# Models for which branching support is explicitly disabled +EXEMPT_MODELS = ( + 'netbox_branching.*', + 'netbox_changes.*', ) From e41727c63c0970ccdb3cc35cd8c4900a7805c95c Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 11 Sep 2024 14:22:36 -0400 Subject: [PATCH 2/2] Add warning --- docs/configuration.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 4bba0ce..bc13a04 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -6,6 +6,9 @@ Default: `[]` (empty list) A list of models provided by other plugins which should be exempt from branching support. (Only models which support change logging need be listed; all other models are ineligible for branching support.) +!!! warning + A model may not be exempted from branching support if it has one or more relationships to models for which branching is supported. Branching **must** be supported consistently for all inter-related models; otherwise, data corruption can occur. Configure this setting only if you have a specific need to disable branching for certain models provided by plugins. + Models must be specified by app label and model name, as such: ```python