Files
arista-evpn-vxlan-clab/infrahub/transforms/templates/mlag_yang.j2
Damien 21e79e3536 feat: Add Infrahub Jinja2 transform for MLAG configuration (#22)
- Add GraphQL query mlag_intent.gql: queries InfraMlagPeerConfig by
  device name, returning local IP, peer address, heartbeat peer IP,
  MLAG domain (domain-id, virtual-mac, heartbeat VRF, dual-primary
  detection settings, peer/iBGP VLANs, peer devices), local interface
  SVI name, and peer-link LAG name
- Add Jinja2 template mlag_yang.j2: renders a JSON object targeting
  Arista-native YANG path /arista-mlag-augments:mlag/config; returns
  empty array [] for devices with no MLAG config (spines); all optional
  relationship accesses guarded with 'is defined and is not none' checks
- Update .infrahub.yml: register mlag_intent query and mlag_yang_transform
- Add unit test fixtures for leaf1 (full MLAG config) and spine1 (empty
  response → []) following jinja2-transform-unit-render format

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-01 11:59:21 +01:00

91 lines
3.3 KiB
Django/Jinja

{#
mlag_yang.j2 — Produce a JSON object with the MLAG configuration payload
for a single device, targeting Arista-native YANG paths.
Input: GraphQL response from mlag_intent query.
Returns an empty array [] if the device has no MLAG peer config (e.g. spines).
Output structure:
{
"mlag": {
"config": { ... }, # /arista-mlag-augments:mlag/config
"dual_primary_detection": { ... },
"virtual_mac": "...",
"peer_vlan_id": ...,
"ibgp_vlan_id": ...,
"local_interface_ip": "..."
}
}
#}
{%- set peer_configs = data.InfraMlagPeerConfig.edges -%}
{%- if peer_configs | length == 0 -%}
[]
{%- else -%}
{%- set cfg = peer_configs[0].node -%}
{#— Resolve local interface name —#}
{%- set local_iface_name = none -%}
{%- if cfg.local_interface is defined and cfg.local_interface is not none and cfg.local_interface.node is not none -%}
{%- set local_iface_name = cfg.local_interface.node.name.value -%}
{%- endif -%}
{#— Resolve peer-link name —#}
{%- set peer_link_name = none -%}
{%- if cfg.peer_link is defined and cfg.peer_link is not none and cfg.peer_link.node is not none -%}
{%- set peer_link_name = cfg.peer_link.node.name.value -%}
{%- endif -%}
{#— Resolve MLAG domain attributes —#}
{%- set domain_id = none -%}
{%- set virtual_mac = none -%}
{%- set heartbeat_vrf = none -%}
{%- set dual_primary_detection = false -%}
{%- set dual_primary_delay = none -%}
{%- set dual_primary_action = none -%}
{%- set peer_vlan_id = none -%}
{%- set ibgp_vlan_id = none -%}
{%- if cfg.mlag_domain is defined and cfg.mlag_domain is not none and cfg.mlag_domain.node is not none -%}
{%- set domain = cfg.mlag_domain.node -%}
{%- set domain_id = domain.domain_id.value -%}
{%- set virtual_mac = domain.virtual_mac.value | default(none) -%}
{%- set heartbeat_vrf = domain.heartbeat_vrf.value | default("mgmt") -%}
{%- set dual_primary_detection = domain.dual_primary_detection.value | default(false) -%}
{%- set dual_primary_delay = domain.dual_primary_delay.value | default(none) -%}
{%- set dual_primary_action = domain.dual_primary_action.value | default(none) -%}
{%- if domain.peer_vlan is defined and domain.peer_vlan is not none and domain.peer_vlan.node is not none -%}
{%- set peer_vlan_id = domain.peer_vlan.node.vlan_id.value -%}
{%- endif -%}
{%- if domain.ibgp_vlan is defined and domain.ibgp_vlan is not none and domain.ibgp_vlan.node is not none -%}
{%- set ibgp_vlan_id = domain.ibgp_vlan.node.vlan_id.value -%}
{%- endif -%}
{%- endif -%}
{%- set result = {
"mlag": {
"config": {
"domain-id": domain_id,
"peer-link": peer_link_name,
"local-interface": local_iface_name,
"peer-address": cfg.peer_address.value,
"shutdown": false
},
"dual_primary_detection": {
"enabled": dual_primary_detection,
"delay": dual_primary_delay,
"action": dual_primary_action,
"heartbeat_peer_ip": cfg.heartbeat_peer_ip.value,
"heartbeat_vrf": heartbeat_vrf
},
"virtual_mac": virtual_mac,
"peer_vlan_id": peer_vlan_id,
"ibgp_vlan_id": ibgp_vlan_id,
"local_interface_ip": cfg.local_interface_ip.value
}
} -%}
{{ result | tojson(indent=2) }}
{%- endif -%}