From 106157c6003a2dc4e64977b374d5dc401fea5e4e Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Fri, 4 Sep 2020 15:41:36 -0400 Subject: [PATCH] get_one now also returns the name field, and modifying modules for get_one and added in some IDs in a handful of unit tests --- .../plugins/module_utils/tower_api.py | 6 +++--- .../plugins/modules/tower_credential.py | 4 +--- .../modules/tower_credential_input_source.py | 2 +- .../plugins/modules/tower_credential_type.py | 6 +----- awx_collection/plugins/modules/tower_group.py | 7 +++---- awx_collection/plugins/modules/tower_host.py | 3 +-- .../plugins/modules/tower_instance_group.py | 6 +----- .../plugins/modules/tower_inventory.py | 3 +-- .../plugins/modules/tower_inventory_source.py | 8 ++++---- .../plugins/modules/tower_job_cancel.py | 2 +- .../plugins/modules/tower_job_launch.py | 6 +----- .../plugins/modules/tower_job_template.py | 7 +++---- .../plugins/modules/tower_job_wait.py | 2 +- awx_collection/plugins/modules/tower_label.py | 3 +-- .../modules/tower_notification_template.py | 3 +-- .../plugins/modules/tower_organization.py | 6 +----- .../plugins/modules/tower_project.py | 3 +-- .../plugins/modules/tower_project_update.py | 4 ++-- .../plugins/modules/tower_schedule.py | 6 +----- awx_collection/plugins/modules/tower_team.py | 3 +-- awx_collection/plugins/modules/tower_token.py | 2 +- awx_collection/plugins/modules/tower_user.py | 4 +--- .../modules/tower_workflow_job_template.py | 4 ++-- .../tower_workflow_job_template_node.py | 10 ++++++---- .../plugins/modules/tower_workflow_launch.py | 4 ++-- .../tasks/main.yml | 12 ++++++------ .../tower_credential_type/tasks/main.yml | 2 +- .../targets/tower_group/tasks/main.yml | 5 +++-- .../targets/tower_host/tasks/main.yml | 5 +++-- .../tower_instance_group/tasks/main.yml | 18 +++++++++++++++--- .../targets/tower_inventory/tasks/main.yml | 4 ++-- .../tower_inventory_source/tasks/main.yml | 5 +++-- .../targets/tower_job_template/tasks/main.yml | 14 +++++++------- .../roles/generate/templates/tower_module.j2 | 3 +-- 34 files changed, 83 insertions(+), 99 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index da8474634d..cf2860a87b 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -141,18 +141,18 @@ class TowerAPIModule(TowerModule): self.fail_json(msg="The endpoint did not provide count and results") if response['json']['count'] == 0: - return None + return None, name_or_id elif response['json']['count'] > 1: if name_or_id: # Since we did a name or ID search and got > 1 return something if the id matches for asset in response['json']['results']: if asset['id'] == name_or_id: - return asset + return asset, asset['id'][name_field] # We got > 1 and either didn't find something by ID (which means multiple names) # Or we weren't running with a or search and just got back too many to begin with. self.fail_json(msg="An unexpected number of items was returned from the API ({0})".format(response['json']['count'])) - return response['json']['results'][0] + return response['json']['results'][0], response['json']['results'][0][name_field] def get_one_by_name_or_id(self, endpoint, name_or_id): name_field = self.get_name_field_from_endpoint(endpoint) diff --git a/awx_collection/plugins/modules/tower_credential.py b/awx_collection/plugins/modules/tower_credential.py index 3f37e0ad62..146484ca66 100644 --- a/awx_collection/plugins/modules/tower_credential.py +++ b/awx_collection/plugins/modules/tower_credential.py @@ -370,9 +370,7 @@ def main(): if organization: lookup_data['organization'] = org_id - credential = module.get_one('credentials', name_or_id=name, **{'data': lookup_data}) - # If we got an item back make sure the name field reflects the actual name (incase we were passed an ID) - name = credential['name'] if (credential) else name + credential, name = module.get_one('credentials', name_or_id=name, **{'data': lookup_data}) 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_credential_input_source.py b/awx_collection/plugins/modules/tower_credential_input_source.py index cdc55cb1f0..0966ff3334 100644 --- a/awx_collection/plugins/modules/tower_credential_input_source.py +++ b/awx_collection/plugins/modules/tower_credential_input_source.py @@ -102,7 +102,7 @@ def main(): 'target_credential': target_credential_id, 'input_field_name': input_field_name, } - credential_input_source = module.get_one('credential_input_sources', **{'data': lookup_data}) + credential_input_source, junk = module.get_one('credential_input_sources', **{'data': lookup_data}) if state == 'absent': module.delete_if_needed(credential_input_source) diff --git a/awx_collection/plugins/modules/tower_credential_type.py b/awx_collection/plugins/modules/tower_credential_type.py index 53f0cc45c9..3ab21c4330 100644 --- a/awx_collection/plugins/modules/tower_credential_type.py +++ b/awx_collection/plugins/modules/tower_credential_type.py @@ -128,11 +128,7 @@ def main(): credential_type_params['injectors'] = module.params.get('injectors') # Attempt to look up credential_type based on the provided name - credential_type = module.get_one('credential_types', **{ - 'data': { - 'name': name, - } - }) + credential_type, name = module.get_one('credential_types', name_or_id=name) 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_group.py b/awx_collection/plugins/modules/tower_group.py index 9e2eaf4e7e..778c54b0c7 100644 --- a/awx_collection/plugins/modules/tower_group.py +++ b/awx_collection/plugins/modules/tower_group.py @@ -108,9 +108,8 @@ def main(): inventory_id = module.resolve_name_to_id('inventories', inventory) # Attempt to look up the object based on the provided name and inventory ID - group = module.get_one('groups', **{ + group, name = module.get_one('groups', name_or_id=name, **{ 'data': { - 'name': name, 'inventory': inventory_id } }) @@ -136,8 +135,8 @@ def main(): continue id_list = [] for sub_name in name_list: - sub_obj = module.get_one(resource, **{ - 'data': {'inventory': inventory_id, 'name': sub_name} + sub_obj, sub_name = module.get_one(resource, name_or_id=sub_name, **{ + 'data': {'inventory': inventory_id}, }) if sub_obj is None: module.fail_json(msg='Could not find {0} with name {1}'.format(resource, sub_name)) diff --git a/awx_collection/plugins/modules/tower_host.py b/awx_collection/plugins/modules/tower_host.py index f6bfe55404..d0cf12bcb9 100644 --- a/awx_collection/plugins/modules/tower_host.py +++ b/awx_collection/plugins/modules/tower_host.py @@ -104,9 +104,8 @@ def main(): inventory_id = module.resolve_name_to_id('inventories', inventory) # Attempt to look up host based on the provided name and inventory ID - host = module.get_one('hosts', **{ + host, name = module.get_one('hosts', name_or_id=name, **{ 'data': { - 'name': name, 'inventory': inventory_id } }) diff --git a/awx_collection/plugins/modules/tower_instance_group.py b/awx_collection/plugins/modules/tower_instance_group.py index 9a7db2f814..cc8c97033f 100644 --- a/awx_collection/plugins/modules/tower_instance_group.py +++ b/awx_collection/plugins/modules/tower_instance_group.py @@ -109,11 +109,7 @@ def main(): state = module.params.get('state') # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('instance_groups', **{ - 'data': { - 'name': name, - } - }) + existing_item, name = module.get_one('instance_groups', name_or_id=name) if state is '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.py b/awx_collection/plugins/modules/tower_inventory.py index 20093038c7..e8462a2844 100644 --- a/awx_collection/plugins/modules/tower_inventory.py +++ b/awx_collection/plugins/modules/tower_inventory.py @@ -109,9 +109,8 @@ def main(): org_id = module.resolve_name_to_id('organizations', organization) # Attempt to look up inventory based on the provided name and org ID - inventory = module.get_one('inventories', **{ + inventory, name = module.get_one('inventories', name_or_id=name, **{ 'data': { - 'name': name, 'organization': org_id } }) diff --git a/awx_collection/plugins/modules/tower_inventory_source.py b/awx_collection/plugins/modules/tower_inventory_source.py index 1714773b32..289707ccd9 100644 --- a/awx_collection/plugins/modules/tower_inventory_source.py +++ b/awx_collection/plugins/modules/tower_inventory_source.py @@ -202,17 +202,17 @@ def main(): source_project = module.params.get('source_project') state = module.params.get('state') - lookup_data = {'name': inventory} +<<<<<<< HEAD + lookup_data = {} if organization: lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - inventory_object = module.get_one('inventories', data=lookup_data) + inventory_object = module.get_one('inventories', name_or_id=inventory, data=lookup_data) if not inventory_object: module.fail_json(msg='The specified inventory, {0}, was not found.'.format(lookup_data)) - inventory_source_object = module.get_one('inventory_sources', **{ + inventory_source_object = module.get_one('inventory_sources', name_or_id=name, **{ 'data': { - 'name': name, 'inventory': inventory_object['id'], } }) diff --git a/awx_collection/plugins/modules/tower_job_cancel.py b/awx_collection/plugins/modules/tower_job_cancel.py index 7404d452a4..0bfe41d9c0 100644 --- a/awx_collection/plugins/modules/tower_job_cancel.py +++ b/awx_collection/plugins/modules/tower_job_cancel.py @@ -68,7 +68,7 @@ def main(): fail_if_not_running = module.params.get('fail_if_not_running') # Attempt to look up the job based on the provided name - job = module.get_one('jobs', **{ + job, job_name = module.get_one('jobs', **{ 'data': { 'id': job_id, } diff --git a/awx_collection/plugins/modules/tower_job_launch.py b/awx_collection/plugins/modules/tower_job_launch.py index 7e2dca0e38..39c5b9caf4 100644 --- a/awx_collection/plugins/modules/tower_job_launch.py +++ b/awx_collection/plugins/modules/tower_job_launch.py @@ -201,11 +201,7 @@ def main(): post_data['credentials'].append(module.resolve_name_to_id('credentials', credential)) # Attempt to look up job_template based on the provided name - job_template = module.get_one('job_templates', **{ - 'data': { - 'name': name, - } - }) + job_template, name = module.get_one('job_templates', name_or_id=name) if job_template is None: module.fail_json(msg="Unable to find job template by name {0}".format(name)) diff --git a/awx_collection/plugins/modules/tower_job_template.py b/awx_collection/plugins/modules/tower_job_template.py index 1f12d5aadd..7f59e3f945 100644 --- a/awx_collection/plugins/modules/tower_job_template.py +++ b/awx_collection/plugins/modules/tower_job_template.py @@ -409,7 +409,7 @@ def main(): credentials.append(credential) new_fields = {} - search_fields = {'name': name} + search_fields = {} # Attempt to look up the related items the user specified (these will fail the module if not found) organization_id = None @@ -419,7 +419,7 @@ def main(): search_fields['organization'] = new_fields['organization'] = organization_id # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('job_templates', **{'data': search_fields}) + existing_item, name = module.get_one('job_templates', name_or_id=name, **{'data': search_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 @@ -453,9 +453,8 @@ def main(): new_fields['inventory'] = module.resolve_name_to_id('inventories', inventory) if project is not None: if organization_id is not None: - project_data = module.get_one('projects', **{ + project_data, project = module.get_one('projects', name_or_id=project, **{ 'data': { - 'name': project, 'organization': organization_id, } }) diff --git a/awx_collection/plugins/modules/tower_job_wait.py b/awx_collection/plugins/modules/tower_job_wait.py index 6f71a1be5b..399926805f 100644 --- a/awx_collection/plugins/modules/tower_job_wait.py +++ b/awx_collection/plugins/modules/tower_job_wait.py @@ -130,7 +130,7 @@ def main(): ) # Attempt to look up job based on the provided id - job = module.get_one('jobs', **{ + job, junk = module.get_one('jobs', **{ 'data': { 'id': job_id, } diff --git a/awx_collection/plugins/modules/tower_label.py b/awx_collection/plugins/modules/tower_label.py index 6a3a8288a2..30b4843c41 100644 --- a/awx_collection/plugins/modules/tower_label.py +++ b/awx_collection/plugins/modules/tower_label.py @@ -80,9 +80,8 @@ def main(): organization_id = module.resolve_name_to_id('organizations', organization) # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('labels', **{ + existing_item, name = module.get_one('labels', name_or_id=name, **{ 'data': { - 'name': name, 'organization': organization_id, } }) diff --git a/awx_collection/plugins/modules/tower_notification_template.py b/awx_collection/plugins/modules/tower_notification_template.py index ec25038e34..4ad387fc5d 100644 --- a/awx_collection/plugins/modules/tower_notification_template.py +++ b/awx_collection/plugins/modules/tower_notification_template.py @@ -380,9 +380,8 @@ def main(): organization_id = module.resolve_name_to_id('organizations', organization) # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('notification_templates', **{ + existing_item, name = module.get_one('notification_templates', name_or_id=name, **{ 'data': { - 'name': name, 'organization': organization_id, } }) diff --git a/awx_collection/plugins/modules/tower_organization.py b/awx_collection/plugins/modules/tower_organization.py index 1637828149..33da6b0f90 100644 --- a/awx_collection/plugins/modules/tower_organization.py +++ b/awx_collection/plugins/modules/tower_organization.py @@ -117,11 +117,7 @@ def main(): state = module.params.get('state') # Attempt to look up organization based on the provided name - organization = module.get_one('organizations', **{ - 'data': { - 'name': name, - } - }) + organization, name = module.get_one('organizations', name_or_id=name) 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_project.py b/awx_collection/plugins/modules/tower_project.py index 8662029f66..bd33ad9ef2 100644 --- a/awx_collection/plugins/modules/tower_project.py +++ b/awx_collection/plugins/modules/tower_project.py @@ -239,9 +239,8 @@ def main(): credential = module.resolve_name_to_id('credentials', credential) # Attempt to look up project based on the provided name and org ID - project = module.get_one('projects', **{ + project, name = module.get_one('projects', name_or_id=name, **{ 'data': { - 'name': name, 'organization': org_id } }) diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index cc32446d65..7622001411 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -109,10 +109,10 @@ def main(): module.fail_json(msg='Could not find Project with ID: {0}'.format(name)) project = results['json']['results'][0] else: - lookup_data = {'name': name} + lookup_data = {} if organization: lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - project = module.get_one('projects', data=lookup_data) + project, name = module.get_one('projects', name_or_id=name, data=lookup_data) if project is None: module.fail_json(msg="Unable to find project") diff --git a/awx_collection/plugins/modules/tower_schedule.py b/awx_collection/plugins/modules/tower_schedule.py index 4922aaa688..c69905bfac 100644 --- a/awx_collection/plugins/modules/tower_schedule.py +++ b/awx_collection/plugins/modules/tower_schedule.py @@ -190,11 +190,7 @@ def main(): unified_job_template_id = module.resolve_name_to_id('unified_job_templates', unified_job_template) # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('schedules', **{ - 'data': { - 'name': name, - } - }) + existing_item, name = module.get_one('schedules', name_or_id=name) # Create the data that gets sent for create and update new_fields = {} diff --git a/awx_collection/plugins/modules/tower_team.py b/awx_collection/plugins/modules/tower_team.py index 8ed56e48dc..7f7f5fa632 100644 --- a/awx_collection/plugins/modules/tower_team.py +++ b/awx_collection/plugins/modules/tower_team.py @@ -87,9 +87,8 @@ def main(): org_id = module.resolve_name_to_id('organizations', organization) # Attempt to look up team based on the provided name and org ID - team = module.get_one('teams', **{ + team, name = module.get_one('teams', name_or_id=name, **{ 'data': { - 'name': name, 'organization': org_id } }) diff --git a/awx_collection/plugins/modules/tower_token.py b/awx_collection/plugins/modules/tower_token.py index ee6fd5c200..2680caaf93 100644 --- a/awx_collection/plugins/modules/tower_token.py +++ b/awx_collection/plugins/modules/tower_token.py @@ -164,7 +164,7 @@ def main(): if state == 'absent': if not existing_token: - existing_token = module.get_one('tokens', **{ + existing_token, token_name = module.get_one('tokens', **{ 'data': { 'id': existing_token_id, } diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index 19cf00fa1f..56f231dae8 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -134,9 +134,7 @@ def main(): # Attempt to look up the related items the user specified (these will fail the module if not found) # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('users', name_or_id=username) - # If we got an item back make sure the name field reflects the actual name (incase we were passed an ID) - username = existing_item['username'] if (existing_item) else username + existing_item, username = module.get_one('users', name_or_id=username) 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_workflow_job_template.py b/awx_collection/plugins/modules/tower_workflow_job_template.py index 8fb350b919..21818cb4c6 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template.py @@ -184,7 +184,7 @@ def main(): state = module.params.get('state') new_fields = {} - search_fields = {'name': name} + search_fields = {} # Attempt to look up the related items the user specified (these will fail the module if not found) organization = module.params.get('organization') @@ -193,7 +193,7 @@ def main(): search_fields['organization'] = new_fields['organization'] = organization_id # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('workflow_job_templates', **{'data': search_fields}) + existing_item, name = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_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 diff --git a/awx_collection/plugins/modules/tower_workflow_job_template_node.py b/awx_collection/plugins/modules/tower_workflow_job_template_node.py index 7ef9e14619..2ba74c1fb9 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template_node.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template_node.py @@ -198,12 +198,14 @@ def main(): workflow_job_template = module.params.get('workflow_job_template') workflow_job_template_id = None if workflow_job_template: - wfjt_search_fields = {'name': workflow_job_template} + wfjt_search_fields = {} organization = module.params.get('organization') if organization: organization_id = module.resolve_name_to_id('organizations', organization) wfjt_search_fields['organization'] = organization_id - wfjt_data = module.get_one('workflow_job_templates', **{'data': wfjt_search_fields}) + wfjt_data, workflow_job_template = module.get_one('workflow_job_templates', name_or_id=workflow_job_template, **{ + 'data': wfjt_search_fields + }) if wfjt_data is None: module.fail_json(msg="The workflow {0} in organization {1} was not found on the Tower server".format( workflow_job_template, organization @@ -212,7 +214,7 @@ def main(): search_fields['workflow_job_template'] = new_fields['workflow_job_template'] = workflow_job_template_id # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('workflow_job_template_nodes', **{'data': search_fields}) + existing_item, junk = module.get_one('workflow_job_template_nodes', **{'data': search_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 @@ -249,7 +251,7 @@ def main(): lookup_data = {'identifier': sub_name} if workflow_job_template_id: lookup_data['workflow_job_template'] = workflow_job_template_id - sub_obj = module.get_one(endpoint, **{'data': lookup_data}) + sub_obj, junk = module.get_one(endpoint, **{'data': lookup_data}) if sub_obj is None: module.fail_json(msg='Could not find {0} entry with name {1}'.format(association, sub_name)) id_list.append(sub_obj['id']) diff --git a/awx_collection/plugins/modules/tower_workflow_launch.py b/awx_collection/plugins/modules/tower_workflow_launch.py index f8ab793bad..0e9cbb9ad6 100644 --- a/awx_collection/plugins/modules/tower_workflow_launch.py +++ b/awx_collection/plugins/modules/tower_workflow_launch.py @@ -138,10 +138,10 @@ def main(): post_data['inventory'] = module.resolve_name_to_id('inventories', inventory) # Attempt to look up job_template based on the provided name - lookup_data = {'name': name} + lookup_data = {} if organization: lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - workflow_job_template = module.get_one('workflow_job_templates', data=lookup_data) + workflow_job_template, name = module.get_one('workflow_job_templates', name_or_id=name, data=lookup_data) if workflow_job_template is None: module.fail_json(msg="Unable to find workflow job template") diff --git a/awx_collection/tests/integration/targets/tower_credential_input_source/tasks/main.yml b/awx_collection/tests/integration/targets/tower_credential_input_source/tasks/main.yml index 45be47ddf7..ecc6d39af4 100644 --- a/awx_collection/tests/integration/targets/tower_credential_input_source/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_credential_input_source/tasks/main.yml @@ -13,11 +13,11 @@ url: "https://cyberark.example.com" app_id: "My-App-ID" organization: Default - register: result + register: src_cred_result - assert: that: - - "result is changed" + - "src_cred_result is changed" - name: Add Tower credential Target tower_credential: @@ -27,17 +27,17 @@ inputs: username: user organization: Default - register: result + register: target_cred_result - assert: that: - - "result is changed" + - "target_cred_result is changed" - name: Add credential Input Source tower_credential_input_source: input_field_name: password - target_credential: "{{ target_cred_name }}" - source_credential: "{{ src_cred_name }}" + target_credential: "{{ target_cred_result.id }}" + source_credential: "{{ src_cred_result.id }}" metadata: object_query: "Safe=MY_SAFE;Object=AWX-user" object_query_format: "Exact" diff --git a/awx_collection/tests/integration/targets/tower_credential_type/tasks/main.yml b/awx_collection/tests/integration/targets/tower_credential_type/tasks/main.yml index a15cd26795..a38f2eeaae 100644 --- a/awx_collection/tests/integration/targets/tower_credential_type/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_credential_type/tasks/main.yml @@ -18,7 +18,7 @@ - name: Remove a Tower credential type tower_credential_type: - name: "{{ cred_type_name }}" + name: "{{ result.id }}" state: absent register: result diff --git a/awx_collection/tests/integration/targets/tower_group/tasks/main.yml b/awx_collection/tests/integration/targets/tower_group/tasks/main.yml index 38e76719cf..e59d3c82db 100644 --- a/awx_collection/tests/integration/targets/tower_group/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_group/tasks/main.yml @@ -14,11 +14,12 @@ name: "{{ inv_name }}" organization: Default state: present + register: result - name: Create a Group tower_group: name: "{{ group_name1 }}" - inventory: "{{ inv_name }}" + inventory: "{{ result.id }}" state: present variables: foo: bar @@ -30,7 +31,7 @@ - name: Delete a Group tower_group: - name: "{{ group_name1 }}" + name: "{{ result.id }}" inventory: "{{ inv_name }}" state: absent register: result diff --git a/awx_collection/tests/integration/targets/tower_host/tasks/main.yml b/awx_collection/tests/integration/targets/tower_host/tasks/main.yml index 597d6ef734..34e02220a7 100644 --- a/awx_collection/tests/integration/targets/tower_host/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_host/tasks/main.yml @@ -9,11 +9,12 @@ name: "{{ inv_name }}" organization: Default state: present + register: result - name: Create a Host tower_host: name: "{{ host_name }}" - inventory: "{{ inv_name }}" + inventory: "{{ result.id }}" state: present variables: foo: bar @@ -25,7 +26,7 @@ - name: Delete a Host tower_host: - name: "{{ host_name }}" + name: "{{ result.id }}" inventory: "{{ inv_name }}" state: absent register: result diff --git a/awx_collection/tests/integration/targets/tower_instance_group/tasks/main.yml b/awx_collection/tests/integration/targets/tower_instance_group/tasks/main.yml index 4ae9cfaffc..0ca696edab 100644 --- a/awx_collection/tests/integration/targets/tower_instance_group/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_instance_group/tasks/main.yml @@ -19,11 +19,11 @@ host: "https://openshift.org" bearer_token: "asdf1234" verify_ssl: false - register: result + register: cred_result - assert: that: - - "result is changed" + - "cred_result is changed" - name: Create an Instance Group tower_instance_group: @@ -37,10 +37,22 @@ that: - "result is changed" + - name: Update an Instance Group + tower_instance_group: + name: "{{ result.id }}" + policy_instance_percentage: 34 + policy_instance_minimum: 24 + state: present + register: result + + - assert: + that: + - "result is changed" + - name: Create a container group tower_instance_group: name: "{{ group_name2 }}" - credential: "{{ cred_name1 }}" + credential: "{{ cred_result.id }}" register: result - assert: diff --git a/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml b/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml index 3360a57b8c..552b32f47c 100644 --- a/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_inventory/tasks/main.yml @@ -29,7 +29,7 @@ tower_inventory: name: "{{ inv_name1 }}" organization: Default - insights_credential: "{{ cred_name1 }}" + insights_credential: "{{ results.id }}" state: present register: result @@ -39,7 +39,7 @@ - name: Test Inventory module idempotency tower_inventory: - name: "{{ inv_name1 }}" + name: "{{ result.id }}" organization: Default insights_credential: "{{ cred_name1 }}" state: present diff --git a/awx_collection/tests/integration/targets/tower_inventory_source/tasks/main.yml b/awx_collection/tests/integration/targets/tower_inventory_source/tasks/main.yml index fb5f33c3ca..17154a54fe 100644 --- a/awx_collection/tests/integration/targets/tower_inventory_source/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_inventory_source/tasks/main.yml @@ -16,6 +16,7 @@ host: https://example.org:5000 password: passw0rd domain: test + register: credential_result - name: Add a Tower inventory tower_inventory: @@ -28,7 +29,7 @@ name: "{{ openstack_inv_source }}" description: Source for Test inventory inventory: "{{ openstack_inv }}" - credential: "{{ openstack_cred }}" + credential: "{{ credential_result.id }}" overwrite: true update_on_launch: true source_vars: @@ -42,7 +43,7 @@ - name: Delete the inventory source with an invalid cred, source_project, sourece_script specified tower_inventory_source: - name: "{{ openstack_inv_source }}" + name: "{{ result.id }}" inventory: "{{ openstack_inv }}" credential: "Does Not Exit" source_project: "Does Not Exist" diff --git a/awx_collection/tests/integration/targets/tower_job_template/tasks/main.yml b/awx_collection/tests/integration/targets/tower_job_template/tasks/main.yml index a744b89464..6b3717d5e5 100644 --- a/awx_collection/tests/integration/targets/tower_job_template/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_job_template/tasks/main.yml @@ -22,14 +22,14 @@ state: present scm_type: git scm_url: https://github.com/ansible/ansible-tower-samples.git - - register: result + register: proj_result - name: Create Credential1 tower_credential: name: "{{ cred1 }}" organization: Default kind: tower + register: cred1_result - name: Create Credential2 tower_credential: @@ -84,19 +84,19 @@ credentials: ["{{ cred1 }}", "{{ cred2 }}"] job_type: run state: present - register: result + register: jt1_result - assert: that: - - "result is changed" + - "jt1_result is changed" - name: Add a credential to this JT tower_job_template: name: "{{ jt1 }}" - project: "{{ proj1 }}" + project: "{{ proj_result.id }}" playbook: hello_world.yml credentials: - - "{{ cred1 }}" + - "{{ cred1_result.id }}" register: result - assert: @@ -105,7 +105,7 @@ - name: Try to add the same credential to this JT tower_job_template: - name: "{{ jt1 }}" + name: "{{ jt1_result.id }}" project: "{{ proj1 }}" playbook: hello_world.yml credentials: diff --git a/awx_collection/tools/roles/generate/templates/tower_module.j2 b/awx_collection/tools/roles/generate/templates/tower_module.j2 index 3606cff547..8c82da660d 100644 --- a/awx_collection/tools/roles/generate/templates/tower_module.j2 +++ b/awx_collection/tools/roles/generate/templates/tower_module.j2 @@ -175,9 +175,8 @@ def main(): {% endfor %} # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('{{ item_type }}', **{ + existing_item, name = module.get_one('{{ item_type }}', name_or_id={{ name_option }}, **{ 'data': { - '{{ name_option }}': {{ name_option }}, {% if 'organization' in item['json']['actions']['POST'] and item['json']['actions']['POST']['organization']['type'] == 'id' %} 'organization': org_id, {% endif %}