Forum Discussion
naladar_65658
Altostratus
Sep 09, 2008Can you refer to a data group from within a switch?
Is it possible to refer to a data group from within a switch? Also, a big thank you to Joe for helping me get this far! You guys are awesome. I have only been at this about a week, but I have learned a ton by reading posts and wiki's here on the f5 site!
Here is a working code snippet:
switch -glob [string tolower [HTTP::uri]] {
"/fc/*" -
"/wpi/*" {
pool WWW2_Pool
}
default {
pool WWW3_Pool
}
Is it possible for me to put those strings into a string type data group (we will call it WWW2_uri) and just call the data group instead of each string? Maybe something like this?:
switch -glob [string tolower [HTTP::uri]] {
$::WWW2_uri {
pool WWW2_Pool
}
default {
pool WWW3_Pool
}
}
I did read the Controlling Bots iRule in the wiki, but I was hoping there was a way to call the external data group without re-writing it in a long if/else statement.
- hoolio
Cirrostratus
I don't think there is a way to combine switch with matchclass. if/else with matchclass seems like the option. I suppose you could get fancy and create a list of the datagroups you want to test against and then loop through them using foreach and exit the loop once you find a match with 'break'. I don't think it would be very elegant though. - naladar_65658
Altostratus
Thanks for the response hoolio. The code I was showing actually used to be written in an if/elseif statement, but with Joe's help I was able to convert it to a switch so it would be more efficient. - hoolio
Cirrostratus
I think a switch and an if/elseif/else chain are pretty close in terms of efficiency. It might be better to have a single datagroup with the URI tokens followed by the corresponding pool. If you need to support wildcards in the class, you can use foreach instead of findclass.class paths_pool_class { "/path1* pool2" "/path2/test* pool2" "/path3/ pool1" }
when HTTP_REQUEST { Check the requested path against the paths pool class foreach element $::paths_pool_class { log local0. "Current \$element: $element" if {[string match -nocase $element [HTTP::path]]}{ log local0. "Matched \$element: $element. Exiting loop" break } set parsed_pool [getfield $element " " 2] log local0. "\$parsed_pool: $parsed_pool" Check if parsed pool is up if {[active_members $parsed_pool]}{ log local0. "pool $parsed_pool has active members" pool $parsed_pool } else { Take some default action if the parsed pool is down? HTTP::redirect "http://maintenance.example.com" } } }
if {[catch {active_members $parsed_pool} result]}{ There was an error log local0. "Error. Catch result: $result" } else { log local0. "OK. Catch result: $result" } Aaron
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects