Forum Discussion

Kalyan_Reddy_Da's avatar
Kalyan_Reddy_Da
Icon for Nimbostratus rankNimbostratus
Jul 20, 2010

Irule with if-elseif to change to Switch-glob

Irule with if-elseif to change to Switch-glob

 

 

Please help me if anyone know how to change this if-else-if irule to switch-glob.

 

 

 

when HTTP_REQUEST {

 

 

if {([HTTP::cookie "TestCookie"] ne "") && ([HTTP::header "TestHeader"] eq "")} {

 

 

Execute some statements

 

}

 

elseif { [HTTP::cookie "ABCD"] contains "testvaluel" } {

 

 

Execute some statements

 

 

}

 

elseif {[HTTP::header "DEF"] ne "" } {

 

 

Execute some statements

 

 

}

 

elseif { ([HTTP::uri] starts_with "/abc/def/") } {

 

 

Execute some statements

 

 

}

 

}

 

 

 

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Kalyan,

     

     

    A switch statement can only be used to check one string against a number of changes. If you wanted to check the requested URI for several different possible values, you could use a switch statement. It cannot be use to check multiple strings for multiple cases. So the current if/elseif/.../else syntax you're using is the best option.

     

     

    For details on the switch statement, you can check the DC and TCL wikis:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/switch

     

    http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm

     

     

    Aaron
  • Thank you Aaron for the quick reply. Appreciate your help.

     

    Aaron,

     

     

     

    Could you please let me know whether this can be changed to switch?

     

     

    set pool_name p_test_$TEST

     

    if { [catch { pool $pool_name } ] } {

     

    Execute some commands

     

    } else {

     

    pool $pool_name

     

    }

     

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Catch can only ever return 0 or 1 based on whether the command it's executing returns an error or not, so I don't think there is any advantage to using switch. But if you wanted to, you could do this:

    
    set pool_name p_test_$TEST
    switch [catch { pool $pool_name } ] {
       0 {
           No error, so the pool was already assigned and you don't have to do anything more
       }
       1 {
           Error, so do something else
          Execute some commands
       }
    }
    

    Note that catch will run the command in the braces. So you don't have to call the pool command again. You could simplify your original rule like this:

    
    set pool_name p_test_$TEST
    if { [catch { pool $pool_name } ] } {
        Error assigning pool $pool_name, so do something else
    }
    

    Aaron