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 23, 2021
1 parent 0f5a882 commit 1fca175
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
14 changes: 13 additions & 1 deletion module_utils/network_lsr/argument_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,23 @@ def _validate_impl(self, value, name):


class ArgValidatorMac(ArgValidatorStr):
def __init__(self, name, force_len=None, required=False, default_value=None):
def __init__(self, name, force_len=[6, 20], required=False, default_value=None):
ArgValidatorStr.__init__(self, name, required, default_value, None)
self.force_len = force_len

def _validate_impl(self, value, name):
if type(value) == int:
try:
value = Util.num_to_mac(value, self.force_len)
except MyError:
raise ValidationError(
name, "value '%s' is not a valid MAC address" % (value)
)
if not isinstance(value, Util.STRING_TYPE):
raise ValidationError(
name,
"must be a string and value should be quoted, but is '%s', " % (value),
)
v = ArgValidatorStr._validate_impl(self, value, name)
try:
addr = Util.mac_aton(v, self.force_len)
Expand Down
20 changes: 19 additions & 1 deletion module_utils/network_lsr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def mac_aton(mac_str, force_len=None):
if i == 1:
raise MyError("not a valid MAC address: '%s'" % (mac_str))
if force_len is not None:
if force_len != len(b):
if not any(a for a in force_len if a == len(b)):
raise MyError(
"not a valid MAC address of length %s: '%s'" % (force_len, mac_str)
)
Expand All @@ -267,6 +267,24 @@ 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 not any(a for a in force_len if a == len(mac)):
raise MyError(
"not a valid 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
14 changes: 14 additions & 0 deletions tests/unit/test_network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3260,6 +3260,20 @@ 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(41135085296), "52:54:00:12:34:56")
self.assertEqual(validator.validate("00:00:5e:00:53:5d"), "00:00:5e:00:53:5d")
self.assertEqual(validator.validate("00:00:00:00:00:00"), "00:00:00:00:00:00")
self.assertEqual(validator.validate("ff:ff:ff:ff:ff:ff"), "ff:ff:ff:ff:ff:ff")
self.assertValidationError(validator, 1234)
self.assertValidationError(validator, "aa:bb")
self.assertValidationError(validator, sys.maxsize)
self.assertValidationError(validator, 0)


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

0 comments on commit 1fca175

Please sign in to comment.