fix(inheritance): recursive SiteGroup/Region/Role ancestor traversal#124
Open
berlikm wants to merge 10 commits into
Open
fix(inheritance): recursive SiteGroup/Region/Role ancestor traversal#124berlikm wants to merge 10 commits into
berlikm wants to merge 10 commits into
Conversation
Adds Site, SiteGroup, and Region as valid assignment targets for all ZabbixServerAssignment, ZabbixHostgroupAssignment, ZabbixTemplateAssignment, ZabbixTagAssignment, ZabbixMacroAssignment, and ZabbixHostInventory objects. This allows assignments made at the Site/SiteGroup/Region level to be inherited by all devices and VMs at that site or below, via the existing inheritance_chain mechanism. Changes: - Constants: Site/SiteGroup/Region added to ASSIGNMENT_MODELS, ASSIGNMENT_TYPE_TO_FIELD, PATH_LABELS - Forms: All 7 assignment forms get Site/SiteGroup/Region DynamicModelChoiceField + FieldSet - Settings: inheritance_chain extended with Site/SiteGroup/Region resolution paths - Jobs: SyncHostJob._prepare_assignment() creates detached copies for inherited assignments - HostSync: _get_sync_target() resolves actual Device/VM for Jinja2 rendering - HostSync: get_tag_attributes() deduplicates tags by (tag, value) to prevent Zabbix 7 rejecting duplicate pairs inherited from multiple sources - HostInterfaceSync: get_create_params() falls back to the device's primary IP when the interface has no IP assigned (e.g. when inherited from SiteGroup) - HostInterfaceSync: errors are caught per-interface so one failing interface does not prevent the remaining interfaces and templates from being synced - Inheritance: ConfigGroup interface expansion removed; interfaces are now resolved naturally via the inheritance chain with primary IP fallback - SyncBase: _is_inherited_copy guard prevents save()/update_sync_info() on inherited copies - Views: ZabbixSiteTabView, ZabbixSiteGroupTabView, ZabbixRegionTabView added - Tests: 9 new tests for inheritance resolution Tested: all 1076 existing tests pass. Verified end-to-end on dev NetBox.
- Filter inherited/direct ZabbixServerAssignment and ZabbixHostInterface by zabbixserver - Resolve ConfigGroup interface IP from primary_ip4/primary_ip6 - Add seen-set cycle guard to resolve_path
- Fixes silent dropping of Site/SiteGroup assignments
Fixes inherited Site/SiteGroup/Region assignments reading custom fields from the intermediate object instead of the actual Device/VM.
berlikm
force-pushed
the
fix/recursive-sitegroup-ancestry
branch
3 times, most recently
from
July 14, 2026 22:44
e46be7a to
fcef724
Compare
berlikm
force-pushed
the
fix/recursive-sitegroup-ancestry
branch
from
July 14, 2026 23:33
fcef724 to
493bf73
Compare
- Add _walk_ancestors() helper that yields an object and each parent until None, cycle-safe. - In resolve_inherited_zabbix_assignments(), when a path ends with 'parent', check the resolved object plus all ancestors; nearest ancestor wins via existing seen_* sets. - Add tests for 3-level SiteGroup hierarchy.
berlikm
force-pushed
the
fix/recursive-sitegroup-ancestry
branch
from
July 14, 2026 23:50
493bf73 to
80b8699
Compare
This was referenced Jul 15, 2026
Open
berlikm
marked this pull request as ready for review
July 15, 2026 09:27
berlikm
added a commit
to berlikm/nbxsync
that referenced
this pull request
Jul 15, 2026
Rewrites the resolver into collect/batch/distribute phases: walk all inheritance-chain paths once gathering (content_type, pk, label) triples, issue one query per assignment model (7) plus one for hostinventory across all triples, then distribute results into the resolved OrderedDicts in ancestor (leaf-first) order so first-seen-wins dedup is preserved. Query count drops from 7×N to a constant (~8) regardless of ancestor depth. Adds two contract tests: query-count-stays-bounded-as-depth-grows, and leaf-assignment-wins-over-root (first-seen-wins ordering).
berlikm
force-pushed
the
fix/recursive-sitegroup-ancestry
branch
from
July 16, 2026 07:34
60c1d5e to
719f21d
Compare
Author
Conflict resolution note for rebaseWhen rebasing this PR after #117 lands (which adds
Correct resolution: Combine both import sets: from django.db.models import Q, QuerySet
from django.db.models.manager import BaseManager
from dcim.models import DeviceRole, Region, SiteGroup
from nbxsync.constants import PATH_LABELS
from nbxsync.models import ZabbixConfigurationGroupAssignment, ..., ZabbixTemplateAssignment, ZabbixTemplateRuleNote: PR #112 (SiteGroup parent traversal) is closed in favor of this PR — Verified in the integration-test branch: 1135 tests pass, E2E confirmed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the fixed one-hop
('site', 'group', 'parent')inheritance path with recursive ancestor traversal via_walk_ancestors().Problem
The
resolve_path()function followed configured path tuples sequentially. For('site', 'group', 'parent'), it resolvedsite.group.parent— exactly one level. For deep hierarchies (e.g.HU → HU-DEB → HU-DEB-NAG → site), a device at siteHU-DEB-NAG-Bresolved its grandparent SiteGroupHU-DEB, but never reached the rootHUwhere the country-level assignment lives.Fix
_walk_ancestors(obj, parent_attr='parent')— yields obj, then each ancestor viaparentattr, until None. Cycle-safe via seen-set.resolve_inherited_zabbix_assignments()— when the resolved object is aSiteGroup,Region, orDeviceRole, walks all ancestors instead of checking just one.seen_*sets prevent duplicates).Dependencies
Stacks on PR #115 (modifies
inheritance.pywhich PR #115 adds). Merge after #115.Testing
9 new tests covering: 3-level SiteGroup hierarchy, nearest-ancestor-wins, Region parents, DeviceRole parents, cycle detection, None input.
Part of #121.