From 51959b29de595d47fb4cd71155bade0f92919f8d Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 3 Sep 2020 10:38:53 -0400 Subject: [PATCH 01/13] Adding name_or_id to get_one to make module more uniform --- .../plugins/module_utils/tower_api.py | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 7eee6561d1..00e8827ea8 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -88,7 +88,28 @@ class TowerAPIModule(TowerModule): response['json']['next'] = next_page return response - def get_one(self, endpoint, *args, **kwargs): + def get_name_field_from_endpoint(self, endpoint): + if endpoint == 'users': + return 'username' + elif endpoint == 'instances': + return 'hostname' + return 'name' + + def get_one(self, endpoint, name_or_id=None, *args, **kwargs): + if name_or_id: + name_field = self.get_name_field_from_endpoint(endpoint) + new_args = kwargs.get('data', {}).copy() + if name_field in new_args: + self.fail_json(msg="You can't specify the field {0} in your search data if using the name_or_id field".format(name_field)) + + new_args['or__{0}'.format(name_field)] = name_or_id + try: + new_args['or__id'] = int(name_or_id) + except ValueError: + # If we get a value error, then we didn't have an integer so we can just pass and fall down to the fail + pass + kwargs['data'] = new_args + response = self.get_endpoint(endpoint, *args, **kwargs) if response['status_code'] != 200: fail_msg = "Got a {0} response when trying to get one from {1}".format(response['status_code'], endpoint) @@ -102,16 +123,19 @@ class TowerAPIModule(TowerModule): if response['json']['count'] == 0: return None 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 + # 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] def get_one_by_name_or_id(self, endpoint, name_or_id): - name_field = 'name' - if endpoint == 'users': - name_field = 'username' - elif endpoint == 'instances': - name_field = 'hostname' + name_field = self.get_name_field_from_endpoint(endpoint) query_params = {'or__{0}'.format(name_field): name_or_id} try: From 5042ad3a2be3bd7253309aae55ea1556efee57b0 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 3 Sep 2020 11:15:59 -0400 Subject: [PATCH 02/13] Updating user module for new get_one --- awx_collection/plugins/modules/tower_user.py | 8 +++----- .../tests/integration/targets/tower_user/tasks/main.yml | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index 15c41cb081..19cf00fa1f 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -134,11 +134,9 @@ 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', **{ - 'data': { - 'username': username, - } - }) + 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 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/tests/integration/targets/tower_user/tasks/main.yml b/awx_collection/tests/integration/targets/tower_user/tasks/main.yml index f04f1a8327..79077479df 100644 --- a/awx_collection/tests/integration/targets/tower_user/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_user/tasks/main.yml @@ -15,9 +15,9 @@ that: - "result is changed" -- name: Change a User +- name: Change a User by ID tower_user: - username: "{{ username }}" + username: "{{ result.id }}" last_name: User email: joe@example.org state: present From 24ec12923578540f80f1be3440466c06a467f53e Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Thu, 3 Sep 2020 11:17:21 -0400 Subject: [PATCH 03/13] Modifying credential for new get_one --- awx_collection/plugins/modules/tower_credential.py | 5 +++-- .../integration/targets/tower_credential/tasks/main.yml | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/awx_collection/plugins/modules/tower_credential.py b/awx_collection/plugins/modules/tower_credential.py index 2d4cbd3331..3f37e0ad62 100644 --- a/awx_collection/plugins/modules/tower_credential.py +++ b/awx_collection/plugins/modules/tower_credential.py @@ -365,13 +365,14 @@ def main(): # Attempt to look up the object based on the provided name, credential type and optional organization lookup_data = { - 'name': name, 'credential_type': cred_type_id, } if organization: lookup_data['organization'] = org_id - credential = module.get_one('credentials', **{'data': lookup_data}) + 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 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/tests/integration/targets/tower_credential/tasks/main.yml b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml index 7c0f4b080d..16eac5b66a 100644 --- a/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_credential/tasks/main.yml @@ -82,9 +82,9 @@ that: - "result is changed" -- name: Re-create the Org-specific credential (new school) +- name: Re-create the Org-specific credential (new school) with an ID tower_credential: - name: "{{ ssh_cred_name1 }}" + name: "{{ result.id }}" organization: Default credential_type: 'Machine' state: present From 4c4d6dad49477c8f2ca50dcbedbc50d32666a6ac Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Fri, 4 Sep 2020 15:02:06 -0400 Subject: [PATCH 04/13] Adding get_item_name and making static methods --- .../plugins/module_utils/tower_api.py | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 00e8827ea8..da8474634d 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -42,6 +42,33 @@ class TowerAPIModule(TowerModule): } return exceptions.get(name, '{0}s'.format(name)) + @staticmethod + def get_name_field_from_endpoint(endpoint): + if endpoint == 'users': + return 'username' + elif endpoint == 'instances': + return 'hostname' + return 'name' + + @staticmethod + def get_item_name(item): + if 'name' in item: + return item['name'] + elif 'username' in item: + return item['username'] + elif 'identifier' in item: + return item['identifier'] + elif 'hostname' in item: + return item['hostname'] + elif item.get('type', None) == 'o_auth2_access_token': + # An oauth2 token has no name, instead we will use its id for any of the messages + rerurn item['id'] + elif item.get('type', None) == 'credential_input_source': + # An credential_input_source has no name, instead we will use its id for any of the messages + return existing_item['id'] + return 'Unknown' + + def head_endpoint(self, endpoint, *args, **kwargs): return self.make_request('HEAD', endpoint, **kwargs) @@ -88,13 +115,6 @@ class TowerAPIModule(TowerModule): response['json']['next'] = next_page return response - def get_name_field_from_endpoint(self, endpoint): - if endpoint == 'users': - return 'username' - elif endpoint == 'instances': - return 'hostname' - return 'name' - def get_one(self, endpoint, name_or_id=None, *args, **kwargs): if name_or_id: name_field = self.get_name_field_from_endpoint(endpoint) @@ -343,24 +363,10 @@ class TowerAPIModule(TowerModule): item_url = existing_item['url'] item_type = existing_item['type'] item_id = existing_item['id'] + item_name = self.get_item_name(existing_item) except KeyError as ke: self.fail_json(msg="Unable to process delete of item due to missing data {0}".format(ke)) - if 'name' in existing_item: - item_name = existing_item['name'] - elif 'username' in existing_item: - item_name = existing_item['username'] - elif 'identifier' in existing_item: - item_name = existing_item['identifier'] - elif item_type == 'o_auth2_access_token': - # An oauth2 token has no name, instead we will use its id for any of the messages - item_name = existing_item['id'] - elif item_type == 'credential_input_source': - # An credential_input_source has no name, instead we will use its id for any of the messages - item_name = existing_item['id'] - else: - self.fail_json(msg="Unable to process delete of {0} due to missing name".format(item_type)) - response = self.delete_endpoint(item_url) if response['status_code'] in [202, 204]: @@ -433,12 +439,7 @@ class TowerAPIModule(TowerModule): # We have to rely on item_type being passed in since we don't have an existing item that declares its type # We will pull the item_name out from the new_item, if it exists - for key in ('name', 'username', 'identifier', 'hostname'): - if key in new_item: - item_name = new_item[key] - break - else: - item_name = 'unknown' + item_name = self.get_item_name(new_item) response = self.post_endpoint(endpoint, **{'data': new_item}) if response['status_code'] == 201: From 106157c6003a2dc4e64977b374d5dc401fea5e4e Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Fri, 4 Sep 2020 15:41:36 -0400 Subject: [PATCH 05/13] 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 %} From 0a8db586d14a68476ad32d6ed1236c780e5bfe17 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 11:02:25 -0400 Subject: [PATCH 06/13] Changing how get_one returns --- .../plugins/module_utils/tower_api.py | 41 ++++++++----------- .../plugins/modules/tower_credential.py | 4 +- .../modules/tower_credential_input_source.py | 2 +- .../plugins/modules/tower_credential_type.py | 5 ++- awx_collection/plugins/modules/tower_group.py | 6 +-- awx_collection/plugins/modules/tower_host.py | 4 +- .../plugins/modules/tower_instance_group.py | 4 +- .../plugins/modules/tower_inventory.py | 4 +- .../plugins/modules/tower_inventory_source.py | 34 +++++---------- .../plugins/modules/tower_job_cancel.py | 2 +- .../plugins/modules/tower_job_launch.py | 2 +- .../plugins/modules/tower_job_template.py | 4 +- .../plugins/modules/tower_job_wait.py | 2 +- awx_collection/plugins/modules/tower_label.py | 4 +- .../modules/tower_notification_template.py | 4 +- .../plugins/modules/tower_organization.py | 4 +- .../plugins/modules/tower_project.py | 4 +- .../plugins/modules/tower_project_update.py | 20 ++++----- .../plugins/modules/tower_schedule.py | 4 +- awx_collection/plugins/modules/tower_team.py | 4 +- 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 | 6 +-- .../plugins/modules/tower_workflow_launch.py | 2 +- 25 files changed, 76 insertions(+), 100 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index cf2860a87b..7ba5b1de7d 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -25,6 +25,11 @@ class TowerAPIModule(TowerModule): } session = None cookie_jar = CookieJar() + IDENTITY_FIELDS = { + 'users': 'username', + 'workflow_job_template_nodes': 'identifier', + 'instances': 'hostname' + } def __init__(self, argument_spec, direct_params=None, error_callback=None, warn_callback=None, **kwargs): kwargs['supports_check_mode'] = True @@ -44,30 +49,20 @@ class TowerAPIModule(TowerModule): @staticmethod def get_name_field_from_endpoint(endpoint): - if endpoint == 'users': - return 'username' - elif endpoint == 'instances': - return 'hostname' - return 'name' + return TowerAPIModule.IDENTITY_FIELDS.get(endpoint, 'name') - @staticmethod - def get_item_name(item): + def get_item_name(self, item): if 'name' in item: return item['name'] - elif 'username' in item: - return item['username'] - elif 'identifier' in item: - return item['identifier'] - elif 'hostname' in item: - return item['hostname'] - elif item.get('type', None) == 'o_auth2_access_token': - # An oauth2 token has no name, instead we will use its id for any of the messages - rerurn item['id'] - elif item.get('type', None) == 'credential_input_source': - # An credential_input_source has no name, instead we will use its id for any of the messages - return existing_item['id'] - return 'Unknown' + for field_name in TowerAPIModule.IDENTITY_FIELDS.values(): + if field_name in item: + return item[field_name] + + if item.get('type', None) in ('o_auth2_access_token', 'credential_input_source'): + return item['id'] + + self.exit_json(msg='Cannot determine identity field for {0} object.'.format(item.get('type', 'unknown'))) def head_endpoint(self, endpoint, *args, **kwargs): return self.make_request('HEAD', endpoint, **kwargs) @@ -141,18 +136,18 @@ class TowerAPIModule(TowerModule): self.fail_json(msg="The endpoint did not provide count and results") if response['json']['count'] == 0: - return None, name_or_id + return None 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, asset['id'][name_field] + return asset # 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], response['json']['results'][0][name_field] + return response['json']['results'][0] 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 146484ca66..f244ae812e 100644 --- a/awx_collection/plugins/modules/tower_credential.py +++ b/awx_collection/plugins/modules/tower_credential.py @@ -370,7 +370,7 @@ def main(): if organization: lookup_data['organization'] = org_id - credential, name = module.get_one('credentials', name_or_id=name, **{'data': lookup_data}) + credential = 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 @@ -396,7 +396,7 @@ def main(): # Create the data that gets sent for create and update credential_fields = { - 'name': new_name if new_name else name, + 'name': new_name if new_name else (module.get_item_name(credential) if credential else name), 'credential_type': cred_type_id, } if has_inputs: diff --git a/awx_collection/plugins/modules/tower_credential_input_source.py b/awx_collection/plugins/modules/tower_credential_input_source.py index 0966ff3334..cdc55cb1f0 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, junk = module.get_one('credential_input_sources', **{'data': lookup_data}) + credential_input_source = 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 3ab21c4330..37bb7f6502 100644 --- a/awx_collection/plugins/modules/tower_credential_type.py +++ b/awx_collection/plugins/modules/tower_credential_type.py @@ -115,7 +115,6 @@ def main(): # These will be passed into the create/updates credential_type_params = { - 'name': new_name if new_name else name, 'managed_by_tower': False, } if kind: @@ -128,12 +127,14 @@ def main(): credential_type_params['injectors'] = module.params.get('injectors') # Attempt to look up credential_type based on the provided name - credential_type, name = module.get_one('credential_types', name_or_id=name) + credential_type = 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 module.delete_if_needed(credential_type) + credential_type_params['name'] = new_name if new_name else (module.get_item_name(credential_type) if credential_type else name) + # If the state was present and we can let the module build or update the existing credential type, this will return on its own module.create_or_update_if_needed(credential_type, credential_type_params, endpoint='credential_types', item_type='credential type') diff --git a/awx_collection/plugins/modules/tower_group.py b/awx_collection/plugins/modules/tower_group.py index 778c54b0c7..6781dc5883 100644 --- a/awx_collection/plugins/modules/tower_group.py +++ b/awx_collection/plugins/modules/tower_group.py @@ -108,7 +108,7 @@ 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, name = module.get_one('groups', name_or_id=name, **{ + group = module.get_one('groups', name_or_id=name, **{ 'data': { 'inventory': inventory_id } @@ -120,7 +120,7 @@ def main(): # Create the data that gets sent for create and update group_fields = { - 'name': new_name if new_name else name, + 'name': new_name if new_name else (module.get_item_name(group) if group else name), 'inventory': inventory_id, } if description is not None: @@ -135,7 +135,7 @@ def main(): continue id_list = [] for sub_name in name_list: - sub_obj, sub_name = module.get_one(resource, name_or_id=sub_name, **{ + sub_obj = module.get_one(resource, name_or_id=sub_name, **{ 'data': {'inventory': inventory_id}, }) if sub_obj is None: diff --git a/awx_collection/plugins/modules/tower_host.py b/awx_collection/plugins/modules/tower_host.py index d0cf12bcb9..6c84f88a99 100644 --- a/awx_collection/plugins/modules/tower_host.py +++ b/awx_collection/plugins/modules/tower_host.py @@ -104,7 +104,7 @@ 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, name = module.get_one('hosts', name_or_id=name, **{ + host = module.get_one('hosts', name_or_id=name, **{ 'data': { 'inventory': inventory_id } @@ -116,7 +116,7 @@ def main(): # Create the data that gets sent for create and update host_fields = { - 'name': new_name if new_name else name, + 'name': new_name if new_name else (module.get_item_name(host) if host else name), 'inventory': inventory_id, 'enabled': enabled, } diff --git a/awx_collection/plugins/modules/tower_instance_group.py b/awx_collection/plugins/modules/tower_instance_group.py index cc8c97033f..77c4ac4ece 100644 --- a/awx_collection/plugins/modules/tower_instance_group.py +++ b/awx_collection/plugins/modules/tower_instance_group.py @@ -109,7 +109,7 @@ def main(): state = module.params.get('state') # Attempt to look up an existing item based on the provided data - existing_item, name = module.get_one('instance_groups', name_or_id=name) + existing_item = 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 @@ -127,7 +127,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) if credential is not None: new_fields['credential'] = credential_id if policy_instance_percentage is not None: diff --git a/awx_collection/plugins/modules/tower_inventory.py b/awx_collection/plugins/modules/tower_inventory.py index e8462a2844..ce4eab93d8 100644 --- a/awx_collection/plugins/modules/tower_inventory.py +++ b/awx_collection/plugins/modules/tower_inventory.py @@ -109,7 +109,7 @@ 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, name = module.get_one('inventories', name_or_id=name, **{ + inventory = module.get_one('inventories', name_or_id=name, **{ 'data': { 'organization': org_id } @@ -121,7 +121,7 @@ def main(): # Create the data that gets sent for create and update inventory_fields = { - 'name': name, + 'name': module.get_item_name(inventory) if inventory else name, 'organization': org_id, 'kind': kind, 'host_filter': host_filter, diff --git a/awx_collection/plugins/modules/tower_inventory_source.py b/awx_collection/plugins/modules/tower_inventory_source.py index 289707ccd9..b6fb7fff3b 100644 --- a/awx_collection/plugins/modules/tower_inventory_source.py +++ b/awx_collection/plugins/modules/tower_inventory_source.py @@ -128,10 +128,6 @@ options: - list of notifications to send on error type: list elements: str - organization: - description: - - Name of the inventory source's inventory's organization. - type: str extends_documentation_fragment: awx.awx.auth ''' @@ -144,7 +140,6 @@ EXAMPLES = ''' credential: previously-created-credential overwrite: True update_on_launch: True - organization: Default source_vars: private: false ''' @@ -173,7 +168,6 @@ def main(): enabled_value=dict(), host_filter=dict(), credential=dict(), - organization=dict(), overwrite=dict(type='bool'), overwrite_vars=dict(type='bool'), custom_virtualenv=dict(), @@ -196,30 +190,22 @@ def main(): name = module.params.get('name') new_name = module.params.get('new_name') inventory = module.params.get('inventory') - organization = module.params.get('organization') source_script = module.params.get('source_script') credential = module.params.get('credential') source_project = module.params.get('source_project') state = module.params.get('state') -<<<<<<< HEAD - lookup_data = {} - if organization: - lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - 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', name_or_id=name, **{ + # Attempt to look up inventory source based on the provided name and inventory ID + inventory_id = module.resolve_name_to_id('inventories', inventory) + inventory_source = module.get_one('inventory_sources', name_or_id=name, **{ 'data': { - 'inventory': inventory_object['id'], + 'inventory': inventory_id, } }) 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_source_object) + module.delete_if_needed(inventory_source) # Attempt to look up associated field items the user specified. association_fields = {} @@ -244,8 +230,8 @@ def main(): # Create the data that gets sent for create and update inventory_source_fields = { - 'name': new_name if new_name else name, - 'inventory': inventory_object['id'], + 'name': new_name if new_name else (module.get_item_name(inventory_source) if inventory_source else name), + 'inventory': inventory_id, } # Attempt to look up the related items the user specified (these will fail the module if not found) @@ -274,12 +260,12 @@ def main(): inventory_source_fields['source_vars'] = dumps(inventory_source_fields['source_vars']) # Sanity check on arguments - if state == 'present' and not inventory_source_object and not inventory_source_fields['source']: + if state == 'present' and not inventory_source and not inventory_source_fields['source']: module.fail_json(msg="If creating a new inventory source, the source param must be present") - # If the state was present we can let the module build or update the existing inventory_source_object, this will return on its own + # If the state was present we can let the module build or update the existing inventory_source, this will return on its own module.create_or_update_if_needed( - inventory_source_object, inventory_source_fields, + inventory_source, inventory_source_fields, endpoint='inventory_sources', item_type='inventory source', associations=association_fields ) diff --git a/awx_collection/plugins/modules/tower_job_cancel.py b/awx_collection/plugins/modules/tower_job_cancel.py index 0bfe41d9c0..7404d452a4 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, job_name = module.get_one('jobs', **{ + job = 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 39c5b9caf4..9b118afcec 100644 --- a/awx_collection/plugins/modules/tower_job_launch.py +++ b/awx_collection/plugins/modules/tower_job_launch.py @@ -201,7 +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, name = module.get_one('job_templates', name_or_id=name) + job_template = 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 7f59e3f945..96ab600cf8 100644 --- a/awx_collection/plugins/modules/tower_job_template.py +++ b/awx_collection/plugins/modules/tower_job_template.py @@ -419,14 +419,14 @@ def main(): search_fields['organization'] = new_fields['organization'] = organization_id # Attempt to look up an existing item based on the provided data - existing_item, name = module.get_one('job_templates', name_or_id=name, **{'data': search_fields}) + existing_item = 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 module.delete_if_needed(existing_item) # Create the data that gets sent for create and update - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) for field_name in ( 'description', 'job_type', 'playbook', 'scm_branch', 'forks', 'limit', 'verbosity', 'job_tags', 'force_handlers', 'skip_tags', 'start_at_task', 'timeout', 'use_fact_cache', diff --git a/awx_collection/plugins/modules/tower_job_wait.py b/awx_collection/plugins/modules/tower_job_wait.py index 399926805f..6f71a1be5b 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, junk = module.get_one('jobs', **{ + job = 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 30b4843c41..7c520064a8 100644 --- a/awx_collection/plugins/modules/tower_label.py +++ b/awx_collection/plugins/modules/tower_label.py @@ -80,7 +80,7 @@ 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, name = module.get_one('labels', name_or_id=name, **{ + existing_item = module.get_one('labels', name_or_id=name, **{ 'data': { 'organization': organization_id, } @@ -88,7 +88,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) if organization: new_fields['organization'] = organization_id diff --git a/awx_collection/plugins/modules/tower_notification_template.py b/awx_collection/plugins/modules/tower_notification_template.py index 4ad387fc5d..95e5971e79 100644 --- a/awx_collection/plugins/modules/tower_notification_template.py +++ b/awx_collection/plugins/modules/tower_notification_template.py @@ -380,7 +380,7 @@ 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, name = module.get_one('notification_templates', name_or_id=name, **{ + existing_item = module.get_one('notification_templates', name_or_id=name, **{ 'data': { 'organization': organization_id, } @@ -403,7 +403,7 @@ def main(): new_fields = {} if final_notification_configuration: new_fields['notification_configuration'] = final_notification_configuration - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) if description is not None: new_fields['description'] = description if organization is not None: diff --git a/awx_collection/plugins/modules/tower_organization.py b/awx_collection/plugins/modules/tower_organization.py index 33da6b0f90..5042ecf22c 100644 --- a/awx_collection/plugins/modules/tower_organization.py +++ b/awx_collection/plugins/modules/tower_organization.py @@ -117,7 +117,7 @@ def main(): state = module.params.get('state') # Attempt to look up organization based on the provided name - organization, name = module.get_one('organizations', name_or_id=name) + organization = 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 @@ -150,7 +150,7 @@ def main(): association_fields['notification_templates_approvals'].append(module.resolve_name_to_id('notification_templates', item)) # Create the data that gets sent for create and update - org_fields = {'name': name} + org_fields = {'name': module.get_item_name(organization) if organization else name} if description is not None: org_fields['description'] = description if custom_virtualenv is not None: diff --git a/awx_collection/plugins/modules/tower_project.py b/awx_collection/plugins/modules/tower_project.py index bd33ad9ef2..468f63e09d 100644 --- a/awx_collection/plugins/modules/tower_project.py +++ b/awx_collection/plugins/modules/tower_project.py @@ -239,7 +239,7 @@ def main(): credential = module.resolve_name_to_id('credentials', credential) # Attempt to look up project based on the provided name and org ID - project, name = module.get_one('projects', name_or_id=name, **{ + project = module.get_one('projects', name_or_id=name, **{ 'data': { 'organization': org_id } @@ -272,7 +272,7 @@ def main(): # Create the data that gets sent for create and update project_fields = { - 'name': name, + 'name': module.get_item_name(project) if project else name, 'scm_type': scm_type, 'scm_url': scm_url, 'scm_branch': scm_branch, diff --git a/awx_collection/plugins/modules/tower_project_update.py b/awx_collection/plugins/modules/tower_project_update.py index 7622001411..c2ebf2e422 100644 --- a/awx_collection/plugins/modules/tower_project_update.py +++ b/awx_collection/plugins/modules/tower_project_update.py @@ -103,18 +103,12 @@ def main(): timeout = module.params.get('timeout') # Attempt to look up project based on the provided name or id - 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] - else: - lookup_data = {} - if organization: - lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - 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") + lookup_data = {} + if organization: + lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) + project = module.get_one('projects', name_or_id=name, 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']) @@ -138,7 +132,7 @@ def main(): # Invoke wait function module.wait_on_url( url=result['json']['url'], - object_name=name, + object_name=module.get_item_name(project), object_type='Project Update', timeout=timeout, interval=interval ) diff --git a/awx_collection/plugins/modules/tower_schedule.py b/awx_collection/plugins/modules/tower_schedule.py index c69905bfac..66498c36cd 100644 --- a/awx_collection/plugins/modules/tower_schedule.py +++ b/awx_collection/plugins/modules/tower_schedule.py @@ -190,13 +190,13 @@ 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, name = module.get_one('schedules', name_or_id=name) + existing_item = module.get_one('schedules', name_or_id=name) # Create the data that gets sent for create and update new_fields = {} if rrule is not None: new_fields['rrule'] = rrule - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) if description is not None: new_fields['description'] = description if extra_data is not None: diff --git a/awx_collection/plugins/modules/tower_team.py b/awx_collection/plugins/modules/tower_team.py index 7f7f5fa632..9339610c75 100644 --- a/awx_collection/plugins/modules/tower_team.py +++ b/awx_collection/plugins/modules/tower_team.py @@ -87,7 +87,7 @@ 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, name = module.get_one('teams', name_or_id=name, **{ + team = module.get_one('teams', name_or_id=name, **{ 'data': { 'organization': org_id } @@ -99,7 +99,7 @@ def main(): # Create the data that gets sent for create and update team_fields = { - 'name': new_name if new_name else name, + 'name': new_name if new_name else (module.get_item_name(team) if team else name), 'organization': org_id } if description is not None: diff --git a/awx_collection/plugins/modules/tower_token.py b/awx_collection/plugins/modules/tower_token.py index 2680caaf93..ee6fd5c200 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, token_name = module.get_one('tokens', **{ + existing_token = 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 56f231dae8..0b0dd6d45d 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -134,7 +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, username = module.get_one('users', name_or_id=username) + existing_item = 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 @@ -143,7 +143,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} if username: - new_fields['username'] = username + new_fields['username'] = module.get_item_name(existing_item) if existing_item else name if first_name: new_fields['first_name'] = first_name if last_name: diff --git a/awx_collection/plugins/modules/tower_workflow_job_template.py b/awx_collection/plugins/modules/tower_workflow_job_template.py index 21818cb4c6..aec78518d9 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template.py @@ -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, name = module.get_one('workflow_job_templates', name_or_id=name, **{'data': search_fields}) + existing_item = 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 @@ -208,7 +208,7 @@ def main(): new_fields['webhook_credential'] = module.resolve_name_to_id('webhook_credential', webhook_credential) # Create the data that gets sent for create and update - new_fields['name'] = new_name if new_name else name + new_fields['name'] = new_name if new_name else (module.get_item_name(existing_item) if existing_item else name) for field_name in ( 'description', 'survey_enabled', 'allow_simultaneous', 'limit', 'scm_branch', 'extra_vars', 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 2ba74c1fb9..0705de6785 100644 --- a/awx_collection/plugins/modules/tower_workflow_job_template_node.py +++ b/awx_collection/plugins/modules/tower_workflow_job_template_node.py @@ -203,7 +203,7 @@ def main(): if organization: organization_id = module.resolve_name_to_id('organizations', organization) wfjt_search_fields['organization'] = organization_id - wfjt_data, workflow_job_template = module.get_one('workflow_job_templates', name_or_id=workflow_job_template, **{ + wfjt_data = module.get_one('workflow_job_templates', name_or_id=workflow_job_template, **{ 'data': wfjt_search_fields }) if wfjt_data is None: @@ -214,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, junk = module.get_one('workflow_job_template_nodes', **{'data': search_fields}) + existing_item = 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 @@ -251,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, junk = module.get_one(endpoint, **{'data': lookup_data}) + sub_obj = 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 0e9cbb9ad6..a1de4a46bc 100644 --- a/awx_collection/plugins/modules/tower_workflow_launch.py +++ b/awx_collection/plugins/modules/tower_workflow_launch.py @@ -141,7 +141,7 @@ def main(): lookup_data = {} if organization: lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) - workflow_job_template, name = module.get_one('workflow_job_templates', name_or_id=name, data=lookup_data) + workflow_job_template = 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") From 6467d34445152ab78adba7ec2a2133dd8ea70fcb Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 11:36:33 -0400 Subject: [PATCH 07/13] Touching up tower_inventory_source after rebase --- .../plugins/modules/tower_inventory_source.py | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/awx_collection/plugins/modules/tower_inventory_source.py b/awx_collection/plugins/modules/tower_inventory_source.py index b6fb7fff3b..5945d411d8 100644 --- a/awx_collection/plugins/modules/tower_inventory_source.py +++ b/awx_collection/plugins/modules/tower_inventory_source.py @@ -128,6 +128,10 @@ options: - list of notifications to send on error type: list elements: str + organization: + description: + - Name of the inventory source's inventory's organization. + type: str extends_documentation_fragment: awx.awx.auth ''' @@ -140,6 +144,7 @@ EXAMPLES = ''' credential: previously-created-credential overwrite: True update_on_launch: True + organization: Default source_vars: private: false ''' @@ -168,6 +173,7 @@ def main(): enabled_value=dict(), host_filter=dict(), credential=dict(), + organization=dict(), overwrite=dict(type='bool'), overwrite_vars=dict(type='bool'), custom_virtualenv=dict(), @@ -190,22 +196,29 @@ def main(): name = module.params.get('name') new_name = module.params.get('new_name') inventory = module.params.get('inventory') + organization = module.params.get('organization') source_script = module.params.get('source_script') credential = module.params.get('credential') source_project = module.params.get('source_project') state = module.params.get('state') - # Attempt to look up inventory source based on the provided name and inventory ID - inventory_id = module.resolve_name_to_id('inventories', inventory) - inventory_source = module.get_one('inventory_sources', name_or_id=name, **{ + lookup_data = {} + if organization: + lookup_data['organization'] = module.resolve_name_to_id('organizations', organization) + 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', name_or_id=name, **{ 'data': { - 'inventory': inventory_id, + 'inventory': inventory_object['id'], } }) 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_source) + module.delete_if_needed(inventory_source_object) # Attempt to look up associated field items the user specified. association_fields = {} @@ -230,8 +243,8 @@ def main(): # Create the data that gets sent for create and update inventory_source_fields = { - 'name': new_name if new_name else (module.get_item_name(inventory_source) if inventory_source else name), - 'inventory': inventory_id, + 'name': new_name if new_name else name, + 'inventory': inventory_object['id'], } # Attempt to look up the related items the user specified (these will fail the module if not found) @@ -260,12 +273,12 @@ def main(): inventory_source_fields['source_vars'] = dumps(inventory_source_fields['source_vars']) # Sanity check on arguments - if state == 'present' and not inventory_source and not inventory_source_fields['source']: + if state == 'present' and not inventory_source_object and not inventory_source_fields['source']: module.fail_json(msg="If creating a new inventory source, the source param must be present") - # If the state was present we can let the module build or update the existing inventory_source, this will return on its own + # If the state was present we can let the module build or update the existing inventory_source_object, this will return on its own module.create_or_update_if_needed( - inventory_source, inventory_source_fields, + inventory_source_object, inventory_source_fields, endpoint='inventory_sources', item_type='inventory source', associations=association_fields ) From d6f35a71d7114573b7b50ab9b0686f6ca8d0468c Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 11:51:47 -0400 Subject: [PATCH 08/13] Adding allow_unknown option for get_item_name --- awx_collection/plugins/module_utils/tower_api.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 7ba5b1de7d..e0d2027ba5 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -51,7 +51,7 @@ class TowerAPIModule(TowerModule): def get_name_field_from_endpoint(endpoint): return TowerAPIModule.IDENTITY_FIELDS.get(endpoint, 'name') - def get_item_name(self, item): + def get_item_name(self, item, allow_unknown=False): if 'name' in item: return item['name'] @@ -62,6 +62,9 @@ class TowerAPIModule(TowerModule): if item.get('type', None) in ('o_auth2_access_token', 'credential_input_source'): return item['id'] + if allow_unknown: + return 'unknown' + self.exit_json(msg='Cannot determine identity field for {0} object.'.format(item.get('type', 'unknown'))) def head_endpoint(self, endpoint, *args, **kwargs): @@ -358,7 +361,7 @@ class TowerAPIModule(TowerModule): item_url = existing_item['url'] item_type = existing_item['type'] item_id = existing_item['id'] - item_name = self.get_item_name(existing_item) + item_name = self.get_item_name(existing_item, allow_unknown=True) except KeyError as ke: self.fail_json(msg="Unable to process delete of item due to missing data {0}".format(ke)) @@ -434,7 +437,7 @@ class TowerAPIModule(TowerModule): # We have to rely on item_type being passed in since we don't have an existing item that declares its type # We will pull the item_name out from the new_item, if it exists - item_name = self.get_item_name(new_item) + item_name = self.get_item_name(new_item, allow_unknown=True) response = self.post_endpoint(endpoint, **{'data': new_item}) if response['status_code'] == 201: From c2cfaec7d122f228cd6de59a3eb300a99ac06c4e Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 11:52:53 -0400 Subject: [PATCH 09/13] Fixing name -> username --- awx_collection/plugins/modules/tower_user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx_collection/plugins/modules/tower_user.py b/awx_collection/plugins/modules/tower_user.py index 0b0dd6d45d..4b648356e4 100644 --- a/awx_collection/plugins/modules/tower_user.py +++ b/awx_collection/plugins/modules/tower_user.py @@ -143,7 +143,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} if username: - new_fields['username'] = module.get_item_name(existing_item) if existing_item else name + new_fields['username'] = module.get_item_name(existing_item) if existing_item else username if first_name: new_fields['first_name'] = first_name if last_name: From 4c0e288feef3fdc0a539ab6565bba52f5ae1aa78 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 13:38:44 -0400 Subject: [PATCH 10/13] Touching up to missing sport of get_one returning multiple params --- awx_collection/plugins/modules/tower_job_template.py | 2 +- awx_collection/tools/roles/generate/templates/tower_module.j2 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/awx_collection/plugins/modules/tower_job_template.py b/awx_collection/plugins/modules/tower_job_template.py index 96ab600cf8..3a42a3ade1 100644 --- a/awx_collection/plugins/modules/tower_job_template.py +++ b/awx_collection/plugins/modules/tower_job_template.py @@ -453,7 +453,7 @@ 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, project = module.get_one('projects', name_or_id=project, **{ + project_data = module.get_one('projects', name_or_id=project, **{ 'data': { 'organization': organization_id, } diff --git a/awx_collection/tools/roles/generate/templates/tower_module.j2 b/awx_collection/tools/roles/generate/templates/tower_module.j2 index 8c82da660d..60e65cf92c 100644 --- a/awx_collection/tools/roles/generate/templates/tower_module.j2 +++ b/awx_collection/tools/roles/generate/templates/tower_module.j2 @@ -175,7 +175,7 @@ def main(): {% endfor %} # Attempt to look up an existing item based on the provided data - existing_item, name = module.get_one('{{ item_type }}', name_or_id={{ name_option }}, **{ + existing_item = module.get_one('{{ item_type }}', name_or_id={{ name_option }}, **{ 'data': { {% if 'organization' in item['json']['actions']['POST'] and item['json']['actions']['POST']['organization']['type'] == 'id' %} 'organization': org_id, @@ -194,7 +194,7 @@ def main(): new_fields = {} {% for option in item['json']['actions']['POST'] %} {% if option == name_option %} - new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else {{ name_option }} + new_fields['{{ name_option }}'] = new_{{ name_option }} if new_{{ name_option }} else (module.get_item_name(existing_item) if existing_item else {{ name_option }}t) {% else %} if {{ option }} is not None: {% if item['json']['actions']['POST'][option]['type'] == 'id' %} From 09b8f82bbb9aa5c12a474819d6d3340d022c3f81 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 15:28:21 -0400 Subject: [PATCH 11/13] Fixing test issue and 'modornizing' test --- .../tasks/main.yml | 184 +++++++++--------- 1 file changed, 96 insertions(+), 88 deletions(-) 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 ecc6d39af4..608c524aaf 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 @@ -1,105 +1,113 @@ --- +- 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: - src_cred_name: src_cred - target_cred_name: target_cred + src_cred_name: "AWX-Collection-tests-tower_credential_input_source-src_cred-{{ test_id }}" + target_cred_name: "AWX-Collection-tests-tower_credential_input_source-target_cred-{{ test_id }}" -- name: Add Tower credential Lookup - tower_credential: - description: Credential for Testing Source - name: "{{ src_cred_name }}" - credential_type: CyberArk AIM Central Credential Provider Lookup - inputs: - url: "https://cyberark.example.com" - app_id: "My-App-ID" - organization: Default - register: src_cred_result +- block: + - name: Add Tower credential Lookup + tower_credential: + description: Credential for Testing Source + name: "{{ src_cred_name }}" + credential_type: CyberArk AIM Central Credential Provider Lookup + inputs: + url: "https://cyberark.example.com" + app_id: "My-App-ID" + organization: Default + register: src_cred_result -- assert: - that: - - "src_cred_result is changed" + - assert: + that: + - "src_cred_result is changed" -- name: Add Tower credential Target - tower_credential: - description: Credential for Testing Target - name: "{{ target_cred_name }}" - credential_type: Machine - inputs: - username: user - organization: Default - register: target_cred_result + - name: Add Tower credential Target + tower_credential: + description: Credential for Testing Target + name: "{{ target_cred_name }}" + credential_type: Machine + inputs: + username: user + organization: Default + register: target_cred_result -- assert: - that: - - "target_cred_result is changed" + - assert: + that: + - "target_cred_result is changed" -- name: Add credential Input Source - tower_credential_input_source: - input_field_name: password - 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" - state: present + - name: Add credential Input Source + tower_credential_input_source: + input_field_name: password + 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" + state: present + register: result -- assert: - that: - - "result is changed" + - assert: + that: + - "result is changed" -- name: Add Second Tower credential Lookup - tower_credential: - description: Credential for Testing Source Change - name: "{{ src_cred_name }}-2" - credential_type: CyberArk AIM Central Credential Provider Lookup - inputs: - url: "https://cyberark-prod.example.com" - app_id: "My-App-ID" - organization: Default - register: result + - name: Add Second Tower credential Lookup + tower_credential: + description: Credential for Testing Source Change + name: "{{ src_cred_name }}-2" + credential_type: CyberArk AIM Central Credential Provider Lookup + inputs: + url: "https://cyberark-prod.example.com" + app_id: "My-App-ID" + organization: Default + register: result -- name: Change credential Input Source - tower_credential_input_source: - input_field_name: password - target_credential: "{{ target_cred_name }}" - source_credential: "{{ src_cred_name }}-2" - state: present + - name: Change credential Input Source + tower_credential_input_source: + input_field_name: password + target_credential: "{{ target_cred_name }}" + source_credential: "{{ src_cred_name }}-2" + state: present -- assert: - that: - - "result is changed" + - assert: + that: + - "result is changed" -- name: Remove a Tower credential source - tower_credential_input_source: - input_field_name: password - target_credential: "{{ target_cred_name }}" - state: absent - register: result + always: + - name: Remove a Tower credential source + tower_credential_input_source: + input_field_name: password + target_credential: "{{ target_cred_name }}" + state: absent + register: result -- assert: - that: - - "result is changed" + - assert: + that: + - "result is changed" -- name: Remove Tower credential Lookup - tower_credential: - name: "{{ src_cred_name }}" - organization: Default - credential_type: CyberArk AIM Central Credential Provider Lookup - state: absent - register: result + - name: Remove Tower credential Lookup + tower_credential: + name: "{{ src_cred_name }}" + organization: Default + credential_type: CyberArk AIM Central Credential Provider Lookup + state: absent + register: result -- name: Remove Alt Tower credential Lookup - tower_credential: - name: "{{ src_cred_name }}-2" - organization: Default - credential_type: CyberArk AIM Central Credential Provider Lookup - state: absent - register: result + - name: Remove Alt Tower credential Lookup + tower_credential: + name: "{{ src_cred_name }}-2" + organization: Default + credential_type: CyberArk AIM Central Credential Provider Lookup + state: absent + register: result -- name: Remove Tower credential - tower_credential: - name: "{{ target_cred_name }}" - organization: Default - credential_type: Machine - state: absent - register: result + - name: Remove Tower credential + tower_credential: + name: "{{ target_cred_name }}" + organization: Default + credential_type: Machine + state: absent + register: result From faa33efdd27aae1ed77f5c79f8f5fc55c1391d04 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 15:28:37 -0400 Subject: [PATCH 12/13] Fixing registered name --- .../tests/integration/targets/tower_inventory/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 552b32f47c..43dfe39a04 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: "{{ results.id }}" + insights_credential: "{{ result.id }}" state: present register: result From 570251dc3d479d1b04e3675b697c2c61690ef2b4 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 8 Sep 2020 15:28:57 -0400 Subject: [PATCH 13/13] Modifying get_item_name to handle a None object --- .../plugins/module_utils/tower_api.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index e0d2027ba5..36cfa21551 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -52,20 +52,24 @@ class TowerAPIModule(TowerModule): return TowerAPIModule.IDENTITY_FIELDS.get(endpoint, 'name') def get_item_name(self, item, allow_unknown=False): - if 'name' in item: - return item['name'] + if item: + if 'name' in item: + return item['name'] - for field_name in TowerAPIModule.IDENTITY_FIELDS.values(): - if field_name in item: - return item[field_name] + for field_name in TowerAPIModule.IDENTITY_FIELDS.values(): + if field_name in item: + return item[field_name] - if item.get('type', None) in ('o_auth2_access_token', 'credential_input_source'): - return item['id'] + if item.get('type', None) in ('o_auth2_access_token', 'credential_input_source'): + return item['id'] if allow_unknown: return 'unknown' - self.exit_json(msg='Cannot determine identity field for {0} object.'.format(item.get('type', 'unknown'))) + if item: + self.exit_json(msg='Cannot determine identity field for {0} object.'.format(item.get('type', 'unknown'))) + else: + self.exit_json(msg='Cannot determine identity field for Undefined object.') def head_endpoint(self, endpoint, *args, **kwargs): return self.make_request('HEAD', endpoint, **kwargs)