mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 23:07:42 -02:30
Add unit test, update lookup method to favor ID over name
This commit is contained in:
@@ -127,24 +127,14 @@ class TowerAPIModule(TowerModule):
|
|||||||
if response['json']['count'] == 1:
|
if response['json']['count'] == 1:
|
||||||
return response['json']['results'][0]
|
return response['json']['results'][0]
|
||||||
elif response['json']['count'] > 1:
|
elif response['json']['count'] > 1:
|
||||||
object_to_return = None
|
|
||||||
for tower_object in response['json']['results']:
|
for tower_object in response['json']['results']:
|
||||||
# Name takes priority, so we match on name first
|
# ID takes priority, so we match on that first
|
||||||
# If we already have an object to return and it has the right name, we need to fail
|
if str(tower_object['id']) == name_or_id:
|
||||||
if object_to_return and object_to_return['name'] == name_or_id:
|
return tower_object
|
||||||
self.fail_json(msg="The requested name or id was ambiguous and resulted in too many items")
|
# We didn't match on an ID but we found more than 1 object, therefore the results are ambiguous
|
||||||
# If we found an object ID with this name (which has to be unique) and we don't already have
|
self.fail_json(msg="The requested name or id was ambiguous and resulted in too many items")
|
||||||
# something to return, select this object for now
|
|
||||||
elif tower_object['id'] == name_or_id and not object_to_return:
|
|
||||||
object_to_return = tower_object
|
|
||||||
# Otherwise this is the first named object we found, so record that as what to return
|
|
||||||
else:
|
|
||||||
object_to_return = tower_object
|
|
||||||
return object_to_return
|
|
||||||
elif response['json']['count'] == 0:
|
elif response['json']['count'] == 0:
|
||||||
self.fail_json(msg="The {0} {1} was not found on the Tower server".format(endpoint, name_or_id))
|
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))
|
|
||||||
|
|
||||||
def resolve_name_to_id(self, endpoint, name_or_id):
|
def resolve_name_to_id(self, endpoint, name_or_id):
|
||||||
return self.get_one_by_name_or_id(endpoint, name_or_id)['id']
|
return self.get_one_by_name_or_id(endpoint, name_or_id)['id']
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ __metaclass__ = type
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from awx.main.models import Organization, Team
|
||||||
from requests.models import Response
|
from requests.models import Response
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
@@ -102,3 +103,25 @@ def test_no_templated_values(collection_import):
|
|||||||
'The inventory plugin FQCN is templated when the collection is built '
|
'The inventory plugin FQCN is templated when the collection is built '
|
||||||
'and the code should retain the default of awx.awx.'
|
'and the code should retain the default of awx.awx.'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_conflicting_name_and_id(run_module, admin_user):
|
||||||
|
"""In the event that 2 related items match our search criteria in this way:
|
||||||
|
one item has an id that matches input
|
||||||
|
one item has a name that matches input
|
||||||
|
We should preference the id over the name.
|
||||||
|
Otherwise, the universality of the tower_api lookup plugin is compromised.
|
||||||
|
"""
|
||||||
|
org_by_id = Organization.objects.create(name='foo')
|
||||||
|
slug = str(org_by_id.id)
|
||||||
|
org_by_name = Organization.objects.create(name=slug)
|
||||||
|
result = run_module('tower_team', {
|
||||||
|
'name': 'foo_team', 'description': 'fooin around',
|
||||||
|
'organization': slug
|
||||||
|
}, admin_user)
|
||||||
|
assert not result.get('failed', False), result.get('msg', result)
|
||||||
|
team = Team.objects.filter(name='foo_team').first()
|
||||||
|
assert str(team.organization_id) == slug, (
|
||||||
|
'Lookup by id should be preferenced over name in cases of conflict.'
|
||||||
|
)
|
||||||
|
assert team.organization.name == 'foo'
|
||||||
|
|||||||
Reference in New Issue
Block a user