From b532012748bf9d13bbe14bf4def8ad104a243ef5 Mon Sep 17 00:00:00 2001 From: beeankha Date: Tue, 25 Feb 2020 23:51:26 -0500 Subject: [PATCH] Make non-required params actually optional, fix idempotency issues --- awx_collection/plugins/modules/tower_group.py | 4 ++-- awx_collection/plugins/modules/tower_host.py | 5 +++-- awx_collection/plugins/modules/tower_inventory.py | 7 +++++-- awx_collection/plugins/modules/tower_inventory_source.py | 6 +++--- awx_collection/plugins/modules/tower_organization.py | 6 +++--- awx_collection/plugins/modules/tower_project.py | 3 ++- awx_collection/plugins/modules/tower_team.py | 3 ++- awx_collection/plugins/modules/tower_user.py | 6 ++++-- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/awx_collection/plugins/modules/tower_group.py b/awx_collection/plugins/modules/tower_group.py index a4d3bb18b0..bb0b22ae77 100644 --- a/awx_collection/plugins/modules/tower_group.py +++ b/awx_collection/plugins/modules/tower_group.py @@ -115,9 +115,9 @@ def main(): 'name': new_name if new_name else name, 'inventory': inventory_id, } - if description: + if description is not None: group_fields['description'] = description - if variables: + if variables is not None: group_fields['variables'] = json.dumps(variables) if state == 'absent': diff --git a/awx_collection/plugins/modules/tower_host.py b/awx_collection/plugins/modules/tower_host.py index 2a2096ab97..d2e59ca67d 100644 --- a/awx_collection/plugins/modules/tower_host.py +++ b/awx_collection/plugins/modules/tower_host.py @@ -123,11 +123,12 @@ def main(): # Create the data that gets sent for create and update host_fields = { 'name': new_name if new_name else name, - 'description': description, 'inventory': inventory_id, 'enabled': enabled, } - if variables: + if description is not None: + host_fields['description'] = description + if variables is not None: host_fields['variables'] = json.dumps(variables) if state == 'absent': diff --git a/awx_collection/plugins/modules/tower_inventory.py b/awx_collection/plugins/modules/tower_inventory.py index 13bee6e51c..32aa79615c 100644 --- a/awx_collection/plugins/modules/tower_inventory.py +++ b/awx_collection/plugins/modules/tower_inventory.py @@ -82,6 +82,7 @@ EXAMPLES = ''' from ..module_utils.tower_api import TowerModule +import json def main(): @@ -122,12 +123,14 @@ def main(): # Create the data that gets sent for create and update inventory_fields = { 'name': name, - 'description': description, 'organization': org_id, - 'variables': variables, 'kind': kind, 'host_filter': host_filter, } + if description is not None: + inventory_fields['description'] = description + if variables is not None: + inventory_fields['variables'] = json.dumps(variables) if state == 'absent': # If the state was absent we can let the module delete it if needed, the module will handle exiting from this diff --git a/awx_collection/plugins/modules/tower_inventory_source.py b/awx_collection/plugins/modules/tower_inventory_source.py index 5e49435f5e..48111b45b8 100644 --- a/awx_collection/plugins/modules/tower_inventory_source.py +++ b/awx_collection/plugins/modules/tower_inventory_source.py @@ -209,11 +209,11 @@ def main(): } # Attempt to look up the related items the user specified (these will fail the module if not found) - if credential: + if credential is not None: inventory_source_fields['credential'] = module.resolve_name_to_id('credentials', credential) - if source_project: + if source_project is not None: inventory_source_fields['source_project'] = module.resolve_name_to_id('projects', source_project) - if source_script: + if source_script is not None: inventory_source_fields['source_script'] = module.resolve_name_to_id('inventory_scripts', source_script) OPTIONAL_VARS = ( diff --git a/awx_collection/plugins/modules/tower_organization.py b/awx_collection/plugins/modules/tower_organization.py index 351bf0a7ff..ebb3d7d0cc 100644 --- a/awx_collection/plugins/modules/tower_organization.py +++ b/awx_collection/plugins/modules/tower_organization.py @@ -112,11 +112,11 @@ def main(): # Create the data that gets sent for create and update org_fields = {'name': name} - if description: + if description is not None: org_fields['description'] = description - if custom_virtualenv: + if custom_virtualenv is not None: org_fields['custom_virtualenv'] = custom_virtualenv - if max_hosts: + if max_hosts is not None: org_fields['max_hosts'] = max_hosts if state == 'absent': diff --git a/awx_collection/plugins/modules/tower_project.py b/awx_collection/plugins/modules/tower_project.py index c52ffd867d..44245fd352 100644 --- a/awx_collection/plugins/modules/tower_project.py +++ b/awx_collection/plugins/modules/tower_project.py @@ -238,7 +238,6 @@ def main(): # Create the data that gets sent for create and update project_fields = { 'name': name, - 'description': description, 'scm_type': scm_type, 'scm_url': scm_url, 'scm_branch': scm_branch, @@ -251,6 +250,8 @@ def main(): 'scm_update_cache_timeout': scm_update_cache_timeout, 'custom_virtualenv': custom_virtualenv, } + if description is not None: + project_fields['description'] = description if scm_credential is not None: project_fields['credential'] = scm_credential_id if scm_allow_override is not None: diff --git a/awx_collection/plugins/modules/tower_team.py b/awx_collection/plugins/modules/tower_team.py index 599b745c30..a8c6045a20 100644 --- a/awx_collection/plugins/modules/tower_team.py +++ b/awx_collection/plugins/modules/tower_team.py @@ -107,9 +107,10 @@ def main(): # Create the data that gets sent for create and update team_fields = { 'name': new_name if new_name else name, - 'description': description, 'organization': org_id } + if description is not None: + team_fields['description'] = description if state == 'absent': # If the state was absent we can let the module delete it if needed, the module will handle exiting from this diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index 8c7c05edd0..c4d9e68972 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -118,7 +118,7 @@ def main(): first_name=dict(), last_name=dict(), password=dict(no_log=True), - email=dict(required=False), + email=dict(required=False, default=''), superuser=dict(type='bool', default=False), auditor=dict(type='bool', default=False), state=dict(choices=['present', 'absent'], default='present'), @@ -129,6 +129,7 @@ def main(): # Extract our parameters state = module.params.get('state') + email = module.params.get('email') # Create the data that gets sent for create and update user_fields = { @@ -136,10 +137,11 @@ def main(): 'first_name': module.params.get('first_name'), 'last_name': module.params.get('last_name'), 'password': module.params.get('password'), - 'email': module.params.get('email'), 'superuser': module.params.get('superuser'), 'auditor': module.params.get('auditor'), } + if email is not None: + user_fields['email'] = email # Attempt to look up user based on the provided username user = module.get_one('users', **{