Juniper / jsnapy

Python version of Junos Snapshot Administrator
Apache License 2.0
116 stars 58 forks source link

Iterate not iterating #332

Open mihaigabi opened 5 years ago

mihaigabi commented 5 years ago

I am trying to get the junos snapshot version on an EX switch but I can't make the tool to display the correct "snapshot-medium". It is always the first in the XML. Am I doing it wrong? Thanks!

The configuration:

test-snapshot:
  - rpc: get-snapshot-information
  - args:
      media: internal
  - iterate:
      xpath: "//snapshot-information/software-version/package[normalize-space(package-name)= 'junos']"
      id: "../../snapshot-medium"
      tests:
        - contains: package-version, 14.1
          info: "Test Succeeded! The snapshot version for <{{post['package-name']}}> on medium <{{id_0}}> is <{{post['package-version']}}>!"
          err: "Test Failed! The snapshot version for <{{post['package-name']}}> on medium <{{id_0}}> is <{{post['package-version']}}>!"

The result:

*************************RPC is get-snapshot-information*************************
----------------------Performing contains Test Operation----------------------
Test Succeeded! The snapshot version for <junos> on medium <internal (/dev/da0s1a) (primary)> is <ex-14.1X53-D42.3>!
Test Succeeded! The snapshot version for <junos> on medium <internal (/dev/da0s1a) (primary)> is <ex-14.1X53-D35.3>!

The XML:

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/14.1X53/junos">
    <multi-routing-engine-results>

        <multi-routing-engine-item>

            <re-name>fpc0</re-name>

            <snapshot-information>
                <snapshot-medium>      internal (/dev/da0s1a) (primary)</snapshot-medium>
                <creation-date>May 10 17:47:04 2017</creation-date>
                <software-version>
                    <package>
                        <package-name>jais</package-name>
                        <package-version>7.0R1.0</package-version>
                    </package>
                    <package>
                        <package-name>jdocs-ex</package-name>
                        <package-version>14.1X53-D42.3</package-version>
                    </package>
                    <package>
                        <package-name>junos</package-name>
                        <package-version>ex-14.1X53-D42.3</package-version>
                    </package>
                    <package>
                        <package-name>junos-ex-4300</package-name>
                        <package-version>14.1X53-D42.3</package-version>
                    </package>
                    <package>
                        <package-name>jweb-ex</package-name>
                        <package-version>14.1X53-D42.3</package-version>
                    </package>
                </software-version>
                  <snapshot-medium>      internal (/dev/da0s2a) (backup)</snapshot-medium>
                <creation-date>Mar 13 14:35:33 2017</creation-date>
                 <software-version>
                    <package>
                        <package-name>jdocs-ex</package-name>
                        <package-version>14.1X53-D35.3</package-version>
                    </package>
                    <package>
                        <package-name>junos</package-name>
                        <package-version>ex-14.1X53-D35.3</package-version>
                    </package>
                    <package>
                        <package-name>junos-ex-4300</package-name>
                        <package-version>14.1X53-D35.3</package-version>
                    </package>          
                    <package>
                        <package-name>jweb-ex</package-name>
                        <package-version>14.1X53-D35.3</package-version>
                    </package>
                </software-version>
            </snapshot-information>
        </multi-routing-engine-item>

    </multi-routing-engine-results>
    <cli>
        <banner>{master:0}</banner>
    </cli>
</rpc-reply>
sidhujasminder commented 5 years ago

@mihaigabi What is the output you are expecting?

mihaigabi commented 5 years ago

I would like to get the below output:

The snapshot version for on medium <internal (/dev/da0s1a) (primary)> is ! The snapshot version for on medium <internal (/dev/da0s2a) (backup)> is !

Thanks!

rahkumar651991 commented 4 years ago

The snapshot-medium is not a part of software-version but a sibling to it. So with the above implementation, it will always take the first value of snapshot-medium as the id-value(ex - /dev/da0s1a in above dataset).

You may try with something like the below.

test:
   - rpc: get-snapshot-information
   - args:
      media: internal
   - iterate:
      xpath: "//snapshot-information/software-version/package[normalize-space(package-name)='junos']"
      id: "../preceding-sibling::snapshot-medium[1]"
      tests:
          - contains: package-version, 14.1
            info: "Test Succeeded! The snapshot version for <{{post['package-name']}}> on medium <{{id_0}}> is <{{post['package-version']}}>!"
            err: "Test Failed! The snapshot version for <{{post['package-name']}}> on medium <{{id_0}}> is <{{post['package-version']}}>!"

I tested it locally and gives the desired output as expected. This workaround should help.