From 8af315cf29f6c227b4b51dad1325546786f90439 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Tue, 11 Aug 2020 08:54:10 -0500 Subject: [PATCH 01/16] Create tower_project_update Add module to update tower project and wait until synced. --- .../plugins/modules/tower_project_update.py | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 awx_collection/plugins/modules/tower_project_update.py diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py new file mode 100644 index 0000000000..1952244c93 --- /dev/null +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -0,0 +1,146 @@ +#!/usr/bin/python +# coding: utf-8 -*- + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.0', + 'status': ['preview'], + 'supported_by': 'community'} + +DOCUMENTATION = ''' +--- +module: tower_project_update +author: "Sean Sullivan (@sean-m-sullivan)" +short_description: Sync a Project in Ansible Tower +description: + - Sync an Ansible Tower Project. See + U(https://www.ansible.com/tower) for an overview. +options: + name: + description: + - The name of the workflow template to run. + required: True + type: str + aliases: + - project + organization: + description: + - Organization the workflow job template exists in. + - Used to help lookup the object, cannot be modified using this module. + - If not provided, will lookup by name only, which does not work with duplicates. + type: str + wait: + description: + - Wait for the workflow to complete. + default: True + type: bool + interval: + description: + - The interval to request an update from Tower. + required: False + default: 1 + type: float + timeout: + description: + - If waiting for the workflow to complete this will abort after this + amount of seconds + type: int +extends_documentation_fragment: awx.awx.auth +''' + +RETURN = ''' +job_info: + description: dictionary containing information about the project updated + returned: If project synced + type: dict +''' + + +EXAMPLES = ''' +- name: Launch a workflow with a timeout of 10 seconds + tower_project_update: + project: "Networking Project" + timeout: 10 + +- name: Launch a Workflow with extra_vars without waiting + tower_project_update: + project: "Networking Project" + wait: False +''' + +from ..module_utils.tower_api import TowerModule +import json +import time + + +def main(): + # Any additional arguments that are not fields of the item can be added here + argument_spec = dict( + name=dict(required=True, aliases=['project']), + organization=dict(), + wait=dict(required=False, default=True, type='bool'), + interval=dict(required=False, default=1.0, type='float'), + timeout=dict(required=False, default=None, type='int'), + ) + + # Create a module for ourselves + module = TowerModule(argument_spec=argument_spec) + + # Extract our parameters + name = module.params.get('name') + organization = module.params.get('organization') + wait = module.params.get('wait') + interval = module.params.get('interval') + timeout = module.params.get('timeout') + + # Attempt to look up project based on the provided name + lookup_data = {'name': name} + if organization: + lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) + project = module.get_one('projects', data=lookup_data) + + if project is None: + module.fail_json(msg="Unable to find workflow job template") + + # Launch the job + result = module.post_endpoint(project['related']['update']) + + if result['status_code'] != 202: + module.fail_json(msg="Failed to update project, see response for details", response=result) + + module.json_output['changed'] = True + module.json_output['id'] = result['json']['id'] + module.json_output['status'] = result['json']['status'] + + if not wait: + module.exit_json(**module.json_output) + + # Grab our start time to compare against for the timeout + start = time.time() + + job_url = result['json']['url'] + while not result['json']['finished']: + # If we are past our time out fail with a message + if timeout and timeout < time.time() - start: + module.json_output['msg'] = "Monitoring aborted due to timeout" + module.fail_json(**module.json_output) + + # Put the process to sleep for our interval + time.sleep(interval) + + result = module.get_endpoint(job_url) + module.json_output['status'] = result['json']['status'] + + # If the update has failed, we want to raise a task failure for that so we get a non-zero response. + if result['json']['failed']: + module.json_output['msg'] = 'The project "{0}" failed'.format(name) + module.fail_json(**module.json_output) + + module.exit_json(**module.json_output) + + +if __name__ == '__main__': + main() From af1fc5a9e9d71733d2b2162fc2ba920ed3481fbc Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 11 Aug 2020 09:28:17 -0500 Subject: [PATCH 02/16] add tests --- .../tower_project_update/tasks/main.yml | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml new file mode 100644 index 0000000000..be49c6734e --- /dev/null +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -0,0 +1,23 @@ +--- +- name: Update a project without waiting + tower_project_update: + name: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}" + organization: Default + wait: False + register: result + +- assert: + that: + - result is changed + +- name: Update a project and wait + tower_project_update: + name: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}" + organization: Default + wait: True + register: result + +- assert: + that: + - result is changed + From 813e38636a18b2055497de64e4414e1dce5e1c94 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 11 Aug 2020 09:38:18 -0500 Subject: [PATCH 03/16] fix documentation --- .../plugins/modules/tower_project_update.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index 1952244c93..b71d187ff2 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -21,20 +21,20 @@ description: options: name: description: - - The name of the workflow template to run. + - The name of the project to update. required: True type: str aliases: - project organization: description: - - Organization the workflow job template exists in. + - Organization the project exists in. - Used to help lookup the object, cannot be modified using this module. - If not provided, will lookup by name only, which does not work with duplicates. type: str wait: description: - - Wait for the workflow to complete. + - Wait for the project to update. default: True type: bool interval: @@ -45,14 +45,14 @@ options: type: float timeout: description: - - If waiting for the workflow to complete this will abort after this + - If waiting for the project to update this will abort after this amount of seconds type: int extends_documentation_fragment: awx.awx.auth ''' RETURN = ''' -job_info: +project_info: description: dictionary containing information about the project updated returned: If project synced type: dict @@ -60,12 +60,12 @@ job_info: EXAMPLES = ''' -- name: Launch a workflow with a timeout of 10 seconds +- name: Launch a project with a timeout of 10 seconds tower_project_update: project: "Networking Project" timeout: 10 -- name: Launch a Workflow with extra_vars without waiting +- name: Launch a Project with extra_vars without waiting tower_project_update: project: "Networking Project" wait: False @@ -103,9 +103,9 @@ def main(): project = module.get_one('projects', data=lookup_data) if project is None: - module.fail_json(msg="Unable to find workflow job template") + module.fail_json(msg="Unable to find project") - # Launch the job + # Update the project result = module.post_endpoint(project['related']['update']) if result['status_code'] != 202: @@ -121,7 +121,7 @@ def main(): # Grab our start time to compare against for the timeout start = time.time() - job_url = result['json']['url'] + project_url = result['json']['url'] while not result['json']['finished']: # If we are past our time out fail with a message if timeout and timeout < time.time() - start: @@ -131,7 +131,7 @@ def main(): # Put the process to sleep for our interval time.sleep(interval) - result = module.get_endpoint(job_url) + result = module.get_endpoint(project_url) module.json_output['status'] = result['json']['status'] # If the update has failed, we want to raise a task failure for that so we get a non-zero response. From 69dc0a892f3037f00fd1bd0f9438fdc6678aad10 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 11 Aug 2020 09:56:08 -0500 Subject: [PATCH 04/16] fix lint --- .../integration/targets/tower_project_update/tasks/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml index be49c6734e..0693f125d1 100644 --- a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -20,4 +20,3 @@ - assert: that: - result is changed - From d6815e51140657f778bc5804282a42f1494eae92 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Thu, 20 Aug 2020 10:30:43 -0500 Subject: [PATCH 05/16] Removed reference to sync Updated references to sync in favour of update. --- awx_collection/plugins/modules/tower_project_update.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index b71d187ff2..6d98c00c77 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -14,9 +14,9 @@ DOCUMENTATION = ''' --- module: tower_project_update author: "Sean Sullivan (@sean-m-sullivan)" -short_description: Sync a Project in Ansible Tower +short_description: Update a Project in Ansible Tower description: - - Sync an Ansible Tower Project. See + - Update a Ansible Tower Project. See U(https://www.ansible.com/tower) for an overview. options: name: From 3794f095cfefef72ed87ee483b937716e00f2443 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Sat, 22 Aug 2020 13:43:14 -0500 Subject: [PATCH 06/16] update to tower_project_update --- .../plugins/modules/tower_project_update.py | 20 +++++---- .../tower_project_update/tasks/main.yml | 41 +++++++++++++++++-- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index 6d98c00c77..e9f85321d5 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -52,10 +52,16 @@ extends_documentation_fragment: awx.awx.auth ''' RETURN = ''' -project_info: - description: dictionary containing information about the project updated - returned: If project synced - type: dict +id: + description: project id of the updated project + returned: success + type: int + sample: 86 +status: + description: status of the updated project + returned: success + type: str + sample: pending ''' @@ -81,9 +87,9 @@ def main(): argument_spec = dict( name=dict(required=True, aliases=['project']), organization=dict(), - wait=dict(required=False, default=True, type='bool'), - interval=dict(required=False, default=1.0, type='float'), - timeout=dict(required=False, default=None, type='int'), + wait=dict(default=True, type='bool'), + interval=dict(default=1.0, type='float'), + timeout=dict(default=None, type='int'), ) # Create a module for ourselves diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml index 0693f125d1..82ef03b9b0 100644 --- a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -1,9 +1,31 @@ --- +- name: Generate a random string for test + set_fact: + test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" + when: test_id is not defined + +- name: Generate names + set_fact: + project_name1: "AWX-Collection-tests-tower_project_update-project-{{ test_id }}" + +- name: Create a git project without credentials without waiting + tower_project: + name: "{{ project_name1 }}" + organization: Default + scm_type: git + scm_url: https://github.com/ansible/test-playbooks + wait: false + register: result + +- assert: + that: + - result is changed + - name: Update a project without waiting tower_project_update: - name: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}" + name: "{{ project_name1 }}" organization: Default - wait: False + wait: false register: result - assert: @@ -12,11 +34,22 @@ - name: Update a project and wait tower_project_update: - name: "AWX-Collection-tests-tower_job_template-proj-{{ test_id }}" + name: "{{ project_name1 }}" organization: Default - wait: True + wait: true register: result - assert: that: - result is changed + +- name: Delete the test project 1 + tower_project: + name: "{{ project_name1 }}" + organization: Default + state: absent + register: result + +- assert: + that: + - result is changed \ No newline at end of file From cda05c4f03528d770ccb81dc72d46a315531ca45 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Sat, 22 Aug 2020 20:16:27 -0500 Subject: [PATCH 07/16] Updated test for lint Updated test for lint --- .../integration/targets/tower_project_update/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml index 82ef03b9b0..e7a03ce4c9 100644 --- a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -52,4 +52,4 @@ - assert: that: - - result is changed \ No newline at end of file + - result is changed From 13f3292af08944c3b483fa5bdceb823f7b26b171 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 25 Aug 2020 07:04:27 -0500 Subject: [PATCH 08/16] updated test --- .../integration/targets/tower_project_update/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml index e7a03ce4c9..1b98e2e74f 100644 --- a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -41,7 +41,7 @@ - assert: that: - - result is changed + - result is successful - name: Delete the test project 1 tower_project: From bed3a9ee4134e51a50dde7f43c8fb3c6cdfeef00 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Wed, 26 Aug 2020 08:11:51 -0500 Subject: [PATCH 09/16] added id lookup --- .../plugins/modules/tower_project_update.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index e9f85321d5..4978052e5c 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -21,7 +21,7 @@ description: options: name: description: - - The name of the project to update. + - The name or id of the project to update. required: True type: str aliases: @@ -102,14 +102,19 @@ def main(): interval = module.params.get('interval') timeout = module.params.get('timeout') - # Attempt to look up project based on the provided name - lookup_data = {'name': name} - if organization: - lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - project = module.get_one('projects', data=lookup_data) - - if project is None: - module.fail_json(msg="Unable to find project") + # Attempt to look up project based on the provided name or id + if name.isnumeric(): + results = module.get_endpoint('projects', **{'data': {'id': name}}) + project = results['json']['results'][0] + if project is None: + module.fail_json(msg="Unable to find project") + else: + lookup_data = {'name': name} + if organization: + lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) + project = module.get_one('projects', data=lookup_data) + if project is None: + module.fail_json(msg="Unable to find project") # Update the project result = module.post_endpoint(project['related']['update']) From 5d4413041eae9f41c6e6d862dc9d66777c66b4ae Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 1 Sep 2020 14:59:16 -0500 Subject: [PATCH 10/16] update to wait --- .../plugins/modules/tower_project_update.py | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index 4978052e5c..b4a81b6aac 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -132,23 +132,16 @@ def main(): # Grab our start time to compare against for the timeout start = time.time() - project_url = result['json']['url'] - while not result['json']['finished']: - # If we are past our time out fail with a message - if timeout and timeout < time.time() - start: - module.json_output['msg'] = "Monitoring aborted due to timeout" - module.fail_json(**module.json_output) + if not wait: + module.exit_json(**module.json_output) - # Put the process to sleep for our interval - time.sleep(interval) - - result = module.get_endpoint(project_url) - module.json_output['status'] = result['json']['status'] - - # If the update has failed, we want to raise a task failure for that so we get a non-zero response. - if result['json']['failed']: - module.json_output['msg'] = 'The project "{0}" failed'.format(name) - module.fail_json(**module.json_output) + # Invoke wait function + module.wait_on_url( + url=result['json']['url'], + object_name=name, + object_type='Project Update', + timeout=timeout, interval=interval + ) module.exit_json(**module.json_output) From d95d8121f9ccd190aba129f88b71a2908ac53e78 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 1 Sep 2020 15:22:14 -0500 Subject: [PATCH 11/16] added to no endpoints for module --- awx_collection/test/awx/test_completeness.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/test/awx/test_completeness.py b/awx_collection/test/awx/test_completeness.py index 92a743daf4..d55c5a41db 100644 --- a/awx_collection/test/awx/test_completeness.py +++ b/awx_collection/test/awx/test_completeness.py @@ -24,7 +24,7 @@ no_module_for_endpoint = [] # Some modules work on the related fields of an endpoint. These modules will not have an auto-associated endpoint no_endpoint_for_module = [ 'tower_import', 'tower_meta', 'tower_export', 'tower_job_launch', 'tower_job_wait', 'tower_job_list', - 'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel', + 'tower_license', 'tower_ping', 'tower_project_update', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel', 'tower_workflow_template', ] From d269a6d23302b6a471a654b3fb54c572338facb6 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 1 Sep 2020 15:59:04 -0500 Subject: [PATCH 12/16] update --- awx_collection/plugins/modules/tower_project_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index b4a81b6aac..c08cfbb76e 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -111,7 +111,7 @@ def main(): else: lookup_data = {'name': name} if organization: - lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) + lookup_data['organization'] = module.get_one_by_name_or_id('organizations', organization) project = module.get_one('projects', data=lookup_data) if project is None: module.fail_json(msg="Unable to find project") From 300a4510ac9db4d6e312316b416284bf9bba4e50 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 1 Sep 2020 15:59:53 -0500 Subject: [PATCH 13/16] update completness --- awx_collection/test/awx/test_completeness.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awx_collection/test/awx/test_completeness.py b/awx_collection/test/awx/test_completeness.py index d55c5a41db..f1caa611f3 100644 --- a/awx_collection/test/awx/test_completeness.py +++ b/awx_collection/test/awx/test_completeness.py @@ -15,7 +15,7 @@ import re # Normally a read-only endpoint should not have a module (i.e. /api/v2/me) but sometimes we reuse a name # For example, we have a tower_role module but /api/v2/roles is a read only endpoint. # This list indicates which read-only endpoints have associated modules with them. -read_only_endpoints_with_modules = ['tower_settings', 'tower_role'] +read_only_endpoints_with_modules = ['tower_settings', 'tower_role', 'tower_project_update'] # If a module should not be created for an endpoint and the endpoint is not read-only add it here # THINK HARD ABOUT DOING THIS @@ -24,7 +24,7 @@ no_module_for_endpoint = [] # Some modules work on the related fields of an endpoint. These modules will not have an auto-associated endpoint no_endpoint_for_module = [ 'tower_import', 'tower_meta', 'tower_export', 'tower_job_launch', 'tower_job_wait', 'tower_job_list', - 'tower_license', 'tower_ping', 'tower_project_update', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel', + 'tower_license', 'tower_ping', 'tower_receive', 'tower_send', 'tower_workflow_launch', 'tower_job_cancel', 'tower_workflow_template', ] From 532447ed40b7e145922866cabecc8b074a1cc4e5 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Tue, 1 Sep 2020 16:43:58 -0500 Subject: [PATCH 14/16] update to tower module --- awx_collection/plugins/modules/tower_project_update.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index c08cfbb76e..fc150cb00d 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -77,7 +77,7 @@ EXAMPLES = ''' wait: False ''' -from ..module_utils.tower_api import TowerModule +from ..module_utils.tower_api import TowerAPIModule import json import time @@ -93,7 +93,7 @@ def main(): ) # Create a module for ourselves - module = TowerModule(argument_spec=argument_spec) + module = TowerAPIModule(argument_spec=argument_spec) # Extract our parameters name = module.params.get('name') @@ -111,7 +111,7 @@ def main(): else: lookup_data = {'name': name} if organization: - lookup_data['organization'] = module.get_one_by_name_or_id('organizations', organization) + lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) project = module.get_one('projects', data=lookup_data) if project is None: module.fail_json(msg="Unable to find project") From e2bf3a02874165c370980a13cd9d82d6bd19bd58 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Wed, 2 Sep 2020 07:19:11 -0500 Subject: [PATCH 15/16] update to use isdigit and updated tests --- .../plugins/modules/tower_project_update.py | 6 +++--- .../targets/tower_project_update/tasks/main.yml | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index fc150cb00d..3586a27340 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -103,11 +103,11 @@ def main(): timeout = module.params.get('timeout') # Attempt to look up project based on the provided name or id - if name.isnumeric(): + if name.isdigit(): results = module.get_endpoint('projects', **{'data': {'id': name}}) + if results['json']['count'] == 0: + module.fail_json(msg='Could not find Project with ID: {0}'.format(name)) project = results['json']['results'][0] - if project is None: - module.fail_json(msg="Unable to find project") else: lookup_data = {'name': name} if organization: diff --git a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml index 1b98e2e74f..08b9852018 100644 --- a/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_project_update/tasks/main.yml @@ -15,11 +15,11 @@ scm_type: git scm_url: https://github.com/ansible/test-playbooks wait: false - register: result + register: project_create_result - assert: that: - - result is changed + - project_create_result is changed - name: Update a project without waiting tower_project_update: @@ -43,6 +43,17 @@ that: - result is successful +- name: Update a project by ID + tower_project_update: + name: "{{ project_create_result.id }}" + organization: Default + wait: true + register: result + +- assert: + that: + - result is successful + - name: Delete the test project 1 tower_project: name: "{{ project_name1 }}" From 5478c5f2fb37c4473a4c8a91d359e8598124d272 Mon Sep 17 00:00:00 2001 From: sean-m-sullivan Date: Wed, 2 Sep 2020 08:30:09 -0500 Subject: [PATCH 16/16] linting --- awx_collection/plugins/modules/tower_project_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index 3586a27340..cc32446d65 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -106,7 +106,7 @@ def main(): if name.isdigit(): results = module.get_endpoint('projects', **{'data': {'id': name}}) if results['json']['count'] == 0: - module.fail_json(msg='Could not find Project with ID: {0}'.format(name)) + module.fail_json(msg='Could not find Project with ID: {0}'.format(name)) project = results['json']['results'][0] else: lookup_data = {'name': name}