heat_template_version: wallaby

description: >
  Creates a port on the {{network.name}} network. The IP address will be chosen
  automatically if FixedIPs is empty.

parameters:
  RootStackName:
    description: The name of the stack/plan.
    type: string
  {{network.name}}NetName:
    description: The name of the {{network.name_lower}} network.
    default: {{network.name_lower|default(network.name|lower)}}
    type: string
  PortName:
    description: Name of the port
    default: ''
    type: string
  DnsName:
    description: DNS name of the port
    default: ''
    type: string
  ControlPlaneIP: # Here for compatibility with noop.yaml
    description: IP address on the control plane
    default: ''
    type: string
  ControlPlaneSubnetCidr: # Here for compatibility with noop.yaml
    default: ''
    description: >
      The subnet CIDR of the control plane network. (The parameter is
      automatically resolved from the ctlplane subnet's cidr attribute.)
    type: string
  ControlPlaneNetwork: # Here for compatibility with ctlplane_vip.yaml
    description: The name of the undercloud Neutron control plane
    default: ctlplane
    type: string
  ControlPlaneSubnet: # Here for compatibility with ctlplane_vip.yaml
    description: The name of the undercloud Neutron control plane subnet
    default: ctlplane-subnet
    type: string
  FixedIPs:
    description: >
        Control the IP allocation for the VIP port. E.g.
        [{'ip_address':'1.2.3.4'}]
    default: []
    type: json
  IPPool: # Here for compatibility with from_pool.yaml
    default: {}
    type: json
  NodeIndex: # Here for compatibility with from_pool.yaml
    default: 0
    type: number
  PortDeletionPolicy:
    description: >
      Whether to retain or delete the neutron port resource on deletion of the
      stack
    type: string
    default: delete
    constraints:
      - allowed_values: [delete, retain]
  DefaultRoute:
    description: >
      Whether this interface is used for the default route
    type: boolean
    default: false
  IsVirtualIP:
    type: boolean
    default: false
  Role:
    description: >
      The TripleO Role Name
    type: string
    default: ''

conditions:
  network_is_ctlplane:
    equals:
    - get_param: {{network.name}}NetName
    - ctlplane
  fixed_ip_not_set:
    equals:
    - yaql:
        expression: $.data.where($.get('ip_address'))
        data: {get_param: FixedIPs}
    - []
  net_is_ctlplane_and_fixed_ip_not_set:
    and:
    - network_is_ctlplane
    - fixed_ip_not_set

resources:

  {{network.name}}Port:
    type: OS::Neutron::Port
    deletion_policy: {get_param: PortDeletionPolicy}
  {%- if set_external_id|default(false) and network.external_resource_vip_id|default('') %}
    external_id: {{network.external_resource_vip_id}}
  {%- endif %}
    properties:
      network: {get_param: {{network.name}}NetName}
      name: {get_param: PortName}
      dns_name: {get_param: DnsName}
      fixed_ips:
        if:
        - net_is_ctlplane_and_fixed_ip_not_set
        - [{subnet: {get_param: ControlPlaneSubnet}}]
        - get_param: FixedIPs
      replacement_policy: AUTO
      tags:
        if:
        - {get_param: IsVirtualIP}
        - - str_replace:
              template: tripleo_vip_net=$NET_NAME
              params:
                $NET_NAME: {get_param: {{network.name}}NetName}
          - str_replace:
              template: tripleo_stack_name=$STACK_NAME
              params:
                $STACK_NAME: {get_param: RootStackName}
        - - str_replace:
              template: tripleo_stack_name=$STACK_NAME
              params:
                $STACK_NAME: {get_param: RootStackName}
          - str_replace:
              template: tripleo_default_route=$BOOL
              params:
                $BOOL: {get_param: DefaultRoute}
          - str_replace:
              template: tripleo_role=$ROLE
              params:
                $ROLE: {get_param: Role}

outputs:
  ip_address:
    description: {{network.name}} network IP
    value: {get_attr: [{{network.name}}Port, fixed_ips, 0, ip_address]}
  ip_address_uri:
    description: {{network.name}} network IP (with brackets for IPv6 URLs)
    value:
      str_replace:
        template:
          make_url:
            host: {get_attr: [{{network.name}}Port, fixed_ips, 0, ip_address]}
        params:
          //: ''
  ip_subnet:
    description: IP/Subnet CIDR for the {{network.name}} network IP
    value:
          list_join:
            - ''
            - - {get_attr: [{{network.name}}Port, fixed_ips, 0, ip_address]}
              - '/'
              - {str_split: ['/', {get_attr: [{{network.name}}Port, subnets, 0, cidr]}, 1]}
