diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 38355e433d..5b4600b3b3 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -475,7 +475,7 @@ class TowerModule(AnsibleModule): # If the two items don't match and we are not comparing '' to None if existing_field != new_field and not (existing_field in (None, '') and new_field == ''): # something dosent match so lets do it - neds_update = True + needs_update = True break if needs_update: diff --git a/awx_collection/plugins/modules/tower_credential_type.py b/awx_collection/plugins/modules/tower_credential_type.py index 30eff59d2d..088e61f29b 100644 --- a/awx_collection/plugins/modules/tower_credential_type.py +++ b/awx_collection/plugins/modules/tower_credential_type.py @@ -124,8 +124,6 @@ def main(): kind = module.params.get('kind') state = module.params.get('state') - json_output = {'credential_type': name, 'state': state} - # Deal with check mode module.default_check_mode() @@ -149,23 +147,17 @@ def main(): } }) - json_output['existing_credential_type'] = credential_type - if state == 'absent' and not credential_type: - # If the state was absent and we had no credential_type, we can just return - module.exit_json(**module.json_output) - elif state == 'absent' and credential_type: - # If the state was absent and we had a team, we can try to delete it, the module will handle exiting from this - module.delete_endpoint('credential_types/{0}'.format(credential_type['id']), item_type='credential type', item_name=name, **{}) - elif state == 'present' and not credential_type: - # if the state was present and we couldn't find a credential_type we can build one, the module will handle exiting on its own - module.post_endpoint('credential_types', item_type='credential type', item_name=name, **{ - 'data': credential_type_params - }) - else: - # If the state was present and we had a credential_type we can see if we need to update it - # This will handle existing on its own - module.update_if_needed(credential_type, credential_type_params) + # Add entries to json_output to match old module + module.json_output['credential_type'] = name + module.json_output['state'] = state + module.json_output['existing_credential_type'] = credential_type + if state == 'absent': + # If the state was absent we can let the module delete it if needed, the module will handle exiting from this + module.delete_if_needed(credential_type) + elif state == 'present': + # If the state was present we can let the module build or update the existing team, this will return on its own + module.create_or_update_if_needed(credential_type, credential_type_params, endpoint='credential_types', item_type='credential type') if __name__ == '__main__': main() diff --git a/awx_collection/plugins/modules/tower_inventory.py b/awx_collection/plugins/modules/tower_inventory.py index 56589ac69f..1d693cf8b9 100644 --- a/awx_collection/plugins/modules/tower_inventory.py +++ b/awx_collection/plugins/modules/tower_inventory.py @@ -127,23 +127,16 @@ def main(): 'host_filter': host_filter, } - if state == 'absent' and not inventory: - # If the state was absent and we had no inventory, we can just return - module.exit_json(**module.json_output) - elif state == 'absent' and inventory: - # If the state was absent and we had a inventory, we can try to delete it, the module will handle exiting from this - module.delete_endpoint('inventories/{0}'.format(inventory['id']), item_type='inventory', item_name=name, **{}) - elif state == 'present' and not inventory: - # If the state was present and we couldn't find a inventory we can build one, the module will handle exiting from this - module.post_endpoint('inventories', item_type='inventory', item_name=name, **{'data': inventory_fields}) - else: - # Throw a more specific error message than what the API page provides. - if inventory['kind'] == '' and inventory_fields['kind'] == 'smart': + if state == 'absent': + # If the state was absent we can let the module delete it if needed, the module will handle exiting from this + module.delete_if_needed(inventory) + elif state == 'present': + # We need to perform a check to make sure you are not trying to convert a regular inventory into a smart one. + if inventory and inventory['kind'] == '' and inventory_fields['kind'] == 'smart': module.fail_json(msg='You cannot turn a regular inventory into a "smart" inventory.') - # If the state was present and we had a inventory, we can see if we need to update it - # This will return on its own - module.update_if_needed(inventory, inventory_fields) + # If the state was present we can let the module build or update the existing team, this will return on its own + module.create_or_update_if_needed(inventory, inventory_fields, endpoint='inventories', item_type='inventory') if __name__ == '__main__': main() diff --git a/awx_collection/plugins/modules/tower_project.py b/awx_collection/plugins/modules/tower_project.py index 00c614aee6..52deb55a18 100644 --- a/awx_collection/plugins/modules/tower_project.py +++ b/awx_collection/plugins/modules/tower_project.py @@ -259,23 +259,16 @@ def main(): # If we are doing a not manual project, register our on_change method # An on_change function, if registered, will fire after an post_endpoint or update_if_needed completes successfully + on_change = None if wait and scm_type != '': - module.on_change = wait_for_project_update - - if state == 'absent' and not project: - # If the state was absent and we had no project, we can just return - module.exit_json(**module.json_output) - elif state == 'absent' and project: - # If the state was absent and we had a project, we can try to delete it, the module will handle exiting from this - module.delete_endpoint('projects/{0}'.format(project['id']), item_type='project', item_name=name, **{}) - elif state == 'present' and not project: - # if the state was present and we couldn't find a project we can build one, the module wikl handle exiting from this - module.post_endpoint('projects', handle_return=False, item_type='project', item_name=name, **{'data': project_fields}) - else: - # If the state was present and we had a project we can see if we need to update it - # This will return on its own - module.update_if_needed(project, project_fields) + on_change = wait_for_project_update + if state == 'absent': + # If the state was absent we can let the module delete it if needed, the module will handle exiting from this + module.delete_if_needed(project) + elif state == 'present': + # If the state was present we can let the module build or update the existing team, this will return on its own + module.create_or_update_if_needed(project, project_fields, endpoint='projects', item_type='project', on_create=on_change, on_update=on_change) if __name__ == '__main__': main() diff --git a/awx_collection/plugins/modules/tower_team.py b/awx_collection/plugins/modules/tower_team.py index a8f504d337..3e8d2c2e31 100644 --- a/awx_collection/plugins/modules/tower_team.py +++ b/awx_collection/plugins/modules/tower_team.py @@ -109,7 +109,7 @@ def main(): } if state == 'absent': - # If the state was absent we can let the module to delete it if needed, the module will handle exiting from this + # If the state was absent we can let the module delete it if needed, the module will handle exiting from this module.delete_if_needed(team) elif state == 'present': # If the state was present we can let the module build or update the existing team, this will return on its own diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index c9a628d26a..3837de6586 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -38,8 +38,8 @@ options: type: str email: description: - - Email address of the user. - required: True + - Email address of the user. Required if creating a new user. + required: False type: str password: description: @@ -117,14 +117,14 @@ def main(): first_name=dict(), last_name=dict(), password=dict(no_log=True), - email=dict(required=True), + email=dict(required=False), superuser=dict(type='bool', default=False), auditor=dict(type='bool', default=False), state=dict(choices=['present', 'absent'], default='present'), ) # Create a module for ourselves - module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) + module = TowerModule(argument_spec=argument_spec, supports_check_mode=True, required_if=[['state', 'present', ['email']]]) # Extract our parameters state = module.params.get('state') @@ -145,22 +145,12 @@ def main(): } }) - if state == 'absent' and not user: - # If the state was absent and we had no user, we can just return - module.exit_json(**module.json_output) - elif state == 'absent' and user: - # If the state was absent and we had a user, we can try to delete it, the module will handle exiting from this - module.delete_endpoint('users/{0}'.format(user['id']), item_type='user', item_name=user_fields['username'], **{}) - elif state == 'present' and not user: - # if the state was present and we couldn't find a user we can build one, the module will handle exiting from this - module.post_endpoint('users', item_type='user', item_name=user_fields['username'], **{ - 'data': user_fields - }) - else: - # If the state was present and we had a user we can see if we need to update it - # This will return on its own - module.update_if_needed(user, user_fields) - + if state == 'absent': + # If the state was absent we can let the module delete it if needed, the module will handle exiting from this + module.delete_if_needed(user) + elif state == 'present': + # If the state was present we can let the module build or update the existing team, this will return on its own + module.create_or_update_if_needed(user, user_fields, endpoint='users', item_type='user') if __name__ == '__main__': main()