feat(infrahub): add Infrahub client for fabric intent (#42) #54

Merged
Damien merged 5 commits from feature/42-infrahub-client into main 2026-02-27 12:08:36 +00:00
Owner

Summary

  • Replace pynetbox with infrahub-sdk>=0.16.0 + pydantic>=2.0 as runtime dependencies; add pytest / pytest-asyncio to dev dependencies
  • Implement FabricInfrahubClient — an async client wrapping infrahub-sdk that queries a remote Infrahub instance and returns typed, immutable Pydantic models
  • Add full unit-test suite with a completely mocked SDK (no real Infrahub instance needed)

Changes

pyproject.toml

  • pynetbox>=7.5.0infrahub-sdk>=0.16.0 + pydantic>=2.0
  • Dev group: added pytest>=8.0.0, pytest-asyncio>=0.23.0

src/infrahub/exceptions.py

Custom exception hierarchy:

  • InfrahubClientError (base)
  • InfrahubConnectionError, InfrahubQueryError, InfrahubNotFoundError

src/infrahub/models.py

Pydantic v2 frozen models for all fabric intent types:
DeviceIntent, VlanIntent, VniIntent, BgpRouterConfigIntent, BgpPeerGroupIntent, BgpSessionIntent, VrfIntent, VtepIntent, MlagDomainIntent, MlagPeerConfigIntent, EvpnInstanceIntent

src/infrahub/client.pyFabricInfrahubClient

All methods fetch from Infrahub and convert SDK nodes → Pydantic models:

  • get_device — device attrs + resolved ASN/platform/site
  • get_device_vlans — via VTEP vlan_vni_mappings, fallback to SVI interfaces
  • get_device_bgp_config / get_device_bgp_peer_groups / get_device_bgp_sessions
  • get_device_vrfs — via VRFDeviceAssignment with route targets
  • get_device_vtep — with all vlan/vni mappings
  • get_device_evpn_instances
  • get_mlag_domain / get_mlag_peer_config
  • TTL-based cache (60 s) per method+key to avoid redundant SDK queries
  • Async context manager (async with FabricInfrahubClient(...) as client)

tests/test_infrahub_client.py

  • SDK fully mocked with unittest.mock.AsyncMock / MagicMock
  • Tests: correct model return, InfrahubNotFoundError on missing device, branch selection forwarded to Config, cache hit (second call skips SDK), VTEP/MLAG/BGP/VRF/EVPN paths

Test plan

  • uvx pytest tests/test_infrahub_client.py -v — all tests pass
  • uvx ruff check src/infrahub/ tests/ — no lint errors
  • uvx ruff format --check src/infrahub/ tests/ — no formatting issues
  • Manual smoke-test against running Infrahub instance with a real token

Closes #42

🤖 Generated with Claude Code

## Summary - Replace `pynetbox` with `infrahub-sdk>=0.16.0` + `pydantic>=2.0` as runtime dependencies; add `pytest` / `pytest-asyncio` to dev dependencies - Implement `FabricInfrahubClient` — an async client wrapping `infrahub-sdk` that queries a remote Infrahub instance and returns typed, immutable Pydantic models - Add full unit-test suite with a completely mocked SDK (no real Infrahub instance needed) ## Changes ### `pyproject.toml` - `pynetbox>=7.5.0` → `infrahub-sdk>=0.16.0` + `pydantic>=2.0` - Dev group: added `pytest>=8.0.0`, `pytest-asyncio>=0.23.0` ### `src/infrahub/exceptions.py` Custom exception hierarchy: - `InfrahubClientError` (base) - `InfrahubConnectionError`, `InfrahubQueryError`, `InfrahubNotFoundError` ### `src/infrahub/models.py` Pydantic v2 frozen models for all fabric intent types: `DeviceIntent`, `VlanIntent`, `VniIntent`, `BgpRouterConfigIntent`, `BgpPeerGroupIntent`, `BgpSessionIntent`, `VrfIntent`, `VtepIntent`, `MlagDomainIntent`, `MlagPeerConfigIntent`, `EvpnInstanceIntent` ### `src/infrahub/client.py` — `FabricInfrahubClient` All methods fetch from Infrahub and convert SDK nodes → Pydantic models: - `get_device` — device attrs + resolved ASN/platform/site - `get_device_vlans` — via VTEP vlan_vni_mappings, fallback to SVI interfaces - `get_device_bgp_config` / `get_device_bgp_peer_groups` / `get_device_bgp_sessions` - `get_device_vrfs` — via `VRFDeviceAssignment` with route targets - `get_device_vtep` — with all vlan/vni mappings - `get_device_evpn_instances` - `get_mlag_domain` / `get_mlag_peer_config` - TTL-based cache (60 s) per method+key to avoid redundant SDK queries - Async context manager (`async with FabricInfrahubClient(...) as client`) ### `tests/test_infrahub_client.py` - SDK fully mocked with `unittest.mock.AsyncMock` / `MagicMock` - Tests: correct model return, `InfrahubNotFoundError` on missing device, branch selection forwarded to `Config`, cache hit (second call skips SDK), VTEP/MLAG/BGP/VRF/EVPN paths ## Test plan - [x] `uvx pytest tests/test_infrahub_client.py -v` — all tests pass - [x] `uvx ruff check src/infrahub/ tests/` — no lint errors - [x] `uvx ruff format --check src/infrahub/ tests/` — no formatting issues - [x] Manual smoke-test against running Infrahub instance with a real token Closes #42 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Damien added 1 commit 2026-02-26 13:04:09 +00:00
- Replace pynetbox with infrahub-sdk>=0.16.0 + pydantic>=2.0 in dependencies
- Add pytest and pytest-asyncio to dev dependencies
- Implement FabricInfrahubClient async client (src/infrahub/client.py)
  - get_device, get_device_vlans, get_device_bgp_config
  - get_device_bgp_peer_groups, get_device_bgp_sessions
  - get_device_vrfs, get_device_vtep, get_device_evpn_instances
  - get_mlag_domain, get_mlag_peer_config
  - TTL-based caching (60s) to avoid redundant SDK queries
  - Async context manager support
- Add Pydantic v2 frozen models for all intent types (src/infrahub/models.py)
- Add custom exception hierarchy (src/infrahub/exceptions.py)
- Add unit tests with fully mocked SDK (tests/test_infrahub_client.py)
  - Tests for correct model return, NotFoundError, branch selection, caching

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Damien added 1 commit 2026-02-26 14:05:10 +00:00
Remove all references to local Infrahub schemas, data files, and
.infrahub.yml config. The orchestrator now treats Infrahub as an
external Source of Truth queried via infrahub-sdk. Schema and data
are managed in the arista-evpn-vxlan-clab repository.
Damien added 1 commit 2026-02-26 14:12:07 +00:00
Damien added 1 commit 2026-02-26 14:15:56 +00:00
Add .ruff_cache to .gitignore
Damien added 1 commit 2026-02-27 08:21:47 +00:00
Update uv.lock to include new dependencies
Damien merged commit 9e025f0c07 into main 2026-02-27 12:08:36 +00:00
Damien deleted branch feature/42-infrahub-client 2026-02-27 12:08:40 +00:00
Sign in to join this conversation.