[%
# vi: ft=tt2

# Return an array of hashes with name and ip within a given type for all nodes.
#
# @param argv.role      The role of the node to process, one of:
#                         proxy, lb, mgmt
# @param argv.type      The interface type of a node to extract the IP
#                       from, one of:
#                         web_int, web_ext, sip_int, sip_ext, ...
# @param argv.status    node status [ online, offline, inactive ]
#                         default value: ['online', 'inactive']
# @return out           The array of hashes with fields "name" and "ip", e.g.:
#                         [
#                           { name => 'sp1', ip = '192.168.255.251' },
#                           { name => 'sp2', ip = '192.168.255.252' }
#                         ]

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;

seen_ips = {};
out = [];
FOREACH host IN hosts.keys.sort;
  NEXT UNLESS status.item(hosts.$host.status);
  NEXT UNLESS ngcp.has_role(host, argv.role);

  FOREACH iface IN hosts.$host.interfaces;
    NEXT UNLESS hosts.$host.exists(iface);
    NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size();
    NEXT UNLESS hosts.$host.$iface.ip;
    NEXT IF     seen_ips.exists(hosts.$host.$iface.ip);

    seen_ips.${hosts.$host.$iface.ip} = 1;
    out.push({ name = host, ip = hosts.$host.$iface.ip });
  END;
END;

-%]
