From af07c2a58df5db1be084d443f515a181d7bfb939 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Mon, 28 Jun 2021 06:37:47 -0400 Subject: [PATCH] arg_validator: accept None as valid input for ArgValidatorList Users might want to use jinja2 templates to set properties. As such, it's convenient to accept None as an alias for an empty list. For exmaple, setting like `"match": {"path": None}` will be allowed by the role: network_connections: - name: enp0s8 type: ethernet persistent_state: present state: up match: path: ip: route_metric4: 10 Signed-off-by: Wen Liang --- module_utils/network_lsr/argument_validator.py | 5 +++++ tests/unit/test_network_connections.py | 1 + 2 files changed, 6 insertions(+) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index e3b0316..b5a9253 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -407,6 +407,11 @@ class ArgValidatorList(ArgValidator): # not supported. If you need that, define a proper list. value = [s for s in value.split(" ") if s] + if value is None: + # Users might want to use jinja2 templates to set properties. As such, + # it's convenient to accept None as an alias for an empty list + # e.g. setting like `"match": {"path": None}` will be allowed by the role + return [] result = [] for (idx, v) in enumerate(value): try: diff --git a/tests/unit/test_network_connections.py b/tests/unit/test_network_connections.py index 79358f7..74f4d37 100644 --- a/tests/unit/test_network_connections.py +++ b/tests/unit/test_network_connections.py @@ -445,6 +445,7 @@ class TestValidator(unittest.TestCase): ) self.assertEqual([1, 5], v.validate(["1", 5])) self.assertValidationError(v, [1, "s"]) + self.assertEqual(v.validate(None), []) def test_empty(self): self.maxDiff = None