Skip to content

Commit 0cff316

Browse files
author
ushiboy
committed
Add device up and device down
1 parent e25761f commit 0cff316

4 files changed

Lines changed: 80 additions & 3 deletions

File tree

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ except Exception as e:
7171
| device | status | supported |
7272
| device | show | supported |
7373
| device | set | not supported |
74-
| device | up | not supported |
74+
| device | up | supported |
7575
| device | connect | supported |
7676
| device | reapply | supported |
7777
| device | modify | not supported |
78-
| device | down | not supported |
78+
| device | down | supported |
7979
| device | disconnect | supported |
8080
| device | delete | supported |
8181
| device | monitor | not supported |
@@ -221,6 +221,16 @@ The `fields` argument applies the same effect to the command as the `-f | --fiel
221221
nmcli.device.show_all(fields: str = None) -> List[DeviceDetails]
222222
```
223223

224+
#### nmcli.device.up
225+
226+
Connect the device.
227+
228+
The `wait` argument applies the same effect to the command as the `--wait` option. If it is omitted, the default behavior is followed.
229+
230+
```
231+
nmcli.device.up(ifname: str, wait: int = None) -> None
232+
```
233+
224234
#### nmcli.device.connect
225235

226236
Connect the device.
@@ -231,6 +241,16 @@ The `wait` argument applies the same effect to the command as the `--wait` optio
231241
nmcli.device.connect(ifname: str, wait: int = None) -> None
232242
```
233243

244+
#### nmcli.device.down
245+
246+
Disconnect a device and prevent the device from automatically activating further connections without user/manual intervention.
247+
248+
The `wait` argument applies the same effect to the command as the `--wait` option. If it is omitted, the default behavior is followed.
249+
250+
```
251+
nmcli.device.down(ifname: str, wait: int = None) -> None
252+
```
253+
234254
#### nmcli.device.disconnect
235255

236256
Disconnect devices.

nmcli/_device.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ def show(self, ifname: str, fields: str = None) -> DeviceDetails:
5252
def show_all(self, fields: str = None) -> List[DeviceDetails]:
5353
raise NotImplementedError
5454

55+
def up(self, ifname: str, wait: int = None) -> None:
56+
raise NotImplementedError
57+
5558
def connect(self, ifname: str, wait: int = None) -> None:
5659
raise NotImplementedError
5760

61+
def down(self, ifname: str, wait: int = None) -> None:
62+
raise NotImplementedError
63+
5864
def disconnect(self, ifname: str, wait: int = None) -> None:
5965
raise NotImplementedError
6066

@@ -131,11 +137,21 @@ def show_all(self, fields: str = None) -> List[DeviceDetails]:
131137
details[key] = None if value in ('--', '""') else value
132138
return results
133139

140+
def up(self, ifname: str, wait: int = None) -> None:
141+
cmd = add_wait_option_if_needed(
142+
wait) + ['device', 'up', ifname]
143+
self._syscmd.nmcli(cmd)
144+
134145
def connect(self, ifname: str, wait: int = None) -> None:
135146
cmd = add_wait_option_if_needed(
136147
wait) + ['device', 'connect', ifname]
137148
self._syscmd.nmcli(cmd)
138149

150+
def down(self, ifname: str, wait: int = None) -> None:
151+
cmd = add_wait_option_if_needed(
152+
wait) + ['device', 'down', ifname]
153+
self._syscmd.nmcli(cmd)
154+
139155
def disconnect(self, ifname: str, wait: int = None) -> None:
140156
cmd = add_wait_option_if_needed(
141157
wait) + ['device', 'disconnect', ifname]

nmcli/dummy/_device.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
from ..data.hotspot import Hotspot
66

77

8-
class DummyDeviceControl(DeviceControlInterface):
8+
class DummyDeviceControl(DeviceControlInterface): # pylint: disable=too-many-public-methods
99

1010
@property
1111
def show_args(self):
1212
return self._show_args
1313

14+
@property
15+
def up_args(self):
16+
return self._up_args
17+
1418
@property
1519
def connect_args(self):
1620
return self._connect_args
1721

22+
@property
23+
def down_args(self):
24+
return self._down_args
25+
1826
@property
1927
def disconnect_args(self):
2028
return self._disconnect_args
@@ -57,7 +65,9 @@ def __init__(self,
5765
self._result_show_all = result_show_all or []
5866
self._result_wifi_hotspot = result_wifi_hotspot
5967
self._show_args: List[Tuple] = []
68+
self._up_args: List[Tuple] = []
6069
self._connect_args: List[Tuple] = []
70+
self._down_args: List[Tuple] = []
6171
self._disconnect_args: List[Tuple] = []
6272
self._reapply_args: List[str] = []
6373
self._delete_args: List[Tuple] = []
@@ -85,10 +95,18 @@ def show_all(self, fields: str = None) -> List[DeviceDetails]:
8595
self._raise_error_if_needed()
8696
return self._result_show_all
8797

98+
def up(self, ifname: str, wait: int = None) -> None:
99+
self._raise_error_if_needed()
100+
self._up_args.append((ifname, wait))
101+
88102
def connect(self, ifname: str, wait: int = None) -> None:
89103
self._raise_error_if_needed()
90104
self._connect_args.append((ifname, wait))
91105

106+
def down(self, ifname: str, wait: int = None) -> None:
107+
self._raise_error_if_needed()
108+
self._down_args.append((ifname, wait))
109+
92110
def disconnect(self, ifname: str, wait: int = None) -> None:
93111
self._raise_error_if_needed()
94112
self._disconnect_args.append((ifname, wait))

tests/test_device.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ def test_show_all():
171171
assert s2.passed_parameters == ['-f', 'all', 'device', 'show']
172172

173173

174+
def test_up():
175+
s = DummySystemCommand()
176+
device = DeviceControl(s)
177+
ifname = 'eth0'
178+
device.up(ifname)
179+
assert s.passed_parameters == ['device', 'up', ifname]
180+
181+
device.up(ifname, wait=10)
182+
assert s.passed_parameters == ['--wait', '10', 'device', 'up', ifname]
183+
184+
174185
def test_connect():
175186
s = DummySystemCommand()
176187
device = DeviceControl(s)
@@ -182,6 +193,18 @@ def test_connect():
182193
assert s.passed_parameters == ['--wait', '10', 'device', 'connect', ifname]
183194

184195

196+
def test_down():
197+
s = DummySystemCommand()
198+
device = DeviceControl(s)
199+
ifname = 'eth0'
200+
device.down(ifname)
201+
assert s.passed_parameters == ['device', 'down', ifname]
202+
203+
device.down(ifname, wait=10)
204+
assert s.passed_parameters == [
205+
'--wait', '10', 'device', 'down', ifname]
206+
207+
185208
def test_disconnect():
186209
s = DummySystemCommand()
187210
device = DeviceControl(s)

0 commit comments

Comments
 (0)