You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/test/units/module_utils/basic
Dag Wieers cd9471ef17 Introduce new 'required_by' argument_spec option (#28662)
* Introduce new "required_by' argument_spec option

This PR introduces a new **required_by** argument_spec option which allows you to say *"if parameter A is set, parameter B and C are required as well"*.

- The difference with **required_if** is that it can only add dependencies if a parameter is set to a specific value, not when it is just defined.
- The difference with **required_together** is that it has a commutative property, so: *"Parameter A and B are required together, if one of them has been defined"*.

As an example, we need this for the complex options that the xml module provides. One of the issues we often see is that users are not using the correct combination of options, and then are surprised that the module does not perform the requested action(s).

This would be solved by adding the correct dependencies, and mutual exclusives. For us this is important to get this shipped together with the new xml module in Ansible v2.4. (This is related to bugfix https://github.com/ansible/ansible/pull/28657)

```python
    module = AnsibleModule(
        argument_spec=dict(
            path=dict(type='path', aliases=['dest', 'file']),
            xmlstring=dict(type='str'),
            xpath=dict(type='str'),
            namespaces=dict(type='dict', default={}),
            state=dict(type='str', default='present', choices=['absent',
'present'], aliases=['ensure']),
            value=dict(type='raw'),
            attribute=dict(type='raw'),
            add_children=dict(type='list'),
            set_children=dict(type='list'),
            count=dict(type='bool', default=False),
            print_match=dict(type='bool', default=False),
            pretty_print=dict(type='bool', default=False),
            content=dict(type='str', choices=['attribute', 'text']),
            input_type=dict(type='str', default='yaml', choices=['xml',
'yaml']),
            backup=dict(type='bool', default=False),
        ),
        supports_check_mode=True,
        required_by=dict(
            add_children=['xpath'],
            attribute=['value', 'xpath'],
            content=['xpath'],
            set_children=['xpath'],
            value=['xpath'],
        ),
        required_if=[
            ['count', True, ['xpath']],
            ['print_match', True, ['xpath']],
        ],
        required_one_of=[
            ['path', 'xmlstring'],
            ['add_children', 'content', 'count', 'pretty_print', 'print_match', 'set_children', 'value'],
        ],
        mutually_exclusive=[
            ['add_children', 'content', 'count', 'print_match','set_children', 'value'],
            ['path', 'xmlstring'],
        ],
    )
```

* Rebase and fix conflict

* Add modules that use required_by functionality

* Update required_by schema

* Fix rebase issue
7 years ago
..
__init__.py
test__log_invocation.py Porting tests to pytest (#33387) 8 years ago
test__symbolic_mode_to_octal.py Split basic units (#33510) 8 years ago
test_argument_spec.py Introduce new 'required_by' argument_spec option (#28662) 7 years ago
test_atomic_move.py Split basic units (#33510) 8 years ago
test_deprecate_warn.py Split basic units (#33510) 8 years ago
test_dict_converters.py Split basic units (#33510) 8 years ago
test_exit_json.py Split basic units (#33510) 8 years ago
test_filesystem.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_get_file_attributes.py AnsibleModule.get_file_attributes: add unit test 8 years ago
test_get_module_path.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_heuristic_log_sanitize.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_imports.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_log.py Fix unit test parametrize order on Python 3.5. 7 years ago
test_no_log.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_platform_distribution.py Cleanups to the common.sys_info API 7 years ago
test_run_command.py Porting tests to pytest (#33387) 8 years ago
test_safe_eval.py Porting tests to pytest (#33387) 8 years ago
test_selinux.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago
test_set_mode_if_different.py If check mode enabled and file missing set changed to true 32676 (#33967) 8 years ago
test_tmpdir.py Move unit test compat code out of `lib/ansible/`. (#46996) 7 years ago