Merge pull request #6369 from AlanCoding/create_associate

Fix bug with association on creation

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot] 2020-03-25 00:28:51 +00:00 committed by GitHub
commit 177867de5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View File

@ -509,9 +509,10 @@ class TowerModule(AnsibleModule):
if not endpoint:
self.fail_json(msg="Unable to create new {0} due to missing endpoint".format(item_type))
item_url = None
if existing_item:
try:
existing_item['url']
item_url = existing_item['url']
except KeyError as ke:
self.fail_json(msg="Unable to process create of item due to missing data {0}".format(ke))
else:
@ -534,6 +535,7 @@ class TowerModule(AnsibleModule):
self.json_output['name'] = response['json'][key]
self.json_output['id'] = response['json']['id']
self.json_output['changed'] = True
item_url = response['json']['url']
else:
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]))
@ -545,7 +547,8 @@ class TowerModule(AnsibleModule):
# Process any associations with this item
if associations is not None:
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 on_create is not None and self.json_output['changed']:

View File

@ -56,6 +56,27 @@ def test_associate_hosts_and_groups(run_module, admin_user, organization):
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
def test_tower_group_idempotent(run_module, admin_user):
# https://github.com/ansible/ansible/issues/46803

View File

@ -53,9 +53,6 @@ def test_create_workflow_job_template_node(run_module, admin_user, wfjt, job_tem
@pytest.mark.django_db
def test_make_use_of_prompts(run_module, admin_user, wfjt, job_template, machine_credential, vault_credential):
# Create to temporarily woraround other issue https://github.com/ansible/awx/issues/5177
WorkflowJobTemplateNode.objects.create(
identifier='42', workflow_job_template=wfjt, unified_job_template=job_template)
result = run_module('tower_workflow_job_template_node', {
'identifier': '42',
'workflow_job_template': 'foo-workflow',
@ -85,9 +82,6 @@ def test_create_with_edges(run_module, admin_user, wfjt, job_template):
unified_job_template=job_template
) for i in range(3)
]
# Create to temporarily woraround other issue https://github.com/ansible/awx/issues/5177
WorkflowJobTemplateNode.objects.create(
identifier='42', workflow_job_template=wfjt, unified_job_template=job_template)
result = run_module('tower_workflow_job_template_node', {
'identifier': '42',