mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Register DataBackend classes in application registry
This commit is contained in:
parent
6f2786da0a
commit
a8ed149840
@ -8,6 +8,8 @@ from django import forms
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from netbox.registry import registry
|
||||||
|
from .choices import DataSourceTypeChoices
|
||||||
from .exceptions import SyncError
|
from .exceptions import SyncError
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -18,6 +20,17 @@ __all__ = (
|
|||||||
logger = logging.getLogger('netbox.data_backends')
|
logger = logging.getLogger('netbox.data_backends')
|
||||||
|
|
||||||
|
|
||||||
|
def register_backend(name):
|
||||||
|
"""
|
||||||
|
Decorator for registering a DataBackend class.
|
||||||
|
"""
|
||||||
|
def _wrapper(cls):
|
||||||
|
registry['data_backends'][name] = cls
|
||||||
|
return cls
|
||||||
|
|
||||||
|
return _wrapper
|
||||||
|
|
||||||
|
|
||||||
class DataBackend:
|
class DataBackend:
|
||||||
parameters = {}
|
parameters = {}
|
||||||
|
|
||||||
@ -34,6 +47,7 @@ class DataBackend:
|
|||||||
raise NotImplemented()
|
raise NotImplemented()
|
||||||
|
|
||||||
|
|
||||||
|
@register_backend(DataSourceTypeChoices.LOCAL)
|
||||||
class LocalBackend(DataBackend):
|
class LocalBackend(DataBackend):
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@ -44,6 +58,7 @@ class LocalBackend(DataBackend):
|
|||||||
yield local_path
|
yield local_path
|
||||||
|
|
||||||
|
|
||||||
|
@register_backend(DataSourceTypeChoices.GIT)
|
||||||
class GitBackend(DataBackend):
|
class GitBackend(DataBackend):
|
||||||
parameters = {
|
parameters = {
|
||||||
'username': forms.CharField(
|
'username': forms.CharField(
|
||||||
|
@ -2,7 +2,7 @@ import copy
|
|||||||
|
|
||||||
from core.models import *
|
from core.models import *
|
||||||
from netbox.forms import NetBoxModelForm, StaticSelect
|
from netbox.forms import NetBoxModelForm, StaticSelect
|
||||||
from ..models.data import BACKEND_CLASSES
|
from netbox.registry import registry
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'DataSourceForm',
|
'DataSourceForm',
|
||||||
@ -33,13 +33,14 @@ class DataSourceForm(NetBoxModelForm):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
if self.is_bound and self.data.get('type') in BACKEND_CLASSES:
|
backend_classes = registry['data_backends']
|
||||||
backend_type = self.data['type']
|
|
||||||
elif self.initial and self.initial.get('type') in BACKEND_CLASSES:
|
if self.is_bound and self.data.get('type') in backend_classes:
|
||||||
backend_type = self.initial['type']
|
backend = backend_classes.get(self.data['type'])
|
||||||
|
elif self.initial and self.initial.get('type') in backend_classes:
|
||||||
|
backend = backend_classes.get(self.initial['type'])
|
||||||
else:
|
else:
|
||||||
backend_type = self.fields['type'].initial
|
backend = backend_classes.get(self.fields['type'].initial)
|
||||||
backend = BACKEND_CLASSES.get(backend_type)
|
|
||||||
for name, form_field in backend.parameters.items():
|
for name, form_field in backend.parameters.items():
|
||||||
field_name = f'backend_{name}'
|
field_name = f'backend_{name}'
|
||||||
self.fields[field_name] = copy.copy(form_field)
|
self.fields[field_name] = copy.copy(form_field)
|
||||||
|
@ -14,10 +14,10 @@ from django.utils.translation import gettext as _
|
|||||||
|
|
||||||
from extras.models import JobResult
|
from extras.models import JobResult
|
||||||
from netbox.models import ChangeLoggedModel
|
from netbox.models import ChangeLoggedModel
|
||||||
|
from netbox.registry import registry
|
||||||
from utilities.files import sha256_hash
|
from utilities.files import sha256_hash
|
||||||
from utilities.querysets import RestrictedQuerySet
|
from utilities.querysets import RestrictedQuerySet
|
||||||
from ..choices import *
|
from ..choices import *
|
||||||
from ..data_backends import GitBackend, LocalBackend
|
|
||||||
from ..exceptions import SyncError
|
from ..exceptions import SyncError
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -27,11 +27,6 @@ __all__ = (
|
|||||||
|
|
||||||
logger = logging.getLogger('netbox.core.data')
|
logger = logging.getLogger('netbox.core.data')
|
||||||
|
|
||||||
BACKEND_CLASSES = {
|
|
||||||
DataSourceTypeChoices.LOCAL: LocalBackend,
|
|
||||||
DataSourceTypeChoices.GIT: GitBackend,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class DataSource(ChangeLoggedModel):
|
class DataSource(ChangeLoggedModel):
|
||||||
"""
|
"""
|
||||||
@ -129,7 +124,7 @@ class DataSource(ChangeLoggedModel):
|
|||||||
return job_result
|
return job_result
|
||||||
|
|
||||||
def get_backend(self):
|
def get_backend(self):
|
||||||
backend_cls = BACKEND_CLASSES.get(self.type)
|
backend_cls = registry['data_backends'].get(self.type)
|
||||||
backend_params = self.parameters or {}
|
backend_params = self.parameters or {}
|
||||||
|
|
||||||
return backend_cls(self.url, **backend_params)
|
return backend_cls(self.url, **backend_params)
|
||||||
|
@ -25,9 +25,10 @@ class Registry(dict):
|
|||||||
|
|
||||||
# Initialize the global registry
|
# Initialize the global registry
|
||||||
registry = Registry()
|
registry = Registry()
|
||||||
|
registry['data_backends'] = dict()
|
||||||
|
registry['denormalized_fields'] = collections.defaultdict(list)
|
||||||
registry['model_features'] = {
|
registry['model_features'] = {
|
||||||
feature: collections.defaultdict(set) for feature in EXTRAS_FEATURES
|
feature: collections.defaultdict(set) for feature in EXTRAS_FEATURES
|
||||||
}
|
}
|
||||||
registry['denormalized_fields'] = collections.defaultdict(list)
|
|
||||||
registry['search'] = dict()
|
registry['search'] = dict()
|
||||||
registry['views'] = collections.defaultdict(dict)
|
registry['views'] = collections.defaultdict(dict)
|
||||||
|
Loading…
Reference in New Issue
Block a user