Forum Discussion

Livius's avatar
Livius
Icon for Cirrus rankCirrus
Jan 19, 2017

Error code when no conditions match in iRule

Hi,

 

I have the following iRule atached to a default HTTP profile

 

ltm rule /Common/test1_iRule {
    when RULE_INIT
{
   set static::RoutingInfo "test1"
   log local0. "------------------------------ INIT ------------------------------------"
   log local0. "RoutingInfo is  $RoutingInfo"
}

when XML_CONTENT_BASED_ROUTING
{
   for {set i 0} { $i < $XML_count } {incr i} {
       Routing by RoutingLabel
      log local0. "XML_queries $XML_queries($i)"  
      log local0. "XML_values $XML_values($i)"  
      if { ($XML_queries($i) contains "RoutingLabel")} {
         if { ($XML_values($i) eq "test1")} {
            set static::RoutingInfo "test1"
            pool test1_p
            break
         } elseif {($XML_values($i) eq "test1_encr")} {
            set static::RoutingInfo "test1_encr"
            pool test1_encr_p
            break
         } elseif {($XML_values($i) contains "SOAPtest")} {
            set static::RoutingInfo "SOAPtest"
            pool test1_sim_p
            break
         } elseif {($XML_values($i) contains "test2")} {
            set static::RoutingInfo "test2"
            pool test2_p
            break
         }

      }
   }
   log local0. "RoutingInfo is $static::RoutingInfo after selecting pools"
}

I need to send HTTP error code 422 in case there is no value in the request, or a value that matches my conditions. I tried with various "else" conditions but no luck yet..

 

Regards.

 

1 Reply

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Would this work for you?

    ltm rule /Common/test1_iRule {
        when RULE_INIT {
            log local0. "------------------------------ INIT ------------------------------------"
        }
    
        when XML_CONTENT_BASED_ROUTING {
            set ValueFound 0
            set RoutingInfo "0"
            for {set i 0} { $i < $XML_count } {incr i} {
                 Routing by RoutingLabel
                log local0. "XML_queries $XML_queries($i)" 
                log local0. "XML_values $XML_values($i)" 
                if { ($XML_queries($i) contains "RoutingLabel")} {
                    if { ($XML_values($i) eq "test1")} {
                       pool test1_p
                       set RoutingInfo "test1"
                       set ValueFound 1
                       break
                    } elseif {($XML_values($i) eq "test1_encr")} {
                       pool test1_encr_p
                       set ValueFound 1
                       set RoutingInfo "test1_encr"
                       break
                    } elseif {($XML_values($i) contains "SOAPtest")} {
                       pool test1_sim_p
                       set RoutingInfo "SOAPtest"
                       set ValueFound 1
                       break
                    } elseif {($XML_values($i) contains "test2")} {
                       pool test2_p
                       set ValueFound 1
                       set RoutingInfo "test2"
                       break
                    }
                }
            }
            log local0. "RoutingInfo is $RoutingInfo after selecting pools"
        }
    
        when LB_FAILED {
            if { $ValueFound == 0 } {
                 HTTP::respond 422 Connection close
            }
       }
    }
    

    ?