diff --git a/awx_collection/plugins/module_utils/tower_api.py b/awx_collection/plugins/module_utils/tower_api.py index 6b5ed87531..a3d6faa4b8 100644 --- a/awx_collection/plugins/module_utils/tower_api.py +++ b/awx_collection/plugins/module_utils/tower_api.py @@ -105,32 +105,32 @@ class TowerAPIModule(TowerModule): return response['json']['results'][0] - def resolve_name_to_id(self, endpoint, name_or_id): - # Try to resolve the object by name + def get_one_by_name_or_id(self, endpoint, name_or_id): name_field = 'name' - if endpoint == 'users': + if endpoint is 'users': name_field = 'username' - response = self.get_endpoint(endpoint, **{'data': {name_field: name_or_id}}) - if response['status_code'] == 400: - self.fail_json(msg="Unable to try and resolve {0} for {1} : {2}".format(endpoint, name_or_id, response['json']['detail'])) + query_params = {'or__{0}'.format(name_field): name_or_id} + try: + query_params['or__id'] = int(name_or_id) + except ValueError: + # If we got a value error than we didn't have an integer so we can just pass and fall down to the fail + pass + response = self.get_endpoint(endpoint, **{'data': query_params}) if response['json']['count'] == 1: - return response['json']['results'][0]['id'] + return response['json']['results'][0] + elif response['json']['count'] == 2: + for tower_object in response['json']['results']: + return tower_object + # We shouldn't get here because we found 2 objects and ID has to be unique, so one of the objects must have a matching name elif response['json']['count'] == 0: - try: - int(name_or_id) - # If we got 0 items by name, maybe they gave us an ID, let's try looking it up by ID - response = self.head_endpoint("{0}/{1}".format(endpoint, name_or_id), **{'return_none_on_404': True}) - if response is not None: - return name_or_id - except ValueError: - # If we got a value error than we didn't have an integer so we can just pass and fall down to the fail - pass - self.fail_json(msg="The {0} {1} was not found on the Tower server".format(endpoint, name_or_id)) else: - self.fail_json(msg="Found too many names {0} at endpoint {1} try using an ID instead of a name".format(name_or_id, endpoint)) + self.fail_json(msg="Found too many names {0} at endpoint {1}, try using an ID instead of a name".format(name_or_id, endpoint)) + + def resolve_name_to_id(self, endpoint, name_or_id): + return self.get_one_by_name_or_id(endpoint, name_or_id)['id'] def make_request(self, method, endpoint, *args, **kwargs): # In case someone is calling us directly; make sure we were given a method, let's not just assume a GET diff --git a/awx_collection/plugins/modules/tower_role.py b/awx_collection/plugins/modules/tower_role.py index d0d010a0a7..634c3028d0 100644 --- a/awx_collection/plugins/modules/tower_role.py +++ b/awx_collection/plugins/modules/tower_role.py @@ -130,7 +130,7 @@ def main(): resource_name = params.get(param) if resource_name: - resource = module.get_one(endpoint, **{'data': {name_field: resource_name}}) + resource = module.get_one_by_name_or_id(module.param_to_endpoint(param), resource_name) if not resource: module.fail_json( msg='Failed to update role, {0} not found in {1}'.format(param, endpoint), @@ -170,14 +170,14 @@ def main(): if response['status_code'] == 204: module.json_output['changed'] = True else: - module.fail_json(msg="Failed to grant role {0}".format(response['json']['detail'])) + module.fail_json(msg="Failed to grant role. {0}".format(response['json'].get('detail', response['json'].get('msg', 'unknown')))) else: for an_id in list(set(existing_associated_ids) & set(new_association_list)): response = module.post_endpoint(association_endpoint, **{'data': {'id': int(an_id), 'disassociate': True}}) if response['status_code'] == 204: module.json_output['changed'] = True else: - module.fail_json(msg="Failed to revoke role {0}".format(response['json']['detail'])) + module.fail_json(msg="Failed to revoke role. {0}".format(response['json'].get('detail', response['json'].get('msg', 'unknown')))) module.exit_json(**module.json_output)