###
# Metadata for the nodes. You can add custom attributes, they are
# accessible through the specialArg meta in the config.

let
  ###
  # Transforms data from :
  # {
  #   zone01 = [ node01 node02 ];
  #   zone02 = [ node03 ];
  # }
  # to :
  # {
  #   node01 = zone01;
  #   node02 = zone01;
  #   node03 = zone02;
  # }
  locations = builtins.foldl'
    (a: loc: a // loc)
    { }
    (builtins.concatLists (builtins.attrValues (builtins.mapAttrs
      (zone: builtins.map (n: { ${n} = zone; }))
      infra)));

  ###
  # Add computed data about the nodes :
  # - zone
  # - deployment tags
  # - network information
  mkNode = node: attrs: attrs // {
    zone = locations.${node};
    deployment = let old = attrs.deployment; in old // {
      tags = (old.tags or [ ]) ++ [ "infra-${locations.${node}}" ];
      targetHost = old.targetHost or (builtins.head network.${node}.addresses.public);
    };
  };

  infra = import ./infrastructure.nix;
  members = import ./members.nix;
  network = import ./network.nix;

  nodes = builtins.mapAttrs mkNode (import ./nodes.nix);
in

{ inherit infra members network nodes; }