Product Documentation

Repeat construct

May 24, 2018

You can use the repeat construct of a component to build multiple configuration objects of the same type.

In the following example, the members-svcg-comp component is used to bind the list of services to the service group created by the parent component. In order to create a configuration object that binds each server to the service group, use the repeat construct to iterate over the list of services specified for the parameter svc-servers. During the iteration, the component creates a NITRO object of type servicegroup_servicegroupmember_binding for each service (referred to as srv in the repeat-item construct) in the service group, and it sets the ip attribute in each NITRO object to the IP address of the corresponding service.

Example:

components:

   -

      name: my-lbvserver-comp

      type: ns::lbvserver

      properties:

           name: $parameters.name + "-lb"

           servicetype: HTTP

           ipv46: $parameters.ip

           port: 80

           lbmethod: $parameters.lb-alg

          components:

                -

                   name: my-svcg-comp

                   type: ns::servicegroup

                   properties:

                        name: $parameters.name + "-svcgrp"

                        servicetype: HTTP

                  components:

                         -

                         name: lbvserver-svg-binding-comp

                         type: ns::lbvserver_servicegroup_binding

                         properties:

                               name: $parent.parent.properties.name

                               servicegroupname: $parent.properties.name

                         -

                         name: members-svcg-comp

                         type: ns::servicegroup_servicegroupmember_binding

                         repeat:

                              repeat-list: $parameters.svc-servers

                              repeat-item: srv

                         properties:

                                ip: $srv

                                port: $parameters.svc-port

                                servicegroupname: $parent.properties.name


The repeat is an object by itself, and repeat-list and repeat-item are attributes for the repeat object.

  • repeat-list is a mandatory attribute that identifies the list that the component iterates on.
  • repeat-item is optional, and is used to give a friendly name to the current item in the iteration.

If not specified, the current item can be accessed using the expression $repeat-item. The last component in the above example can also be written as follows:

          -

          name: members-svcg-comp

          type: ns::servicegroup_servicegroupmember_binding

          repeat:

               repeat-list: $parameters.svc-servers

          properties:

                 ip: $repeat-item

                 port: $parameters.svc-port

                 servicegroupname: $parent.properties.name

 

In addition to being able to refer to the current item white iterating over a list, it is also possible to refer to the current index of the item in the list using repeat-index. In the following example, repeat-index is used to calculate a port number based on the current index:

             name: services

             type: ns::service

             repeat:

                repeat-list: $parameters.app-services

                repeat-item: srv

             properties:

                 ip: $parameters.app-ip

                 port: $parameters.base-port + repeat-index

                 servicegroupname: $parent.properties.name



Similar to the repeat-item construct, you can assign a different variable name to refer to the current index of the iteration. The previous example is equivalent to the following example:

     -

             name: services

             type: ns::service

             repeat:

                    repeat-list: $parameters.app-services

                    repeat-item: srv

                    repeat-index: idx

             properties:

                 ip: $parameters.app-ip

                 port: $parameters.base-port + $idx

                 servicegroupname: $parent.properties.name