2016-04-06 20:58:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-12-12 21:19:58 +01:00
|
|
|
|
|
|
|
# Copyright: (c) 2015, Matt Martz <matt@sivel.net>
|
|
|
|
# Copyright: (c) 2015, Rackspace US, Inc.
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2016-04-06 20:58:35 +02:00
|
|
|
|
2018-11-21 18:29:29 +01:00
|
|
|
import re
|
|
|
|
from voluptuous import ALLOW_EXTRA, PREVENT_EXTRA, All, Any, Length, Invalid, Required, Schema, Self
|
2017-04-10 16:43:39 +02:00
|
|
|
from ansible.module_utils.six import string_types
|
2018-11-21 18:29:29 +01:00
|
|
|
from ansible.module_utils.common.collections import is_iterable
|
2017-04-10 16:43:39 +02:00
|
|
|
list_string_types = list(string_types)
|
2018-09-24 23:38:04 +02:00
|
|
|
any_string_types = Any(*string_types)
|
2016-03-01 21:20:57 +01:00
|
|
|
|
2018-11-21 18:29:29 +01:00
|
|
|
# Valid DOCUMENTATION.author lines
|
|
|
|
# Based on Ansibulbot's extract_github_id()
|
|
|
|
# author: First Last (@name) [optional anything]
|
|
|
|
# "Ansible Core Team" - Used by the Bot
|
|
|
|
# "Michael DeHaan" - nop
|
|
|
|
# "Name (!UNKNOWN)" - For the few untraceable authors
|
|
|
|
author_line = re.compile(r'^\w.*(\(@([\w-]+)\)|!UNKNOWN)(?![\w.])|^Ansible Core Team$|^Michael DeHaan$')
|
|
|
|
|
2018-07-31 22:04:22 +02:00
|
|
|
|
|
|
|
def sequence_of_sequences(min=None, max=None):
|
|
|
|
return All(
|
|
|
|
Any(
|
|
|
|
None,
|
|
|
|
[Length(min=min, max=max)],
|
|
|
|
tuple([Length(min=min, max=max)]),
|
|
|
|
),
|
|
|
|
Any(
|
|
|
|
None,
|
|
|
|
[Any(list, tuple)],
|
|
|
|
tuple([Any(list, tuple)]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-12-12 21:19:58 +01:00
|
|
|
seealso_schema = Schema(
|
|
|
|
[
|
|
|
|
Any(
|
|
|
|
{
|
|
|
|
Required('module'): Any(*string_types),
|
|
|
|
'description': Any(*string_types),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Required('ref'): Any(*string_types),
|
|
|
|
Required('description'): Any(*string_types),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Required('name'): Any(*string_types),
|
|
|
|
Required('link'): Any(*string_types),
|
|
|
|
Required('description'): Any(*string_types),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2018-07-31 22:04:22 +02:00
|
|
|
ansible_module_kwargs_schema = Schema(
|
|
|
|
{
|
|
|
|
'argument_spec': dict,
|
|
|
|
'bypass_checks': bool,
|
|
|
|
'no_log': bool,
|
|
|
|
'check_invalid_arguments': Any(None, bool),
|
|
|
|
'mutually_exclusive': sequence_of_sequences(min=2),
|
|
|
|
'required_together': sequence_of_sequences(min=2),
|
|
|
|
'required_one_of': sequence_of_sequences(min=2),
|
|
|
|
'add_file_common_args': bool,
|
|
|
|
'supports_check_mode': bool,
|
|
|
|
'required_if': sequence_of_sequences(min=3),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-03-13 20:49:27 +01:00
|
|
|
suboption_schema = Schema(
|
2016-03-01 21:20:57 +01:00
|
|
|
{
|
2017-04-10 16:43:39 +02:00
|
|
|
Required('description'): Any(list_string_types, *string_types),
|
2016-03-01 21:20:57 +01:00
|
|
|
'required': bool,
|
2016-03-01 23:18:56 +01:00
|
|
|
'choices': list,
|
2018-01-16 01:29:20 +01:00
|
|
|
'aliases': Any(list_string_types),
|
2017-04-10 16:43:39 +02:00
|
|
|
'version_added': Any(float, *string_types),
|
|
|
|
'default': Any(None, float, int, bool, list, dict, *string_types),
|
2017-03-13 20:49:27 +01:00
|
|
|
# Note: Types are strings, not literal bools, such as True or False
|
2018-12-18 22:25:30 +01:00
|
|
|
'type': Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'str'),
|
2018-03-10 00:09:58 +01:00
|
|
|
# Recursive suboptions
|
|
|
|
'suboptions': Any(None, *list({str_type: Self} for str_type in string_types)),
|
2016-03-01 21:20:57 +01:00
|
|
|
},
|
2017-03-13 20:49:27 +01:00
|
|
|
extra=PREVENT_EXTRA
|
2016-03-01 21:20:57 +01:00
|
|
|
)
|
|
|
|
|
2017-04-10 16:43:39 +02:00
|
|
|
# This generates list of dicts with keys from string_types and suboption_schema value
|
|
|
|
# for example in Python 3: {str: suboption_schema}
|
|
|
|
list_dict_suboption_schema = [{str_type: suboption_schema} for str_type in string_types]
|
|
|
|
|
2017-03-13 20:49:27 +01:00
|
|
|
option_schema = Schema(
|
2016-03-01 21:20:57 +01:00
|
|
|
{
|
2017-04-10 16:43:39 +02:00
|
|
|
Required('description'): Any(list_string_types, *string_types),
|
2017-03-13 20:49:27 +01:00
|
|
|
'required': bool,
|
|
|
|
'choices': list,
|
2018-01-16 01:29:20 +01:00
|
|
|
'aliases': Any(list_string_types),
|
2017-04-10 16:43:39 +02:00
|
|
|
'version_added': Any(float, *string_types),
|
|
|
|
'default': Any(None, float, int, bool, list, dict, *string_types),
|
|
|
|
'suboptions': Any(None, *list_dict_suboption_schema),
|
2017-03-13 20:49:27 +01:00
|
|
|
# Note: Types are strings, not literal bools, such as True or False
|
2018-12-18 22:25:30 +01:00
|
|
|
'type': Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json', 'jsonarg', 'list', 'path', 'raw', 'str'),
|
2016-03-01 21:20:57 +01:00
|
|
|
},
|
2017-03-13 20:49:27 +01:00
|
|
|
extra=PREVENT_EXTRA
|
2016-03-01 21:20:57 +01:00
|
|
|
)
|
2017-02-02 20:45:22 +01:00
|
|
|
|
2017-04-10 16:43:39 +02:00
|
|
|
# This generates list of dicts with keys from string_types and option_schema value
|
|
|
|
# for example in Python 3: {str: option_schema}
|
|
|
|
list_dict_option_schema = [{str_type: option_schema} for str_type in string_types]
|
|
|
|
|
2017-05-02 10:01:53 +02:00
|
|
|
|
2018-09-24 23:38:04 +02:00
|
|
|
def return_contains(v):
|
|
|
|
schema = Schema(
|
|
|
|
{
|
|
|
|
Required('contains'): Any(dict, list, *string_types)
|
|
|
|
},
|
|
|
|
extra=ALLOW_EXTRA
|
2017-05-02 10:01:53 +02:00
|
|
|
)
|
2018-09-24 23:38:04 +02:00
|
|
|
if v.get('type') == 'complex':
|
|
|
|
return schema(v)
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
|
|
|
return_schema = Any(
|
|
|
|
All(
|
|
|
|
Schema(
|
|
|
|
{
|
|
|
|
any_string_types: {
|
|
|
|
Required('description'): Any(list_string_types, *string_types),
|
|
|
|
Required('returned'): Any(*string_types),
|
2018-12-18 22:25:30 +01:00
|
|
|
Required('type'): Any('bool', 'complex', 'dict', 'float', 'int', 'list', 'str'),
|
2018-09-24 23:38:04 +02:00
|
|
|
'version_added': Any(float, *string_types),
|
|
|
|
'sample': Any(None, list, dict, int, float, *string_types),
|
|
|
|
'example': Any(None, list, dict, int, float, *string_types),
|
|
|
|
'contains': object,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
Schema({any_string_types: return_contains})
|
|
|
|
),
|
|
|
|
Schema(type(None)),
|
|
|
|
)
|
2017-05-02 10:01:53 +02:00
|
|
|
|
2017-05-03 17:25:08 +02:00
|
|
|
|
2018-09-24 23:38:04 +02:00
|
|
|
deprecation_schema = Schema(
|
|
|
|
{
|
2018-01-30 13:23:52 +01:00
|
|
|
# Only list branches that are deprecated or may have docs stubs in
|
|
|
|
# Deprecation cycle changed at 2.4 (though not retroactively)
|
|
|
|
# 2.3 -> removed_in: "2.5" + n for docs stub
|
|
|
|
# 2.4 -> removed_in: "2.8" + n for docs stub
|
2018-09-17 16:54:00 +02:00
|
|
|
Required('removed_in'): Any("2.2", "2.3", "2.4", "2.5", "2.6", "2.8", "2.9", "2.10", "2.11", "2.12"),
|
2018-01-30 13:23:52 +01:00
|
|
|
Required('why'): Any(*string_types),
|
|
|
|
Required('alternative'): Any(*string_types),
|
|
|
|
'removed': Any(True),
|
2018-09-24 23:38:04 +02:00
|
|
|
},
|
|
|
|
extra=PREVENT_EXTRA
|
|
|
|
)
|
2018-01-30 13:23:52 +01:00
|
|
|
|
|
|
|
|
2018-11-21 18:29:29 +01:00
|
|
|
def author(value):
|
|
|
|
|
|
|
|
if not is_iterable(value):
|
|
|
|
value = [value]
|
|
|
|
|
|
|
|
for line in value:
|
|
|
|
m = author_line.search(line)
|
|
|
|
if not m:
|
|
|
|
raise Invalid("Invalid author")
|
|
|
|
|
|
|
|
|
2017-03-13 20:49:27 +01:00
|
|
|
def doc_schema(module_name):
|
2018-01-30 13:23:52 +01:00
|
|
|
deprecated_module = False
|
|
|
|
|
2017-03-13 20:49:27 +01:00
|
|
|
if module_name.startswith('_'):
|
|
|
|
module_name = module_name[1:]
|
2018-01-30 13:23:52 +01:00
|
|
|
deprecated_module = True
|
|
|
|
doc_schema_dict = {
|
|
|
|
Required('module'): module_name,
|
|
|
|
Required('short_description'): Any(*string_types),
|
|
|
|
Required('description'): Any(list_string_types, *string_types),
|
|
|
|
Required('version_added'): Any(float, *string_types),
|
2018-11-21 18:29:29 +01:00
|
|
|
Required('author'): All(Any(None, list_string_types, *string_types), author),
|
2018-01-30 13:23:52 +01:00
|
|
|
'notes': Any(None, list_string_types),
|
2018-12-12 21:19:58 +01:00
|
|
|
'seealso': Any(None, seealso_schema),
|
2018-01-30 13:23:52 +01:00
|
|
|
'requirements': list_string_types,
|
|
|
|
'todo': Any(None, list_string_types, *string_types),
|
|
|
|
'options': Any(None, *list_dict_option_schema),
|
|
|
|
'extends_documentation_fragment': Any(list_string_types, *string_types)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deprecated_module:
|
|
|
|
deprecation_required_scheme = {
|
2018-09-24 23:38:04 +02:00
|
|
|
Required('deprecated'): Any(deprecation_schema),
|
2018-01-30 13:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doc_schema_dict.update(deprecation_required_scheme)
|
2017-03-13 20:49:27 +01:00
|
|
|
return Schema(
|
2018-01-30 13:23:52 +01:00
|
|
|
doc_schema_dict,
|
2017-03-13 20:49:27 +01:00
|
|
|
extra=PREVENT_EXTRA
|
|
|
|
)
|
|
|
|
|
2017-05-02 10:01:53 +02:00
|
|
|
|
2017-08-05 20:28:21 +02:00
|
|
|
def metadata_1_0_schema(deprecated):
|
2017-03-13 20:49:27 +01:00
|
|
|
valid_status = Any('stableinterface', 'preview', 'deprecated', 'removed')
|
|
|
|
if deprecated:
|
|
|
|
valid_status = Any('deprecated')
|
|
|
|
|
|
|
|
return Schema(
|
|
|
|
{
|
|
|
|
Required('status'): [valid_status],
|
2017-03-14 17:07:22 +01:00
|
|
|
Required('metadata_version'): '1.0',
|
|
|
|
Required('supported_by'): Any('core', 'community', 'curated')
|
2017-03-13 20:49:27 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-24 10:17:46 +02:00
|
|
|
def metadata_1_1_schema():
|
2017-08-05 20:28:21 +02:00
|
|
|
valid_status = Any('stableinterface', 'preview', 'deprecated', 'removed')
|
|
|
|
|
|
|
|
return Schema(
|
|
|
|
{
|
|
|
|
Required('status'): [valid_status],
|
|
|
|
Required('metadata_version'): '1.1',
|
|
|
|
Required('supported_by'): Any('core', 'community', 'certified', 'network')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-03-13 20:49:27 +01:00
|
|
|
# Things to add soon
|
|
|
|
####################
|
2017-05-02 10:01:53 +02:00
|
|
|
# 1) Recursively validate `type: complex` fields
|
2017-03-13 20:49:27 +01:00
|
|
|
# This will improve documentation, though require fair amount of module tidyup
|
|
|
|
|
|
|
|
# Possible Future Enhancements
|
|
|
|
##############################
|
|
|
|
|
|
|
|
# 1) Don't allow empty options for choices, aliases, etc
|
|
|
|
# 2) If type: bool ensure choices isn't set - perhaps use Exclusive
|
|
|
|
# 3) both version_added should be quoted floats
|
|
|
|
|
|
|
|
# Tool that takes JSON and generates RETURN skeleton (needs to support complex structures)
|