Skip to content

Commit 1fe6c83

Browse files
author
Bernhard Kaindl
committed
Examples: Use NetworkManagerSettings().delete_connection_by_uuid(uuid)
1 parent cbf0f32 commit 1fe6c83

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

examples/async/delete-connection-by-uuid-async.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
#!/usr/bin/env python
22
# SPDX-License-Identifier: LGPL-2.1-or-later
33
#
4-
# Update a property of a connection profile, looked up by connection id
5-
#
6-
# The IPv4 settings of connections profiles are documented here:
7-
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
4+
# Create and delete a connection profile using the unique connection uuid
85
#
96
import asyncio
107
import logging
118
import sdbus
129
from uuid import uuid4
1310
from argparse import Namespace
1411
from sdbus_async.networkmanager import NetworkManagerSettings
15-
from sdbus_async.networkmanager import NetworkConnectionSettings
12+
from sdbus_async.networkmanager import NmSettingsInvalidConnectionError
1613

1714

1815
async def delete_connection_by_uuid(uuid: str) -> bool:
1916
"""Find and delete the connection identified by the given UUID"""
20-
settings_manager = NetworkManagerSettings()
21-
connection_path = await settings_manager.get_connection_by_uuid(uuid)
22-
if not connection_path:
17+
try:
18+
await NetworkManagerSettings().delete_connection_by_uuid(uuid)
19+
except NmSettingsInvalidConnectionError:
2320
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
2421
return False
25-
connection_settings = NetworkConnectionSettings(connection_path)
26-
await connection_settings.delete()
2722
return True
2823

2924

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
#!/usr/bin/env python
22
# SPDX-License-Identifier: LGPL-2.1-or-later
33
#
4-
# Update a property of a connection profile, looked up by connection id
5-
#
6-
# The IPv4 settings of connections profiles are documented here:
7-
# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
4+
# Create and delete a connection profile using the unique connection uuid
85
#
96
import logging
107
import sdbus
118
from uuid import uuid4
129
from argparse import Namespace
1310
from sdbus_block.networkmanager import NetworkManagerSettings
14-
from sdbus_block.networkmanager import NetworkConnectionSettings
11+
from sdbus_block.networkmanager import NmSettingsInvalidConnectionError
1512

1613

1714
def delete_connection_by_uuid(uuid: str) -> bool:
1815
"""Find and delete the connection identified by the given UUID"""
19-
settings_manager = NetworkManagerSettings()
20-
connection_path = settings_manager.get_connection_by_uuid(uuid)
21-
if not connection_path:
16+
try:
17+
NetworkManagerSettings().delete_connection_by_uuid(uuid)
18+
except NmSettingsInvalidConnectionError:
2219
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
2320
return False
24-
connection_settings = NetworkConnectionSettings(connection_path)
25-
connection_settings.delete()
2621
return True
2722

2823

29-
def create_and_delete_wifi_psk_connection_(args: Namespace) -> bool:
24+
def create_and_delete_wifi_psk_connection_async(args: Namespace) -> bool:
3025
"""Add a temporary (not yet saved) network connection profile
3126
:param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
3227
:return: dbus connection path of the created connection profile
@@ -41,5 +36,5 @@ def create_and_delete_wifi_psk_connection_(args: Namespace) -> bool:
4136
logging.basicConfig(format="%(message)s", level=logging.WARNING)
4237
sdbus.set_default_bus(sdbus.sd_bus_open_system())
4338
args = Namespace(conn_id="Example", uuid=uuid4(), ssid="S", psk="Password")
44-
if create_and_delete_wifi_psk_connection_(args):
39+
if create_and_delete_wifi_psk_connection_async(args):
4540
print(f"Succeeded in creating and deleting connection {args.uuid}")

0 commit comments

Comments
 (0)