2
2
# -*- coding: utf-8 -*-
3
3
# SPDX-License-Identifier: BSD-3-Clause
4
4
5
+ from __future__ import absolute_import , division , print_function
6
+
7
+ __metaclass__ = type
8
+
9
+ DOCUMENTATION = """
10
+ ---
11
+ module: network_connections
12
+ author: Thomas Haller (@thom311)
13
+ short_description: module for network role to manage connection profiles
14
+ requirements: [pygobject, dbus, NetworkManager]
15
+ version_added: "2.0"
16
+ description:
17
+ - "WARNING: Do not use this module directly! It is only for role internal use."
18
+ - |
19
+ Manage networking profiles (connections) for NetworkManager and
20
+ initscripts networking providers. Documentation needs to be written. Note
21
+ that the network_connections module tightly integrates with the network
22
+ role and currently it is not expected to use this module outside the role.
23
+ Thus, consult README.md for examples for the role. The requirements are
24
+ only for the NetworkManager (nm) provider.
25
+ options: {}
26
+ """
27
+
28
+
5
29
import errno
6
30
import functools
7
31
import os
16
40
# pylint: disable=import-error, no-name-in-module
17
41
from ansible .module_utils .basic import AnsibleModule
18
42
from ansible .module_utils .network_lsr import ethtool # noqa:E501
19
- from ansible .module_utils .network_lsr import MyError # noqa:E501
43
+ from ansible .module_utils .network_lsr . myerror import MyError # noqa:E501
20
44
21
45
from ansible .module_utils .network_lsr .argument_validator import ( # noqa:E501
22
46
ArgUtil ,
30
54
# pylint: enable=import-error, no-name-in-module
31
55
32
56
33
- DOCUMENTATION = """
34
- ---
35
- module: network_connections
36
- author: "Thomas Haller ([email protected] )"
37
- short_description: module for network role to manage connection profiles
38
- requirements: for 'nm' provider requires pygobject, dbus and NetworkManager.
39
- version_added: "2.0"
40
- description: Manage networking profiles (connections) for NetworkManager and
41
- initscripts networking providers.
42
- options: Documentation needs to be written. Note that the network_connections
43
- module tightly integrates with the network role and currently it is not
44
- expected to use this module outside the role. Thus, consult README.md for
45
- examples for the role.
46
- """
47
-
48
-
49
57
###############################################################################
50
58
PERSISTENT_STATE = "persistent_state"
51
59
ABSENT_STATE = "absent"
@@ -772,7 +780,7 @@ def connection_compare(
772
780
if compare_flags is None :
773
781
compare_flags = NM .SettingCompareFlags .IGNORE_TIMESTAMP
774
782
775
- return not ( not ( con_a .compare (con_b , compare_flags )) )
783
+ return con_a .compare (con_b , compare_flags )
776
784
777
785
def connection_is_active (self , con ):
778
786
NM = Util .NM ()
@@ -1399,7 +1407,7 @@ def _check_mode_changed(self, old_check_mode, new_check_mode, connections):
1399
1407
def check_mode_set (self , check_mode , connections = None ):
1400
1408
c = self ._check_mode
1401
1409
self ._check_mode = check_mode
1402
- assert (
1410
+ if not (
1403
1411
(c is None and check_mode in [CheckMode .PREPARE ])
1404
1412
or (
1405
1413
c == CheckMode .PREPARE
@@ -1408,7 +1416,12 @@ def check_mode_set(self, check_mode, connections=None):
1408
1416
or (c == CheckMode .PRE_RUN and check_mode in [CheckMode .REAL_RUN ])
1409
1417
or (c == CheckMode .REAL_RUN and check_mode in [CheckMode .DONE ])
1410
1418
or (c == CheckMode .DRY_RUN and check_mode in [CheckMode .DONE ])
1411
- )
1419
+ ):
1420
+ raise AssertionError (
1421
+ "updating check_mode value from {0} into {1} is incorrect" .format (
1422
+ c , check_mode
1423
+ )
1424
+ )
1412
1425
self ._check_mode_changed (c , check_mode , connections )
1413
1426
1414
1427
@@ -1470,7 +1483,8 @@ def log(
1470
1483
warn_traceback = False ,
1471
1484
force_fail = False ,
1472
1485
):
1473
- assert idx >= - 1
1486
+ if not idx >= - 1 :
1487
+ raise AssertionError ("idx {0} is less than -1" .format (idx ))
1474
1488
self ._log_idx += 1
1475
1489
self .run_results [idx ]["log" ].append ((severity , msg , self ._log_idx ))
1476
1490
if severity == LogLevel .ERROR :
@@ -1607,14 +1621,15 @@ def connections(self):
1607
1621
def connections_data (self ):
1608
1622
c = self ._connections_data
1609
1623
if c is None :
1610
- assert self .check_mode in [
1624
+ if self .check_mode not in [
1611
1625
CheckMode .DRY_RUN ,
1612
1626
CheckMode .PRE_RUN ,
1613
1627
CheckMode .REAL_RUN ,
1614
- ]
1615
- c = []
1616
- for _ in range (0 , len (self .connections )):
1617
- c .append ({"changed" : False })
1628
+ ]:
1629
+ raise AssertionError (
1630
+ "invalid value {0} for self.check_mode" .format (self .check_mode )
1631
+ )
1632
+ c = [{"changed" : False }] * len (self .connections )
1618
1633
self ._connections_data = c
1619
1634
return c
1620
1635
@@ -1623,11 +1638,14 @@ def connections_data_reset(self):
1623
1638
c ["changed" ] = False
1624
1639
1625
1640
def connections_data_set_changed (self , idx , changed = True ):
1626
- assert self ._check_mode in [
1641
+ if self ._check_mode not in [
1627
1642
CheckMode .PRE_RUN ,
1628
1643
CheckMode .DRY_RUN ,
1629
1644
CheckMode .REAL_RUN ,
1630
- ]
1645
+ ]:
1646
+ raise AssertionError (
1647
+ "invalid value {0} for self._check_mode" .format (self ._check_mode )
1648
+ )
1631
1649
if not changed :
1632
1650
return
1633
1651
self .connections_data [idx ]["changed" ] = changed
@@ -1697,7 +1715,10 @@ def connection_modified_earlier(self, idx):
1697
1715
# modify the connection.
1698
1716
1699
1717
con = self .connections [idx ]
1700
- assert con ["state" ] in ["up" , "down" ]
1718
+ if con ["state" ] not in ["up" , "down" ]:
1719
+ raise AssertionError (
1720
+ "connection state {0} not 'up' or 'down'" .format (con ["state" ])
1721
+ )
1701
1722
1702
1723
# also check, if the current profile is 'up' with a 'type' (which
1703
1724
# possibly modifies the connection as well)
@@ -1745,7 +1766,9 @@ def check_mode_next(self):
1745
1766
elif self ._check_mode != CheckMode .DONE :
1746
1767
c = CheckMode .DONE
1747
1768
else :
1748
- assert False
1769
+ raise AssertionError (
1770
+ "invalid value {0} for self._check_mode" .format (self ._check_mode )
1771
+ )
1749
1772
self ._check_mode = c
1750
1773
self .run_env .check_mode_set (c )
1751
1774
return c
@@ -1911,7 +1934,12 @@ def run_prepare(self):
1911
1934
1912
1935
name = connection ["name" ]
1913
1936
if not name :
1914
- assert connection ["persistent_state" ] == "absent"
1937
+ if not connection ["persistent_state" ] == "absent" :
1938
+ raise AssertionError (
1939
+ "persistent_state must be 'absent' not {0} when there is no connection 'name'" .format (
1940
+ connection ["persistent_state" ]
1941
+ )
1942
+ )
1915
1943
continue
1916
1944
if name in names :
1917
1945
exists = names [name ]["nm.exists" ]
@@ -1988,7 +2016,7 @@ def _check_ethtool_setting_support(self, idx, connection):
1988
2016
idx , "ethtool.%s specified but not supported by NM" , specified
1989
2017
)
1990
2018
1991
- for option , _ in specified .items ():
2019
+ for option in specified .keys ():
1992
2020
nm_name = nm_get_name_fcnt (option )
1993
2021
if not nm_name :
1994
2022
self .log_fatal (
0 commit comments