heat_template_version: rocky

parameters:
  ControlPlaneIpList:
    default: []
    type: comma_delimited_list
{%- for network in networks if network.enabled|default(true) %}
  {{network.name}}IpList:
    default: []
    type: comma_delimited_list
  {{network.name}}NetName:
    default: {{network.name_lower}}
    description: The name of the {{network.name_lower}} network.
    type: string
{%- endfor %}
  EnabledServices:
    default: []
    type: comma_delimited_list
  ServiceNetMap:
    default: {}
    type: json
  ServiceHostnameList:
    default: []
    type: comma_delimited_list
  NetworkHostnameMap:
    default: []
    type: json
  NovaAdditionalCell:
    default: false
    description: Whether this is an cell additional to the default cell.
    type: boolean

conditions:
  is_cell: {equals: [{get_param: NovaAdditionalCell}, true]}

resources:
   # This adds the extra "services" on for keystone
   # so that keystone_admin_api_network and
   # keystone_public_api_network point to the correct
   # network on the nodes running the "keystone" service
  EnabledServicesValue:
    type: OS::Heat::Value
    properties:
      type: comma_delimited_list
      value:
        yaql:
          expression: let(root => $) -> $.data.extra_services.items().where($[0] in $root.data.enabled_services).select($[1]).flatten() + $root.data.enabled_services
          data:
            enabled_services: {get_param: EnabledServices}
            extra_services:
              # If anything other than keystone needs this
              # then we should add an extra_networks interface
              # to the service templates role_data but for
              # now we hard-code the keystone special case
              keystone:
                - keystone_admin_api
                - keystone_public_api

  NetIpMapValue:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        map_replace:
          - ctlplane: {get_param: ControlPlaneIpList}
{%- for network in networks if network.enabled|default(true) %}
            {{network.name_lower}}: {get_param: {{network.name}}IpList}
{%- endfor %}
          - keys:
              ctlplane: ctlplane  # Ensure one key is present, in case no enabled networks
{%- for network in networks if network.enabled|default(true) %}
              {{network.name_lower}}: {get_param: {{network.name}}NetName}
{%- endfor %}

outputs:
  net_ip_map:
    description: >
      A Hash containing a mapping of network names to assigned lists
      of IP addresses.
    value: {get_attr: [NetIpMapValue, value]}
  ctlplane_service_ips:
    description: >
      Map of enabled services to a list of their ctlplane IP addresses
    value:
      map_merge:
        repeat:
          template:
            SERVICE_ctlplane_node_ips: $$ groups['SERVICE'] | default ([]) | map('extract', hostvars, 'ctlplane_ip') | list | to_json $$
          for_each:
            SERVICE: {get_attr: [EnabledServicesValue, value]}
