Skip to content

Commit

Permalink
KeyInfo: prevent read_only usage with can_disable, remove_value, abse…
Browse files Browse the repository at this point in the history
…nt_value, default, or required (ansible-collections#265)

* Prevent read_only usage with can_disable, remove_value, absent_value, default, or required.

* Add test.
  • Loading branch information
felixfontein authored Mar 3, 2024
1 parent 8400926 commit 2ea1fef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugins/module_utils/_api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ def __init__(self,
raise ValueError('absent_value can not be combined with default, automatically_computed_from, can_disable=True, or absent_value')
if read_only and write_only:
raise ValueError('read_only and write_only cannot be used at the same time')
if read_only and any([can_disable, remove_value is not None, absent_value is not None, default is not None, required]):
raise ValueError('read_only can not be combined with can_disable, remove_value, absent_value, default, or required')
self.can_disable = can_disable
self.remove_value = remove_value
self.automatically_computed_from = automatically_computed_from
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/plugins/module_utils/test__api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def test_key_info_errors():
KeyInfo(read_only=True, write_only=True)
assert exc.value.args[0] == 'read_only and write_only cannot be used at the same time'

with pytest.raises(ValueError) as exc:
KeyInfo(read_only=True, default=0)
assert exc.value.args[0] == 'read_only can not be combined with can_disable, remove_value, absent_value, default, or required'


SPLIT_PATHS = [
('', [], ''),
Expand Down

0 comments on commit 2ea1fef

Please sign in to comment.