## 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
89 lines
3.3 KiB
Django/Jinja
89 lines
3.3 KiB
Django/Jinja
{#
|
|
vxlan_yang.j2 — Produce a JSON object representing the VXLAN/VTEP configuration
|
|
for the device, including VLAN-to-VNI mappings and VRF-to-VNI mappings.
|
|
|
|
Input: GraphQL response from vxlan_intent query.
|
|
#}
|
|
{%- set vtep_edges = data.InfraVTEP.edges -%}
|
|
{%- set vrf_edges = data.InfraVRFDeviceAssignment.edges -%}
|
|
|
|
{#— Build VTEP section (there is one VTEP per device) —#}
|
|
{%- if vtep_edges | length > 0 -%}
|
|
{%- set vtep = vtep_edges[0].node -%}
|
|
{%- set source_iface = none -%}
|
|
{%- if vtep.source_interface is defined and vtep.source_interface is not none and vtep.source_interface.node is not none -%}
|
|
{%- set source_iface = vtep.source_interface.node.name.value -%}
|
|
{%- endif -%}
|
|
|
|
{#— Build VLAN-to-VNI mapping list —#}
|
|
{%- set vlan_vni_list = [] -%}
|
|
{%- for mapping_edge in vtep.vlan_vni_mappings.edges -%}
|
|
{%- set m = mapping_edge.node -%}
|
|
{%- set vlan_id = none -%}
|
|
{%- set vlan_name = none -%}
|
|
{%- if m.vlan is defined and m.vlan is not none and m.vlan.node is not none -%}
|
|
{%- set vlan_id = m.vlan.node.vlan_id.value -%}
|
|
{%- set vlan_name = m.vlan.node.name.value -%}
|
|
{%- endif -%}
|
|
{%- set vni_val = none -%}
|
|
{%- set vni_type_val = none -%}
|
|
{%- if m.vni is defined and m.vni is not none and m.vni.node is not none -%}
|
|
{%- set vni_val = m.vni.node.vni.value -%}
|
|
{%- set vni_type_val = m.vni.node.vni_type.value -%}
|
|
{%- endif -%}
|
|
{%- set _ = vlan_vni_list.append({
|
|
"vlan_id": vlan_id,
|
|
"vlan_name": vlan_name,
|
|
"vni": vni_val,
|
|
"vni_type": vni_type_val
|
|
}) -%}
|
|
{%- endfor -%}
|
|
|
|
{#— Build VRF-to-VNI mapping list —#}
|
|
{%- set vrf_vni_list = [] -%}
|
|
{%- for vrf_edge in vrf_edges -%}
|
|
{%- set assignment = vrf_edge.node -%}
|
|
{%- set vrf_name = none -%}
|
|
{%- set l3vni = none -%}
|
|
{%- set import_rts = [] -%}
|
|
{%- set export_rts = [] -%}
|
|
{%- if assignment.vrf is defined and assignment.vrf is not none and assignment.vrf.node is not none -%}
|
|
{%- set vrf_node = assignment.vrf.node -%}
|
|
{%- set vrf_name = vrf_node.name.value -%}
|
|
{%- if vrf_node.l3vni is defined and vrf_node.l3vni is not none and vrf_node.l3vni.node is not none -%}
|
|
{%- set l3vni = vrf_node.l3vni.node.vni.value -%}
|
|
{%- endif -%}
|
|
{%- for rt_edge in vrf_node.import_targets.edges -%}
|
|
{%- set _ = import_rts.append(rt_edge.node.target.value) -%}
|
|
{%- endfor -%}
|
|
{%- for rt_edge in vrf_node.export_targets.edges -%}
|
|
{%- set _ = export_rts.append(rt_edge.node.target.value) -%}
|
|
{%- endfor -%}
|
|
{%- endif -%}
|
|
{%- set _ = vrf_vni_list.append({
|
|
"vrf": vrf_name,
|
|
"l3vni": l3vni,
|
|
"route_distinguisher": assignment.route_distinguisher.value | default(none),
|
|
"import_targets": import_rts,
|
|
"export_targets": export_rts
|
|
}) -%}
|
|
{%- endfor -%}
|
|
|
|
{%- set result = {
|
|
"vtep": {
|
|
"source_address": vtep.source_address.value,
|
|
"source_interface": source_iface,
|
|
"udp_port": vtep.udp_port.value,
|
|
"learn_restrict": vtep.learn_restrict.value | default(none),
|
|
"vlan_vni_mappings": vlan_vni_list | sort(attribute='vlan_id'),
|
|
"vrf_vni_mappings": vrf_vni_list
|
|
}
|
|
} -%}
|
|
{%- else -%}
|
|
{%- set result = {
|
|
"vtep": none,
|
|
"vrf_vni_mappings": []
|
|
} -%}
|
|
{%- endif -%}
|
|
{{ result | tojson(indent=2) }}
|