<template name="cisco_nxos_interface_config" results="per_template">
<doc>
Template to parse Cisco NX-OS interfaces configuration and normalize it to a
flat list of dictionaries suitable for Netbox import.

This template requires output of 'show running-config interface'.

The transform macro returns a list of dictionaries where each dictionary
contains the following keys (missing values are set to `null` / `None`):

- `name`: interface name string (e.g. Ethernet1/1, Port-channel10)
- `type`: ``other`` by default; ``bridge`` if "vlan" in name;
    ``lag`` if "port-channel" in name; ``virtual`` if "loopback", "tunnel",
    or "nve" in name, or if the interface name contains a dot (`.`)
- `enabled`: boolean; interfaces are `True` by default unless explicitly
    shutdown in the config
- `parent`: parent interface name or `null` (if the interface name contains
    a dot the parent is the part before the dot)
- `lag_id`: lag identifier integer (channel-group / vPC number) or `null`
- `lag_type`: ``lag`` for standard port channels, ``mlag`` for vPC port
    channels, or `null`
- `lacp_mode`: LACP mode string (e.g. ``active``, ``passive``) or `null`
- `lag`: Name of parent LAG interface e.g. `Port-channel10` or `null`
- `mtu`: integer MTU or `null`
- `mac_address`: string in MAC EUI format or `null`
- `speed`: integer speed in kbit/s or `null`
- `duplex`: string or `null`
- `description`: string (empty string when not set)
- `mode`: ``tagged`` / ``access`` or `null`
- `untagged_vlan`: integer or `null`
- `tagged_vlans`: list of integers (empty list when none)
- `qinq_svlan`: always `null`
- `vrf`: string or `null`
- `ipv4_addresses`: list of strings with IP/prefix (e.g. 10.0.0.1/24)
- `ipv6_addresses`: list of strings with IP/prefix (e.g. 2001:db8::1/64)

Example normalized output (YAML):

```yaml
- description: Server access
  duplex: null
  enabled: true
  ipv4_addresses: []
  ipv6_addresses: []
  lacp_mode: active
  lag: Port-channel10
  lag_id: 10
  lag_type: lag
  mac_address: null
  mode: access
  mtu: null
  name: Ethernet1/1
  parent: null
  qinq_svlan: null
  speed: null
  tagged_vlans: []
  type: other
  untagged_vlan: 100
  vrf: null
```

</doc>

<input>
commands = [
    "show running-config interface"
]
platform = [
    "cisco_nxos",
    "nxos",
]
</input>

<macro>
def transform_interfaces_to_records(data):
    from ttp_templates.utils.cisco_nxos_process_show_running_config_interface import transform_interfaces_config

    return transform_interfaces_config(data)
</macro>

## ------------------------------------------------------------------------------------------
## Interfaces configuration
## ------------------------------------------------------------------------------------------
<group functions="contains('name')">
interface {{ name | _start_ }}
  description {{ description | re(".*") | default("") }}
  no shutdown {{ enabled | set(True) | default(True) }}
  shutdown {{ enabled | set(False) }}
  mtu {{ mtu | to_int }}
  vrf member {{ vrf }}
  no switchport {{ is_l3_interface | set(True) }}
  switchport mode trunk {{ mode | set("tagged") }}
  switchport mode access {{ mode | set("access") }}
  switchport access vlan {{ untagged_vlan | to_int | let("mode", "access") }}
  switchport trunk native vlan {{ untagged_vlan | to_int }}
  switchport trunk allowed vlan {{ tagged_vlans | unrange(rangechar='-', joinchar=',') | split(",") | joinmatches }}
  switchport trunk allowed vlan add {{ tagged_vlans | unrange(rangechar='-', joinchar=',') | split(",") | joinmatches }}
  encapsulation dot1q {{ dot1q | to_int | let("mode", "tagged") }}
  channel-group {{ lag_id | to_int | let("lag_type", "lag") }} mode {{ lacp_mode }}
  vpc {{ lag_id | to_int | let("lag_type", "mlag") }}
  mac-address {{ mac_address | mac_eui }}
  speed {{ speed | to_int }}
  duplex {{ duplex }}

  <group name="ipv4_addresses*" method="table">
  ip address {{ ip | _exact_ }}/{{ mask }}
  ip address {{ ip | _exact_ }}/{{ mask }} secondary
  ip address {{ ip | _exact_ }} {{ mask }}
  ip address {{ ip | _exact_ }} {{ mask }} secondary
  </group>

  <group name="ipv6_addresses*" method="table">
  ipv6 address {{ ip | _exact_ }}/{{ mask }}
  </group>

!{{ _end_ }}
</group>

<output macro="transform_interfaces_to_records"/>

</template>
