Netconf-Module/junos/vlans.nix

107 lines
3.3 KiB
Nix
Raw Normal View History

2024-04-29 15:47:03 +02:00
{ lib, config, ... }:
with lib;
let
vlan =
{ name, config, ... }:
{
options = {
id = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
2024-09-03 23:04:30 +02:00
description = ''
The ID of this vlan, `null` means no ID.
Incompatible with vlans.${name}.id-list.
'';
2024-04-29 15:47:03 +02:00
};
id-list = mkOption {
type =
let
range_type =
{ config, ... }:
{
config.__toString = _: "${toString config.begin}-${toString config.end}";
options = {
2024-09-03 23:04:30 +02:00
begin = mkOption {
type = types.ints.unsigned;
visible = false;
};
end = mkOption {
type = types.ints.unsigned;
visible = false;
};
2024-04-29 15:47:03 +02:00
__toString = mkOption {
visible = false;
internal = true;
readOnly = true;
type = types.unspecified;
};
};
};
in
types.listOf (types.either types.ints.unsigned (types.submodule range_type));
default = [ ];
2024-09-03 23:04:30 +02:00
example = [
42
{ begin = 100; end = 200; }
];
description = ''
List of IDs or IDs range to classify as this vlan.
Incompatible with vlans.${name}.id.
'';
2024-04-29 15:47:03 +02:00
};
l3-interface = mkOption {
type = types.nullOr types.str;
default = null;
2024-09-03 23:04:30 +02:00
example = "irb.0";
description = ''
Switch's logical interface to connect directly to this vlan.
This allows to communicate with the switch from a vlan without
having a cable looping back on it's management interface.
'';
2024-04-29 15:47:03 +02:00
};
xml = mkOption {
type = types.str;
readOnly = true;
visible = false;
};
};
config.xml =
let
id = optionalString (!isNull config.id) "<vlan-id>${toString config.id}</vlan-id>";
2024-05-22 13:33:28 +02:00
id-list = concatStringsSep "" (
map (vlan: "<vlan-id-list>${toString vlan}</vlan-id-list>") config.id-list
);
2024-04-29 15:47:03 +02:00
l3-intf = optionalString (
!isNull config.l3-interface
) "<l3-interface>${config.l3-interface}</l3-interface>";
in
2024-09-03 23:04:30 +02:00
assert assertMsg (config.id == null || config.id-list == [ ]) "vlans.${name}.id and vlans.${name}.id-list are incompatible."; ''
2024-04-29 15:47:03 +02:00
<vlan>
<name>${name}</name>
${id}${id-list}${l3-intf}
</vlan>
'';
};
in
{
options = {
2024-09-03 23:04:30 +02:00
vlans = mkOption {
type = types.attrsOf (types.submodule vlan);
description = ''
Named vlans configuration. Allows to name vlans inside interface configuration,
instead of just using their IDs.
'';
};
2024-04-29 15:47:03 +02:00
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>
'';
}