Product Documentation

Optional properties

May 24, 2018

In some cases, a property of a component takes its value from an expression, which can be a simple expression such as a parameter reference, or a more complex one. Setting this property value is optional in the component. You can choose to set the property value only if the expression returns an actual value, else you can choose not to set this property.

For example, consider that one of the properties you want to set is the lbmethod (loadbalancing algorithm) of a component whose type is ns::lbvserver. The value of the property lbmethod is taken from a parameter value provided by the user, as shown below:

components

  -

      name: lbvserver_comp

      type: ns::lbvserver

      properties:

           name: $parameters.lb-appname + "-lb"

           servicetype: $parameters.lb-service-type

           ipv46: $parameters.lb-virtual-ip

           port: 80

           lbmethod: $parameters.lb-advanced.algorithm

Now, consider that the parameter lb-advanced.algorithm is an optional parameter. And, if the user does not provide a value for this parameter because it is optional, the expression $parameters.lb-advanced.algorithm evaluates to blank value. Therefore, an invalid value is passed for the lbmethod property. In order to avoid such a situation, you can annotate the property as optional by suffixing its name with "?" as follows:

components

   -

      name: lbvserver_comp

      type: ns::lbvserver

      properties:

          name: $parameters.lb-appname + "-lb"

          servicetype: $parameters.lb-service-type

          ipv46: $parameters.lb-virtual-ip

          port: 80

          lbmethod?: $parameters.lb-advanced.algorithm

 

The use of "?" omits the property if the expression on the right evaluates to nothing, which would be equivalent, in this case, to a component defined as follows:

components

  -

      name: lbvserver_comp

      type: ns::lbvserver

      properties:

           name: $parameters.lb-appname + "-lb"

           servicetype: $parameters.lb-service-type

           ipv46: $parameters.lb-virtual-ip

           port: 80

     

Because lbmethod is optional, omitting it still makes this a valid component. Note that lbmethod might take its default value if one is defined in its type "ns::lbvserver."