heat_template_version: rocky

parameters:
  ControlPlaneIp:
    default: ''
    type: string
  ControlPlaneSubnetCidr:
    default: ''
    description: >
      The subnet CIDR of the control plane network. (The parameter is
      automatically resolved from the ctlplane subnet's cidr attribute.)
    type: string
{%- for network in networks if network.enabled|default(true) %}
  {{network.name}}Ip:
    default: ''
    type: string
  {{network.name}}IpSubnet:
    description: 'IP address/subnet on the {{network.name_lower}} network'
    default: ''
    type: string
  {{network.name}}IpUri:
    default: ''
    type: string
    description: IP address with brackets in case of IPv6
  {{network.name}}NetName:
    default: {{network.name_lower}}
    description: The name of the {{network.name_lower}} network.
    type: string
{%- endfor %}

resources:

  NetIpMapValue:
    type: OS::Heat::Value
    properties:
      type: json
      value:
        map_replace:
          - ctlplane: {get_param: ControlPlaneIp}
            ctlplane_subnet:
              list_join:
                - ''
                - - {get_param: ControlPlaneIp}
                  - '/'
                  - {get_param: ControlPlaneSubnetCidr}
            ctlplane_uri:
                str_replace:
                  template:
                    make_url:
                      host: {get_param: ControlPlaneIp}
                  params:
                    //: ''
{%- for network in networks if network.enabled|default(true) %}
            {{network.name_lower}}: {get_param: {{network.name}}Ip}
            {{network.name_lower}}_subnet: {get_param: {{network.name}}IpSubnet}
            {{network.name_lower}}_uri: {get_param: {{network.name}}IpUri}
{%- 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}
              {{network.name_lower}}_subnet:
                str_replace:
                  template: NAME_subnet
                  params:
                    NAME: {get_param: {{network.name}}NetName}
              {{network.name_lower}}_uri:
                str_replace:
                  template: NAME_uri
                  params:
                    NAME: {get_param: {{network.name}}NetName}
{%- endfor %}

outputs:
  net_ip_map:
    description: >
      A Hash containing a mapping of network names to assigned IPs
      for a specific machine.
    value: {get_attr: [NetIpMapValue, value]}
