5.3 KiB
5.3 KiB
applyTo, description
| applyTo | description |
|---|---|
| **/views/base/**,**/views/object_sync/**,**/views/sync/**,**/tables/**,**/librenms_sync.js | Sync page architecture, base views, and sync action patterns |
Sync Pages
Three-Layer View Architecture
All four sync resources (interfaces, cables, IP addresses, VLANs) follow the same pattern:
-
Base views (
views/base/) — abstract classes that define the data pipeline:BaseLibreNMSSyncView— tabbed sync page, orchestrates all tabs via abstractget_*_context()methods.BaseInterfaceTableView— fetch ports → enrich with VLANs → cache → compare with NetBox interfaces → render table.BaseCableTableView— fetch links → match remote devices → check cable status → render table.BaseIPAddressTableView— fetch IPs → resolve interfaces → detect existing/update/new → render table.BaseVLANTableView— fetch VLANs → compare with NetBox VLANs → auto-select groups → render table.
-
Object sync views (
views/object_sync/) — wire base views to NetBox models:- Use
@register_model_view(Device, name="librenms_sync", path="librenms-sync")to inject as a tab on Device/VM detail pages. - Each
get_*_context()method creates an instance of the concrete table view, copiesrequest, and callsget_context_data(). - VMs skip cables and VLANs (return
None).
- Use
-
Sync action views (
views/sync/) — POST-only views that create/update/delete NetBox objects:- Follow a consistent pattern: check permissions → read selected rows from POST → load cached data → apply changes in
transaction.atomic()→ redirect to sync tab.
- Follow a consistent pattern: check permissions → read selected rows from POST → load cached data → apply changes in
Data Pipeline (Base Views)
Every base table view follows: fetch → cache → compare → render.
- Fetch: Call LibreNMS API (e.g.,
get_ports(),get_device_ips(),get_device_vlans()). - Cache: Store results via
CacheMixinkeys:librenms_{data_type}_{model_name}_{pk}. Also store fetch timestamp atlibrenms_{data_type}_last_fetched_{model_name}_{pk}. - Compare: Match LibreNMS data against NetBox objects. Each resource implements its own comparison (interface matching by name, IP matching by address/mask, VLAN matching by VID+group).
- Render: Build a django-tables2 table, return a partial template (
_*_sync_content.html).
Sync Action View Pattern
class SyncSomeResourceView(LibreNMSPermissionMixin, NetBoxObjectPermissionMixin, CacheMixin, View):
required_object_permissions = {"POST": [("add", Model), ("change", Model)]}
def post(self, request, object_type, object_id):
if error := self.require_all_permissions("POST"):
return error
# 1. Resolve object (Device or VM)
# 2. Read selected items from request.POST.getlist("select")
# 3. Load cached data from cache.get(self.get_cache_key(obj, "..."))
# 4. Apply changes inside transaction.atomic()
# 5. Redirect to sync tab with ?tab=<resource>
Table Conventions (tables/*.py)
- Tables define HTMX-enabled columns and checkboxes. Selection uses
ToggleColumn(attrs={"input": {"name": "select"}}). - Constructor takes contextual params (e.g.,
device,interface_name_field,vlan_groups) to customize rendering. - Tables set
self.tabandself.prefixfor multi-table pagination viaget_table_paginate_count(). - Row attrs include
data-*attributes for JavaScript filtering and identification. - VLAN columns use
render_vlans()with hidden inputs for per-row group selection and JSON data for modals.
Key Mixins Used by Sync Views
LibreNMSAPIMixin— lazy-createsLibreNMSAPIinstance viaself.librenms_apiproperty. Also providesget_server_info()for template context.CacheMixin— generates consistent cache keys viaget_cache_key(obj, data_type)andget_last_fetched_key(obj, data_type). Also providesget_vlan_overrides_key(obj)for VLAN group override persistence.VlanAssignmentMixin— VLAN group scope resolution: Rack → Location → Site → SiteGroup → Region → Global. Used by interface and VLAN sync for auto-selecting the most-specific VLAN group and building lookup maps.
JavaScript (librenms_sync.js)
- Not wrapped in an IIFE — functions are global. Master initializer
initializeScripts()runs onDOMContentLoadedandhtmx:afterSwap. - Key function groups:
- Checkbox management:
initializeTableCheckboxes(),updateBulkActionButton(). - TomSelect dropdowns:
initializeVCMemberSelect(),initializeVRFSelects(),initializeVlanGroupSelects(),initializeVlanSyncGroupSelects(). UsesTOMSELECT_INIT_DELAY_MS = 100for delayed initialization after HTMX swaps. - Verification:
handleInterfaceChange(),handleCableChange(),handleVRFChange()— POST to single-item verify endpoints. - VLAN modals:
openVlanDetailModal(),verifyVlanInGroup(),verifyVlanSyncGroup()— per-interface VLAN detail editing. - Bulk operations:
initializeBulkEditApply(),deleteSelectedInterfaces(). - Table filtering:
initializeTableFilters(),filterTable()— client-side row filtering. - URL/tab state:
initializeTabs(),getDeviceIdFromUrl(),setInterfaceNameFieldFromURL(). - Cache countdowns:
initializeCountdown(),initializeCountdowns().
- Checkbox management:
- CSRF token extracted via
document.querySelector('[name=csrfmiddlewaretoken]').value.