# iRule for APM Ressource Assignment

**URL:** <https://community.f5.com/t/irule-for-apm-ressource-assignment/18136>\
**Category:** Technical Forums\
**Tags:** irules, big-ip-access-policy-manager-apm, application-delivery\
**Created:** [March 28, 2018, 5:14am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136 "2018-03-28T05:14:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Universal-Inves](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@Universal-Inves](https://community.f5.com/u/Universal-Inves)\
**Post date:** [March 28, 2018, 5:14am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/1 "2018-03-28T05:14:54Z")

</div>

Hello,

we use our F5 as a gateway for RDP sessions. We have a logon page, a radius authentication and then an Advanced Resource Assign to assign the RDP session. Every user has a different RDP connection. I have configured this via VPE and it works.

 ![0691T000006ApqBQAS.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/0/0cb04e435c48173b320b54f387742559a031b305.png)

Now over 200 users should be added and this is wasteful over VPE. That’s why I’m writing an iRule to solve the resource assignment.

![0691T000006ApqCQAS.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/f/f64cffb7e7b12aac48755d7b637b2a85101cbefd.png)

In the Advanced Ressource Assign I only assign a webtop.

My iRule:

```tcl
when ACCESS_POLICY_AGENT_EVENT {
switch [ACCESS::policy agent_id] {
        "do_something" {
        set username [ACCESS::session data get session.logon.last.username]
        set domain "xxx"
        set terminalrdp "/Common/Terminalserver-RDP-pool"
        set c2123 "/Common/C2123"
        set c2124 "/Common/C2124"

        if {$username contains "Testuser"} {
            ACCESS::session data set session.logon.last.domain $domain  
            ACCESS::session data set session.assigned.resources.rd $terminalrdp
            }

        if {$username == "UserX"} {
            ACCESS::session data set session.assigned.resources.rd $c2123
            }

        if {$username == "UserY"} {
            ACCESS::session data set session.assigned.resources.rd $c2124
            }
        }   
    }
}

```

But this doesn´t work as expected and I hope someone can help me. Maybe there is a simpler and better solution for the problem.

---

<div class="post-metadata">

**Author:** ![stan\_piron](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stan\_piron](https://community.f5.com/u/stan_piron)\
**Post date:** [March 28, 2018, 5:21am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/2 "2018-03-28T05:21:12Z")

</div>

How many rdp resources do you want to assign to each user?

For a customer we created only one rdp resource with hostname

```tcl
%{session.logon.username}.company.local

```

and we created dns record for each users!

---

<div class="post-metadata">

**Author:** ![Universal-Inves](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@Universal-Inves](https://community.f5.com/u/Universal-Inves)\
**Post date:** [April 5, 2018, 7:36am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/3 "2018-04-05T07:36:33Z")

</div>

Are there other ways to solve the problem? What’s wrong with my iRule that it doesn`t work?

---

<div class="post-metadata">

**Author:** ![iaine](https://avatars.discourse-cdn.com/v4/letter/i/f07891/32.png) [@iaine](https://community.f5.com/u/iaine)\
**Post date:** [April 5, 2018, 10:28am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/4 "2018-04-05T10:28:34Z")

</div>

Where are the rdp resources stored? If in AD then you can query the attribute and parse the response into APM variables. You can then create RDP links using the APM variable and tailor your APM policy so that the RDP icon is only presented to the user if the APM variable has been populated

---

<div class="post-metadata">

**Author:** ![youssef1](https://avatars.discourse-cdn.com/v4/letter/y/ed655f/32.png) [@youssef1](https://community.f5.com/u/youssef1)\
**Post date:** [April 5, 2018, 12:35pm UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/5 "2018-04-05T12:35:58Z")

</div>

Hello,

you could manage your needs in 2 different ways.

→ Populate RDP User’s in an AD/LDAP attribute (multivalue). and Using Irule to feed dynamic RDP.

→ Or via an irule using a datagroup in order to store your rdp.

Second point I advise you to create 5 (or more depending on your convenience) Dynamic RDP that you will feed according the number of rdp allocated to the user.

all you have to do is feed your dynamics rdp with the IPs addresses of the user machines. by default if the user has only one machine it will feed the other RDP with a non resolvable fqdn and therefore it will not display them on the portal.

for information, when I speak dynamic RDP, I mean that you create an RDP with instead of the fqdn a session variable that you feed according to the user.

I have already done a similar job at a customer if you want help on the subject let me know.

Regards

---

<div class="post-metadata">

**Author:** ![Universal-Inves](https://avatars.discourse-cdn.com/v4/letter/u/ecd19e/32.png) [@Universal-Inves](https://community.f5.com/u/Universal-Inves)\
**Post date:** [April 16, 2018, 10:33am UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/6 "2018-04-16T10:33:42Z")

</div>

Hello, I finally found my mistake, why the iRule never worked. The iRule was only assigned to the access policy in the VPE, but not to the virtual server. After that was solved, the iRule also worked. However, I could only assign a value to each user in the data group. That’s why I wrote the iRule as follows:

```tcl
when ACCESS_POLICY_AGENT_EVENT {
switch [ACCESS::policy agent_id] {
    "ui-rdp" {
        set dynrdp "notresolvable.net"
        set username [ACCESS::session data get session.logon.last.username]

        ACCESS::session data set session.assigned.resources.dynrd1 $dynrdp
        ACCESS::session data set session.assigned.resources.dynrd2 $dynrdp

        switch $username {

            User1 {
                    ACCESS::session data set session.assigned.resources.dynrd1 "Client1"
                    ACCESS::session data set session.assigned.resources.dynrd2 "Server1"}
            User2 {
                    ACCESS::session data set session.assigned.resources.dynrd1 "Client2"
                    ACCESS::session data set session.assigned.resources.dynrd2 "Server2"}
            UserX {
                    ACCESS::session data set session.assigned.resources.dynrd1 "ClientX"}

            }
        }
    }
}

```

I still have 2 problems:

1. even if the client is not resolvable, it will be displayed in the webtop with the name that is not resolvable. Is this possibly a bug in version 13.1, which we use? The function will be described in a Configuration Guide from 11.4
2. I have an RDP with “user defined”, if I enter there the IP of a client, on which the access works I get the error message “Your user account is not listed in the RD Gateway’s permission list”. Does anyone know this error?

Best Regards Tina

---

<div class="post-metadata">

**Author:** ![Abdessamad\_851](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Abdessamad\_851](https://community.f5.com/u/Abdessamad_851)\
**Post date:** [April 16, 2018, 12:34pm UTC](https://community.f5.com/t/irule-for-apm-ressource-assignment/18136/7 "2018-04-16T12:34:48Z")

</div>

I think you can still work with data group. Just define all necessary RDPs in a list in each user entry, and then loop that list and assign all rdp resources.

```tcl
ltm data-group internal myClassName {
    records {
        User1 {
            data "{Client1 Server1}"
        }
        User2 {
            data "{Client2 Server2}"
        }
    }
    type string
}

```

The irule DG part should look like this:

```tcl
set RDPs [class match -value $username equals myClassName]
foreach rdp $RDPs {
    set rdpIndex [lsearch $RDPs rdp]
    set dynrdVar "dynrd"$rdpIndex
    ACCESS::session data set session.assigned.resources.$dynrdVar $rdp
}

```

I didn’t test it, so the code might have to be adjusted a little.

regards.
