## Summary Closes #20 Adds Infrahub Jinja2 transforms that query device intent from Infrahub via GraphQL and produce structured JSON payloads (YANG-style) suitable for gNMI Set operations on Arista EOS devices. - **3 GraphQL queries** — `vlan_intent`, `interface_intent`, `vxlan_intent` — each parameterized by `$device_name` - **3 Jinja2 templates** — `vlan_yang.j2`, `interface_yang.j2`, `vxlan_yang.j2` — producing JSON arrays/objects from the GraphQL response - **Integration test fixtures** — one directory per transform with `input.json`, `output.json`, and `test.yml`, using leaf1 from the lab topology as sample device - **`.infrahub.yml` updated** with `queries` and `jinja2_transforms` sections ## Files added ``` transforms/ ├── queries/ │ ├── vlan_intent.gql # VLANs via VTEP mappings + SVI interfaces │ ├── interface_intent.gql # All interface types with IPs │ └── vxlan_intent.gql # VTEP config + VLAN/VNI/VRF mappings ├── templates/ │ ├── vlan_yang.j2 # JSON array of VLANs (merged, deduplicated, sorted) │ ├── interface_yang.j2 # JSON array of interfaces with type discriminator │ └── vxlan_yang.j2 # JSON object: vtep + vlan_vni + vrf_vni mappings └── tests/ ├── vlan_yang/{input,output,test}.{json,yml} ├── interface_yang/{input,output,test}.{json,yml} └── vxlan_yang/{input,output,test}.{json,yml} ``` ## Transform usage ```bash # Render locally infrahubctl render vlan_yang_transform device_name=leaf1 infrahubctl render interface_yang_transform device_name=leaf1 infrahubctl render vxlan_yang_transform device_name=leaf1 # Via API GET /api/transform/jinja2/vlan_yang_transform?device_name=leaf1 GET /api/transform/jinja2/interface_yang_transform?device_name=leaf1 GET /api/transform/jinja2/vxlan_yang_transform?device_name=leaf1 ``` ## Test plan - [x] Load branch into Infrahub (`infrahubctl schema load` + `infrahubctl object load`) - [x] Run `infrahubctl render vlan_yang_transform device_name=leaf1` and verify JSON output matches expected VLANs (40, 4090, 4091) - [x] Run `infrahubctl render interface_yang_transform device_name=leaf1` and verify all interface types are present with correct attributes - [x] Run `infrahubctl render vxlan_yang_transform device_name=leaf1` and verify VTEP source address, UDP port, and VLAN-VNI mapping for VLAN 40 / VNI 110040 - [x] Run `infrahubctl render vxlan_yang_transform device_name=leaf3` and verify VRF gold / L3VNI 100001 appears in `vrf_vni_mappings` - ~~[ ] Verify `infrahubctl test` passes for all three test fixtures~~ Reviewed-on: #24
68 lines
2.8 KiB
Django/Jinja
68 lines
2.8 KiB
Django/Jinja
{#
|
|
vlan_yang.j2 — Produce a JSON array of VLAN configuration objects.
|
|
|
|
Input: GraphQL response from vlan_intent query.
|
|
The query returns VLANs from two sources:
|
|
1. data.InfraVTEP[].vlan_vni_mappings[].vlan (L2/VXLAN VLANs)
|
|
2. data.InfraInterfaceVlan[].vlan (SVI/routing/MLAG VLANs)
|
|
|
|
We merge both sources, deduplicate by vlan_id, and emit one object per VLAN.
|
|
#}
|
|
{%- set vlans = {} -%}
|
|
|
|
{#— Collect VLANs from VTEP VLAN-VNI mappings —#}
|
|
{%- for vtep_edge in data.InfraVTEP.edges -%}
|
|
{%- for mapping_edge in vtep_edge.node.vlan_vni_mappings.edges -%}
|
|
{%- set vlan_node = mapping_edge.node.vlan.node -%}
|
|
{%- set vid = vlan_node.vlan_id.value | string -%}
|
|
{%- if vid not in vlans -%}
|
|
{%- set vni_val = none -%}
|
|
{%- set vni_type_val = none -%}
|
|
{%- if vlan_node.vni is defined and vlan_node.vni is not none and vlan_node.vni.node is not none -%}
|
|
{%- set vni_val = vlan_node.vni.node.vni.value -%}
|
|
{%- set vni_type_val = vlan_node.vni.node.vni_type.value -%}
|
|
{%- endif -%}
|
|
{%- set _ = vlans.update({vid: {
|
|
"vlan_id": vlan_node.vlan_id.value,
|
|
"name": vlan_node.name.value,
|
|
"status": vlan_node.status.value | default('') | upper,
|
|
"vlan_type": vlan_node.vlan_type.value | default(none),
|
|
"trunk_groups": vlan_node.trunk_groups.value | default([]),
|
|
"stp_enabled": vlan_node.stp_enabled.value,
|
|
"vni": vni_val,
|
|
"vni_type": vni_type_val
|
|
}}) -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{%- endfor -%}
|
|
|
|
{#— Collect VLANs from SVI interfaces —#}
|
|
{%- for svi_edge in data.InfraInterfaceVlan.edges -%}
|
|
{%- if svi_edge.node.vlan is defined and svi_edge.node.vlan is not none and svi_edge.node.vlan.node is not none -%}
|
|
{%- set vlan_node = svi_edge.node.vlan.node -%}
|
|
{%- set vid = vlan_node.vlan_id.value | string -%}
|
|
{%- if vid not in vlans -%}
|
|
{%- set vni_val = none -%}
|
|
{%- set vni_type_val = none -%}
|
|
{%- if vlan_node.vni is defined and vlan_node.vni is not none and vlan_node.vni.node is not none -%}
|
|
{%- set vni_val = vlan_node.vni.node.vni.value -%}
|
|
{%- set vni_type_val = vlan_node.vni.node.vni_type.value -%}
|
|
{%- endif -%}
|
|
{%- set _ = vlans.update({vid: {
|
|
"vlan_id": vlan_node.vlan_id.value,
|
|
"name": vlan_node.name.value,
|
|
"status": vlan_node.status.value | default('') | upper,
|
|
"vlan_type": vlan_node.vlan_type.value | default(none),
|
|
"trunk_groups": vlan_node.trunk_groups.value | default([]),
|
|
"stp_enabled": vlan_node.stp_enabled.value,
|
|
"vni": vni_val,
|
|
"vni_type": vni_type_val
|
|
}}) -%}
|
|
{%- endif -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
|
|
{#— Sort by vlan_id and emit JSON array —#}
|
|
{%- set sorted_vlans = vlans.values() | sort(attribute='vlan_id') -%}
|
|
{{ sorted_vlans | tojson(indent=2) }}
|