summaryrefslogtreecommitdiff
path: root/examples/python/gi/nm-connection-update-stable-id.py
blob: 163b3018e01bf00d7555fe3a815a66d82c21eff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0+
#
# Copyright 2017 Red Hat, Inc.
#

#
# This example updates a connection's stable-id by appending -#number.

import sys
import re

import gi
gi.require_version('NM', '1.0')
from gi.repository import GLib, NM

def usage():
    print('Usage: %s [[id] <id>]' % (sys.argv[0]))
    print('       %s [[uuid] <uuid>]' % (sys.argv[0]))
    return 1

def find_connection(nm_client, arg_type, arg_id):
    for c in nm_client.get_connections():
        if arg_type in [None, 'id'] and c.get_id() == arg_id:
            return c
        if arg_type in [None, 'uuid'] and c.get_uuid() == arg_id:
            return c

def main():
    if len(sys.argv) < 2 or len(sys.argv) > 3:
        return usage()
    if len(sys.argv) == 3:
        arg_type = sys.argv[1]
        arg_id = sys.argv[2]
        if arg_type not in ['id', 'uuid']:
            return usage()
    else:
        arg_type = None
        arg_id = sys.argv[1]
    arg_log = '%s"%s"' % ((' with %s ' % (arg_type)) if arg_type else '', arg_id)

    main_loop = GLib.MainLoop()

    nm_client = NM.Client.new(None)

    con = find_connection(nm_client, arg_type, arg_id)
    if con is None:
        print('could not find a connection %s' % (arg_log))
        return 1

    s_con = con.get_setting_connection()
    if s_con is None:
        print('connection %s has no [connection] setting' % (arg_log))
        return 1

    arg_log = '"%s" (%s)' % (s_con.get_id(), s_con.get_uuid())

    stable_id = s_con.get_stable_id()
    if not stable_id:
        print('connection %s has no stable-id set' % (arg_log))
        return 1

    re_match = re.search('\A(.*)-([0-9]+)\Z', stable_id)
    if not re_match:
        stable_id = stable_id + '-1'
    else:
        stable_id = re_match.group(1) + '-' + str(int(re_match.group(2)) + 1)

    con2 = NM.SimpleConnection.new_clone(con)
    s_con = con2.get_setting_connection()
    s_con.set_property(NM.SETTING_CONNECTION_STABLE_ID, stable_id)

    result = {}
    def _update2_cb(con, async_result, user_data):
        try:
            r = con.update2_finish(async_result)
        except Exception as e:
            result['error'] = e
        else:
            result['result'] = r
        main_loop.quit()

    con.update2(con2.to_dbus(NM.ConnectionSerializationFlags.ALL),
                NM.SettingsUpdate2Flags.BLOCK_AUTOCONNECT,
                None,
                None,
                _update2_cb,
                None)

    main_loop.run()

    if 'error' in result:
        print('update connection %s failed: %s' % (arg_log, result['error']))
        return 1

    print('update connection %s succeeded: %s' % (arg_log, result['result']))
    print('set stable-id to "%s"' % (stable_id))
    return 0

if __name__ == '__main__':
    sys.exit(main())