# See morph/grid/local/grid.nix for additional commentary.
let
  pkgs = import <nixpkgs> { };

  gridlib = import ../../lib;
  rawConfig = pkgs.lib.trivial.importJSON ./config.json;
  config = rawConfig // {
    sshUsers = import ./secrets/users.nix;

    # Get absolute vpn key directory path, as a string:
    monitoringvpnKeyDir = toString ./. + "/${rawConfig.monitoringvpnKeyDir}";
  };

  payments = {
    imports = [
      gridlib.issuer
      gridlib.hardware-aws
      (gridlib.customize-issuer (config // {
        monitoringvpnIPv4 = "172.23.23.11";
      }))
    ];
  };

  storage001 = let publicIPv4 = "3.120.26.190"; in {
    imports = [
      gridlib.storage
      ./testing001-hardware.nix
      (gridlib.customize-storage (config // {
        inherit publicIPv4;
        monitoringvpnIPv4 = "172.23.23.12";
        stateVersion = "19.03";
      }))
    ];
  };

  monitoring = let publicIPv4 = "18.156.171.217"; in {
    imports = [
      gridlib.monitoring
      gridlib.hardware-aws
      (gridlib.customize-monitoring {
        inherit hostsMap publicIPv4 vpnClientIPs nodeExporterTargets;
        inherit (config) monitoringvpnKeyDir;
        monitoringvpnIPv4 = "172.23.23.1";
        stateVersion = "19.09";
      })
    ];
  };

  # TBD: derive these automatically:
  hostsMap = {
    "172.23.23.1"  = [ "monitoring" "monitoring.monitoringvpn" ];
    "172.23.23.11" = [ "payments"   "payments.monitoringvpn"   ];
    "172.23.23.12" = [ "storage001" "storage001.monitoringvpn" ];
  };
  vpnClientIPs = [ "172.23.23.11" "172.23.23.12" ];
  nodeExporterTargets = [ "monitoring" "payments" "storage001" ];

in {
  network = {
    description = "PrivateStorage.io Testing Grid";
  };
  inherit payments monitoring storage001;
}