Skip to content

Enable plugins to register custom model features #20129

@jeremystretch

Description

@jeremystretch

NetBox version

v4.4.0-beta1

Feature type

New functionality

Proposed functionality

NetBox support various model features such as tags, custom fields, change logging, etc. which enhance the application's functionality on a per-model basis. These features are enabled for models by inheriting a designated mixin class: For example, tagging is enabled for all models which inherit the netbox.models.features.TagsMixin class. These feature-to-class mappings are defined statically in the FEATURES_MAP constant:

FEATURES_MAP = {
    'bookmarks': BookmarksMixin,
    'change_logging': ChangeLoggingMixin,
    'cloning': CloningMixin,
    ...
}

This static mapping can be replaced with a dynamic registration system which can be employed by both core NetBox apps and plugins to register model features. For example, the tagging feature mentioned above would be registered like this:

@register_model_feature('tags')
def support_tagging(model):
    return issubclass(model, TagsMixin)

This approach allows for complex logic within the litmus function bound to the declared feature, however in many cases it could be simplified to a lambda invoking issubclass() directly:

register_model_feature('tags', lambda model: issubclass(model, TagsMixin))

The result is that registry['model_features'] will now hold a mapping of feature names to evaluation functions, rather than to mixin classes.

Use case

This approach provides two benefits over the current implementation:

  1. Plugins can introduce new features and apply them to core models.
  2. The logic for determining whether a feature is supported for a given model can be extended beyond simple inheritance checking.

For example, the netbox-branching plugin would register branching as a model feature, using a function it provides to determine eligibility on a per-model basis.

While this has previously been possible by modifying the model_features registry directly, FR #19924 in NetBox v4.4 now records model features on ObjectType records in the database, so registry hacking is no longer feasible.

Database changes

N/A

External dependencies

N/A

Metadata

Metadata

Assignees

Labels

complexity: mediumRequires a substantial but not unusual amount of effort to implementstatus: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions