Use dynamic argument_spec from documentation

This change modifies the argument_spec making it so we load the options
key from the documentation constant. This will reduce the code we have
to maintain and ensure our documentation is always in sync with the
module capabilities.

Change-Id: Iaf94b6300a58de4367b4c9f2c83cc112e7c5361a
Co-Authored-by: Kevin Carter <kecarter@redhat.com>
Signed-off-by: Gael Chamoulaud <gchamoul@redhat.com>
This commit is contained in:
Gael Chamoulaud
2019-10-11 15:42:06 +02:00
parent b1d1af33d4
commit 08533b45c3
18 changed files with 90 additions and 92 deletions

View File

@@ -15,6 +15,7 @@
# under the License.
from os import path
from yaml import safe_load as yaml_safe_load
from ansible.module_utils.basic import AnsibleModule
@@ -58,9 +59,9 @@ def read_int(module, file_path):
def main():
module = AnsibleModule(argument_spec=dict(
drive=dict(required=True, type='str')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
drive = module.params.get('drive')
queue_path = path.join('/sys/class/block', drive, 'queue')

View File

@@ -15,6 +15,8 @@
# under the License.
from ansible.module_utils.basic import AnsibleModule # noqa
from yaml import safe_load as yaml_safe_load
import re
DOCUMENTATION = '''
@@ -156,10 +158,9 @@ def validate_roles_and_flavors(roles_info, flavors):
def main():
module = AnsibleModule(argument_spec=dict(
roles_info=dict(required=True, type='list'),
flavors=dict(required=True, type='dict')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
roles_info = module.params.get('roles_info')
flavors = module.params.get('flavors')

View File

@@ -18,6 +18,7 @@ import collections
from ansible.module_utils.basic import AnsibleModule # noqa
from oslo_utils import uuidutils
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -108,9 +109,9 @@ def validate_boot_config(nodes):
def main():
module = AnsibleModule(argument_spec=dict(
nodes=dict(required=True, type='list')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
nodes = module.params.get('nodes')

View File

@@ -20,6 +20,7 @@ import collections
import subprocess
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -131,10 +132,9 @@ def check_update(module, package, pkg_mgr):
def main():
module = AnsibleModule(argument_spec=dict(
package=dict(required=True, type='str'),
pkg_mgr=dict(required=True, type='str')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
check_update(module,
module.params.get('package'),

View File

@@ -17,6 +17,7 @@
import re
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -69,9 +70,9 @@ def parse_haproxy_conf(file_path):
def main():
module = AnsibleModule(argument_spec=dict(
path=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
haproxy_conf_path = module.params.get('path')

View File

@@ -17,6 +17,7 @@
import subprocess
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -42,9 +43,9 @@ EXAMPLES = '''
def main():
module = AnsibleModule(argument_spec=dict(
name=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
name = module.params.get('name')

View File

@@ -14,7 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -44,9 +46,7 @@ EXAMPLES = '''
def main():
module = AnsibleModule(
argument_spec=dict(
host=dict(required=True, type='str'),
)
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
host = module.params.pop('host')

View File

@@ -34,6 +34,7 @@ from enum import Enum
import os
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
# Possible return values
@@ -119,12 +120,9 @@ EXAMPLES = '''
def main():
module = AnsibleModule(argument_spec=dict(
path=dict(required=True, type='str'),
section=dict(required=True, type='str'),
key=dict(required=True, type='str'),
ignore_missing_file=dict(required=False, type='bool'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
ini_file_path = module.params.get('path')
ignore_missing = module.params.get('ignore_missing_file')

View File

@@ -17,6 +17,7 @@
import netaddr
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -102,11 +103,9 @@ def check_IP_range(start, end, min_size):
def main():
module = AnsibleModule(argument_spec=dict(
start=dict(required=True, type='str'),
end=dict(required=True, type='str'),
min_size=dict(required=True, type='int'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
start = module.params.get('start')
end = module.params.get('end')

View File

@@ -19,7 +19,6 @@ import collections
import itertools
import netaddr
import os.path
import yaml
import six
@@ -27,6 +26,7 @@ from ansible.module_utils.basic import AnsibleModule
# from os_net_config import validator
from tripleo_validations.utils import get_nested
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
@@ -75,7 +75,7 @@ def open_network_environment_files(netenv_path, template_files):
errors = []
try:
network_data = yaml.safe_load(template_files[netenv_path])
network_data = yaml_safe_load(template_files[netenv_path])
except Exception as e:
return ({}, {}, ["Can't open network environment file '{}': {}"
.format(netenv_path, e)])
@@ -88,7 +88,7 @@ def open_network_environment_files(netenv_path, template_files):
try:
nic_configs.append((
nic_name, nic_config_path,
yaml.safe_load(template_files[nic_config_path])))
yaml_safe_load(template_files[nic_config_path])))
except Exception as e:
errors.append(
"Can't open the resource '{}' reference file '{}': {}"
@@ -453,8 +453,8 @@ def duplicate_static_ips(static_ips):
def validate_node_pool_size(plan_env_path, ip_pools_path, template_files):
warnings = []
plan_env = yaml.safe_load(template_files[plan_env_path])
ip_pools = yaml.safe_load(template_files[ip_pools_path])
plan_env = yaml_safe_load(template_files[plan_env_path])
ip_pools = yaml_safe_load(template_files[ip_pools_path])
param_defaults = plan_env.get('parameter_defaults')
node_counts = {
@@ -498,12 +498,9 @@ def validate_node_pool_size(plan_env_path, ip_pools_path, template_files):
def main():
module = AnsibleModule(argument_spec=dict(
netenv_path=dict(required=True, type='str'),
plan_env_path=dict(required=True, type='str'),
ip_pools_path=dict(required=True, type='str'),
template_files=dict(required=True, type='list')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
netenv_path = module.params.get('netenv_path')
plan_env_path = module.params.get('plan_env_path')

View File

@@ -14,7 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from ansible.module_utils.basic import AnsibleModule # noqa
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -33,7 +35,7 @@ options:
required: true
description:
- A list of flavors
type: list
type: dict
introspection_data:
required: true
description:
@@ -130,11 +132,9 @@ def validate_node_disks(nodes, flavors, introspection_data):
def main():
module = AnsibleModule(argument_spec=dict(
nodes=dict(required=True, type='list'),
flavors=dict(required=True, type='dict'),
introspection_data=dict(required=True, type='list')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
nodes = {node['name']: node for node in module.params.get('nodes')}
flavors = module.params.get('flavors')

View File

@@ -14,11 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from ansible.module_utils.basic import AnsibleModule
import os.path
import subprocess
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
module: overcloudrc
@@ -38,14 +39,15 @@ EXAMPLES = '''
- hosts: webservers
tasks:
- name: Source overcloudrc
overcloudrc: path=/home/stack/overcloudrc
overcloudrc:
path: /home/stack/overcloudrc
'''
def main():
module = AnsibleModule(argument_spec=dict(
path=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
overcloudrc_path = os.path.expanduser(module.params.get('path'))

View File

@@ -15,6 +15,7 @@
# limitations under the License.
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -126,9 +127,10 @@ def validate_pmd_cpus(module, pmd_cpu_mask):
def main():
module = AnsibleModule(argument_spec=dict(
pmd_cpu_mask=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
validate_pmd_cpus(module,
module.params.get('pmd_cpu_mask'))

View File

@@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from xml.etree import ElementTree
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -62,9 +62,9 @@ def format_failure(failure):
def main():
module = AnsibleModule(argument_spec=dict(
status=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
pcs_status = parse_pcs_status(module.params.get('status'))
failures = pcs_status['failures']

View File

@@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -26,8 +26,11 @@ options:
required: true
description:
- The report status. Should be 'OK', 'ERROR' or 'SKIPPED'.
choices:
- 'OK'
- 'ERROR'
- 'SKIPPED'
type: str
choices: ['OK', 'ERROR', 'SKIPPED']
report_reason:
required: true
description:
@@ -63,17 +66,10 @@ def format_msg_report(status, reason, recommendations):
def main():
fields = {
"report_reason": {"required": True, "type": "str"},
"report_recommendations": {"required": True, "type": "list"},
"report_status": {
"default": "OK",
"choices": ['OK', 'ERROR', 'SKIPPED'],
"type": 'str'
},
}
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
module = AnsibleModule(argument_spec=fields)
status = module.params.get('report_status')
msg = format_msg_report(module.params.get('report_status'),
module.params.get('report_reason'),

View File

@@ -22,12 +22,12 @@ except AttributeError:
collectionsAbc = collections
import os.path
import yaml
import six
from ansible.module_utils.basic import AnsibleModule # noqa
from tripleo_validations import utils
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -73,7 +73,7 @@ def open_network_environment_files(netenv_path, template_files):
errors = []
try:
network_data = yaml.safe_load(template_files[netenv_path])
network_data = yaml_safe_load(template_files[netenv_path])
except Exception as e:
return ({}, {}, ["Can't open network environment file '{}': {}"
.format(netenv_path, e)])
@@ -86,7 +86,7 @@ def open_network_environment_files(netenv_path, template_files):
try:
nic_configs.append((
nic_name, nic_config_path,
yaml.safe_load(template_files[nic_config_path])))
yaml_safe_load(template_files[nic_config_path])))
except Exception as e:
errors.append(
"Can't open the resource '{}' reference file '{}': {}"
@@ -204,11 +204,9 @@ def vlan_exists_on_switch(vlan_id, introspection_data):
def main():
module = AnsibleModule(argument_spec=dict(
path=dict(required=True, type='str'),
template_files=dict(required=True, type='list'),
introspection_data=dict(required=True, type='list')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
netenv_path = module.params.get('path')
template_files = {name: content[1] for (name, content) in

View File

@@ -15,6 +15,7 @@
# under the License.
from ansible.module_utils.basic import AnsibleModule # noqa
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -143,10 +144,9 @@ def verify_profiles(nodes, flavors):
def main():
module = AnsibleModule(argument_spec=dict(
nodes=dict(required=True, type='list'),
flavors=dict(required=True, type='dict')
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
nodes = module.params.get('nodes')
flavors = module.params.get('flavors')

View File

@@ -15,6 +15,7 @@
# under the License.
from ansible.module_utils.basic import AnsibleModule
from yaml import safe_load as yaml_safe_load
DOCUMENTATION = '''
---
@@ -40,9 +41,9 @@ EXAMPLES = '''
def main():
module = AnsibleModule(argument_spec=dict(
msg=dict(required=True, type='str'),
))
module = AnsibleModule(
argument_spec=yaml_safe_load(DOCUMENTATION)['options']
)
msg = module.params.get('msg')