Skip to content

Commit

Permalink
ncm-network: Move route schema into seperate backend templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jrha committed Sep 20, 2024
1 parent c63d9c0 commit 15a8349
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,58 @@ declaration template components/network/types/network/backend/initscripts;

@{implement types specific for initscripts / network.pm}

include 'components/network/types/network/route';

type structure_network_backend_specific = {
};

@documentation{
Add route (IPv4 of IPv6)
Presence of ':' in any of the values indicates this is IPv6 related.
}
type network_route = {
@{The ADDRESS in ADDRESS/PREFIX via GATEWAY}
"address" ? string with {SELF == 'default' || is_ip(SELF)}
@{The PREFIX in ADDRESS/PREFIX via GATEWAY}
"prefix" ? long
@{The GATEWAY in ADDRESS/PREFIX via GATEWAY}
"gateway" ? type_ip
@{alternative notation for prefix (cannot be combined with prefix)}
"netmask" ? type_ip
@{routing table}
"table" ? network_valid_routing_table
@{pretend that the nexthop is directly attached to this link}
"onlink" ? boolean
@{route add command options to use (cannot be combined with other options)}
"command" ? string with !match(SELF, '[;]')
} with {
if (exists(SELF['command'])) {
if (length(SELF) != 1) error("Cannot use command and any of the other attributes as route");
} else {
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
pref = SELF['prefix'];
ipv6 = false;
foreach (k; v; SELF) {
if (match(to_string(v), ':')) {
ipv6 = true;
};
};
if (ipv6) {
if (!is_ipv6_prefix_length(pref)) {
error("Prefix %s is not a valid IPv6 prefix", pref);
};
} else {
if (!is_ipv4_prefix_length(pref)) {
error("Prefix %s is not a valid IPv4 prefix", pref);
};
};
};

true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ declaration template components/network/types/network/backend/nmstate;

@{implement types specific for nmstate / nmstate.pm}

include 'components/network/types/network/route';

type structure_network_nmstate = {
@{let NetworkManager manage the dns}
"manage_dns" : boolean = false
Expand All @@ -12,3 +14,47 @@ type structure_network_nmstate = {
type structure_network_backend_specific = {
"nmstate" : structure_network_nmstate
};

@documentation{
Add route (IPv4 of IPv6)
Presence of ':' in any of the values indicates this is IPv6 related.
}
type network_route = {
@{The ADDRESS in ADDRESS/PREFIX via GATEWAY}
"address" : string with {SELF == 'default' || is_ip(SELF)}
@{The PREFIX in ADDRESS/PREFIX via GATEWAY}
"prefix" ? long
@{The GATEWAY in ADDRESS/PREFIX via GATEWAY}
"gateway" ? type_ip
@{alternative notation for prefix (cannot be combined with prefix)}
"netmask" ? type_ip
@{routing table}
"table" ? network_valid_routing_table
@{pretend that the nexthop is directly attached to this link}
"onlink" ? boolean
} with {
if (exists(SELF['prefix']) && exists(SELF['netmask'])) {
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
pref = SELF['prefix'];
ipv6 = false;
foreach (k; v; SELF) {
if (match(to_string(v), ':')) {
ipv6 = true;
};
};
if (ipv6) {
if (!is_ipv6_prefix_length(pref)) {
error("Prefix %s is not a valid IPv6 prefix", pref);
};
} else {
if (!is_ipv4_prefix_length(pref)) {
error("Prefix %s is not a valid IPv4 prefix", pref);
};
};
};

true;
};
Original file line number Diff line number Diff line change
@@ -1,55 +1,3 @@
declaration template components/network/types/network/route;

type network_valid_routing_table = string with exists("/system/network/routing_table/" + SELF);

@documentation{
Add route (IPv4 of IPv6)
Presence of ':' in any of the values indicates this is IPv6 related.
}
type network_route = {
@{The ADDRESS in ADDRESS/PREFIX via GATEWAY}
"address" ? string with {SELF == 'default' || is_ip(SELF)}
@{The PREFIX in ADDRESS/PREFIX via GATEWAY}
"prefix" ? long
@{The GATEWAY in ADDRESS/PREFIX via GATEWAY}
"gateway" ? type_ip
@{alternative notation for prefix (cannot be combined with prefix)}
"netmask" ? type_ip
@{routing table}
"table" ? network_valid_routing_table
@{pretend that the nexthop is directly attached to this link}
"onlink" ? boolean
@{route add command options to use (cannot be combined with other options)}
"command" ? string with !match(SELF, '[;]')
} with {
if (exists(SELF['command'])) {
network_exclude_backend('nmstate', 'command routes');
if (length(SELF) != 1) error("Cannot use command and any of the other attributes as route");
} else {
if (!exists(SELF['address']))
error("Address is mandatory for route (in absence of command)");
if (exists(SELF['prefix']) && exists(SELF['netmask']))
error("Use either prefix or netmask as route");
};

if (exists(SELF['prefix'])) {
pref = SELF['prefix'];
ipv6 = false;
foreach (k; v; SELF) {
if (match(to_string(v), ':')) {
ipv6 = true;
};
};
if (ipv6) {
if (!is_ipv6_prefix_length(pref)) {
error("Prefix %s is not a valid IPv6 prefix", pref);
};
} else {
if (!is_ipv4_prefix_length(pref)) {
error("Prefix %s is not a valid IPv4 prefix", pref);
};
};
};

true;
};

0 comments on commit 15a8349

Please sign in to comment.