Skip to content

Commit

Permalink
Closes #97: Introduce the exempt_models config parameter (#109)
Browse files Browse the repository at this point in the history
* Closes #97: Introduce the exempt_models config parameter to disable branching support for specific plugin models

* Add warning
  • Loading branch information
jeremystretch authored Sep 11, 2024
1 parent d5193bd commit be5c2a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
28 changes: 28 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# 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.)

!!! 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
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
Expand Down
24 changes: 18 additions & 6 deletions netbox_branching/__init__.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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_',
}
Expand All @@ -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
8 changes: 4 additions & 4 deletions netbox_branching/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*',
)

0 comments on commit be5c2a4

Please sign in to comment.