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 ec386e7 commit 4f4c11c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
13 changes: 8 additions & 5 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,18 @@ sub generate_vip_config {
}

# private sub to extract vlan id from name
# i.e given eth0.123 return 123
# i.e given eth0.123 or vlan123 return 123
sub _get_vlan_id {
my ($self, $name) = @_;
return $1 if ($name =~ m/\.(\d+)$/);
return $1 if ($name =~ m/^[a-z]+(?:\d+\.)?(\d+)$/);
}

# 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 find_vlan_id {
my ($self, $iface, $device) = @_;
# a vlan interface can defined in two ways
Expand Down
31 changes: 31 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ 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: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: vlan123
- 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 @@ -218,4 +246,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 @@ -39,4 +39,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 4f4c11c

Please sign in to comment.