fix(infrahub): fetch a stored peer again when it lacks a required relationship

Draft
PC
2d ago
+54-5

Fixes #461.

Bug

Loading the Infrahub side of a diff or sync failed with Cannot build unique_id for peer LocationRack[...] (relationship DcimDevice.location ...): missing identifier key(s) ['site'], although the mapping declares site and the rack has its site in Infrahub. --continue-on-error only skipped the relationship, so devices lost their location.

model_loader loads a kind with client.all(..., include=..., populate_store=True). When include names a relationship, the SDK also builds a node for each peer and stores it. DcimDevice.location points to the generic LocationHosting, so that node carries only what the generic declares, without site, and it replaces the rack loaded earlier in the store. resolve_peer_node fetched a stored peer again only when a required attribute was empty, so the partial rack was used.

Circumstances

A peer whose identifiers include a relationship that a generic does not declare, reached through that generic by a kind loaded after it. Here LocationRack (identifiers site, name) through DcimDevice.location.

How it was caught

Syncing NetBox racks and devices into Infrahub 1.12.5 with the dcim schemas and the rack extension of the schema library.

client.all(kind="LocationRack", include=["name"], populate_store=True)
client.store.get(key=rack_id, kind="LocationRack").site.id   # '<site id>'
client.all(kind="DcimDevice", include=["location"], populate_store=True)
client.store.get(key=rack_id, kind="LocationRack").site.id   # None

Fix

The completeness check in resolve_peer_node (renamed _node_is_complete) also treats a stored peer as incomplete when a required cardinality-one relationship holds no id, so the peer is fetched again by its concrete kind. Before, resolve_peer_node returned the partial rack with site.id == None. After, it returns the rack with its site. The cost is one fetch per such peer each time a kind that points to it is loaded.

Two tests cover a stored peer with and without its required relationship. The fake relationship schema in test_infrahub_node_to_diffsync.py gains the optional field that real relationship schemas carry.

No user-visible changes: no CLI flag or config key changes.

Not covered

  • An identifier on an optional relationship, reached through a generic, can still fail the same way: an empty optional relationship cannot be told apart from one that was not fetched.
  • The underlying SDK behaviour is tracked in opsmill/infrahub-sdk-python#347 (a stored node replaced by the minimal copy fetched through a relationship) and opsmill/infrahub-sdk-python#521 (a peer reached through a generic lacks the concrete kind's fields).

Test plan

  • pytest tests/adapters: 226 passed, 3 skipped.
  • ruff format, ruff check and ty check clean on the changed files.
  • Replayed the failing load order against a local Infrahub 1.12.5: resolve_peer_node now returns the rack with its site.
  • The local rumdl hook was skipped for the commit only because it lints the whole working tree, including an untracked local folder. An export of the committed tree passes rumdl with no issues.

šŸ¤– Generated with Claude Code