Netconf-Module/junos/vlans.nix

122 lines
3.2 KiB
Nix
Raw Normal View History

2024-12-13 16:03:36 +01:00
{ lib, config, xml, ... }:
2024-04-29 15:47:03 +02:00
let
2024-12-13 14:37:43 +01:00
inherit (lib)
mkOption
assertMsg
mapAttrsToList
2024-12-13 16:03:36 +01:00
mkMerge
mkIf
2024-12-13 14:37:43 +01:00
;
inherit (lib.types)
nullOr
ints
unspecified
listOf
either
submodule
str
attrsOf
;
2024-04-29 15:47:03 +02:00
vlan =
{ name, config, ... }:
{
options = {
id = mkOption {
2024-12-13 14:37:43 +01:00
type = nullOr ints.unsigned;
2024-04-29 15:47:03 +02:00
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 {
2024-12-13 14:37:43 +01:00
type = ints.unsigned;
2024-09-03 23:04:30 +02:00
visible = false;
};
end = mkOption {
2024-12-13 14:37:43 +01:00
type = ints.unsigned;
2024-09-03 23:04:30 +02:00
visible = false;
};
2024-04-29 15:47:03 +02:00
__toString = mkOption {
visible = false;
internal = true;
readOnly = true;
2024-12-13 14:37:43 +01:00
type = unspecified;
2024-04-29 15:47:03 +02:00
};
};
};
in
2024-12-13 14:37:43 +01:00
listOf (either ints.unsigned (submodule range_type));
2024-04-29 15:47:03 +02:00
default = [ ];
2024-09-03 23:04:30 +02:00
example = [
42
2024-12-13 14:37:43 +01:00
{
begin = 100;
end = 200;
}
2024-09-03 23:04:30 +02:00
];
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 {
2024-12-13 14:37:43 +01:00
type = nullOr str;
2024-04-29 15:47:03 +02:00
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 {
2024-12-13 16:03:36 +01:00
type = xml.type;
2024-04-29 15:47:03 +02:00
readOnly = true;
visible = false;
};
};
config.xml =
let
2024-12-13 16:03:36 +01:00
id = mkIf (!isNull config.id) { vlan-id = toString config.id; };
id-list = mkIf (config.id-list != [ ]) {
vlan-id-list = map toString config.id-list;
};
l3-intf = mkIf (!isNull config.l3-interface) { inherit (config) l3-interface; };
2024-04-29 15:47:03 +02:00
in
2024-12-13 14:37:43 +01:00
assert assertMsg (
config.id == null || config.id-list == [ ]
) "vlans.${name}.id and vlans.${name}.id-list are incompatible.";
2024-12-13 16:03:36 +01:00
mkMerge [
{ inherit name; }
id
id-list
l3-intf
];
2024-04-29 15:47:03 +02:00
};
in
{
options = {
2024-09-03 23:04:30 +02:00
vlans = mkOption {
2024-12-13 14:37:43 +01:00
type = attrsOf (submodule vlan);
2024-09-03 23:04:30 +02:00
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
};
2024-12-13 16:03:36 +01:00
config.netconf.xml.vlans = {
"@operation" = "replace";
vlan = mapAttrsToList (_: vlan: vlan.xml) config.vlans;
};
2024-04-29 15:47:03 +02:00
}