Product Documentation

Built-in functions

May 24, 2018

Expressions in StyleBooks can make use of built-in functions.

For example, you can use the built-in function, str() to transform a number to a string.

str($parameters.order)

Or, you can use the built-in function, int() to transform a string into an integer.

int($parameters.priority)

The following is the list of built-in functions supported in StyleBook expressions with examples of how they can be used:

 

Function Description Examples of Functions in StyleBook Expressions

str()

The str function transforms the input argument to a string value.

Allowed argument types:

  • string
  • number
  • TCP-port
  • boolean
  •  IP address

Examples:

  • "set-" + str(10) returns "set-10"
  • str(10) returns "10"
  • str(1.1.1.1) returns "1.1.1.1"
  • str(True) returns "True"
  • str(mas) returns "mas"

int()

The int function takes a string, number, or tcp-port as an argument and returns an integer.

Examples:

  • int("10") returns 10
  • int(10) returns 10

bool()

The bool function takes any type as an argument. If the argument value is false, empty, or non-existent, this function returns false. Otherwise, it returns true.

Examples:

  • bool(true) returns "true"
  • bool(false) returns "false"
  • bool($parameters.a) returns false if the $parameters.a is false, empty, or not present. Otherwise, it returns true.

len()

The len function takes a string or a list as an argument, and returns the number of characters in a string or the number of items in a list.

Example 1:

If you define a substitution as follows:

items: ["123", "abc", "xyz"]

len($substitutions.items) returns 3

Example 2:

len("netscaler mas") returns 13

Example 3:

len($parameters.vips) returns 3 if $parameters.vip is assigned a value ['1.1.1.1', '1.1.1.2', '1.1.1.3']

min()

The min function takes either a list or a series of numbers or tcp-ports as arguments, and returns the smallest item.

Examples with a series of numbers/tcp-ports:

  • min(80, 100, 1000) returns 80
  • min(-20, 100, 400) returns -20
  • min(-80, -20, -10) returns -80
  • min(0, 100, -400) returns -400

Examples with a list of numbers/tcp-ports:

  • Support $parameters.ports is list of tcp-ports and has value: [80, 81, 8080].

           min($parameters.ports) returns 80.

max()

The max function takes either a list or a series of numbers or tcp-ports as arguments, and returns the largest item.

Examples with a series of numbers/tcp-ports:

  • max(80, 100, 1000) returns 1000
  • max(-20, 100, 400) returns 400
  • max(-80, -20, -10) returns -10
  • max(0, 100, -400) returns 100

Examples with a list of numbers/tcp-ports:

  • Support $parameters.ports is list of tcp-ports and has value: [80, 81, 8080].

           max($parameters.ports) returns 8080.

bin()

The bin function takes a number as an argument, and returns a string that represents the number in binary format.

Examples of expressions:

  • bin(100) returns "0b1100100"

 

oct()

The oct function takes a number as an argument, and returns a string that represents the number in octal format.

Examples of expressions:

  • oct(100) returns "0144"

hex()

The hex function takes a number as an argument, and returns a lowercase string that represents the number in hexadecimal format.

Examples of expressions:

  • hex(100) returns "0x64"

lower()

The lower function takes a string as an argument and returns the same string in lowercase.

Example:

  • lower("MAS") returns "mas"

upper()

The upper function takes a string as an argument and returns the same string in uppercase.

Example:

  • upper("netscaler_mas") returns "NETSCALER_MAS"

sum()

The sum function takes a list of numbers or tcp-ports as arguments and returns the sum of the numbers in the list.

Example 1:

If you define a substitution as follows:

substitutions:

  list-of-numbers:

     - 11

     - 22

     - 55

sum($substitutions.list-of-numbers) returns 88 

Example 2:

If $parameters.ports is [80, 81, 82],

sum($parameters.ports) returns 243

pow()

The "power" function takes two numbers as arguments and returns a number that represents the first argument raised to the power of the second one.

Example:

  • pow(3,2) returns 9

ip()

The ip function takes a string or an ipaddress as argument returns the IP address based on the input value.

Examples:

  • ip("2.1.1.1") returns "2.1.1.1"
  • ip(3.1.1.1) returns "3.1.1.1"

base64.encode

The base64.encode function takes a string argument and returns the base64 encoded string.

Example:

  • base64.encode("abcd") returns "YWJjZA=="

base64.decode

The base64.decode function takes base64 encoded string as an argument and returns the decoded string.

Example:

  • base64.decode("YWJjZA==") returns "abcd"

exists

The exists function takes an argument of any type and returns a Boolean. The return value is True if the input has any value. The return value is False If the input argument does not have a value (i.e., no value).

Consider that the $parameters.monitor is an optional parameter. If you provide a value to this parameter when creating a configpack, exists($parameters.monitor) returns True. Otherwise, it returns False.

filter

The filter function takes two arguments.

  • Argument 1: a substitution function that takes one argument and returns a Boolean value.
  • Argument 2: a list.

The function returns a subset of the original list where each element evaluates to "True" when passed to the substitution function in the first argument.

Example:

Suppose we have defined a substitution function as follows.

substitutions:

  x(a): $a != 81

 

This function returns True if the input value is not equal to 81. Otherwise, it returns False.

Suppose $parameters.ports is [81, 80, 81, 89].

filter($substitutions.x, $parameters.ports) returns [80, 89] by removing all occurrences of 81 from the list.

if-then-else

The function if-then-else takes three arguments.

  • Argument 1: Boolean expression
  • Argument 2: Any expression
  • Argument 3: Any expression (optional)

If the expression in argument 1 evaluates to True, the function returns the value of the expression provided as argument 2.

Otherwise, if argument 3 is provided, the function returns the value of the expression in argument 3. If argument 3 is not provided, the function returns no value.

 

Example 1:

if-then-else($parameters.servicetype == HTTP, 80, 443) returns "80" if $parameters.servicetype has value "HTTP." Otherwise, the function returns "443".

Example 2:

if-then-else($parameters.servicetype == HTTP, $parameters.hport, $parameters.sport) returns the value of "$parameters.hport" if $parameters.servicetype has value "HTTP." Otherwise, the function returns the value of "$parameters.sport."

Example 3:

if-then-else($parameters.servicetype == HTTP, 80) returns "80" if $parameters.servicetype has value "HTTP." Otherwise, the function returns no value.

join

The join function takes two arguments:

  • Argument 1: list of numbers, tcp-ports, strings, or ipaddresses
  • Argument 2: delimiter string (optional)

The function joins the elements of the list provided as argument one into a string, where each element is separated by the delimiter string provided as argument two. If argument two is not provided, then elements in the list are joined together as one string.

Example:

$parameters.ports is [81, 82, 83].

  •  With delimiter argument: join($parameters.ports, '-' returns "81-82-83"
  • Without delimiter argument: join($parameters.ports) returns "818283"

map

The map function takes two arguments;

  • Argument 1: Any function
  • Argument 2: A list of elements.

 

The function returns a list where each element in the list is the result of applying the map-function (argument one) to the corresponding element in argument two.

 

Allowed functions in argument 1:

  • Built-in functions that take one argument: base64.encode, base64.decode, bin, bool, exists, hex, int, ip, len, lower, upper, oct, quotewrap, str, trim, upper, url.encode, url.decode
  • Substitution functions that take at least one argument.

 

Example:

Suppose $parameters.nums is [81, 82, 83].

  • Map using a built-in function, str

map(str, $parameters.nums) returns ["81", "82", "83"]

The result of the map function is the list of strings where each element is string is computed by applying the str function on the corresponding element in the input list ($parameters.nums).

  • Map using a substitution function

substitutions:

  add-10(port): $port + 10

Expression:

 map($substitutions.add-10, $parameters.nums) returns a list of numbers: [ 91, 92, 93 ]

The result of this map function is a list of numbers, each element is computed by applying the substitution function $substitutions.add-10 on the corresponding element in the input list ($parameters.nums).

 

quotewrap

The quotewrap function takes a string as argument and returns a string after adding double quote character before and after the input value.

Example:

  • quotewrap("mas") returns "\"mas\""

replace

The replace function takes three arguments:

  • Argument 1: string
  • Argument 2: string
  • Argument 3: string (optional)

The function replaces all the occurrences of argument two with argument three in argument one.

If argument three is not provided, all occurrences of argument two are removed from argument one (in other words, replaced with empty string).

 

 

Replace a substring with another substring:

 

  • replace('abcdef', 'def', 'xyz') returns "abcxyz". All occurrences of "def" are replaced by "xyz".
  • replace('abcdefabc', 'def') returns "abcabc". As there is no third argument, "def" is removed from the resulting string.

 

 

trim

The trim function returns a string where the leading and trailing whitespaces are stripped from the input string.

Example:

  • trim(' abc    ') returns "abc"

truncate

The truncate function takes two arguments:

  • Argument 1: string
  • Argument 2: number

The function returns a string where the input string in argument one is truncated to the length specified by argument two.

Example:

  • truncate('netscaler mas', 9) returns "netscaler"

url.encode

The url.encode function returns a string where characters are transformed using ASCII character set according to RFC 3986.

Example:

  • url.encode("a/b/c") returns "a%2Fb%2Fc"

url.decode

The url.decode function returns a string where the URL encoded argument is decoded in to a regular string according to RFC 3986.

Example:

  • url.decode("a%2Fb%2Fc") returns "a/b/c"
is_ipv4()
The is_ipv4 function takes an IP address as an argument and returns "True" if the IP address is of the IPv4 format.
is_ipv4(10.10.10.10) returns "True"
is_ipv6()
The is_ipv6 function takes an IP address as an argument and returns "True" if the IP address is of the IPv6 format.
is_ipv6(2001:DB8::) returns "True"