Skip to content

Commit

Permalink
Support specifying mac address without quotation mark
Browse files Browse the repository at this point in the history
Signed-off-by: Wen Liang <[email protected]>
  • Loading branch information
liangwen12year committed Mar 18, 2021
1 parent 0f5a882 commit f06095e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ By default, profiles are created with autoconnect enabled.
The `mac` address is optional and restricts the profile to be usable only on
devices with the given MAC address. `mac` is only allowed for `type`
`ethernet` or `infiniband` to match a non-virtual device with the
profile.
profile. The value of `mac` address is string and should use with quotation mark.

- For `NetworkManager`, `mac` is the permanent MAC address, `ethernet.mac-address`.

Expand Down Expand Up @@ -574,7 +574,7 @@ network_connections:
#persistent_state: present # default
type: ethernet
autoconnect: yes
mac: 00:00:5e:00:53:5d
mac: "00:00:5e:00:53:5d"
ip:
dhcp4: yes
```
Expand Down
2 changes: 2 additions & 0 deletions module_utils/network_lsr/argument_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ def __init__(self, name, force_len=None, required=False, default_value=None):
self.force_len = force_len

def _validate_impl(self, value, name):
if type(value) == int:
value = Util.num_to_mac(value, self.force_len)
v = ArgValidatorStr._validate_impl(self, value, name)
try:
addr = Util.mac_aton(v, self.force_len)
Expand Down
19 changes: 19 additions & 0 deletions module_utils/network_lsr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ def mac_ntoa(mac):
def mac_norm(mac_str, force_len=None):
return Util.mac_ntoa(Util.mac_aton(mac_str, force_len))

@staticmethod
def num_to_mac(num, force_len=None):
mac = []
s_num = num
while num:
num, mod = divmod(num, 60)
if mod < 10:
mac.insert(0, "0" + str(mod))
else:
mac.insert(0, str(mod))
mac_addr = ":".join(mac)
if force_len is not None:
if force_len != len(mac):
raise MyError(
"not a valid sexagesimal number '%d' for MAC address: '%s'"
% (s_num, mac_addr)
)
return mac_addr

@staticmethod
def boolean(arg):
if arg is None or isinstance(arg, bool):
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3260,6 +3260,15 @@ def test_set_deprecated_slave_type(self):
self.assertTrue("port_type" in connection)
self.assertTrue("slave_type" not in connection)

def test_mac_address_argvalidator(self):
"""
Test that argvalidator for validating mac address is correctly defined.
"""
validator = network_lsr.argument_validator.ArgValidatorMac("mac")
self.assertEqual(
validator._validate_impl(41135085296, "mac"), "52:54:00:12:34:56"
)


@my_test_skipIf(nmutil is None, "no support for NM (libnm via pygobject)")
class TestNM(unittest.TestCase):
Expand Down

0 comments on commit f06095e

Please sign in to comment.