Reply

Examples of each XML array command.

Disturbedend
Techie
Posts: 7
3145     0

All,

 

I am having trouble intepreting the XML schema mostly because it does not give examples of command use I think.

 

So far I have mode some progress on defining and array and pushing values into it:

 

  <!---#################################################################-->
  <!---Define Variables-->
  <!---#################################################################-->

  <Expr op='array' output='BadSNMPPollCommunities' value='public'/>



  <!---#################################################################-->
  <!---Push Variables-->
  <!---#################################################################-->
  <Expr op='push'>
    <Expr value='Private'/>
    <Expr value='Admin'/>
    <Expr value='operator'/>
    <Expr variable='BadSNMPPollCommunities'/>
  </Expr>

 

 

I seem to start running into issues when using any of the other array functions specifically searching the array to see if a matching entry exists:

 

 

  <ConfigBlockCheck block-end='^' block-start='/^snmp-server community ([0-9a-zA-Z]+) RO ACL-SNMP-READ/i' boundary-method='regexp' end-on-block-start='true'>
    <If>
      <Expr op='in'>
        <Expr variable='_start_match_1'/>
        <Expr variable='BadSNMPPollCommunities'/>
      </Expr>
      <Then>
        <Assign variable='err-Status'>
          <Expr op='concat'>
            <Expr variable='err-Status'/>
            <Expr value='Failed: Device has INvalid SNMP RO community defined with ACL:ACL-SNMP-READ specified: '/>
          </Expr>
        </Assign>
      </Then>

 

 

When I do the debug it does not seem to be expanding the second expression as described in the XLM schema so maybe i am not understanding the format for expressions and sub expressions?:

 

<xs:documentation>If the first expression is a field that returns an IP address, or is explicitly given type ‘ip’, then the second operator will be treated as a CIDR range, and this operator will return true if the IP is in the CIDR. If the type of the first expression is not ‘ip’, then the second expression will be treated as a comma-delimited string, and split on the commas. This operator will return true if the first expression is in the list generated by splitting the second expression. </xs:documentation>

 

I would appreciate if someone could post and example usage of each of the following:

 

 

■  Support for arrays. Array indices begin at Zero. In XML rules, the Expr element can use the following operators to work with arrays:

array
    

Convert child expressions into an array.

element-at
    

Return the element at a specific array index.

push
    

Add one or more elements to the end of an array.

pop
    

Remove and return the last element of an array.

unshift
    

Add one or more elements to the beginning of an array.

shift
    

Remove and return the element at the beginning of an array.

join
    

Convert an array to a delimited string.

size
    

Return the size of an array.

delete-at
    

Remove an element at a location in the array, moving later elements inward. Returns the removed element.

insert-at
    

Insert an element at a specified location in the array, moving later elements outward.

assign-at
    

Sets the value of an element in a specified location in the array, overwriting the existing element.

in
    

Determine if an array contains a specific value.

not-in
    

Determine if an array does not contain a specific value.

 

 

 

Specific to the IN operation I have also tried the following and the debug does not give a true or false:

 

    <If>
      <Expr op="in" value="_start_match_1,BadSNMPPollCommunities"/>
      <Then>
        <Assign variable="err-Status">
          <Expr op="concat">
            <Expr variable="err-Status"/>
            <Expr value="Failed: Device has a Security Violating SNMP RO community defined with ACL:ACL-SNMP-READ specified: "/>
            <Expr variable="_start_match_1"/>
          </Expr>
        </Assign>
      </Then>
      <Else/>
    </If>

 

instead it returns:

<_start_match_1,BadSNMPPollCommunities>



    <If>
      <Expr op='in' value='_start_match_1,BadSNMPPollCommunities'>
      </Expr> result value <_start_match_1,BadSNMPPollCommunities>
      <Then>
        <Assign variable='err-Status'>
          <Expr op='concat'>

 

Re: Examples of each XML array command.

Adviser
Posts: 357
3146     0

Please take a look at the examples here: https://github.com/infobloxopen/netmri-toolkit/tree/master/policy

 

For array operations, I believe the first element should be the array. In your example, it is the last element, so that won't work.

Re: Examples of each XML array command.

Disturbedend
Techie
Posts: 7
3146     0

Digging in, I tried both but the schema says the second value is the comma delemited array it would search through:

 

"then the second expression will be treated as a comma-delimited string,"

Re: Examples of each XML array command.

Adviser
Posts: 357
3146     0
I was referring to your first example, with the ”push”. Can you post a complete file?

Re: Examples of each XML array command.

Disturbedend
Techie
Posts: 7
3146     0

<PolicyRuleLogic editor="raw-xml" xmlns='http://www.infoblox.com/NetworkAutomation/1.0/ScriptXml'>
  <!---#################################################################-->
  <!---Define Variables-->
  <!---#################################################################-->
  <Expr op='array' output='CUSTGoodSNMPPollCommunitiesRO' value='goodRO'/>
  <Expr op='array' output='CUSTGoodSNMPPollCommunitiesRW' value='goodRW'/>
  <Expr op='array' output='CUSTGoodSNMPPollCommunitiesDeny' value='badcom'/>
  <Expr op='array' output='CUSTGoodSNMPTrapCommunities' value='goodTrap'/>
  <Expr op='array' output='CUSTGoodSNMPPollTrapCommunities' value='goodRO'/>
  <!---#################################################################-->
  <!--- Start TRAP receivers-->
  <!---#################################################################-->
  <Expr op='array' output='CUSTGoodSNMPTrapIPs' value='10.1.1.58'/>
  <Expr op='push'>
    <Expr value='10.1.1.44'/>
    <Expr variable='CUSTGoodSNMPTrapIPs'/>
  </Expr>
  <Expr op='size'>
    <Expr variable='CUSTGoodSNMPTrapIPs'/>
  </Expr>
  <!---#################################################################-->
  <!-- END TRAP receivers-->
  <!---#################################################################-->
  <Expr op='array' output='BadSNMPPollCommunities' value='public'/>
  <Expr op='push'>
    <Expr value='Private'/>
    <Expr value='Admin'/>
    <Expr value='operator'/>
    <Expr variable='BadSNMPPollCommunities'/>
  </Expr>
  <Expr op='array' output='BadSNMPTrapIPs' value='1.1.1.1'/>
  <Expr op='array' output='BadSNMPTrapCommunities' value='public'/>
  <Expr op='push'>
    <Expr value='Private'/>
    <Expr value='Admin'/>
    <Expr value='operator'/>
    <Expr variable='BadSNMPTrapCommunities'/>
  </Expr>
  <Assign variable='err-Status'>
    <Expr value='Error messages:'/>
  </Assign>
  <Assign variable='pass-Status'>
    <Expr value='Pass messages:'/>
  </Assign>
  <!---#################################################################-->
  <!---END Define Variables-->
  <!---#################################################################-->
  <ConfigBlockCheck block-end='^' block-start='/^snmp-server community ([0-9a-zA-Z]+) RO ACL-SNMP-READ/i' boundary-method='regexp' end-on-block-start='true'>
    <If>
      <Expr op='in' value='_start_match_1,CUSTGoodSNMPPollCommunitiesRO'/>
      <Then>
        <Assign variable='pass-Status'>
          <Expr op='concat'>
            <Expr variable='pass-Status'/>
            <Expr value='Device has Valid SNMP RO community defined with ACL:ACL-SNMP-READ specified. '/>
          </Expr>
        </Assign>
      </Then>
      <Else/>
    </If>
    <If>
      <Expr op='in' value='_start_match_1,BadSNMPPollCommunities'/>
      <Then>
        <Assign variable='err-Status'>
          <Expr op='concat'>
            <Expr variable='err-Status'/>
            <Expr value='Failed: Device has a Security Violating SNMP RO community defined with ACL:ACL-SNMP-READ specified: '/>
            <Expr variable='_start_match_1'/>
          </Expr>
        </Assign>
      </Then>
      <Else/>
    </If>
  </ConfigBlockCheck>
  <!---#################################################################-->
  <!---Check for Fail-->
  <!---#################################################################-->
  <If>
    <Expr op='does-not-contain'>
      <Expr variable='err-Status'/>
      <Expr value='Failed'/>
    </Expr>
    <Then>
      <Return>
        <PolicyRulePass>
          <Expr variable='pass-Status'/>
        </PolicyRulePass>
      </Return>
    </Then>
    <Else>
      <Assign variable='err-Status'>
        <Expr op='concat'>
          <Expr variable='err-Status'/>
          <Expr variable='pass-Status'/>
        </Expr>
      </Assign>
      <Return>
        <PolicyRuleFail>
          <Expr variable='err-Status'/>
        </PolicyRuleFail>
      </Return>
    </Else>
  </If>
</PolicyRuleLogic>

Re: Examples of each XML array command.

Adviser
Posts: 357
3146     0

Ok! I see the issue. Writing this:

 

 

  <Expr op='in' value='_start_match_1,CUSTGoodSNMPPollCommunitiesRO'/>

 

Actually just creates a constant expression with that "value". What you want is:

 

 

  <Expr op='in'>
    <Expr variable='_start_match_1'/>
    <Expr variable='CUSTGoodSNMPPollCommunitiesRO'/>
  </Expr>

Now, I think that should work. I checked the code, and the second operand (ie, second 'variable' Expr) can be an array; if not, it will be converted to a string and split on ',' to make it into an array. But it can be one to start out, so this should work.

 

Re: Examples of each XML array command.

Disturbedend
Techie
Posts: 7
3146     0

Thank you but I now seem to be having issues with adding to the array with the push. Can check the array declaration and the pushes that are attempting to add to it as they dont seem to be properly adding for me using the code I have provided.

 

Note: this is really why i wanted to see an example of declaring and array, pushing to the array, searching the array for a match.

Re: Examples of each XML array command.

Adviser
Posts: 357
3146     0

The array has to be first in the push. Here is an example:

 

https://github.com/infobloxopen/netmri-toolkit/blob/master/policy/L3-Ifc-Descr.xml#L35

 

John

Re: Examples of each XML array command.

Disturbedend
Techie
Posts: 7
3146     0

I tried that and it is not working. Seriously I would love it if Infoblox would provide actualy examples of use in their documentation. along with the descriptions. Below is my code and debug output. I am adding specific string values to the array and not referencing another variable like the link to your code showed. Any chance you could moc up the code I provided and assist me in the request of getting a functional example of all the array expressions shown in the actual thread:

 

!!Code:

  <Expr op='array' output='BadSNMPPollCommunities' value='public'/>
  <Expr op='push'>
    <Expr variable='BadSNMPPollCommunities'/>
    <Expr value='private'/>
    <Expr value='admin'/>
    <Expr value='operator'/>
  </Expr>
  <Expr op='size'>
    <Expr variable='BadSNMPPollCommunities'/>
  </Expr>
  <Expr op='push'>
    <Expr variable='BadSNMPPollCommunities'/>
    <Expr value='private'/>
  </Expr>
  <Expr op='size'>
    <Expr variable='BadSNMPPollCommunities'/>
  </Expr>


!!Debug output below this line:
  </Expr> set $BadSNMPPollCommunities to result value <public>
  <Expr op='push'>
    <Expr variable='BadSNMPPollCommunities'>
    </Expr> result value <public>
    <Expr value='private'>
    </Expr> result value <private>
    <Expr value='admin'>
    </Expr> result value <admin>
    <Expr value='operator'>
    </Expr> result value <operator>
  </Expr> result value <["public", "private", "admin", "operator"]>
  <Expr op='size'>
    <Expr variable='BadSNMPPollCommunities'>
    </Expr> result value <public>
  </Expr> result value <6>
  <Expr op='push'>
    <Expr variable='BadSNMPPollCommunities'>
    </Expr> result value <public>
    <Expr value='private'>
    </Expr> result value <private>
  </Expr> result value <["public", "private"]>
  <Expr op='size'>
    <Expr variable='BadSNMPPollCommunities'>
    </Expr> result value <public>
  </Expr> result value <6>

 

 

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You