Skip to content

Commit dcb2b06

Browse files
authored
Merge pull request #13 from bernhardkaindl/del-conn-by-uuid
Add delete-connection-by-uuid example
2 parents 9c2d811 + 5bfa627 commit dcb2b06

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

examples/async/add-wifi-psk-connection-async.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def add_wifi_psk_connection_profile_async(args: Namespace) -> str:
7373
"id": ("s", args.conn_id),
7474
"uuid": ("s", str(args.uuid)),
7575
"type": ("s", "802-11-wireless"),
76-
"autoconnect": ("b", args.auto),
76+
"autoconnect": ("b", bool(hasattr(args, "auto") and args.auto)),
7777
},
7878
"802-11-wireless": {
7979
"mode": ("s", "infrastructure"),
@@ -90,13 +90,14 @@ async def add_wifi_psk_connection_profile_async(args: Namespace) -> str:
9090
}
9191

9292
# To bind the new connection to a specific interface, use this:
93-
if args.interface_name:
93+
if hasattr(args, "interface_name") and args.interface_name:
9494
properties["connection"]["interface-name"] = ("s", args.interface_name)
9595

9696
s = NetworkManagerSettings()
97-
addconnection = s.add_connection if args.save else s.add_connection_unsaved
97+
save = bool(hasattr(args, "save") and args.save)
98+
addconnection = s.add_connection if save else s.add_connection_unsaved
9899
connection_settings_dbus_path = await addconnection(properties)
99-
created = "created and saved" if args.save else "created"
100+
created = "created and saved" if save else "created"
100101
info(f"New unsaved connection profile {created}, show it with:")
101102
info(f'nmcli connection show "{args.conn_id}"|grep -v -e -- -e default')
102103
info("Settings used:")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
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
8+
#
9+
import asyncio
10+
import logging
11+
import sdbus
12+
from uuid import uuid4
13+
from argparse import Namespace
14+
from sdbus_async.networkmanager import NetworkManagerSettings
15+
from sdbus_async.networkmanager import NetworkConnectionSettings
16+
17+
18+
async def delete_connection_by_uuid(uuid: str) -> bool:
19+
"""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:
23+
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
24+
return False
25+
connection_settings = NetworkConnectionSettings(connection_path)
26+
await connection_settings.delete()
27+
return True
28+
29+
30+
async def create_and_delete_wifi_psk_connection_async(args: Namespace) -> bool:
31+
"""Add a temporary (not yet saved) network connection profile
32+
:param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
33+
:return: dbus connection path of the created connection profile
34+
"""
35+
add_wifi_psk_connection = __import__("add-wifi-psk-connection-async")
36+
if not await add_wifi_psk_connection.add_wifi_psk_connection_async(args):
37+
return False
38+
return await delete_connection_by_uuid(str(args.uuid))
39+
40+
41+
if __name__ == "__main__":
42+
logging.basicConfig(format="%(message)s", level=logging.WARNING)
43+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
44+
args = Namespace(conn_id="Example", uuid=uuid4(), ssid="S", psk="Password")
45+
if asyncio.run(create_and_delete_wifi_psk_connection_async(args)):
46+
print(f"Succeeded in creating and deleting connection {args.uuid}")

examples/block/add-wifi-psk-connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def add_wifi_psk_connection_profile(args: Namespace) -> str:
7272
"id": ("s", args.conn_id),
7373
"uuid": ("s", str(args.uuid)),
7474
"type": ("s", "802-11-wireless"),
75-
"autoconnect": ("b", args.auto),
75+
"autoconnect": ("b", bool(hasattr(args, "auto") and args.auto)),
7676
},
7777
"802-11-wireless": {
7878
"mode": ("s", "infrastructure"),
@@ -89,13 +89,14 @@ def add_wifi_psk_connection_profile(args: Namespace) -> str:
8989
}
9090

9191
# To bind the new connection to a specific interface, use this:
92-
if args.interface_name:
92+
if hasattr(args, "interface_name") and args.interface_name:
9393
properties["connection"]["interface-name"] = ("s", args.interface_name)
9494

9595
s = NetworkManagerSettings()
96-
addconnection = s.add_connection if args.save else s.add_connection_unsaved
96+
save = bool(hasattr(args, "save") and args.save)
97+
addconnection = s.add_connection if save else s.add_connection_unsaved
9798
connection_settings_dbus_path = addconnection(properties)
98-
created = "created and saved" if args.save else "created"
99+
created = "created and saved" if save else "created"
99100
info(f"New unsaved connection profile {created}, show it with:")
100101
info(f'nmcli connection show "{args.conn_id}"|grep -v -e -- -e default')
101102
info("Settings used:")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
#
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
8+
#
9+
import logging
10+
import sdbus
11+
from uuid import uuid4
12+
from argparse import Namespace
13+
from sdbus_block.networkmanager import NetworkManagerSettings
14+
from sdbus_block.networkmanager import NetworkConnectionSettings
15+
16+
17+
def delete_connection_by_uuid(uuid: str) -> bool:
18+
"""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:
22+
logging.getLogger().fatal(f"Connection {uuid} for deletion not found")
23+
return False
24+
connection_settings = NetworkConnectionSettings(connection_path)
25+
connection_settings.delete()
26+
return True
27+
28+
29+
def create_and_delete_wifi_psk_connection_(args: Namespace) -> bool:
30+
"""Add a temporary (not yet saved) network connection profile
31+
:param Namespace args: autoconnect, conn_id, psk, save, ssid, uuid
32+
:return: dbus connection path of the created connection profile
33+
"""
34+
add_wifi_psk_connection = __import__("add-wifi-psk-connection")
35+
if not add_wifi_psk_connection.add_wifi_psk_connection(args):
36+
return False
37+
return delete_connection_by_uuid(str(args.uuid))
38+
39+
40+
if __name__ == "__main__":
41+
logging.basicConfig(format="%(message)s", level=logging.WARNING)
42+
sdbus.set_default_bus(sdbus.sd_bus_open_system())
43+
args = Namespace(conn_id="Example", uuid=uuid4(), ssid="S", psk="Password")
44+
if create_and_delete_wifi_psk_connection_(args):
45+
print(f"Succeeded in creating and deleting connection {args.uuid}")

0 commit comments

Comments
 (0)