Skip to content

Commit

Permalink
state/status: Rename Members to Interfaces
Browse files Browse the repository at this point in the history
The name Interfaces is used in the Netplan configuration already.

Also, check if the interface 'Type' is being reported as 'none'. I have
an active OpenVPN tunnel and networkctl return 'none' as type, but the
'Kind' property is 'tun'. So use the Kind property in these cases. Also
add a test case for that.
  • Loading branch information
daniloegea committed Jan 26, 2024
1 parent 5399f4f commit 9e50b9f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions netplan_cli/cli/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ def pretty_print(self, data: JSON, total: int, _console_width=None) -> None:
value=val,
))

lst = data.get('members', [])
lst = data.get('interfaces', [])
for i, val in enumerate(lst):
pprint(('{title:>'+pad+'} {value}').format(
title='Members:' if i == 0 else '',
title='Interfaces:' if i == 0 else '',
value=val,
))

Expand Down
17 changes: 13 additions & 4 deletions netplan_cli/cli/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
DEVICE_TYPES = {
'bond': 'bond',
'bridge': 'bridge',
'dummy': 'dummy',
'dummy': 'dummy-device',
'ether': 'ethernet',
'ipgre': 'tunnel',
'ip6gre': 'tunnel',
Expand All @@ -52,7 +52,7 @@
'wireguard': 'tunnel',
'wlan': 'wifi',
'wwan': 'modem',
'veth': 'veth',
'veth': 'virtual-ethernet',
'vlan': 'vlan',
'vrf': 'vrf',
'vxlan': 'tunnel',
Expand All @@ -63,7 +63,7 @@
'bridges': 'bridge',
'bonds': 'bond',
'nm-devices': 'nm-device',
'dummy-devices': 'dummy',
'dummy-devices': 'dummy-device',
'modems': 'modem',
'vlans': 'vlan',
'vrfs': 'vrf',
Expand Down Expand Up @@ -227,7 +227,7 @@ def json(self) -> JSON:
if self.vrf:
json['vrf'] = self.vrf
if self.members:
json['members'] = self.members
json['interfaces'] = self.members
return (self.name, json)

@property
Expand All @@ -241,6 +241,9 @@ def down(self) -> bool:
@property
def type(self) -> str:
nd_type = self.nd.get('Type') if self.nd else None
if nd_type == 'none':
# If the Type is reported as 'none' by networkd, the interface still might have a Kind.
nd_type = self.nd.get('Kind')
if nd_type == 'ether':
# There are different kinds of 'ether' devices, such as VRFs, veth and dummies
if kind := self.nd.get('Kind'):
Expand Down Expand Up @@ -516,6 +519,7 @@ def query_resolved(cls) -> tuple:

@classmethod
def query_members(cls, ifname: str) -> List[str]:
''' Return a list containing the interfaces that are members of a bond/bridge/vrf '''
members = []
output: str = None
try:
Expand All @@ -533,6 +537,11 @@ def query_members(cls, ifname: str) -> List[str]:

@classmethod
def correlate_members_and_uplink(cls, interfaces: List[Interface]) -> None:
'''
Associate interfaces with their members and parent interfaces.
If an interface is a member of a bond/bridge/vrf, identify which interface
if a member of. If an interface has members, identify what are the members.
'''
uplink_types = ['bond', 'bridge', 'vrf']
members_to_uplink = {}
uplink_to_members = defaultdict(list)
Expand Down
8 changes: 4 additions & 4 deletions tests/cli/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def test_query_iproute2(self, mock):
mock.return_value = IPROUTE2
res = SystemConfigState.query_iproute2()
mock.assert_called_with(['ip', '-d', '-j', 'addr'], text=True)
self.assertEqual(len(res), 6)
self.assertEqual(len(res), 7)
self.assertListEqual([itf.get('ifname') for itf in res],
['lo', 'enp0s31f6', 'wlan0', 'wg0', 'wwan0', 'tun0'])
['lo', 'enp0s31f6', 'wlan0', 'wg0', 'wwan0', 'tun0', 'tun1'])

@patch('subprocess.check_output')
def test_query_iproute2_fail(self, mock):
Expand All @@ -77,9 +77,9 @@ def test_query_networkd(self, mock):
mock.return_value = NETWORKD
res = SystemConfigState.query_networkd()
mock.assert_called_with(['networkctl', '--json=short'], text=True)
self.assertEqual(len(res), 9)
self.assertEqual(len(res), 10)
self.assertListEqual([itf.get('Name') for itf in res],
['lo', 'enp0s31f6', 'wlan0', 'wg0', 'wwan0', 'tun0', 'mybr0', 'mybond0', 'myvrf0'])
['lo', 'enp0s31f6', 'wlan0', 'wg0', 'wwan0', 'tun0', 'mybr0', 'mybond0', 'myvrf0', 'tun1'])

@patch('subprocess.check_output')
def test_query_networkd_fail(self, mock):
Expand Down
Loading

0 comments on commit 9e50b9f

Please sign in to comment.