diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 2eb37e6..98b584a 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -610,8 +610,8 @@ class ArgValidator_DictMacvlan(ArgValidatorDict): class ArgValidator_DictConnection(ArgValidatorDict): - VALID_STATES = ["up", "down"] VALID_PERSISTENT_STATES = ["absent", "present"] + VALID_STATES = VALID_PERSISTENT_STATES + ["up", "down"] VALID_TYPES = [ "ethernet", "infiniband", @@ -701,7 +701,14 @@ class ArgValidator_DictConnection(ArgValidatorDict): """ actions = [] state = result.get("state") - persistent_state = result.get("persistent_state") + if state in self.VALID_PERSISTENT_STATES: + del result["state"] + persistent_state_default = state + state = None + else: + persistent_state_default = None + + persistent_state = result.get("persistent_state", persistent_state_default) # default persistent_state to present (not done via default_value in the # ArgValidatorStr, the value will only be set at the end of diff --git a/tests/test_network_connections.py b/tests/test_network_connections.py index 1d0338e..c4bc384 100755 --- a/tests/test_network_connections.py +++ b/tests/test_network_connections.py @@ -1908,6 +1908,24 @@ class TestValidator(unittest.TestCase): {"actions": ["present"], "persistent_state": "present", "state": None}, ) + def test_invalid_persistent_state_up(self): + network_connections = [{"name": "internal", "persistent_state": "up"}] + self.assertRaises( + n.ValidationError, ARGS_CONNECTIONS.validate, network_connections + ) + + def test_invalid_persistent_state_down(self): + network_connections = [{"name": "internal", "persistent_state": "down"}] + self.assertRaises( + n.ValidationError, ARGS_CONNECTIONS.validate, network_connections + ) + + def test_invalid_state_test(self): + network_connections = [{"name": "internal", "state": "test"}] + self.assertRaises( + n.ValidationError, ARGS_CONNECTIONS.validate, network_connections + ) + def test_default_states_type(self): self.check_partial_connection_zero( {"name": "eth0", "type": "ethernet"}, @@ -1920,6 +1938,18 @@ class TestValidator(unittest.TestCase): {"actions": ["present"], "persistent_state": "present", "state": None}, ) + def test_state_present(self): + self.check_partial_connection_zero( + {"name": "eth0", "state": "present", "type": "ethernet"}, + {"actions": ["present"], "persistent_state": "present", "state": None}, + ) + + def test_state_absent(self): + self.check_partial_connection_zero( + {"name": "eth0", "state": "absent"}, + {"actions": ["absent"], "persistent_state": "absent", "state": None}, + ) + def test_persistent_state_absent(self): self.check_partial_connection_zero( {"name": "eth0", "persistent_state": "absent"},