summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/common/validation.py
Commit message (Collapse)AuthorAgeFilesLines
* Use ansible.module_utils.common.text.converters (#80704)Matt Clay2023-05-031-1/+1
| | | Replace use of old `ansible.module_utils._text` and add a unit test to maintain backwards compatibility.
* argspec - fix validating type for required options that are None (#79677)Sloane Hertel2023-04-171-1/+1
| | | | | * Only bypass type validation for null parameters if the default is None. A default is mutually exclusive with required. * Prevent coercing None to str type. Fail the type check instead.
* test: Unit tests for validation methods (#75061)Abhijeet Kasurde2021-06-221-0/+10
| | | | | | | | | | * check_required_one_of() * check_required_by() * check_required_if() * check_missing_parameters() Fixes: #55994 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
* Add porting guide and documentation for changes to argument spec validation ↵Sam Doran2021-04-201-48/+54
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (#74268) * Add ArgumentSpecValidator to docs * Improve docs for ArgumentSpecValidator * Document removal of private methods * Update module_utils documentation - Add docs for argument spec classes as well as validation and parameters files. - preserve the order in the source for errors.py - document DEFAULT_TYPE_VALIDATORS so it can be referenced elsewhere - fix automodule directive for validation.py * Update docs in arg_spec and paremeters - This improves the generated documentation. * Document breaking changes in porting guide. * Update formatting in porting guide and add a Deprecated section * Fine tune module_utils documentation * Move instance docstring to the __init__ method Remove optional description since it fails the sanity test and I am not 100% it is valid anyway. * Remoe incorrect parameter from docstring This was changed a while ago but wasn't removed from the docstring. * Use attr rather than attribute The py:attribute: domain only exists in newer Sphinx >= 3.1. * Improve documentation on exceptions * Final pass - use args/kwargs instead of param - fix formatting errors that didn't display examples correctly - format TypeErrors so they are referenced as classes - specify complex types
* Use ArgumentSpecValidator in AnsibleModule (#73703)Sam Doran2021-03-191-34/+46
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Begin using ArgumentSpecValidator in AnsibleModule * Add check parameters to ArgumentSpecValidator Add additional parameters for specifying required and mutually exclusive parameters. Add code to the .validate() method that runs these additional checks. * Make errors related to unsupported parameters match existing behavior Update the punctuation in the message slightly to make it more readable. Add a property to ArgumentSpecValidator to hold valid parameter names. * Set default values after performining checks * FIx sanity test failure * Use correct parameters when checking sub options * Use a dict when iterating over check functions Referencing by key names makes things a bit more readable IMO. * Fix bug in comparison for sub options evaluation * Add options_context to check functions This allows the parent parameter to be added the the error message if a validation error occurs in a sub option. * Fix bug in apply_defaults behavior of sub spec validation * Accept options_conext in get_unsupported_parameters() If options_context is supplied, a tuple of parent key names of unsupported parameter will be created. This allows the full "path" to the unsupported parameter to be reported. * Build path to the unsupported parameter for error messages. * Remove unused import * Update recursive finder test * Skip if running in check mode This was done in the _check_arguments() method. That was moved to a function that has no way of calling fail_json(), so it must be done outside of validation. This is a silght change in behavior, but I believe the correct one. Previously, only unsupported parameters would cause a failure. All other checks would not be executed if the modlue did not support check mode. This would hide validation failures in check mode. * The great purge Remove all methods related to argument spec validation from AnsibleModule * Keep _name and kind in the caller and out of the validator This seems a bit awkward since this means the caller could end up with {name} and {kind} in the error message if they don't run the messages through the .format() method with name and kind parameters. * Double moustaches work I wasn't sure if they get stripped or not. Looks like they do. Neat trick. * Add changelog * Update unsupported parameter test The error message changed to include name and kind. * Remove unused import * Add better documentation for ArgumentSpecValidator class * Fix example * Few more docs fixes * Mark required and mutually exclusive attributes as private * Mark validate functions as private * Reorganize functions in validation.py * Remove unused imports in basic.py related to argument spec validation * Create errors is module_utils We have errors in lib/ansible/errors/ but those cannot be used by modules. * Update recursive finder test * Move errors to file rather than __init__.py * Change ArgumentSpecValidator.validate() interface Raise AnsibleValidationErrorMultiple on validation error which contains all AnsibleValidationError exceptions for validation failures. Return the validated parameters if validation is successful rather than True/False. Update docs and tests. * Get attribute in loop so that the attribute name can also be used as a parameter * Shorten line * Update calling code in AnsibleModule for new validator interface * Update calling code in validate_argument_spec based in new validation interface * Base custom exception class off of Exception * Call the __init__ method of the base Exception class to populate args * Ensure no_log values are always updated * Make custom exceptions more hierarchical This redefines AnsibleError from lib/ansible/errors with a different signature since that cannot be used by modules. This may be a bad idea. Maybe lib/ansible/errors should be moved to module_utils, or AnsibleError defined in this commit should use the same signature as the original. * Just go back to basing off Exception * Return ValidationResult object on successful validation Create a ValidationResult class. Return a ValidationResult from ArgumentSpecValidator.validate() when validation is successful. Update class and method docs. Update unit tests based on interface change. * Make it easier to get error objects from AnsibleValidationResultMultiple This makes the interface cleaner when getting individual error objects contained in a single AnsibleValidationResultMultiple instance. * Define custom exception for each type of validation failure These errors indicate where a validation error occured. Currently they are empty but could contain specific data for each exception type in the future. * Update tests based on (yet another) interface change * Mark several more functions as private These are all doing rather "internal" things. The ArgumentSpecValidator class is the preferred public interface. * Move warnings and deprecations to result object Rather than calling deprecate() and warn() directly, store them on the result object so the caller can decide what to do with them. * Use subclass for module arg spec validation The subclass uses global warning and deprecations feature * Fix up docs * Remove legal_inputs munging from _handle_aliases() This is done in AnsibleModule by the _set_internal_properties() method. It only makes sense to do that for an AnsibleModule instance (it should update the parameters before performing validation) and shouldn't be done by the validator. Create a private function just for getting legal inputs since that is done in a couple of places. It may make sense store that on the ValidationResult object. * Increase test coverage * Remove unnecessary conditional ci_complete * Mark warnings and deprecations as private in the ValidationResult They can be made public once we come up with a way to make them more generally useful, probably by creating cusom objects to store the data in more structure way. * Mark valid_parameter_names as private and populate it during initialization * Use a global for storing the list of additonal checks to perform This list is used by the main validate method as well as the sub spec validation.
* Add argument spec validator (#73335)Sam Doran2021-02-111-32/+35
| | | Add argument spec validator class
* arg_spec - rework _check_arguments() (#72447)Sam Doran2020-11-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | * Move _syslog_facitily to __init__ No good reason it should not be set for each object * Move internal property setting to private method * Create check_arguments() function * Remove unused import * Rename function to better match its behavior Change the behavior to return a set, either empty or populated, with unsupported keys. Accept legal_inputs as optional which will not required calling handle_aliases before calling get_unsupported_parameters(). * Add changelog * Rework function behavior and documentation I realized I missed the original intent of this method when moving it to a function. It is meant to compared the parameter keys to legal inputs always, not compare parameter keys to argument spec keys, even though the argument spec keys should be a subset of legal inputs. * Add tests * Fix typo. * Set internal properties when handling suboptions
* Fix and add tests for some module_utils.common.validation (#67771)Rick Elrod2020-03-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Fix test_check_mutually_exclusive exception-checking Asserting inside of the `with` context of `pytest.raises` doesn't actually have any effect. So we move the assert out, using the exception that gets placed into the scope after we leave the context, and ensure that it actually gets checked. This is also what the pytest documentation says to do: https://docs.pytest.org/en/latest/assert.html#assertions-about-expected-exceptions Signed-off-by: Rick Elrod <rick@elrod.me> * Add some tests for check_required_together Signed-off-by: Rick Elrod <rick@elrod.me> * use to_native instead of str, for consistency Signed-off-by: Rick Elrod <rick@elrod.me> * Add newlines for pep8 Signed-off-by: Rick Elrod <rick@elrod.me> * Add tests for check_required_arguments Signed-off-by: Rick Elrod <rick@elrod.me> * Sort missing keys in error message, since hashes are unsorted and this can be random Signed-off-by: Rick Elrod <rick@elrod.me> * Add changelog entry Signed-off-by: Rick Elrod <rick@elrod.me>
* Replace import of pycompat24.literal_eval with ast.literal_eval. (#64088)Mads Jensen2019-11-041-1/+1
|
* Add human_to_bytes isbits arg unit tests&docstringAndrey Klychkov2019-07-231-2/+4
| | | | | | | PR #58623 Co-Authored-By: Sviatoslav Sydorenko <webknjaz@redhat.com> Co-Authored-By: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
* Move type checking methods out of basic.py and add unit tests (#53687)Sam Doran2019-03-211-13/+275
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Move check_type_str() out of basic.py * Move check_type_list() out of basic.py * Move safe_eval() out of basic.py * Move check_type_dict() out of basic.py * Move json importing code to common location * Move check_type_bool() out of basic.py * Move _check_type_int() out of basic.py * Move _check_type_float() out of basic.py * Move _check_type_path() out of basic.py * Move _check_type_raw() out of basic.py * Move _check_type_bytes() out of basic.py * Move _check_type_bits() out of basic.py * Create text.formatters.py Move human_to_bytes, bytes_to_human, and _lenient_lowercase out of basic.py into text.formatters.py Change references in modules to point to function at new location * Move _check_type_jsonarg() out of basic.py * Rename json related functions and put them in common.text.converters Move formatters.py to common.text.formatters.py and update references in modules. * Rework check_type_str() Add allow_conversion option to make the function more self-contained. Move the messaging back to basic.py since those error messages are more relevant to using this function in the context of AnsibleModule and not when using the function in isolation. * Add unit tests for type checking functions * Change _lenient_lowercase to lenient_lowercase per feedback
* Move utility functions out of basic.py (#51715)Sam Doran2019-03-141-0/+283
Move the following methods to lib/anisble/module_utils/common/validation.py: - _count_terms() - _check_mutually_exclusive() - _check_required_one_of() - _check_required_together() - _check_required_by() - _check_required_arguments() - _check_required_if - fail_on_missing_params() --> create check_missing_parameters()