|
@@ -15,6 +15,7 @@ from django.utils import timezone
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from netbox.constants import ADVISORY_LOCK_KEYS, CENSOR_TOKEN, CENSOR_TOKEN_CHANGED
|
|
from netbox.constants import ADVISORY_LOCK_KEYS, CENSOR_TOKEN, CENSOR_TOKEN_CHANGED
|
|
|
|
|
+from netbox.data_backends import data_backend_is_allowed
|
|
|
from netbox.models import PrimaryModel
|
|
from netbox.models import PrimaryModel
|
|
|
from netbox.models.features import JobsMixin
|
|
from netbox.models.features import JobsMixin
|
|
|
from netbox.registry import registry
|
|
from netbox.registry import registry
|
|
@@ -151,9 +152,13 @@ class DataSource(JobsMixin, PrimaryModel):
|
|
|
return self.revision[:7]
|
|
return self.revision[:7]
|
|
|
return self.revision
|
|
return self.revision
|
|
|
|
|
|
|
|
|
|
+ @property
|
|
|
|
|
+ def backend_allowed(self):
|
|
|
|
|
+ return data_backend_is_allowed(self.type)
|
|
|
|
|
+
|
|
|
@property
|
|
@property
|
|
|
def ready_for_sync(self):
|
|
def ready_for_sync(self):
|
|
|
- return self.enabled and self.status not in (
|
|
|
|
|
|
|
+ return self.enabled and self.backend_allowed and self.status not in (
|
|
|
DataSourceStatusChoices.QUEUED,
|
|
DataSourceStatusChoices.QUEUED,
|
|
|
DataSourceStatusChoices.SYNCING
|
|
DataSourceStatusChoices.SYNCING
|
|
|
)
|
|
)
|
|
@@ -166,6 +171,11 @@ class DataSource(JobsMixin, PrimaryModel):
|
|
|
raise ValidationError({
|
|
raise ValidationError({
|
|
|
'type': _("Unknown backend type: {type}".format(type=self.type))
|
|
'type': _("Unknown backend type: {type}".format(type=self.type))
|
|
|
})
|
|
})
|
|
|
|
|
+ # Allow edits that keep an existing source's disallowed backend
|
|
|
|
|
+ if self.type and not self.backend_allowed and self._type_changed():
|
|
|
|
|
+ raise ValidationError({
|
|
|
|
|
+ 'type': _("The {type} backend is not enabled.").format(type=self.type)
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
# Ensure URL scheme matches selected type
|
|
# Ensure URL scheme matches selected type
|
|
|
if self.backend_class.is_local and self.url_scheme not in ('file', ''):
|
|
if self.backend_class.is_local and self.url_scheme not in ('file', ''):
|
|
@@ -175,6 +185,11 @@ class DataSource(JobsMixin, PrimaryModel):
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ def _type_changed(self):
|
|
|
|
|
+ if self._state.adding:
|
|
|
|
|
+ return True
|
|
|
|
|
+ return not DataSource.objects.filter(pk=self.pk, type=self.type).exists()
|
|
|
|
|
+
|
|
|
def save(self, *args, **kwargs):
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# If recurring sync is disabled for an existing DataSource, clear any pending sync jobs for it and reset its
|
|
# If recurring sync is disabled for an existing DataSource, clear any pending sync jobs for it and reset its
|
|
@@ -222,6 +237,8 @@ class DataSource(JobsMixin, PrimaryModel):
|
|
|
|
|
|
|
|
using = router.db_for_write(self.__class__, instance=self)
|
|
using = router.db_for_write(self.__class__, instance=self)
|
|
|
|
|
|
|
|
|
|
+ if not self.backend_allowed:
|
|
|
|
|
+ raise SyncError(_("Cannot initiate sync; the {type} backend is not enabled.").format(type=self.type))
|
|
|
if self.status == DataSourceStatusChoices.SYNCING:
|
|
if self.status == DataSourceStatusChoices.SYNCING:
|
|
|
raise SyncError(_("Cannot initiate sync; syncing already in progress."))
|
|
raise SyncError(_("Cannot initiate sync; syncing already in progress."))
|
|
|
|
|
|