From 9aa1fc1f78835f3d2e0944ca3ceaa685f336a5af Mon Sep 17 00:00:00 2001 From: Anton M Date: Thu, 18 Jan 2024 03:41:00 +0400 Subject: [PATCH] netbox 3.7 support --- .github/workflows/ci.yml | 2 +- pyproject.toml | 2 +- validity/models/base.py | 5 +++-- validity/netbox_changes.py | 12 +++++++++++- validity/scripts/run_tests.py | 5 ++--- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a05549a..c1e98d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - netbox_version: [v3.5.9, v3.6.5] + netbox_version: [v3.5.9, v3.6.9, v3.7.1] steps: - name: Checkout uses: actions/checkout@v3 diff --git a/pyproject.toml b/pyproject.toml index 00d4e6b..5b6e187 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,7 +67,7 @@ known_first_party = ["validity"] [tool.flake8] max-line-length = 120 exclude = ["migrations", "__pycache__"] -per-file-ignores = ["__init__.py:F401"] +per-file-ignores = ["__init__.py:F401", "validity/netbox_changes.py:F401"] extend-ignore = "E203" diff --git a/validity/models/base.py b/validity/models/base.py index 4444ca1..55d761c 100644 --- a/validity/models/base.py +++ b/validity/models/base.py @@ -11,9 +11,10 @@ ExportTemplatesMixin, NetBoxModel, RestrictedQuerySet, - WebhooksMixin, ) +from validity.netbox_changes import EventRulesMixin + logger = logging.getLogger(__name__) @@ -81,7 +82,7 @@ class BaseReadOnlyModel( CustomLinksMixin, CustomValidationMixin, ExportTemplatesMixin, - WebhooksMixin, + EventRulesMixin, models.Model, ): created = models.DateTimeField(auto_now_add=True, blank=True, null=True) diff --git a/validity/netbox_changes.py b/validity/netbox_changes.py index 2dc7622..0a54ed4 100644 --- a/validity/netbox_changes.py +++ b/validity/netbox_changes.py @@ -1,8 +1,18 @@ """ This module is going to contain all duct tape stuff which is required to support -newer versions of NetBox +different versions of NetBox together """ from validity import config DEVICE_ROLE_RELATION = "device_role" if config.netbox_version < "3.6.0" else "role" + + +if config.netbox_version < "3.7.0": + from extras.webhooks import enqueue_object + from netbox.context import webhooks_queue as events_queue + from netbox.models.features import WebhooksMixin as EventRulesMixin +else: + from extras.events import enqueue_object + from netbox.context import events_queue + from netbox.models.features import EventRulesMixin diff --git a/validity/scripts/run_tests.py b/validity/scripts/run_tests.py index 2f5850c..e8b7b27 100644 --- a/validity/scripts/run_tests.py +++ b/validity/scripts/run_tests.py @@ -12,8 +12,6 @@ from extras.choices import ObjectChangeActionChoices from extras.models import Tag from extras.scripts import BooleanVar, MultiObjectVar, ObjectVar -from extras.webhooks import enqueue_object -from netbox.context import webhooks_queue import validity from validity.choices import ExplanationVerbosityChoices @@ -27,6 +25,7 @@ VDataSource, VDevice, ) +from validity.netbox_changes import enqueue_object, events_queue from validity.utils.misc import datasource_sync, null_request from .script_data import RunTestsScriptData, ScriptDataMixin from .variables import VerbosityVar @@ -151,7 +150,7 @@ def run_tests_for_selector( def fire_report_webhook(self, report_id: int) -> None: report = ComplianceReport.objects.filter(pk=report_id).annotate_result_stats().count_devices_and_tests().first() - queue = webhooks_queue.get() + queue = events_queue.get() enqueue_object(queue, report, self.request.user, self.request.id, ObjectChangeActionChoices.ACTION_CREATE) def save_to_db(self, results: Iterable[ComplianceTestResult], report: ComplianceReport | None) -> None: