Expressions

One of the most powerful features of StyleBook is the use of expressions. You can use StyleBooks expressions in various scenarios to compute dynamic values. The example below shows an expression to concatenate a parameter value with a literal string.

Example:

      $parameters.appname + “-mon”

This expression retrieves the parameter named appname, and concatenates it with the string “-mon”.

The following types of expressions are supported:

localized image

Note

You can type cast expressions where strings can be converted into numbers and numbers can be converted to strings. Similarly, a tcp-port can be cast to a number, and an IP address can be cast to a string.

You must use a delimiter before and after any operator. You can use the following delimiters:

Before an operator: space, tab, comma, (, ), [, ]

After an operator: space, tab, (, [

For example:

abc + def

100 % 10

10 > 9

Expression Type Validation

StyleBook engine now allows for stronger type checking during compile time, that is, the expressions used while writing the StyleBook are validated during the import of StyleBook itself rather than while creating the configuration pack.

All references to parameters, substitutions, components, properties of components, outputs of components, user-defined variables (repeat-item, repeat-index, arguments to substitution functions,) and so on are all validated for their existence and types.

Example of Type Checks:

In the following example, the expected type of port property of lbvserver StyleBook is tcp-port. In NetScaler MA Service earlier releases, the StyleBook compiler computed the value as a string and the StyleBook was imported and executed. Now, type validations happen at compile-time (import-time). The compiler finds that string and tcp-port are not compatible types and therefore, the StyleBook compiler throws an error and fails import or migration of the StyleBook.

components:

  -

    name: lbvserver-comp

    type: ns::lbvserver

    properties:

      name: mylb

      ipv46: 10.102.190.15

      port: str(“80”)

      servicetype: HTTP

You should now declare this as a number for the compiler to successfully compile this StyleBook.

  port: 80

Example of Flagging Invalid Expressions:

In earlier releases, when an invalid expression was assigned to a property name, the compiler did not detect invalid expressions and allowed the StyleBooks to be imported into NetScaler MAS. Now if this StyleBook is imported to NetScaler MAS, the compiler will identify such invalid expressions and flag it. As a result, the StyleBook will not be imported to NetScaler MAS.

In this example, the expression assigned to name property in lb-sg-binding-comp component is:  $components.lbvserver-comp.properties.lbvservername. However, there is no property called lbvservername in component lbvserver-comp. In earlier NetScaler MA Service releases, the compiler would have allowed this expression and successfully imported it. The actual failure would happen when a user wants to create a configuration pack using this StyleBook. However now, this kind of error is identified during import and the StyleBook is not imported to NetScaler MAS. You must manually correct such errors and import the StyleBooks.

components:

  -

    name: lbvserver-comp

    type: ns::lbvserver

    properties:

      name: mylb

      ipv46: 10.102.190.15

      port: 80

      servicetype: HTTP

  -

    name: sg-comp

    type: ns::servicegroup

    properties:

      servicegroupname: mysg

      servicetype: HTTP

  -

    name: lb-sg-binding-comp

    type: ns::lbvserver_servicegroup_binding

    condition: $parameters.create-binding

    properties:

      name: $components.lbvserver-comp.properties.lbvservername

      servicegroupname: $components.sg-comp.properties.servicegroupname

Indexing Lists

Items of a list can be accessed now by indexing them directly:

   
Expression Description
$components.test-lbs[0] Refers to the first item in test-lbs component
$components.test-lbs[0].properties.p1 Refers to property p1 of the first item in test-lbs component
$components.lbcomps[0].outputs.servicegroups[1].properties.servicegroupname Refers to property servicegroupname of the second item in servicegroups component, which is an output from the first item of lbcomps component

Expressions

In this article