iRules if / switch / class builder

Describe one decision as rules and get it written as an if / elseif chain, a switch and a class match with its tmsh data groups, then see which pool each picks for a test value. Shows where first match and longest match disagree. Local, no network.

Reti

Rules, in order

  1. 1
  2. 2
  3. 3
Try

The forms disagree. Tested with /api/v2/users

  • if / elseif → api_pool
  • switch → api_pool
  • class match → api_v2_pool
  • Rule 2 can never be chosen by if or switch: rule 1 comes first and matches every value rule 2 matches.
  • The rules use more than one comparison, so the class form needs one data group per comparison, looked up in the order the comparisons first appear.
  • class match with starts_with or ends_with returns the longest matching entry, whatever the order of the rules (F5's class reference). if and switch take the first rule that matches, in order.

if / elseif

Chose for the test value: api_pool

when HTTP_REQUEST priority 500 {
set value [string tolower [HTTP::path]]
if { ${value} starts_with "/api" } {
pool api_pool
} elseif { ${value} starts_with "/api/v2" } {
pool api_v2_pool
} elseif { ${value} eq "/login" } {
pool login_pool
} else {
pool web_pool
}
}

Conditions are tested from the top; the first true one wins.

switch

Chose for the test value: api_pool

when HTTP_REQUEST priority 500 {
switch -glob -- [string tolower [HTTP::path]] {
/api* {
pool api_pool
}
/api/v2* {
pool api_v2_pool
}
/login {
pool login_pool
}
default {
pool web_pool
}
}
}

Patterns are tested from the top; the first match wins. The -- keeps a value that starts with a dash from being read as an option (F5 K15650046).

class match

Chose for the test value: api_v2_pool

when HTTP_REQUEST priority 500 {
set value [string tolower [HTTP::path]]
set target [class match -value -- ${value} starts_with path_pools_starts_with]
if { ${target} eq "" } {
set target [class match -value -- ${value} equals path_pools_equals]
}
if { ${target} ne "" } {
pool ${target}
} else {
pool web_pool
}
}
The data groups (tmsh)
ltm data-group internal path_pools_starts_with {
records {
/api {
data api_pool
}
/api/v2 {
data api_v2_pool
}
}
type string
}
ltm data-group internal path_pools_equals {
records {
/login {
data login_pool
}
}
type string
}

Each lookup returns the matching entry's value, here the pool name. The data groups must exist on the BIG-IP; their tmsh definitions are below.

API endpointGEThttps://ronutz.com/api/v1/f5-irules-conditional-builderDocumented, not served. Opens the specification.

Riferimenti

Appears in these workflows