Files
NetBox-Import-Desktop/tests/fake_netbox.py
T
MrBlakeandClaude Opus 5.5 9ce25d820a feat: import Sophos access points from a Sophos Central CSV export
- New "Access Points" page: load the Sophos Central export, check missing
  sites and duplicate IPs, serial numbers and MAC addresses
- Find existing APs globally by serial number and MAC (chunked queries) and
  by name within the tenant group; list NetBox APs missing in the export
- Map Sophos sites to NetBox sites of the tenant group (suggested by name,
  stored per profile, per-AP choice for APs without site)
- Create or update devices with Sophos device type, role, site, serial,
  firmware, interface, MAC and primary IP; tenant from the site or the chosen
  tenant, devices of other tenants are never changed; preview before writing

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 16:50:11 +01:00

83 lines
3.1 KiB
Python

"""Tiny in-memory stand-in for the parts of pynetbox the sync code uses."""
import itertools
from types import SimpleNamespace
_ids = itertools.count(1000)
class Record(SimpleNamespace):
def __init__(self, endpoint=None, **values):
super().__init__(**values)
self._endpoint = endpoint
def update(self, values):
changed = any(getattr(self, key, None) != value for key, value in values.items())
for key, value in values.items():
setattr(self, key, value)
if changed and self._endpoint is not None:
self._endpoint.updates.append((self.id, dict(values)))
return changed
def _matches(record, key, value):
if isinstance(value, list): # pynetbox sends lists as repeated query parameters (OR)
return any(_matches(record, key, item) for item in value)
if key == "brief":
return True
if key.endswith("__ie"):
return str(getattr(record, key[:-4], "")).casefold() == str(value).casefold()
if key == "vrf_id":
vrf = getattr(record, "vrf", None)
vrf_id = vrf if isinstance(vrf, int) else getattr(vrf, "id", None)
return (vrf_id is None) if value == "null" else vrf_id == value
if key == "address":
return str(getattr(record, "address", "")).split("/")[0] == str(value).split("/")[0]
if key.endswith("_id") and not hasattr(record, key):
related = getattr(record, key[:-3], None)
return getattr(related, "id", related) == value
return getattr(record, key, None) == value
class Endpoint:
def __init__(self, records=()):
self.records = []
self.created = []
self.updates = []
for values in records:
self.records.append(Record(self, **values))
def all(self):
return list(self.records)
def filter(self, **kwargs):
return [record for record in self.records if all(_matches(record, k, v) for k, v in kwargs.items())]
def get(self, *args, **kwargs):
if args:
kwargs = {"id": args[0]}
found = self.filter(**kwargs)
if len(found) > 1:
raise ValueError("get() returned more than one result")
return found[0] if found else None
def create(self, payload):
record = Record(self, id=next(_ids), cable=None, **payload)
self.records.append(record)
self.created.append(payload)
return record
class FakeNetBox:
netbox_version = "4.3.0"
def __init__(self):
self.dcim = SimpleNamespace(devices=Endpoint(), interfaces=Endpoint(), cables=Endpoint(),
mac_addresses=Endpoint(), platforms=Endpoint(),
manufacturers=Endpoint(), device_types=Endpoint(), device_roles=Endpoint(),
sites=Endpoint())
self.ipam = SimpleNamespace(ip_addresses=Endpoint(), prefixes=Endpoint(), vrfs=Endpoint(), vlans=Endpoint())
self.virtualization = SimpleNamespace(clusters=Endpoint(), cluster_types=Endpoint(), virtual_machines=Endpoint(),
interfaces=Endpoint())
self.tenancy = SimpleNamespace(tenants=Endpoint())