Netconf-Module/junos/vlans.nix
2024-04-30 20:06:21 +02:00

74 lines
2.1 KiB
Nix

{ 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>
'';
}