mirror of
https://github.com/ansible/awx.git
synced 2026-03-22 19:35:02 -02:30
Fix bug with association on creation
This commit is contained in:
@@ -509,9 +509,10 @@ class TowerModule(AnsibleModule):
|
|||||||
if not endpoint:
|
if not endpoint:
|
||||||
self.fail_json(msg="Unable to create new {0} due to missing endpoint".format(item_type))
|
self.fail_json(msg="Unable to create new {0} due to missing endpoint".format(item_type))
|
||||||
|
|
||||||
|
item_url = None
|
||||||
if existing_item:
|
if existing_item:
|
||||||
try:
|
try:
|
||||||
existing_item['url']
|
item_url = existing_item['url']
|
||||||
except KeyError as ke:
|
except KeyError as ke:
|
||||||
self.fail_json(msg="Unable to process create of item due to missing data {0}".format(ke))
|
self.fail_json(msg="Unable to process create of item due to missing data {0}".format(ke))
|
||||||
else:
|
else:
|
||||||
@@ -534,6 +535,7 @@ class TowerModule(AnsibleModule):
|
|||||||
self.json_output['name'] = response['json'][key]
|
self.json_output['name'] = response['json'][key]
|
||||||
self.json_output['id'] = response['json']['id']
|
self.json_output['id'] = response['json']['id']
|
||||||
self.json_output['changed'] = True
|
self.json_output['changed'] = True
|
||||||
|
item_url = response['json']['url']
|
||||||
else:
|
else:
|
||||||
if 'json' in response and '__all__' in response['json']:
|
if 'json' in response and '__all__' in response['json']:
|
||||||
self.fail_json(msg="Unable to create {0} {1}: {2}".format(item_type, item_name, response['json']['__all__'][0]))
|
self.fail_json(msg="Unable to create {0} {1}: {2}".format(item_type, item_name, response['json']['__all__'][0]))
|
||||||
@@ -545,7 +547,8 @@ class TowerModule(AnsibleModule):
|
|||||||
# Process any associations with this item
|
# Process any associations with this item
|
||||||
if associations is not None:
|
if associations is not None:
|
||||||
for association_type in associations:
|
for association_type in associations:
|
||||||
self.modify_associations(response, associations[association_type])
|
sub_endpoint = '{0}{1}/'.format(item_url, association_type)
|
||||||
|
self.modify_associations(sub_endpoint, associations[association_type])
|
||||||
|
|
||||||
# If we have an on_create method and we actually changed something we can call on_create
|
# If we have an on_create method and we actually changed something we can call on_create
|
||||||
if on_create is not None and self.json_output['changed']:
|
if on_create is not None and self.json_output['changed']:
|
||||||
|
|||||||
@@ -56,6 +56,27 @@ def test_associate_hosts_and_groups(run_module, admin_user, organization):
|
|||||||
assert set(group.children.all()) == set([child])
|
assert set(group.children.all()) == set([child])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_associate_on_create(run_module, admin_user, organization):
|
||||||
|
inv = Inventory.objects.create(name='test-inv', organization=organization)
|
||||||
|
child = Group.objects.create(name='test-child', inventory=inv)
|
||||||
|
host = Host.objects.create(name='test-host', inventory=inv)
|
||||||
|
|
||||||
|
result = run_module('tower_group', dict(
|
||||||
|
name='Test Group',
|
||||||
|
inventory='test-inv',
|
||||||
|
hosts=[host.name],
|
||||||
|
groups=[child.name],
|
||||||
|
state='present'
|
||||||
|
), admin_user)
|
||||||
|
assert not result.get('failed', False), result.get('msg', result)
|
||||||
|
assert result['changed'] is True
|
||||||
|
|
||||||
|
group = Group.objects.get(pk=result['id'])
|
||||||
|
assert set(group.hosts.all()) == set([host])
|
||||||
|
assert set(group.children.all()) == set([child])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_tower_group_idempotent(run_module, admin_user):
|
def test_tower_group_idempotent(run_module, admin_user):
|
||||||
# https://github.com/ansible/ansible/issues/46803
|
# https://github.com/ansible/ansible/issues/46803
|
||||||
|
|||||||
Reference in New Issue
Block a user