{# If both udp_ports and tcp_ports appear in the item, \
   udp_ports are used in the rsyslog config and tcp_ports are dropped. \
   If both udp_ports and tcp_ports are not configured, \
   it is default to 'tcp_ports: [514]'. #}
{% if item.udp_ports | d([]) %}
# Log messages from remote hosts over UDP
{%   set __logging_loop_index = loop.index %}
input(name="{{ item.name }}" type="imudp" port=["{{ item.udp_ports | join('","') }}"])
{{ lookup('template', 'input_template.j2') }}
{% else %}
{%   if not item.tcp_ports | d([]) %}
{%     set item_tcp_ports = ["514"] %}
{%   else %}
{%     set item_tcp_ports = item.tcp_ports %}
{%   endif %}
{%   set rsyslog_flows = logging_flows | d([ {"name": "default_flow", "inputs": [ item.name ], "outputs": ["default_files"]} ], true) %}
{%   set outdict = {} %}
{%   for flow in rsyslog_flows %}
{%     if flow.inputs | intersect([ item.name ]) %}
{%       for oname in flow.outputs %}
{%         set _ = outdict.__setitem__(oname, outdict.get(oname,[])|union([ item.name ])) %}
{%       endfor %}
{%     endif %}
{%   endfor %}
{%   for tport in item_tcp_ports %}
{%     set __logging_loop_index = loop.index %}
{%     if not item.tls | d(false) | bool %}
# Log messages from remote hosts over plain TCP
input(name="{{ item.name }}_{{ __logging_loop_index }}" type="imptcp" port="{{ tport }}")
{%     else %}
# Log messages from remote hosts over TLS
input(name="{{ item.name }}_{{ __logging_loop_index }}" type="imtcp" port="{{ tport }}")
{%     endif %}
{%     for output in rsyslog_outputs %}
{%       if outdict[output.name] | d(false) %}
if
{%         for inputname in outdict[output.name] %}
{%           if inputname == item.name %}
{%             if not loop.first %}
  or
{%             endif %}
  ($inputname == "{{ item.name }}_{{ __logging_loop_index }}")
{%           endif %}
{%         endfor %}
  then {
    call {{ output.name }}
}
{%       endif %}
{%     endfor %}
{%   endfor %}
{% endif %}
