network/nix/pkgs/switch-report.nix

81 lines
2.0 KiB
Nix

{ self, nixpkgs, system }:
with nixpkgs.legacyPackages.${system};
let
config = self.lib.config;
reportHosts = lib.concatMapStrings (hostName:
let
inherit (config.site.hosts.${hostName}) links model location;
linksByPorts = builtins.foldl' (linksByPorts: linkName:
if links.${linkName}.group != null
then
# group ports
linksByPorts // {
${lib.concatStringsSep "<br>" links.${linkName}.ports} = linkName;
}
else
# expand lists of access ports to seperate rows
builtins.foldl' (linksByPorts': port:
linksByPorts' // {
${port} = linkName;
}
) linksByPorts links.${linkName}.ports
) {} (builtins.attrNames links);
in ''
## ${hostName}
`${model}` @ **${location}**
|Name|Ports|Group|Trunk|Networks|
|-:|-:|:-:|:-:|:-|
${lib.concatMapStrings (ports:
let
linkName = linksByPorts.${ports};
link = links.${linkName};
in ''
|**${
linkName
}**|${
ports
}|${
lib.optionalString (link.group != null) (
toString link.group
)
}|${
lib.optionalString link.trunk ""
}|${
lib.concatStringsSep "<br>" (
map (net:
"${net} (`${toString config.site.net.${net}.vlan}`)"
) (builtins.sort (a: b:
config.site.net.${a}.vlan < config.site.net.${b}.vlan
) link.nets)
)
}|
'') (lib.naturalSort (
builtins.attrNames linksByPorts
))}
'');
reportRole = role:
reportHosts (
lib.naturalSort (
builtins.filter (hostName:
config.site.hosts.${hostName}.role == "switch"
) (builtins.attrNames config.site.hosts)
)
);
in
writeText "switch-report.md" ''
# Switch Report
${reportRole "switch"}
# Access Point Report
${reportRole "ap"}
''