Getting Conditional Data Using PyEZ Tables

233 Views Asked by At

I'm trying to create a custom table/view for Junos interfaces. Yes, I know it already exists as a built-in/default table but this is a learning exercise that I plan to expand on later.

The problem is that I want to only grab the inet family address, so that is the ifa-local only if the address-family stanza includes address-family/address-family-name of inet.

Is there a way to do this from within my view YAML, or do I have to grab all address-family stanzas with my view and programmatically only keep the inet stanza in my python script? And how would that work?

Here is the XML structure (with unimportant bits removed for clarity):

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1F6/junos">
    <interface-information xmlns="http://xml.juniper.net/junos/15.1F6/junos-interface" junos:style="normal">
        <physical-interface>
            <name>ge-0/0/9</name>
            <logical-interface>
                <name>ge-0/0/9.0</name>
                <address-family>
                    <address-family-name>inet</address-family-name>
                    <mtu>3986</mtu>
                    <route-table>0</route-table>
                    <interface-address>
                        <ifa-destination>10.0.2.88/30</ifa-destination>
                        <ifa-local>10.0.2.89</ifa-local>
                        <ifa-broadcast>10.0.2.91</ifa-broadcast>
                    </interface-address>
                </address-family>
                <address-family>
                    <address-family-name>iso</address-family-name>
                    <mtu>3983</mtu>
                    <route-table>0</route-table>
                </address-family>
            </logical-interface>
        </physical-interface>
    </interface-information>
</rpc-reply>

This is my YAML:

JunosDCDiscoveryInterfaces:
    rpc: get-interface-information
    item: physical-interface/logical-interface
    key: name
    view: JunosDCDiscoveryInterfacesView

JunosDCDiscoveryInterfacesView:
    fields:
        name: name
        description: description
        active: { if-config-flags/iff-up : flag }
        # ipv4_address only if address-family/address-family-name is 'inet'
        ipv4_address: {address-family/interface-address/ifa-local}

Finally, this is the python code I'd be using to display each interface on the router:

logical_interfaces = JunosDCDiscoveryInterfaces(client.device).get()
for logical_interface in logical_interfaces:
    print(f'{logical_interface.name} {logical_interface.active} {logical_interface.description} {logical_interface.ipv4_address}')
0

There are 0 best solutions below