Update addressing lib to remove making the hosts list optional.

This commit is contained in:
Yaro Kasear 2025-11-28 20:07:12 -06:00
parent e5326f0af6
commit 1b23a16e9f
2 changed files with 50 additions and 53 deletions

View file

@ -270,19 +270,18 @@ let
spec.domain or (throw "metatron-addressing: spec.domain is required for FQDN generation");
in
concatMapAttrs
(locationName: locCfg:
(locationName: locationCfg:
let
loc = locationId locationName;
locCode = mkLocationCode locationName;
# Strip meta-level hints; whats left are subnets (main, dmz, etc.)
# Only treat attributes that are attrsets with a `hosts` attr as subnets
subnets =
builtins.removeAttrs locCfg [ "owner" "admins" "users" ];
lib.filterAttrs (_: v: builtins.isAttrs v && v ? hosts) locationCfg;
in
concatMapAttrs
(typeName: subnetCfg:
let
# New shape: subnetCfg.hosts = { hostName = { role = "..."; ... }; ... }
hostsForSubnet = subnetCfg.hosts or { };
# Only treat non-underscore-prefixed keys as hosts
@ -347,7 +346,7 @@ let
subnets)
locations;
# Subnet map: { "home-dmz" = { cidr = "..."; vlan = ...; zone = "..."; ... }; ... }
# Subnet map: { "home-dmz" = "10.1.0.0/19"; "home-main" = "..."; ... }
mkSubnetsFromSpec =
spec:
let
@ -358,20 +357,18 @@ let
spec.domain or (throw "metatron-addressing: spec.domain is required");
in
concatMapAttrs
(locationName: locCfg:
(locationName: locationCfg:
let
loc = locationId locationName;
locCode = mkLocationCode locationName;
# Strip meta-level hints; remaining attrs are subnets
# Only attributes with `hosts` are subnets
subnets =
builtins.removeAttrs locCfg [ "owner" "admins" "users" ];
lib.filterAttrs (_: v: builtins.isAttrs v && v ? hosts) locationCfg;
in
mapAttrs'
(typeName: subnetCfg:
let
hostsForSubnet = subnetCfg.hosts or { };
cidr = mkSubnetNamed { location = loc; inherit typeName; };
subnetSlug =
@ -382,21 +379,42 @@ let
zone = "${subnetSlug}.${locCode}.${domain}";
vlan =
# Priority: explicit vlan on subnet > old-style _vlan > computed
subnetCfg.vlan or
hostsForSubnet._vlan or
subnetCfg._vlan or
(mkVlanNamed { location = loc; inherit typeName; });
dhcpCfg = subnetCfg.dhcp or null;
dhcpRange =
if dhcpCfg == null then null else {
startIp = mkIpNamed {
location = loc;
inherit typeName;
roleName = "pool";
host = dhcpCfg.start;
};
endIp = mkIpNamed {
location = loc;
inherit typeName;
roleName = "pool";
host = dhcpCfg.end;
};
};
in
nameValuePair
"${locationName}-${typeName}"
{
inherit cidr locationName typeName zone vlan;
})
(
{
inherit cidr locationName typeName zone vlan;
}
// optionalAttrs (dhcpCfg != null) {
dhcp = dhcpCfg;
dhcpRange = dhcpRange;
}
))
subnets)
locations;
# Combined view, if you want both
mkNetworkFromSpec =
spec: