Forum Discussion

Alan_Johnson_FB's avatar
Alan_Johnson_FB
Icon for Nimbostratus rankNimbostratus
Jan 29, 2019

Convert language set iRule to LTM Policy

Hi folks,

Hoping someone can offer some advice on converting an iRule to a LTM Policy. The rule is used to set a cookie which specifies either English or Spanish language on one of our websites. I have very limited experience writing iRules or Policies (the iRule in question was written by a consultant for us long ago).

Below is the existing iRule:

 Determine whether to write the cookie
when HTTP_REQUEST {
     Log our host and IP address
     log local0. "Host = [HTTP::host]; Client = [IP::client_addr]"

     Set our values based on host name, then by cookie's existence + value
    if { [HTTP::host] contains "es.test.company.com" } {
     Does cookie exist? Is its value 'es'?
        if { ([HTTP::cookie exists "i18next"]) && ([HTTP::cookie value i18next] equals "es") } {
             log local0. "Cookie exists and is set properly to 'en'"
            set write_cookie "0"
        } else {
         log local0. "Cookie will be written as 'es'"
         Flag to write a cookie
            set write_cookie "1"
        set value "es"
        }
    } else {
     Does cookie exist? Is its value 'en'?
        if { ([HTTP::cookie exists "i18next"]) && ([HTTP::cookie value i18next] equals "en") } {
             log local0. "Cookie exists and is set properly to 'en' "
            set write_cookie "0"
        } else {
             log local0. "Cookie will be written as 'en'"

         Flag to write a cookie
            set write_cookie "1"
            set value "en"
        }
    }

}

 Now we set/update the cookie
when HTTP_RESPONSE {
    if { $write_cookie == "1" } {
         log local0. "Setting cookie to $value"
        HTTP::cookie insert name "i18next" value $value domain "company.com" path "/"
           HTTP::cookie secure i18next enable
    }
}

...and here is what I've come up with so far for a LTM Policy (which isn't working):

ltm policy es.test.company.com-locality {
    requires { http }
    rules {
        domain_es {
            actions {
                0 {
                    http-set-cookie
                    response
                    insert
                    domain company.com
                    name i18next
                    path /
                    value es
                }
            }
            conditions {
                0 {
                    http-host
                    host
                    values { es.test.company.com }
                }
                1 {
                    http-cookie
                    name i18next
                    not
                    values { es }
                }
            }
            ordinal 2
        }
        domain_es_i18next_es {
            conditions {
                0 {
                    http-host
                    host
                    values { es.test.company.com }
                }
                1 {
                    http-cookie
                    name i18next
                    values { es }
                }
            }
            ordinal 1
        }
        en {
            actions {
                0 {
                    http-set-cookie
                    response
                    insert
                    domain company.com
                    name i18next
                    path /
                    value en
                }
            }
            conditions {
                0 {
                    http-host
                    host
                    not
                    values { es.test.company.com }
                }
                1 {
                    http-cookie
                    name i18next
                    not
                    contains
                    values { en }
                }
            }
            ordinal 4
        }
        i18next_en {
            conditions {
                0 {
                    http-host
                    host
                    not
                    values { es.test.company.com }
                }
                1 {
                    http-cookie
                    name i18next
                    contains
                    values { en }
                }
            }
            ordinal 3
        }
    }
    strategy all-match
}

Thanks!

  • Here is an LTM Policy I got working that should fit your needs.

    ltm policy set_language {
        last-modified 2019-01-31:10:49:10
        requires { http }
        rules {
            set_en {
                actions {
                    0 {
                        http-set-cookie
                        response
                        insert
                        name i18next
                        value en
                    }
                    1 {
                        log
                        response
                        write
                        facility local0
                        message "Set EN Triggered"
                        priority info
                    }
                }
                conditions {
                    0 {
                        http-cookie
                        name i18next
                        not
                        values { en }
                    }
                }
            }
            set_es {
                actions {
                    0 {
                        http-set-cookie
                        response
                        insert
                        name i18next
                        value es
                    }
                    1 {
                        log
                        response
                        write
                        facility local0
                        message "Set ES triggered"
                        priority info
                    }
                }
                conditions {
                    0 {
                        http-host
                        host
                        contains
                        values { es.test.company.com }
                    }
                    1 {
                        http-cookie
                        name i18next
                        not
                        values { es }
                    }
                }
                ordinal 1
            }
        }
        status published
        strategy all-match
    }