heat_template_version: pike

description: >
  Utility stack to convert an array of services into a set of combined
  role configs.

parameters:
  Services:
    default: []
    description: |
        List nested stack service templates.
    type: comma_delimited_list
  ServiceData:
    default: {}
    description: Dictionary packing service data
    type: json
  ServiceNetMap:
    default: {}
    description: Mapping of service_name -> network name. Typically set
                 via parameter_defaults in the resource registry.  This
                 mapping overrides those in ServiceNetMapDefaults.
    type: json
  EndpointMap:
    default: {}
    description: Mapping of service endpoint -> protocol. Typically set
                 via parameter_defaults in the resource registry.
    type: json
  DefaultPasswords:
    default: {}
    description: Mapping of service -> default password. Used to help
                 pass top level passwords managed by Heat into services.
    type: json
  RoleName:
    default: ''
    description: Role name on which the service is applied
    type: string
  RoleParameters:
    description: Parameters specific to the role
    default: {}
    type: json

resources:

  ServiceChain:
    type: OS::Heat::ResourceChain
    properties:
      resources: {get_param: Services}
      concurrent: true
      resource_properties:
        ServiceData: {get_param: ServiceData}
        ServiceNetMap: {get_param: ServiceNetMap}
        EndpointMap: {get_param: EndpointMap}
        DefaultPasswords: {get_param: DefaultPasswords}
        RoleName: {get_param: RoleName}
        RoleParameters: {get_param: RoleParameters}

  LoggingConfiguration:
    type: OS::TripleO::LoggingConfiguration

  ServiceServerMetadataHook:
    type: OS::TripleO::ServiceServerMetadataHook
    properties:
      RoleData: {get_attr: [ServiceChain, role_data]}

  PuppetStepConfig:
    type: OS::Heat::Value
    properties:
      type: string
      value:
        yaql:
          expression:
            # select 'step_config' only from services that do not have a docker_config
            coalesce($.data.service_names, []).zip(coalesce($.data.step_config, []), coalesce($.data.docker_config, [])).where($[2] = null).where($[1] != null).select($[1]).join("\n")
          data:
            service_names: {get_attr: [ServiceChain, role_data, service_name]}
            step_config: {get_attr: [ServiceChain, role_data, step_config]}
            docker_config: {get_attr: [ServiceChain, role_data, docker_config]}

  DockerConfig:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression:
            # select 'docker_config' only from services that have it
            coalesce($.data.service_names, []).zip(coalesce($.data.docker_config, [])).where($[1] != null).select($[1]).reduce($1.mergeWith($2), {})
          data:
            service_names: {get_attr: [ServiceChain, role_data, service_names]}
            docker_config: {get_attr: [ServiceChain, role_data, docker_config]}

  DockerConfigScripts:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression:
            # select 'docker_config_scripts' only from services that have it
            coalesce($.data.service_names, []).zip(coalesce($.data.docker_config_scripts, [])).where($[1] != null).select($[1]).reduce($1.mergeWith($2), {})
          data:
            service_names: {get_attr: [ServiceChain, role_data, service_names]}
            docker_config_scripts: {get_attr: [ServiceChain, role_data, docker_config_scripts]}

  CellV2Discovery:
    type: OS::Heat::Value
    properties:
      type: boolean
      value:
        yaql:
          expression:
            # If any service in this role requires cellv2_discovery then this value is true
            coalesce($.data.cellv2_discovery, []).contains(true)
          data:
            cellv2_discovery: {get_attr: [ServiceChain, role_data, cellv2_discovery]}

  LoggingSourcesConfig:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        # Transform the individual logging_source configuration from
        # each service in the chain into a global list, adding some
        # default configuration at the same time.
        yaql:
          expression: >
            let(
            default_format => coalesce($.data.default_format, ''),
            pos_file_path => coalesce($.data.pos_file_path, ''),
            sources => coalesce($.data.sources, {}).flatten()
            ) ->
            $sources.where($ != null).select({
            'type' => 'tail',
            'tag' => $.tag,
            'path' => $.path,
            'format' => $.get('format', $default_format),
            'pos_file' => $.get('pos_file', $pos_file_path + '/' + $.tag + '.pos')
            })
          data:
            sources:
              - {get_attr: [LoggingConfiguration, LoggingDefaultSources]}
              - yaql:
                  expression: list(coalesce($.data.role_data, []).where($ != null).select($.get('logging_source')).where($ != null))
                  data: {role_data: {get_attr: [ServiceChain, role_data]}}

              - {get_attr: [LoggingConfiguration, LoggingExtraSources]}
            default_format: {get_attr: [LoggingConfiguration, LoggingDefaultFormat]}
            pos_file_path: {get_attr: [LoggingConfiguration, LoggingPosFilePath]}

  LoggingGroupsConfig:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        # Build a list of unique groups to which we should add the
        # fluentd user.
        yaql:
          expression: >
            set((coalesce($.data.default, []) + coalesce($.data.extra, []) + coalesce($.data.role_data, []).where($ != null).select($.get('logging_groups'))).flatten()).where($)
          data:
            default: {get_attr: [LoggingConfiguration, LoggingDefaultGroups]}
            extra: {get_attr: [LoggingConfiguration, LoggingExtraGroups]}
            role_data: {get_attr: [ServiceChain, role_data]}

  MonitoringSubscriptionsConfig:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          expression: list(coalesce($.data.role_data, []).where($ != null).select($.get('monitoring_subscription')).where($ != null))
          data: {role_data: {get_attr: [ServiceChain, role_data]}}

  ServiceNames:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        filter:
          - [null]
          - {get_attr: [ServiceChain, role_data, service_name]}

  GlobalConfigSettings:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        map_merge:
          yaql:
            expression: list(coalesce($.data.role_data, []).where($ != null).select($.get('global_config_settings')).where($ != null))
            data: {role_data: {get_attr: [ServiceChain, role_data]}}

  ServiceConfigSettings:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression: coalesce($.data.role_data, []).where($ != null).select($.get('service_config_settings')).where($ != null).reduce($1.mergeWith($2), {})
          data: {role_data: {get_attr: [ServiceChain, role_data]}}

  WorkflowTasks:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression: coalesce($.data.role_data, []).where($ != null).select($.get('workflow_tasks')).where($ != null).reduce($1.mergeWith($2), {})
          data: {role_data: {get_attr: [ServiceChain, role_data]}}

  UpgradeTasks:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          # Note we use distinct() here to filter any identical tasks, e.g yum update for all services
          expression: coalesce($.data, []).where($ != null).select($.get('upgrade_tasks')).where($ != null).flatten().distinct()
          data: {get_attr: [ServiceChain, role_data]}

  PostUpgradeTasks:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          # Note we use distinct() here to filter any identical tasks, e.g yum update for all services
          expression: coalesce($.data, []).where($ != null).select($.get('post_upgrade_tasks')).where($ != null).flatten().distinct()
          data: {get_attr: [ServiceChain, role_data]}

  UpdateTasks:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          # Note we use distinct() here to filter any identical tasks, e.g yum update for all services
          expression: coalesce($.data, []).where($ != null).select($.get('update_tasks')).where($ != null).flatten().distinct()
          data: {get_attr: [ServiceChain, role_data]}

  UpgradeBatchTasks:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          expression: coalesce($.data, []).where($ != null).select($.get('upgrade_batch_tasks')).where($ != null).flatten().distinct()
          data: {get_attr: [ServiceChain, role_data]}

  PuppetConfig:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          expression: coalesce($.data, []).where($ != null).select($.get('puppet_config')).where($ != null).distinct()
          data: {get_attr: [ServiceChain, role_data]}

  KollaConfig:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression: coalesce($.data.role_data, []).where($ != null).select($.get('kolla_config')).where($ != null).reduce($1.mergeWith($2), {})
          data: {role_data: {get_attr: [ServiceChain, role_data]}}

  DockerPuppetTasks:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        yaql:
          expression: dict(coalesce($.data, []).where($ != null).select($.get('docker_puppet_tasks')).where($ != null).selectMany($.items()).groupBy($[0], $[1]))
          data: {get_attr: [ServiceChain, role_data]}

  HostPrepTasks:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          # Note we use distinct() here to filter any identical tasks
          expression: coalesce($.data, []).where($ != null).select($.get('host_prep_tasks')).where($ != null).flatten().distinct()
          data: {get_attr: [ServiceChain, role_data]}

outputs:
  role_data:
    description: Combined Role data for this set of services.
    value:
      service_names: {get_attr: [ServiceNames, value]}
      monitoring_subscriptions: {get_attr: [MonitoringSubscriptionsConfig, value]}
      logging_sources: {get_attr: [LoggingSourcesConfig, value]}
      logging_groups: {get_attr: [LoggingGroupsConfig, value]}
      config_settings: {map_merge: {get_attr: [ServiceChain, role_data, config_settings]}}
      global_config_settings: {get_attr: [GlobalConfigSettings, value]}
      service_config_settings: {get_attr: [ServiceConfigSettings, value]}
      workflow_tasks: {get_attr: [WorkflowTasks, value]}
      step_config: {get_attr: [PuppetStepConfig, value]}
      upgrade_tasks: {get_attr: [UpgradeTasks, value]}
      post_upgrade_tasks: {get_attr: [PostUpgradeTasks, value]}
      update_tasks: {get_attr: [UpdateTasks, value]}
      upgrade_batch_tasks: {get_attr: [UpgradeBatchTasks, value]}
      service_metadata_settings: {get_attr: [ServiceServerMetadataHook, metadata]}

      # Keys to support docker/services
      puppet_config: {get_attr: [PuppetConfig, value]}
      kolla_config: {get_attr: [KollaConfig, value]}
      docker_config: {get_attr: [DockerConfig, value]}
      docker_config_scripts: {get_attr: [DockerConfigScripts, value]}
      docker_puppet_tasks: {get_attr: [DockerPuppetTasks, value]}
      host_prep_tasks: {get_attr: [HostPrepTasks, value]}
      cellv2_discovery: {get_attr: [CellV2Discovery, value]}
