Skip to content

Commit

Permalink
ncm-nmstate: relax VLAN name pattern
Browse files Browse the repository at this point in the history
- Does not require a . between interface name and VLAN ID

Fixes quattor#1678
  • Loading branch information
jouvin committed Apr 16, 2024
1 parent 73e3e22 commit a7b41d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
18 changes: 10 additions & 8 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,24 @@ sub generate_vip_config {
return $iface_cfg;
}

# find vlan id from either the name or device.
# i.e eth0.123 will return 123. by checking iface name and device
# returns vlan id.
# find vlan id by parsing either the name or device, if they follow
# the syntax intname[.]vlanid. '.' before the VLAN ID is optional
# if the interface name contains no digit.
# i.e. vlan123 will return 123 and eth0.123 will return 123.
# If there is no '.'i and the interface name ends with a number, this number
# must be the VLAN ID.
sub get_vlanid {
my ($self, $iface, $device) = @_;
my $vlanid = $iface;
# a vlan interface can defined in two ways
# interface/name.vlanid/
# or interface/name/device=device.vlanid
# replace everything up-to and including . to get vlan id of the interface.
# interface/name[.i]vlanid/
# or interface/name/device=device[.]vlanid
# favors ifacename.vlanid, then device.vlanid
$vlanid =~ s/^[^.]*.//;
$vlanid =~ s/^[a-z]++(\d+\.)?(?<id>\d+)$/$+{id}/;
# if vlanid is empty here, lets check if device has vlan id.
if ((! $vlanid) && ($device)) {
$vlanid = $device;
$vlanid =~ s/^[^.]*.//;
$vlanid =~ s/^[a-z]++(\d+\.)?(?<id>\d+)$/$+{id}/;
}
return $vlanid;
}
Expand Down
28 changes: 28 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ routes:
next-hop-interface: vlan0
EOF

Readonly my $VLAN123_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
interfaces:
- ipv4:
address:
- ip: 4.3.2.1
prefix-length: 24
dhcp: false
enabled: true
name: vlan123
profile-name: vlan123
state: up
type: vlan
vlan:
base-iface: eth0
id: '123'
routes:
config:
- next-hop-interface: vlan123
state: absent
- destination: 1.2.3.4/32
next-hop-interface: vlan123
EOF


Readonly my $DHCP_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
Expand Down Expand Up @@ -206,4 +231,7 @@ is($vlanyml, $VLAN_YML, "Exact eth0.123 vlan yml config");

my $vlanyml2 = get_file_contents("/etc/nmstate/vlan0.yml");
is($vlanyml2, $VLAN0_YML, "Exact vlan0 yml config");

my $vlanyml3 = get_file_contents("/etc/nmstate/vlan123.yml");
is($vlanyml3, $VLAN123_YML, "Exact vlan123 yml config");
done_testing();
10 changes: 9 additions & 1 deletion ncm-network/src/test/resources/nmstate_advance.pan
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ prefix "/system/network/interfaces/eth0.123";
prefix "/system/network/interfaces/vlan0";
"device" = "eth0.123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");
"route/0" = dict("address", "1.2.3.4");

# test vlan interface route on vlan for backward compatibily with network.pm
# No dot between 'vlan' and VLAN ID
"/system/network/interfaces/vlan123" = create("defaultinterface");
prefix "/system/network/interfaces/vlan123";
"device" = "vlan123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");

0 comments on commit a7b41d5

Please sign in to comment.