[%
# vi: ft=tt2

# Return an array of hashes, each hash containing dispatcher_id and ips,
# which is an array of shared IPs, for a given cluster set type.
#
# @param argv.role      The role of element we are interested in (rtp, lb).
# @param argv.type      The type of the interfaces (rtp_int, sip_int...).
# @param argv.status    node status [ online, offline, inactive ]
#                         default value: ['online', 'inactive']
# @return out           The array of hashes.

IF !argv.status.size;
  argv.status = ['online', 'inactive'];
END;

status = {
  online = 0
  offline = 0
  inactive = 0
};
FOREACH val IN argv.status;
  status.$val = 1;
END;

out = [];

FOREACH set IN cluster_sets.sets;
  blktmp.processed_hosts = {};
  theset = { dispatcher_id = set.dispatcher_id };
  theset.ips = [];

  FOREACH host IN hosts.keys.sort;
    NEXT UNLESS status.item(hosts.$host.status);
    NEXT IF blktmp.processed_hosts.$host.defined;
    NEXT UNLESS ngcp.has_role(host, argv.role);
    peer = hosts.$host.peer;
    FOREACH iface IN hosts.$host.interfaces;
      NEXT UNLESS hosts.$host.$iface.cluster_sets.grep('^' _ set.name _ '$').size();
      NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size();
      blktmp.processed_hosts.$peer = 1;
      FOREACH ip IN hosts.$host.$iface.shared_ip;
        NEXT UNLESS ip;
        theset.ips.push(ip) UNLESS theset.ips.defined(ip);
      END;
    END;
  END;

  FOREACH instance IN instances;
    NEXT UNLESS status.item(instance.status);
    NEXT UNLESS argv.role == instance.label;
    host = instance.host;
    FOREACH iface IN instance.interfaces;
      ifname = iface.name;
      NEXT UNLESS hosts.$host.$ifname.cluster_sets.grep('^' _ set.name _ '$').size();
      NEXT UNLESS iface.type.grep('^' _ argv.type _ '$').size();
      theset.ips.push(iface.ip) UNLESS theset.ips.defined(iface.ip);
    END;
  END;

  IF theset.ips.size();
    out.push(theset);
  END;

END;
out = out.sort;

-%]
