Netconf-Module/junos/interfaces.nix

154 lines
5 KiB
Nix
Raw Normal View History

2024-04-28 22:58:42 +02:00
{ lib, config, ... }:
with lib;
let
interface =
{ name, config, ... }:
let
intf-name = name;
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-09-03 23:04:30 +02:00
type =
2024-04-28 22:58:42 +02:00
types.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 {
type = types.listOf (types.either types.str types.ints.unsigned);
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-04-28 22:58:42 +02:00
type = types.listOf types.str;
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-04-28 22:58:42 +02:00
type = types.listOf types.str;
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 {
type = types.str;
visible = false;
readOnly = true;
};
};
config.xml =
let
members = map (
vlan: "<members>${builtins.toString vlan}</members>"
) config.family.ethernet-switching.vlans;
eth = optionalString config.family.ethernet-switching.enable ''
<ethernet-switching>
<interface-mode>${config.family.ethernet-switching.interface-mode}</interface-mode>
<vlan>${builtins.concatStringsSep "" members}</vlan>
<storm-control><profile-name>default</profile-name></storm-control>
</ethernet-switching>
'';
addr4 = map (addr: "<name>${addr}</name>") config.family.inet.addresses;
2024-04-28 22:58:42 +02:00
inet = optionalString config.family.inet.enable ''
<inet>
<address>${builtins.concatStringsSep "" addr4}</address>
</inet>
'';
addr6 = map (addr: "<name>${addr}</name>") config.family.inet6.addresses;
2024-04-28 22:58:42 +02:00
inet6 = optionalString config.family.inet6.enable ''
<inet6>
<address>${builtins.concatStringsSep "" addr6}</address>
</inet6>
'';
in
''
<unit>
<name>${name}</name>
${optionalString (!config.enable) "<disable/>"}
<family>
${eth}${inet}${inet6}
</family>
</unit>'';
};
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 {
type = types.attrsOf (types.submodule unit);
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 {
type = types.str;
visible = false;
readOnly = true;
};
};
config.xml =
let
units = attrsets.mapAttrsToList (_: unit: unit.xml) config.unit;
in
''
<interface>
<name>${name}</name>
${optionalString (!config.enable) "<disable/>"}
${builtins.concatStringsSep "" units}
</interface>
'';
};
in
{
options = {
2024-09-03 23:04:30 +02:00
interfaces = mkOption {
type = types.attrsOf (types.submodule interface);
description = ''
The interfaces configuration.
'';
};
2024-04-28 22:58:42 +02:00
netconf.xmls.interfaces = mkOption {
type = types.str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.interfaces = ''
<interfaces operation="replace">
2024-05-22 13:33:28 +02:00
${builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: intf: intf.xml) config.interfaces)}
2024-04-28 22:58:42 +02:00
</interfaces>
'';
}