# \[class exists $redir\_class\]

**URL:** <https://community.f5.com/t/class-exists-redir-class/21159>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [November 18, 2011, 8:38pm UTC](https://community.f5.com/t/class-exists-redir-class/21159 "2011-11-18T20:38:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlg\_23340](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@dlg\_23340](https://community.f5.com/u/dlg_23340)\
**Post date:** [November 18, 2011, 8:38pm UTC](https://community.f5.com/t/class-exists-redir-class/21159/1 "2011-11-18T20:38:50Z")

</div>

Hello, all. I have an irule that needs to check if a class exists, then if it does, pull a record from it. If not, it should do nothing. in this case, the value contained in redir\_class does not exist as a class name.

```tcl
if { [class exists $redir_class] } {
        log local0. { [class match -value -- [HTTP::uri] starts_with $redir_class]}
      } else {
        log local0. { class $redir_class does not exist}
      }

```

I would expect to see " class x does not exist" in my logs, but instead i get this:

“TCL error: testirule - Could not find class x”

If i replace the [class match… with a simple text string to log, i get the expected behavior. Why would it try to process code in a block that doesn’t match?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![George\_Watkins](https://avatars.discourse-cdn.com/v4/letter/g/7993a0/32.png) [@George\_Watkins](https://community.f5.com/u/George_Watkins)\
**Post date:** [November 18, 2011, 9:37pm UTC](https://community.f5.com/t/class-exists-redir-class/21159/2 "2011-11-18T21:37:33Z")

</div>

Hi David,

It looks like you’re almost there. One thing that I think may be throwing you off is the logging messages. The curly braces around your log commands will prevent variable substitution and command execution. Here is the iRule and datagroup I used to test with.

```tcl
ltm data-group internal /Common/test_vars {
    records {
        /var1 {
            data 4
        }
        /var2 {
            data 8
        }
    }
    type string
}

ltm rule /Common/test_vars_class_exists_2156869 {
when HTTP_REQUEST {
  set redir_class test_vars

  if { [class exists $redir_class] } {
    log local0. " [class match -value -- [HTTP::uri] starts_with $redir_class]"
  } else {
    log local0. " class $redir_class does not exist"
  }
}
}

```

Here are the results I got while testing various URIs:

```tcl
http://test-http-vs/var1 => Nov 18 07:35:42 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 : 4
http://test-http-vs/var2 => Nov 18 07:35:49 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 : 8
http://test-http-vs/foo1 => Nov 18 07:35:55 tmm info tmm[14987]: Rule /Common/test_vars_class_exists_2156869 :

```

-George

---

<div class="post-metadata">

**Author:** ![smp\_86112](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@smp\_86112](https://community.f5.com/u/smp_86112)\
**Post date:** [November 21, 2011, 2:54pm UTC](https://community.f5.com/t/class-exists-redir-class/21159/3 "2011-11-21T14:54:43Z")

</div>

With the exact same class and rule, what happens if you set redir\_class to something other than “test\_vars”? That’s the case I think he is trying to demonstrate.

---

<div class="post-metadata">

**Author:** ![George\_Watkins](https://avatars.discourse-cdn.com/v4/letter/g/7993a0/32.png) [@George\_Watkins](https://community.f5.com/u/George_Watkins)\
**Post date:** [November 21, 2011, 7:59pm UTC](https://community.f5.com/t/class-exists-redir-class/21159/4 "2011-11-21T19:59:48Z")

</div>

I’m getting the expected behavior when $redir\_class is undefined or the referenced class doesn’t exist on v11.0.0 (build 8037):

```tcl
Nov 21 06:01:50 tmm err tmm[20592]: 01220001:3: TCL error: /Common/test_vars_class_exists_2156869 - can't read "redir_class": no such variable while executing "class exists $redir_class"
Nov 21 06:02:14 tmm info tmm[20592]: Rule /Common/test_vars_class_exists_2156869 : class ttt does not exist

```

This sounds like it could be a problem with the version of LTM you are running. What version are you testing on?

-George

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [November 22, 2011, 10:32am UTC](https://community.f5.com/t/class-exists-redir-class/21159/5 "2011-11-22T10:32:25Z")

</div>

mine is 10.2.3.

```tcl
[root@ve1023:Active] config b version|grep -iA 1 version
BIG-IP Version 10.2.3 112.0
Final Edition
[root@ve1023:Active] config b virtual bar list
virtual bar {
   snat automap
   pool foo
   destination 172.28.19.79:80
   ip protocol 6
   rules myrule
   profiles {
      http {}
      tcp {}
   }
}
[root@ve1023:Active] config b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set redir_class test_vars
        if {[class exists $redir_class]}{
                log local0. "[class match -value -- [HTTP::uri] starts_with $redir_class]"
        } else {
                log local0. "class $redir_class does not exist"
        }
}
}

[root@ve1023:Active] config curl -I http://172.28.19.79/var1
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:21 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:07 local/tmm info tmm[24220]: Rule myrule : 4

[root@ve1023:Active] config curl -I http://172.28.19.79/var2
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:24 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:10 local/tmm info tmm[24220]: Rule myrule : 8

[root@ve1023:Active] config b rule myrule list
rule myrule {
   when HTTP_REQUEST {
        set redir_class fake_test_vars
        if {[class exists $redir_class]}{
                log local0. "[class match -value -- [HTTP::uri] starts_with $redir_class]"
        } else {
                log local0. "class $redir_class does not exist"
        }
}
}

[root@ve1023:Active] config curl -I http://172.28.19.79/var1
HTTP/1.1 404 Not Found
Date: Tue, 22 Nov 2011 11:35:58 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

[root@ve1023:Active] config  
Nov 22 03:35:43 local/tmm info tmm[24220]: Rule myrule : class fake_test_vars does not exist

```
