diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index e7f1d5cf8a..8e0775c34e 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -2692,7 +2692,10 @@ class JobTemplateCallback(GenericAPIView): # Permission class should have already validated host_config_key. job_template = self.get_object() # Attempt to find matching hosts based on remote address. - matching_hosts = self.find_matching_hosts() + if job_template.inventory: + matching_hosts = self.find_matching_hosts() + else: + return Response({"msg": _("Cannot start automatically, an inventory is required.")}, status=status.HTTP_400_BAD_REQUEST) # If the host is not found, update the inventory before trying to # match again. inventory_sources_already_updated = [] diff --git a/awx/main/tests/functional/api/test_job_template.py b/awx/main/tests/functional/api/test_job_template.py index dc6ce0c7a0..3e154766f8 100644 --- a/awx/main/tests/functional/api/test_job_template.py +++ b/awx/main/tests/functional/api/test_job_template.py @@ -3,7 +3,7 @@ import pytest # AWX from awx.api.serializers import JobTemplateSerializer from awx.api.versioning import reverse -from awx.main.models import Job, JobTemplate, CredentialType, WorkflowJobTemplate, Organization, Project +from awx.main.models import Job, JobTemplate, CredentialType, WorkflowJobTemplate, Organization, Project, Inventory from awx.main.migrations import _save_password_keys as save_password_keys # Django @@ -353,3 +353,19 @@ def test_job_template_branch_prompt_error(project, inventory, post, admin_user): expect=400, ) assert 'Project does not allow overriding branch' in str(r.data['ask_scm_branch_on_launch']) + + +@pytest.mark.django_db +def test_job_template_missing_inventory(project, inventory, admin_user, post): + jt = JobTemplate.objects.create( + name='test-jt', inventory=inventory, ask_inventory_on_launch=True, project=project, playbook='helloworld.yml', host_config_key='abcd' + ) + Inventory.objects.get(pk=inventory.pk).delete() + r = post( + url=reverse('api:job_template_callback', kwargs={'pk': jt.pk}), + data={'host_config_key': 'abcd'}, + user=admin_user, + expect=400, + ) + assert r.status_code == 400 + assert "Cannot start automatically, an inventory is required." in str(r.data)