fix: give inventory assets the tenant of the transfer or the device
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ef12531c8e
commit
839c5ad2ac
2 files changed
+36
-5
No files matched your search
@@ -246,8 +246,13 @@ class ClientSyncEngine:
|
||||
self.log(f"netbox-inventory nicht verfügbar – Assets werden übersprungen ({error})", "info")
|
||||
return self._asset_endpoint
|
||||
|
||||
def _sync_asset(self, client, device, type_id, dry_run, report):
|
||||
"""Create or update the inventory asset of a client (found by serial number)."""
|
||||
def _sync_asset(self, client, device, type_id, dry_run, report, tenant_id=None):
|
||||
"""Create or update the inventory asset of a client (found by serial number).
|
||||
|
||||
The asset gets the tenant of the transfer or of the device – NetBox installations with a
|
||||
validator that requires a tenant on every object reject assets without one.
|
||||
"""
|
||||
tenant_id = tenant_id or (related_id(device, "tenant") if device is not None else None)
|
||||
endpoint = self.asset_endpoint()
|
||||
serial = clean_serial(client.get("serial"))
|
||||
if endpoint is None or not serial:
|
||||
@@ -264,6 +269,8 @@ class ClientSyncEngine:
|
||||
"description": description}
|
||||
if device is not None:
|
||||
payload["device"] = device.id
|
||||
if tenant_id:
|
||||
payload["tenant"] = tenant_id
|
||||
if report_text:
|
||||
payload["comments"] = merge_report("", report_text)
|
||||
endpoint.create(payload)
|
||||
@@ -278,6 +285,8 @@ class ClientSyncEngine:
|
||||
updates["device_type"] = type_id
|
||||
if device is not None and related_id(asset, "device") != device.id:
|
||||
updates["device"] = device.id
|
||||
if tenant_id and related_id(asset, "tenant") is None:
|
||||
updates["tenant"] = tenant_id
|
||||
if client.get("uuid") and (getattr(asset, "description", "") or "") != description:
|
||||
updates["description"] = description
|
||||
current = getattr(asset, "comments", "") or ""
|
||||
@@ -288,7 +297,7 @@ class ClientSyncEngine:
|
||||
report["assets_updated"] += 1
|
||||
if not dry_run:
|
||||
asset.update(updates)
|
||||
labels = {"name": "Name", "status": "Status", "device_type": "Gerätetyp", "device": "Gerät",
|
||||
labels = {"name": "Name", "status": "Status", "device_type": "Gerätetyp", "device": "Gerät", "tenant": "Mandant",
|
||||
"description": "UUID", "comments": "Inventarbericht"}
|
||||
return ["Asset aktualisieren: " + ", ".join(labels[key] for key in updates)]
|
||||
|
||||
@@ -329,7 +338,7 @@ class ClientSyncEngine:
|
||||
changes.append(f"Inventarbericht in den Kommentaren ({len(client.get('software') or [])} Programme)")
|
||||
report["devices_created"] += 1
|
||||
if dry_run:
|
||||
changes.extend(self._sync_asset(client, None, type_id, dry_run, report))
|
||||
changes.extend(self._sync_asset(client, None, type_id, dry_run, report, tenant_id))
|
||||
if interfaces:
|
||||
for interface in client.get("interfaces", []):
|
||||
changes.append(f"Schnittstelle {interface['name']} anlegen"
|
||||
@@ -378,7 +387,7 @@ class ClientSyncEngine:
|
||||
if interfaces:
|
||||
changes.extend(self._sync_interfaces(device, client, tenant_id, dry_run, report))
|
||||
try:
|
||||
changes.extend(self._sync_asset(client, device, type_id, dry_run, report))
|
||||
changes.extend(self._sync_asset(client, device, type_id, dry_run, report, tenant_id))
|
||||
except Exception as error: # noqa: BLE001 - the device itself is fine; report the asset problem
|
||||
report["errors"].append(f"{name}: Asset – {error}")
|
||||
changes.append(f"Asset fehlgeschlagen: {error}")
|
||||
|
||||
@@ -316,3 +316,25 @@ def test_without_inventory_plugin_no_asset_is_touched():
|
||||
engine.load_targets()
|
||||
report = engine.sync([client], 1, 2, dry_run=False)
|
||||
assert report["errors"] == [] and report["assets_created"] == 0 and engine.asset_endpoint() is None
|
||||
|
||||
|
||||
def test_asset_gets_the_tenant_of_the_transfer_or_the_device():
|
||||
from types import SimpleNamespace
|
||||
nb, client = _setup()
|
||||
assets = _with_inventory_plugin(nb)
|
||||
engine = ClientSyncEngine(nb)
|
||||
engine.load_targets()
|
||||
engine.sync([client], 1, 2, tenant_id=7, dry_run=False)
|
||||
assert assets.created[0]["tenant"] == 7
|
||||
|
||||
# an existing device keeps its tenant; an asset without tenant gets the device's one
|
||||
nb2, client2 = _setup()
|
||||
assets2 = _with_inventory_plugin(nb2)
|
||||
nb2.dcim.devices.records.append(Record(nb2.dcim.devices, id=300, name="PC01", tenant=SimpleNamespace(id=9),
|
||||
device_type=None, platform=None, serial="", primary_ip4=None))
|
||||
assets2.records.append(Record(assets2, id=501, name="PC01", serial="PW0K84A9", status="used", device=None,
|
||||
device_type=None, tenant=None, description="", comments=""))
|
||||
again = ClientSyncEngine(nb2)
|
||||
again.load_targets()
|
||||
report = again.sync([client2], 1, 2, dry_run=False)
|
||||
assert report["errors"] == [] and assets2.records[0].tenant == 9 and assets2.records[0].device == 300
|
||||
Reference in new issue
Block a user