From 00c57c88bfb75d07391f3af48926a3d59fd2b729 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 27 Jun 2022 22:09:16 +0200 Subject: [PATCH] lib/config/options: add assertions for switch links --- config/switch.nix | 2 +- nix/lib/config/options.nix | 42 +++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/config/switch.nix b/config/switch.nix index 26f2e63..ccad3d0 100644 --- a/config/switch.nix +++ b/config/switch.nix @@ -246,7 +246,7 @@ links = { switch-d1 = { group = "1"; - ports = [ "1" ]; + ports = [ "12" ]; }; ap3.ports = [ "1" ]; ap59.ports = [ "2" ]; diff --git a/nix/lib/config/options.nix b/nix/lib/config/options.nix index 9956e93..65cf3f8 100644 --- a/nix/lib/config/options.nix +++ b/nix/lib/config/options.nix @@ -680,5 +680,45 @@ in in map (vlan: { assertion = builtins.length vlanNets.${vlan} == 1; message = "VLAN ${vlan} is used by more than one network: ${lib.concatStringsSep " " vlanNets.${vlan}}"; - }) (builtins.attrNames vlanNets)); + }) (builtins.attrNames vlanNets)) + ++ + # Duplicate switch port check + builtins.concatMap (hostName: + let + ports = lib.unique ( + builtins.concatMap (linkName: + config.site.hosts.${hostName}.links.${linkName}.ports + ) (builtins.attrNames config.site.hosts.${hostName}.links) + ); + linksOfPort = port: + builtins.attrNames ( + lib.filterAttrs (_: { ports, ... }: builtins.elem port ports) + config.site.hosts.${hostName}.links + ); + in map (port: { + assertion = builtins.length (linksOfPort port) == 1; + message = "${hostName}: port ${port} is used in more than one link: ${lib.concatStringsSep " " (linksOfPort port)}"; + }) ports + ) (builtins.attrNames config.site.hosts) + ++ + # Duplicate switch port group check + builtins.concatMap (hostName: + let + groups = lib.unique ( + builtins.filter builtins.isString ( + builtins.map (linkName: + config.site.hosts.${hostName}.links.${linkName}.group + ) (builtins.attrNames config.site.hosts.${hostName}.links) + ) + ); + linksOfGroup = wantedGroup: + builtins.attrNames ( + lib.filterAttrs (_: { group, ... }: group == wantedGroup) + config.site.hosts.${hostName}.links + ); + in map (group: { + assertion = builtins.length (linksOfGroup group) == 1; + message = "${hostName}: group ${group} is used in more than one link: ${lib.concatStringsSep " " (linksOfGroup group)}"; + }) groups + ) (builtins.attrNames config.site.hosts); }