Skip to content

Commit b4f06c7

Browse files
author
ushiboy
committed
Add general reload
1 parent 0cff316 commit b4f06c7

4 files changed

Lines changed: 86 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ except Exception as e:
4444
| general | hostname | supported |
4545
| general | permissions | not supported |
4646
| general | logging | not supported |
47-
| general | reload | not supported |
47+
| general | reload | supported |
4848
| networking | | supported |
4949
| networking | on | supported |
5050
| networking | off | supported |
@@ -355,6 +355,21 @@ Change persistent system hostname.
355355
nmcli.general.set_hostname(hostname: str) -> None
356356
```
357357

358+
#### nmcli.general.reload
359+
360+
Reload NetworkManager's configuration and perform certain updates.
361+
362+
The `flags` argument specifies which configurations to reload. Valid flags are:
363+
- `conf`: Reload NetworkManager.conf configuration from disk
364+
- `dns-rc`: Update DNS configuration (equivalent to SIGUSR1)
365+
- `dns-full`: Restart the DNS plugin
366+
367+
If no flags are provided, everything that is supported is reloaded.
368+
369+
```
370+
nmcli.general.reload(flags: Optional[List[str]] = None) -> None
371+
```
372+
358373
### networking
359374

360375
#### nmcli.networking

nmcli/_general.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List, Optional
2+
13
from ._system import SystemCommand, SystemCommandInterface
24
from .data import General
35

@@ -16,9 +18,14 @@ def get_hostname(self) -> str:
1618
def set_hostname(self, hostname: str):
1719
raise NotImplementedError
1820

21+
def reload(self, flags: Optional[List[str]] = None) -> None:
22+
raise NotImplementedError
23+
1924

2025
class GeneralControl(GeneralControlInterface):
2126

27+
VALID_RELOAD_FLAGS = ['conf', 'dns-rc', 'dns-full']
28+
2229
def __init__(self, syscmd: SystemCommandInterface = None):
2330
self._syscmd = syscmd or SystemCommand()
2431

@@ -35,3 +42,16 @@ def get_hostname(self) -> str:
3542

3643
def set_hostname(self, hostname: str):
3744
self._syscmd.nmcli(['general', 'hostname', hostname])
45+
46+
def reload(self, flags: Optional[List[str]] = None) -> None:
47+
if flags is not None:
48+
for flag in flags:
49+
if flag not in self.VALID_RELOAD_FLAGS:
50+
raise ValueError(
51+
f"Invalid reload flag '{flag}'. "
52+
f"Valid flags are: {', '.join(self.VALID_RELOAD_FLAGS)}"
53+
)
54+
cmd = ['general', 'reload']
55+
if flags:
56+
cmd += flags
57+
self._syscmd.nmcli(cmd)

nmcli/dummy/_general.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import List, Optional
22

33
from .._general import GeneralControlInterface
44
from ..data.general import General
@@ -10,6 +10,10 @@ class DummyGeneralControl(GeneralControlInterface):
1010
def set_hostname_args(self):
1111
return self._set_hostname_args
1212

13+
@property
14+
def reload_args(self):
15+
return self._reload_args
16+
1317
def __init__(self,
1418
result_call: General = None,
1519
result_status: General = None,
@@ -20,6 +24,7 @@ def __init__(self,
2024
self._result_status = result_status
2125
self._result_hostname = result_hostname
2226
self._set_hostname_args: List[str] = []
27+
self._reload_args: List[Optional[List[str]]] = []
2328

2429
def __call__(self) -> General:
2530
self._raise_error_if_needed()
@@ -41,6 +46,10 @@ def set_hostname(self, hostname: str):
4146
self._raise_error_if_needed()
4247
self._set_hostname_args.append(hostname)
4348

49+
def reload(self, flags: Optional[List[str]] = None) -> None:
50+
self._raise_error_if_needed()
51+
self._reload_args.append(flags)
52+
4453
def _raise_error_if_needed(self):
4554
if not self._raise_error is None:
4655
raise self._raise_error

tests/test_general.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from nmcli._const import NetworkConnectivity, NetworkManagerState
24
from nmcli._general import GeneralControl
35
from nmcli.data import General
@@ -37,3 +39,41 @@ def test_set_hostname():
3739
general = GeneralControl(s)
3840
general.set_hostname('test')
3941
assert s.passed_parameters == ['general', 'hostname', 'test']
42+
43+
44+
def test_reload_without_flags():
45+
s = DummySystemCommand()
46+
general = GeneralControl(s)
47+
general.reload()
48+
assert s.passed_parameters == ['general', 'reload']
49+
50+
51+
def test_reload_with_single_flag():
52+
s = DummySystemCommand()
53+
general = GeneralControl(s)
54+
general.reload(['conf'])
55+
assert s.passed_parameters == ['general', 'reload', 'conf']
56+
57+
58+
def test_reload_with_all_valid_flags():
59+
s = DummySystemCommand()
60+
general = GeneralControl(s)
61+
general.reload(['conf', 'dns-rc', 'dns-full'])
62+
assert s.passed_parameters == ['general', 'reload', 'conf', 'dns-rc', 'dns-full']
63+
64+
65+
def test_reload_with_invalid_flag():
66+
s = DummySystemCommand()
67+
general = GeneralControl(s)
68+
with pytest.raises(ValueError) as exc_info:
69+
general.reload(['invalid-flag'])
70+
assert "Invalid reload flag 'invalid-flag'" in str(exc_info.value)
71+
assert "Valid flags are: conf, dns-rc, dns-full" in str(exc_info.value)
72+
73+
74+
def test_reload_with_mixed_valid_and_invalid_flags():
75+
s = DummySystemCommand()
76+
general = GeneralControl(s)
77+
with pytest.raises(ValueError) as exc_info:
78+
general.reload(['conf', 'invalid'])
79+
assert "Invalid reload flag 'invalid'" in str(exc_info.value)

0 commit comments

Comments
 (0)