Netconf-Module/junos/interfaces.nix
2024-05-22 13:33:28 +02:00

131 lines
4.2 KiB
Nix

{ lib, config, ... }:
with lib;
let
interface =
{ name, config, ... }:
let
intf-name = name;
unit =
{ name, config, ... }:
{
options = {
enable = mkEnableOption "the logical interface ${intf-name}.${name}" // {
default = true;
};
family = {
ethernet-switching = {
enable = mkEnableOption "the ethernet on the logical interface ${intf-name}.${name}";
interface-mode = mkOption {
type = types.nullOr (
types.enum [
"trunk"
"access"
]
);
default = null;
};
vlans = mkOption {
type = types.listOf (types.either types.str types.ints.unsigned);
default = [ ];
};
};
#TODO : DHCP
inet = {
enable = mkEnableOption "the IPv4 configuration of the logical interface ${intf-name}.${name}";
address = mkOption {
type = types.listOf types.str;
default = [ ];
};
};
inet6 = {
enable = mkEnableOption "the IPv6 configuration of the logical interface ${intf-name}.${name}";
address = mkOption {
type = types.listOf types.str;
default = [ ];
};
};
};
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.address;
inet = optionalString config.family.inet.enable ''
<inet>
<address>${builtins.concatStringsSep "" addr4}</address>
</inet>
'';
addr6 = map (addr: "<name>${addr}</name>") config.family.inet6.address;
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 = {
enable = mkEnableOption "the physical interface ${intf-name}";
unit = mkOption {
type = types.attrsOf (types.submodule unit);
default = { };
};
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 = {
interfaces = mkOption { type = types.attrsOf (types.submodule interface); };
netconf.xmls.interfaces = mkOption {
type = types.str;
visible = false;
readOnly = true;
};
};
config.netconf.xmls.interfaces = ''
<interfaces operation="replace">
${builtins.concatStringsSep "" (attrsets.mapAttrsToList (_: intf: intf.xml) config.interfaces)}
</interfaces>
'';
}