diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 87d5e42ef..1d9692194 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,7 +60,6 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt - pip install boto3 dulwich pip install pycodestyle coverage tblib - name: Build documentation diff --git a/netbox/core/data_backends.py b/netbox/core/data_backends.py index 73c248d08..d947602a6 100644 --- a/netbox/core/data_backends.py +++ b/netbox/core/data_backends.py @@ -54,13 +54,6 @@ class DataBackend: """ return - def handle_missing_dependency(self, e): - """ - Hook for handling exceptions related to the attempted import of missing modules. Returns an exception - to be raised. - """ - return e - @property def url_scheme(self): return urlparse(self.url).scheme.lower() @@ -105,10 +98,7 @@ class GitBackend(DataBackend): sensitive_parameters = ['password'] def init_config(self): - try: - from dulwich.config import ConfigDict - except ModuleNotFoundError as e: - raise self.handle_missing_dependency(e) + from dulwich.config import ConfigDict # Initialize backend config config = ConfigDict() @@ -120,18 +110,9 @@ class GitBackend(DataBackend): return config - def handle_missing_dependency(self, e): - return ImportError(_( - "Unable to initialize the git data backend: dulwich library is not installed. Run 'pip install dulwich' " - "within the NetBox Python environment to install it." - )) - @contextmanager def fetch(self): - try: - from dulwich import porcelain - except ModuleNotFoundError as e: - raise self.handle_missing_dependency(e) + from dulwich import porcelain local_path = tempfile.TemporaryDirectory() @@ -179,28 +160,16 @@ class S3Backend(DataBackend): REGION_REGEX = r's3\.([a-z0-9-]+)\.amazonaws\.com' def init_config(self): - try: - from botocore.config import Config as Boto3Config - except ModuleNotFoundError as e: - raise self.handle_missing_dependency(e) + from botocore.config import Config as Boto3Config # Initialize backend config return Boto3Config( proxies=settings.HTTP_PROXIES, ) - def handle_missing_dependency(self, e): - return ImportError(_( - "Unable to initialize the Amazon S3 backend: boto3 library is not installed. Run 'pip install boto3' " - "within the NetBox Python environment to install it." - )) - @contextmanager def fetch(self): - try: - import boto3 - except ModuleNotFoundError as e: - raise self.handle_missing_dependency(e) + import boto3 local_path = tempfile.TemporaryDirectory() diff --git a/netbox/core/models/data.py b/netbox/core/models/data.py index cdd5bf49b..54f50046b 100644 --- a/netbox/core/models/data.py +++ b/netbox/core/models/data.py @@ -151,7 +151,7 @@ class DataSource(JobsMixin, PrimaryModel): Create/update/delete child DataFiles as necessary to synchronize with the remote source. """ if self.status == DataSourceStatusChoices.SYNCING: - raise SyncError(f"Cannot initiate sync; syncing already in progress.") + raise SyncError("Cannot initiate sync; syncing already in progress.") # Emit the pre_sync signal pre_sync.send(sender=self.__class__, instance=self) @@ -160,7 +160,10 @@ class DataSource(JobsMixin, PrimaryModel): DataSource.objects.filter(pk=self.pk).update(status=self.status) # Replicate source data locally - backend = self.get_backend() + try: + backend = self.get_backend() + except ModuleNotFoundError as e: + raise SyncError(f"There was an error initializing the backend: {e}") with backend.fetch() as local_path: logger.debug(f'Syncing files from source root {local_path}')