Netconf-Module/junos/interfaces.nix

157 lines
4.3 KiB
Nix
Raw Normal View History

2024-12-13 16:03:36 +01:00
{
lib,
config,
xml,
...
}:
2024-04-28 22:58:42 +02:00
let
2024-12-13 14:37:43 +01:00
inherit (lib)
mkEnableOption
mkOption
optionalString
mapAttrsToList
2024-12-13 16:03:36 +01:00
mkMerge
mkIf
2024-12-13 14:37:43 +01:00
;
inherit (lib.types)
enum
listOf
either
str
ints
submodule
attrsOf
;
2024-04-28 22:58:42 +02:00
interface =
{ name, config, ... }:
let
unit =
{ name, config, ... }:
{
options = {
2024-09-03 23:04:30 +02:00
enable = mkEnableOption "this logical interface" // {
2024-05-22 13:33:28 +02:00
default = true;
2024-09-03 23:04:30 +02:00
example = false;
2024-05-22 13:33:28 +02:00
};
2024-04-28 22:58:42 +02:00
family = {
ethernet-switching = {
2024-09-03 23:04:30 +02:00
enable = mkEnableOption "the ethernet switching on this logical interface";
2024-04-28 22:58:42 +02:00
interface-mode = mkOption {
2024-12-13 14:37:43 +01:00
type = enum [
"trunk"
"access"
];
2024-09-03 23:04:30 +02:00
description = ''
Mode of operation for vlan addressing of this interface.
"trunk" means that the traffic is tagged, "access" means the
traffic is tagged by the switch.
'';
2024-04-28 22:58:42 +02:00
};
vlans = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf (either str ints.unsigned);
2024-04-28 22:58:42 +02:00
default = [ ];
2024-09-03 23:04:30 +02:00
description = ''
Vlans that can be used on this interface.
Only one ID should be here for "access" mode of operation.
'';
2024-04-28 22:58:42 +02:00
};
};
#TODO : DHCP
inet = {
2024-09-03 23:04:30 +02:00
enable = mkEnableOption "the IPv4 configuration of this logical interface";
addresses = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf str;
2024-04-28 22:58:42 +02:00
default = [ ];
2024-09-03 23:04:30 +02:00
description = ''
ipv4 addresses of this interface.
'';
2024-04-28 22:58:42 +02:00
};
};
inet6 = {
2024-09-03 23:04:30 +02:00
enable = mkEnableOption "the IPv6 configuration of this logical interface";
addresses = mkOption {
2024-12-13 14:37:43 +01:00
type = listOf str;
2024-04-28 22:58:42 +02:00
default = [ ];
2024-09-03 23:04:30 +02:00
description = ''
ipv6 addresses of this interface.
'';
2024-04-28 22:58:42 +02:00
};
};
};
xml = mkOption {
2024-12-13 16:03:36 +01:00
type = xml.type;
2024-04-28 22:58:42 +02:00
visible = false;
readOnly = true;
};
};
config.xml =
let
2024-12-13 16:03:36 +01:00
eth = mkIf config.family.ethernet-switching.enable {
family.ethernet-switching = {
interface-mode = config.family.ethernet-switching.interface-mode;
vlan.members = map toString config.family.ethernet-switching.vlans;
storm-control.profile-name = "default";
};
};
2024-04-28 22:58:42 +02:00
2024-12-13 16:03:36 +01:00
addr4 = map (name: { inherit name; }) config.family.inet.addresses;
inet = mkIf config.family.inet.enable { family.inet.address = addr4; };
2024-04-28 22:58:42 +02:00
2024-12-13 16:03:36 +01:00
addr6 = map (name: { inherit name; }) config.family.inet6.addresses;
inet6 = mkIf config.family.inet6.enable { family.inet6.address = addr6; };
2024-04-28 22:58:42 +02:00
in
2024-12-13 16:03:36 +01:00
mkMerge [
{
inherit name;
}
(mkIf (!config.enable) { disable = { }; })
eth
inet
inet6
];
2024-04-28 22:58:42 +02:00
};
in
{
options = {
2024-09-03 23:04:30 +02:00
enable = mkEnableOption "this physical interface";
2024-05-22 13:33:28 +02:00
unit = mkOption {
2024-12-13 14:37:43 +01:00
type = attrsOf (submodule unit);
2024-05-22 13:33:28 +02:00
default = { };
2024-09-03 23:04:30 +02:00
description = ''
Configuration of the logical interfaces on this physical interface.
'';
2024-05-22 13:33:28 +02:00
};
2024-04-28 22:58:42 +02:00
xml = mkOption {
2024-12-13 16:03:36 +01:00
type = xml.type;
2024-04-28 22:58:42 +02:00
visible = false;
readOnly = true;
};
};
config.xml =
let
2024-12-13 16:03:36 +01:00
unit = mapAttrsToList (_: unit: unit.xml) config.unit;
2024-04-28 22:58:42 +02:00
in
2024-12-13 16:03:36 +01:00
mkMerge [
{
inherit name unit;
}
(mkIf (!config.enable) { disable = { }; })
];
2024-04-28 22:58:42 +02:00
};
in
{
options = {
2024-09-03 23:04:30 +02:00
interfaces = mkOption {
2024-12-13 14:37:43 +01:00
type = attrsOf (submodule interface);
2024-09-03 23:04:30 +02:00
description = ''
The interfaces configuration.
'';
};
2024-04-28 22:58:42 +02:00
};
2024-12-13 16:03:36 +01:00
config.netconf.xml.interfaces = {
"@operation" = "replace";
interface = mapAttrsToList (_: intf: intf.xml) config.interfaces;
};
2024-04-28 22:58:42 +02:00
}