# Dynamically selecting a class (based on poolname)

**URL:** <https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [September 30, 2005, 12:02pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806 "2005-09-30T12:02:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sake\_Blok](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@Sake\_Blok](https://community.f5.com/u/Sake_Blok)\
**Post date:** [September 30, 2005, 12:02pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806/1 "2005-09-30T12:02:52Z")

</div>

Hi,

Is it possible to dynamically select a class? I want to use one class per pool which lists a cookie per server and select that server based on the cookie. Using passive cookies is not an option since I cannot change the cookie-values on the server. Of course I can write an iRule per virtual server, but it would be nicer to have a general one that is able to select the appropriate class.

It would be something like this:

```tcl
rule prefCookie-iRule {
   when CLIENT_ACCEPTED {
      set defpool [LB::server pool]
      log local0. "defpool selected for -> $defpool"
   }
   when HTTP_REQUEST {
     if { [HTTP::cookie exists "lbsid"] } {
        set tmp [findclass [HTTP::cookie "lbsid"] ::$defpool " "]
        if { $tmp ne "" } {
           log local0. $tmp              
           node $tmp
        } else {
           log local0. "[HTTP::cookie "lbsid"] is not a valid cookie-value!"
           pool $defpool
        }
     } else {
        log local0. "No lbsid cookie found!"
        pool $defpool
     }
   }
   when LB_FAILED {
      log local0. "LB failed, so chose a new member!"
      pool $defpool
      LB::reselect
   }
}

```

The problem is that the line ‘set tmp [findclass [HTTP::cookie “lbsid”] ::$defpool " "]’ does not work, it looks like this line is interpreted \_\_before\_\_ $defpool is set. How can I solve this? I guess I need a command that has a class as output and a string as input, but is there anything capable of doing that?

Cheers, Sake

---

<div class="post-metadata">

**Author:** ![drteeth\_127330](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@drteeth\_127330](https://community.f5.com/u/drteeth_127330)\
**Post date:** [September 30, 2005, 5:56pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806/2 "2005-09-30T17:56:43Z")

</div>

You also should be able to do this with the subst command which might not be as heavy weight as eval. subst performs variable substitution, so the argument is expanded twice, once by the TCL interpreter and once by subst. Something like this should work:

```tcl
set tmp [findclass [HTTP::cookie "lbsid"] [subst $::$defpool] " "]

```

---

<div class="post-metadata">

**Author:** ![drteeth\_127330](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@drteeth\_127330](https://community.f5.com/u/drteeth_127330)\
**Post date:** [October 3, 2005, 5:01pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806/3 "2005-10-03T17:01:45Z")

</div>

Did you try my original suggestion?

```tcl
set tmp [findclass [HTTP::cookie "lbsid"] [subst $::$defpool] " "]

```

What you posted is subtly different.

---

<div class="post-metadata">

**Author:** ![unRuleY\_95363](https://avatars.discourse-cdn.com/v4/letter/u/58f4c7/32.png) [@unRuleY\_95363](https://community.f5.com/u/unRuleY_95363)\
**Post date:** [October 3, 2005, 5:04pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806/4 "2005-10-03T17:04:23Z")

</div>

You need to have a $ in front of the ::.

Please use Dr.Teeth’s suggestion and watch the placement of the punctuation.

---

<div class="post-metadata">

**Author:** ![unRuleY\_95363](https://avatars.discourse-cdn.com/v4/letter/u/58f4c7/32.png) [@unRuleY\_95363](https://community.f5.com/u/unRuleY_95363)\
**Post date:** [October 3, 2005, 9:49pm UTC](https://community.f5.com/t/dynamically-selecting-a-class-based-on-poolname/45806/5 "2005-10-03T21:49:51Z")

</div>

BTW, you could still use subst, but like this:

```tcl
set tmp [eval [subst {findclass [HTTP::cookie "lbsid"] \$::$defpool " "}]]

```

This also doesn’t require the extra variable.
