feat: add update_from_dict to update node fields from a dict

LA
5h ago
+275-0

Why

Creating a node accepts a dict of attributes and relationships, but updating an existing node requires assigning each field one by one โ€” integrations that receive external data as dicts end up hand-rolling Attribute and RelationshipManager rebuilding (as shown in the linked issue).

This PR adds update_from_dict() so an existing node can be updated from a dict in one call, mirroring the data formats accepted at creation time.

Non-goals: hierarchical relationships (parent/children) are not addressed; they are rejected as unknown fields.

Closes #272

What changed

  • New update_from_dict() method on InfrahubNode and InfrahubNodeSync:
    • Attributes accept scalars, dicts with value and optional properties, or from_pool allocations; updated attributes are marked mutated so partial updates via node.update() include them (including clearing an optional attribute with None).
    • Cardinality-one relationships accept an ID, HFID list, dict, or node object; None clears them.
    • Cardinality-many relationships replace the peer list from a list of IDs/dicts/node objects; [] (or None) clears them.
    • Unknown field names raise ValueError before anything is touched, so a typo cannot half-apply an update.
    • Cardinality-many relationships that take peers as fields raise FeatureNotSupportedError before anything is touched.
  • Method declared on the CoreNodeBase protocol so protocol-typed code can call it.
  • No changes to existing save/update payload generation โ€” the method reuses the existing Attribute, RelatedNode, and RelationshipManager machinery.

How to review

  • infrahub_sdk/node/node.py: all the logic is in InfrahubNodeBase.update_from_dict(), shared by the async and sync nodes. Plain attribute values use the existing value setter. Cardinality-one relationships use the existing __setattr__ path. Cardinality-many relationships replace the peers of the existing manager from _get_relationship_many().
  • docs/.../node.mdx is regenerated; changelog/272.added.md is the towncrier fragment.

How to test

uv run pytest tests/unit/sdk/test_node.py -k update_from_dict

14 new test cases (7 scenarios ร— async/sync) covering attribute scalars, attribute dicts with properties, from_pool without changing the caller's dict, both relationship cardinalities, replacing peers that were not fetched, clearing relationships, and the unknown-field error with payload assertions via _generate_input_data().

Impact & rollout

  • Backward compatibility: purely additive, no changes to existing behavior.

Checklist

  • Tests added/updated
  • Changelog entry added
  • External docs updated (generated SDK reference)

๐Ÿค– Generated with Claude Code


Summary by cubic

Add update_from_dict() to InfrahubNode and InfrahubNodeSync to update multiple attributes and relationships from a dict in one call, matching create-time payloads. This simplifies integrations and validates keys up front to prevent partial updates.

  • New Features
    • Attributes accept scalars, { value, ...props }, or from_pool; changes are marked mutated and None clears optional values.
    • Relationships accept an ID, dict, node object, or lists for cardinality-many; None clears ONE and [] or None clears MANY.
    • Unknown fields raise ValueError before any change is applied.
    • Method added to the CoreNodeBase protocol; existing save/update payload generation is unchanged.