feat(vlans): Refactor

This commit is contained in:
catvayor 2024-04-29 15:47:03 +02:00
parent 458cd1cf58
commit 887dbbc6c8

74
junos/vlans.nix Normal file
View file

@ -0,0 +1,74 @@
{ lib, config, ... }:
with lib;
let
vlan =
{ name, config, ... }:
{
options = {
id = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
};
id-list = mkOption {
type =
let
range_type =
{ config, ... }:
{
config.__toString = _: "${toString config.begin}-${toString config.end}";
options = {
begin = mkOption { type = types.ints.unsigned; };
end = mkOption { type = types.ints.unsigned; };
__toString = mkOption {
visible = false;
internal = true;
readOnly = true;
type = types.unspecified;
};
};
};
in
types.listOf (types.either types.ints.unsigned (types.submodule range_type));
default = [ ];
};
l3-interface = mkOption {
type = types.nullOr types.str;
default = null;
};
xml = mkOption {
type = types.str;
readOnly = true;
visible = false;
};
};
config.xml =
let
id = optionalString (!isNull config.id) "<vlan-id>${toString config.id}</vlan-id>";
id-list = concatStringsSep "" (map (vlan: "<vlan-id-list>${toString vlan}</vlan-id-list>") config.id-list);
l3-intf = optionalString (
!isNull config.l3-interface
) "<l3-interface>${config.l3-interface}</l3-interface>";
in
''
<vlan>
<name>${name}</name>
${id}${id-list}${l3-intf}
</vlan>
'';
};
in
{
options = {
vlans = mkOption { type = types.attrsOf (types.submodule vlan); };
netconf.xmls.vlans = mkOption {
type = types.str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.vlans = ''
<vlans operation="replace">
${builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: vlan: vlan.xml) config.vlans)}
</vlans>
'';
}